diff --git a/api-gateway/src/app.controller.ts b/api-gateway/src/app.controller.ts index d9b740b9..e8e98459 100644 --- a/api-gateway/src/app.controller.ts +++ b/api-gateway/src/app.controller.ts @@ -353,6 +353,33 @@ export class AppController { ); } + @Put('community/updateCommunity/:id') + updateCommunity( + @Param('id') id: string, + @Body('name') name: string, + @Body('province') province: string, + @Body('canton') canton: string, + @Body('district') district: string, + @Body('num_houses') num_houses: number, + @Body('phone') phone: string, + @Body('status') status: string, + @Body('date_entry') date_entry: Date, + @Body('houses') houses: [], + ) { + return this.appService.updateCommunity( + id, + name, + province, + canton, + district, + num_houses, + phone, + status, + date_entry, + houses, + ); + } + @Get('community/allCommunities') allcommunities() { return this.appService.allCommunities(); diff --git a/api-gateway/src/app.service.ts b/api-gateway/src/app.service.ts index 235090ec..f8a24bbe 100644 --- a/api-gateway/src/app.service.ts +++ b/api-gateway/src/app.service.ts @@ -426,6 +426,25 @@ export class AppService { .pipe(map((message: string) => ({ message }))); } + updateCommunity(id: string, name: string, province: string, canton: string, district: string, num_houses: number, phone: string, status: string, date_entry: Date, houses: unknown) { + const pattern = { cmd: 'updateCommunity' }; + const payload = { + id: id, + name: name, + province: province, + canton: canton, + district: district, + num_houses: num_houses, + phone: phone, + status: status, + date_entry: date_entry, + houses: houses, + }; + return this.clientCommunityApp + .send(pattern, payload) + .pipe(map((message: string) => ({ message }))); + } + allCommunities() { const pattern = { cmd: 'findAllCommunities' }; const payload = {}; diff --git a/web-ui/web-react/src/components/ComunidadViviendas.js b/web-ui/web-react/src/components/ComunidadViviendas.js index 36f22a48..548388fb 100644 --- a/web-ui/web-react/src/components/ComunidadViviendas.js +++ b/web-ui/web-react/src/components/ComunidadViviendas.js @@ -203,59 +203,80 @@ const Communities = () => { districtId && community.phone ) { - let _communities = [...communitiesList]; - let _community = { ...community }; - _community.province = provinciaId; - _community.canton = cantonId; - _community.district = districtId; + if (saveButtonLabel === 'Registrar') { + let _communities = [...communitiesList]; + let _community = { ...community }; + _community.province = provinciaId; + _community.canton = cantonId; + _community.district = districtId; - for (let i = 0; i < _community.num_houses; i++) { - _community.houses.push({ - number_house: codeHouses + (i + 1), - }); - } - fetch('http://localhost:4000/community/createCommunity', { - cache: 'no-cache', - method: 'POST', - body: JSON.stringify(_community), - headers: { - 'Content-Type': 'application/json', - }, - }) - .then((response) => { - if (response.status != 201) + for (let i = 0; i < _community.num_houses; i++) { + _community.houses.push({ + number_house: codeHouses + (i + 1), + }); + } + fetch('http://localhost:4000/community/createCommunity', { + cache: 'no-cache', + method: 'POST', + body: JSON.stringify(_community), + headers: { + 'Content-Type': 'application/json', + }, + }) + .then((response) => { + if (response.status != 201) + console.log('Ocurrió un error con el servicio: ' + response.status); + else return response.json(); + }) + .then(() => { + _community.province = provincesList.find( + (p) => p.code === _community.province, + ).name; + _community.canton = cantonsList.find( + (p) => p.code === _community.canton, + ).name; + _community.district = districtsList.find( + (p) => p.code === _community.district, + ).name; + + _communities.push(_community); + toast.current.show({ + severity: 'success', + summary: 'Registro exitoso', + detail: 'Comunidad de vivienda Creada', + life: 3000, + }); + setCommunitiesList(_communities); + setProvinciaId(''); + setCantonId(''); + setDistrictId(''); + setCodeHouses(''); + getCommunites(); + setCommunity(emptyCommunity); + }) + .catch((err) => console.log('Ocurrió un error con el fetch', err)); + } else { + let _community = { ...community }; + _community.province = provinciaId; + _community.canton = cantonId; + _community.district = districtId; + console.log(`Actualizando comunidad: ${_community}`); + fetch(`http://localhost:4000/community/updateCommunity/${community._id}`, { + method: 'PUT', + cache: 'no-cache', + body: JSON.stringify(_community), + headers: { + 'Content-Type': 'application/json', + } + }).then((response) => { + getCommunites(); + if (response.status != 200) console.log('Ocurrió un error con el servicio: ' + response.status); else return response.json(); - }) - .then(() => { - _community.province = provincesList.find( - (p) => p.code === _community.province, - ).name; - _community.canton = cantonsList.find( - (p) => p.code === _community.canton, - ).name; - _community.district = districtsList.find( - (p) => p.code === _community.district, - ).name; - - _communities.push(_community); - toast.current.show({ - severity: 'success', - summary: 'Registro exitoso', - detail: 'Comunidad de vivienda Creada', - life: 3000, - }); - - setCommunitiesList(_communities); - - setProvinciaId(''); - setCantonId(''); - setDistrictId(''); - setCodeHouses(''); - - setCommunity(emptyCommunity); - }) - .catch((err) => console.log('Ocurrió un error con el fetch', err)); + }).catch((err) => console.log('Ocurrió un error con el fetch', err)); + setSaveButtonLabel('Registrar'); + setCommunity(emptyCommunity); + } } else { setSubmitted(true); } @@ -356,6 +377,7 @@ const Communities = () => { detail: 'Comunidad de Viviendas Actualizada', life: 3000, }); + getCommunites(); }) .catch((err) => console.log('Ocurrió un error con el fetch', err)); };