From a9c02244a8d16ea5120c3f40c788da760174da44 Mon Sep 17 00:00:00 2001 From: Mariela Date: Mon, 22 Aug 2022 16:39:20 -0600 Subject: [PATCH] finalizar eliminar inquilino --- api-gateway/src/app.controller.ts | 26 ++++++---- api-gateway/src/app.service.ts | 8 +-- .../src/communities/communities.controller.ts | 1 - .../src/communities/communities.service.ts | 4 +- .../src/users/users.controller.ts | 5 +- servicio-usuarios/src/users/users.service.ts | 29 +++++++---- web-ui/web-react/src/components/Inquilinos.js | 52 +++++++++++++++---- 7 files changed, 85 insertions(+), 40 deletions(-) diff --git a/api-gateway/src/app.controller.ts b/api-gateway/src/app.controller.ts index 2975a60a..510dfeb9 100644 --- a/api-gateway/src/app.controller.ts +++ b/api-gateway/src/app.controller.ts @@ -2,7 +2,7 @@ import { Controller, Get, Post, Put, Body, Param, Delete } from '@nestjs/common' import { AppService } from './app.service'; @Controller() export class AppController { - constructor(private readonly appService: AppService) {} + constructor(private readonly appService: AppService) { } // #==== API Users @Post('user/createAdminSystem') createAdminSystem( @@ -15,8 +15,8 @@ export class AppController { @Body('status') status: string, @Body('date_entry') date_entry: Date, ) { - return this.appService.createAdminSystem(dni, name, last_name, email, phone, - user_type, status, date_entry); + return this.appService.createAdminSystem(dni, name, last_name, email, phone, + user_type, status, date_entry); } @Post('user/createGuard') @@ -33,7 +33,7 @@ export class AppController { @Body('community_id') community_id: string, ) { return this.appService.createGuard(dni, name, last_name, email, phone, - user_type, status, date_entry,community_id); + user_type, status, date_entry, community_id); } @Post('user/createAdminCommunity') @@ -47,10 +47,10 @@ export class AppController { @Body('user_type') user_type: string, @Body('status') status: string, @Body('date_entry') date_entry: Date, - @Body('community_id') community_id:string + @Body('community_id') community_id: string ) { return this.appService.createAdminCommunity(dni, name, last_name, email, phone, - user_type, status, date_entry,community_id); + user_type, status, date_entry, community_id); } @Post('user/createUser') @@ -82,7 +82,7 @@ export class AppController { ); } - + @Post('user/createTenant') createTenant( @Body('dni') dni: string, @@ -203,7 +203,7 @@ export class AppController { allUsersTenants(@Param('community_id') paramCommunity_id: string) { return this.appService.findTenantsCommunity(paramCommunity_id); } - + @Get('user/find/:dni') findUser(@Param('dni') paramUserDNI: string) { return this.appService.findUser(paramUserDNI); @@ -224,9 +224,13 @@ export class AppController { return this.appService.deleteAdminCommunity(id); } - @Delete('user/deleteTenant/:id') - deleteTenant(@Param('id') id: string) { - return this.appService.deleteTenant(id); + @Put('user/deleteTenant/:id') + deleteTenant( + @Param('id') id: string, + @Body('community_id') community_id: string, + @Body('number_house') number_house: string + ) { + return this.appService.deleteTenant(id, community_id, number_house); } @Post('user/changeStatus') diff --git a/api-gateway/src/app.service.ts b/api-gateway/src/app.service.ts index be6a794c..42475489 100644 --- a/api-gateway/src/app.service.ts +++ b/api-gateway/src/app.service.ts @@ -271,9 +271,9 @@ export class AppService { .pipe(map((message: string) => ({ message }))); } - deleteTenant(id: string) { + deleteTenant(id: string, community_id: string, number_house: string) { const pattern = { cmd: 'deleteTenant' }; - const payload = { id: id }; + const payload = { _id: id, community_id: community_id, number_house: number_house }; return this.clientUserApp .send(pattern, payload) .pipe(map((message: string) => ({ message }))); @@ -382,7 +382,7 @@ export class AppService { const finalValue = await lastValueFrom(callback); const response = finalValue['response']; const houses = response['houses']; - + return houses; } @@ -393,7 +393,7 @@ export class AppService { .send(pattern, payload) .pipe(map((message: string) => ({ message }))); } - + // ====================== COMMON AREAS =============================== //POST parameter from API createCommonArea( diff --git a/servicio-comunidad-viviendas/src/communities/communities.controller.ts b/servicio-comunidad-viviendas/src/communities/communities.controller.ts index d8302399..a0fa2672 100644 --- a/servicio-comunidad-viviendas/src/communities/communities.controller.ts +++ b/servicio-comunidad-viviendas/src/communities/communities.controller.ts @@ -66,7 +66,6 @@ export class CommunitiesController { @MessagePattern({ cmd: 'deleteTenant' }) deleteTenant(@Payload() body: string) { - let id = body['_id']; let tenant_id = body['tenant_id']; let number_house = body['number_house']; diff --git a/servicio-comunidad-viviendas/src/communities/communities.service.ts b/servicio-comunidad-viviendas/src/communities/communities.service.ts index b718a024..dc3773e7 100644 --- a/servicio-comunidad-viviendas/src/communities/communities.service.ts +++ b/servicio-comunidad-viviendas/src/communities/communities.service.ts @@ -105,12 +105,10 @@ export class CommunitiesService { async deleteTenant(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) { + if (house.number_house === number_house) { house.tenants.tenant_id = ""; house.state = "desocupada" } diff --git a/servicio-usuarios/src/users/users.controller.ts b/servicio-usuarios/src/users/users.controller.ts index 06f9cd88..07cee85d 100644 --- a/servicio-usuarios/src/users/users.controller.ts +++ b/servicio-usuarios/src/users/users.controller.ts @@ -133,7 +133,10 @@ export class UsersController { @MessagePattern({ cmd: 'deleteTenant' }) deleteTenant(@Payload() user: any) { - return this.userService.deleteTenant(user['_id']); + let tenant_id = user['_id']; + return this.userService.deleteTenant(tenant_id, + user['community_id'], + user['number_house']); } @MessagePattern({ cmd: 'changeStatus' }) diff --git a/servicio-usuarios/src/users/users.service.ts b/servicio-usuarios/src/users/users.service.ts index 96c973d7..d42cffb9 100644 --- a/servicio-usuarios/src/users/users.service.ts +++ b/servicio-usuarios/src/users/users.service.ts @@ -29,7 +29,7 @@ export class UsersService { let passwordEncriptada = Md5.init(user.password); user.password = passwordEncriptada; let userCreated = await this.userModel.create(user); - await this.saveTenantNumHouse(user.community_id, user.number_house, userCreated['_id']); + await this.saveTenantNumHouse(user.community_id, user.number_house, userCreated['_id']); let community = await this.findCommunity(user.community_id); user.community_id = community['name']; @@ -195,7 +195,7 @@ export class UsersService { } async deleteAdminSystem(id: string) { - return this.userModel.findOneAndUpdate({ _id: id }, { status: '-1' }, { + return this.userModel.findOneAndUpdate({ _id: id }, { status: '-1', number_house:'' }, { new: true, }); } @@ -206,10 +206,18 @@ export class UsersService { }); } - async deleteTenant(id: string) { - return this.userModel.findOneAndUpdate({ _id: id }, { status: '-1' }, { - new: true, - }); + async deleteTenant(tenant_id: string, community_id: string, number_house: string) { + + try{ + this.userModel.findOneAndUpdate({ _id: tenant_id }, { status: '-1' }, { + new: true, + }); + + return await this.deleteTenantNumHouse(community_id, number_house, tenant_id); + } catch(error){ + console.log(error) + return error; + } } async validateEmail(email: string) { @@ -257,9 +265,9 @@ export class UsersService { } - async saveTenantNumHouse(community_id: string, number_house:string, tenant_id: string) { + 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 } + const payload = { _id: community_id, number_house: number_house, tenant_id: tenant_id } return await this.clientCommunityApp .send(pattern, payload) @@ -269,10 +277,9 @@ export class UsersService { } - async deleteTenantNumHouse(community_id: string, number_house:string, tenant_id: string) { + async deleteTenantNumHouse(community_id: string, number_house: string, tenant_id: string) { const pattern = { cmd: 'deleteTenant' } - const payload = { _id: community_id, number_house: number_house, tenant_id: tenant_id } - + const payload = { _id: community_id, number_house: number_house, tenant_id: tenant_id } return await this.clientCommunityApp .send(pattern, payload) .pipe( diff --git a/web-ui/web-react/src/components/Inquilinos.js b/web-ui/web-react/src/components/Inquilinos.js index 94e4ab4e..0f8fa2a3 100644 --- a/web-ui/web-react/src/components/Inquilinos.js +++ b/web-ui/web-react/src/components/Inquilinos.js @@ -184,15 +184,49 @@ const Inquilinos = () => { } const deleteTenant = () => { - let _tenants = tenants.filter((val) => val._id !== tenant._id) - setTenants(_tenants) - setDeleteTenantDialog(false) - setTenant(emptyTenant) - toast.current.show({ - severity: 'success', - summary: 'Inquilino Eliminado', - life: 3000, - }) + + let _tenant = { + community_id: tenant.community_id, + number_house: tenant.number_house + }; + + fetch('http://localhost:4000/user/deleteTenant/' + tenant._id, { + cache: 'no-cache', + method: 'PUT', + body: JSON.stringify(_tenant), + headers: { + 'Content-Type': 'application/json' + } + }) + .then( + function (response) { + if (response.status != 201) + console.log('OcurriĆ³ un error con el servicio: ' + response.status); + else + return response.json(); + } + ) + .then( + function (response) { + + let _tenants = tenants.filter((val) => val._id !== tenant._id) + setTenants(_tenants) + setDeleteTenantDialog(false) + setTenant(emptyTenant) + toast.current.show({ + severity: 'success', + summary: 'Inquilino Eliminado', + life: 3000, + }) } + ) + .catch( + err => { + console.log('OcurriĆ³ un error con el fetch', err) + toast.current.show({ severity: 'danger', summary: 'Error', detail: 'Inquilino no se pudo eliminar', life: 3000 }); + } + ); + + } const deleteSelectedTenants = () => {