From d710e7c0ed978a096a6b86be662e12bcc117f0ea Mon Sep 17 00:00:00 2001 From: Mariela Date: Tue, 16 Aug 2022 22:23:03 -0600 Subject: [PATCH] edicion para agregar inquilino al numero de vivienda --- api-gateway/src/app.controller.ts | 8 +++++ api-gateway/src/app.service.ts | 7 +++++ .../src/communities/communities.controller.ts | 9 ++++++ .../src/communities/communities.service.ts | 29 +++++++++++++++---- servicio-usuarios/src/users/users.service.ts | 13 +++------ web-ui/web-react/src/components/Inquilinos.js | 28 +++++++++++++++--- 6 files changed, 76 insertions(+), 18 deletions(-) diff --git a/api-gateway/src/app.controller.ts b/api-gateway/src/app.controller.ts index 39e15aad..58b9c650 100644 --- a/api-gateway/src/app.controller.ts +++ b/api-gateway/src/app.controller.ts @@ -223,6 +223,14 @@ export class AppController { return this.appService.findHousesCommunity(community_id); } + @Post('community/saveTenant') + saveTenant( + @Body('community_id') community_id: string, + @Body('number_house') number_house: string, + @Body('tenant_id') tenant_id: string, + ) { + return this.appService.saveTenant(community_id, number_house, tenant_id); + } // #==== API Common Areas @Post('commonArea/createCommonArea') createCommonArea( diff --git a/api-gateway/src/app.service.ts b/api-gateway/src/app.service.ts index 78425570..4c17da9b 100644 --- a/api-gateway/src/app.service.ts +++ b/api-gateway/src/app.service.ts @@ -301,6 +301,13 @@ export class AppService { return houses; } + saveTenant(id: string, number_house: string, tenant_id: string) { + const pattern = { cmd: 'saveTenant' }; + const payload = { _id: id, number_house: number_house, tenant_id: tenant_id }; + return this.clientCommunityApp + .send(pattern, payload) + .pipe(map((message: string) => ({ message }))); + } // ====================== COMMON AREAS =============================== //POST parameter from API diff --git a/servicio-comunidad-viviendas/src/communities/communities.controller.ts b/servicio-comunidad-viviendas/src/communities/communities.controller.ts index 83335420..6ef0c9fc 100644 --- a/servicio-comunidad-viviendas/src/communities/communities.controller.ts +++ b/servicio-comunidad-viviendas/src/communities/communities.controller.ts @@ -53,4 +53,13 @@ export class CommunitiesController { let pstatus = body['status']; return this.communitiesService.changeStatus(pid,pstatus); } + + + @MessagePattern({ cmd: 'saveTenant' }) + saveTenant(@Payload() body: string) { + let id = body['_id']; + let tenant_id = body['tenant_id']; + let number_house = body['number_house']; + return this.communitiesService.saveTenant(id, number_house, tenant_id); + } } diff --git a/servicio-comunidad-viviendas/src/communities/communities.service.ts b/servicio-comunidad-viviendas/src/communities/communities.service.ts index 8b5ce685..c494222b 100644 --- a/servicio-comunidad-viviendas/src/communities/communities.service.ts +++ b/servicio-comunidad-viviendas/src/communities/communities.service.ts @@ -13,7 +13,7 @@ export class CommunitiesService { @InjectModel(Community.name) private readonly communityModel: Model, @Inject('SERVICIO_USUARIOS') private readonly clientUserApp: ClientProxy, - ) {} + ) { } async create(community: CommunityDocument): Promise { return this.communityModel.create(community); @@ -56,15 +56,15 @@ export class CommunitiesService { } async remove(id: string) { - return this.communityModel.findOneAndUpdate({ _id: id }, {status: '-1'}, { + return this.communityModel.findOneAndUpdate({ _id: id }, { status: '-1' }, { new: true, - }); + }); } async changeStatus(id: string, status: string) { - return this.communityModel.findOneAndUpdate({ _id: id }, {status: status}, { + return this.communityModel.findOneAndUpdate({ _id: id }, { status: status }, { new: true, - }); + }); } async findCommunityAdmin(community: string, user_type: string) { @@ -78,4 +78,23 @@ export class CommunitiesService { const finalValue = await lastValueFrom(callback); return finalValue['response']; } + + + async saveTenant(id: string, number_house: string, tenant_id: string) { + + let community = await this.findOne(id); + + await community.houses.map(house => { + if(house.number_house == number_house){ + house.tenants.tenant_id = tenant_id + } + return house; + }) + + console.log(community.houses) + + return await this.communityModel.findOneAndUpdate({ _id: id }, community, { + new: true, + }); + } } diff --git a/servicio-usuarios/src/users/users.service.ts b/servicio-usuarios/src/users/users.service.ts index 5f9f50b5..22e5c487 100644 --- a/servicio-usuarios/src/users/users.service.ts +++ b/servicio-usuarios/src/users/users.service.ts @@ -229,20 +229,15 @@ export class UsersService { } - async findHousesCommunity(community_id: string) { - const pattern = { cmd: 'findOneCommunity' } - const payload = { _id: community_id } + async saveTenantNumHouse(community_id: string, number_house:string, tenant_id: string) { + const pattern = { cmd: 'saveTenant' } + const payload = { _id: community_id, number_house: number_house, tenant_id: tenant_id } - let callback = await this.clientCommunityApp + return await this.clientCommunityApp .send(pattern, payload) .pipe( map((response: string) => ({ response })) ) - const finalValue = await lastValueFrom(callback); - const response = finalValue['response']; - const houses = response['houses']; - - return houses; } } diff --git a/web-ui/web-react/src/components/Inquilinos.js b/web-ui/web-react/src/components/Inquilinos.js index 837e0379..9b12eddd 100644 --- a/web-ui/web-react/src/components/Inquilinos.js +++ b/web-ui/web-react/src/components/Inquilinos.js @@ -129,16 +129,16 @@ const Inquilinos = () => { email: document.getElementById('correo_electronico').value, phone: '', password: '', - community_id: document.getElementById('numero_vivienda').value, + community_id: cookies.community_id, community_name: '', - number_house: 'Sin nĂºmero de vivienda', + number_house: document.getElementById('numero_vivienda').value, date_entry: new Date(), user_type: '3', status: '1', status_text: '', } - fetch('http://localhost:3000/api/createUser', { + fetch('http://localhost:4000/user/createUser', { method: 'POST', cache: 'no-cache', body: JSON.stringify(newTenant), @@ -147,11 +147,31 @@ const Inquilinos = () => { }, }).then((response) => { if (response.ok) { - alert('Inquilino registrado correctamente') + return response.json() } else { alert('Error al registrar inquilino') } }) + /* .then(res => { + console.log(data.message) + let data = { + community_id: cookies.community_id, + number_house: document.getElementById('numero_vivienda').value, + tenant_id: data.message._id + } + return fetch('http://localhost:4000/community/saveTenant', + { + method: 'POST', + cache: 'no-cache', + body: JSON.stringify(data), + headers: { + 'Content-Type': 'application/json', + } + } + ).then(res => alert('Inquilino registrado correctamente')) + .catch(error => alert('Error al registrar inquilino')) + }) + .catch(error => alert('Error al registrar inquilino'))*/ } const deleteTenant = () => {