diff --git a/api-gateway/src/app.controller.ts b/api-gateway/src/app.controller.ts index 56c1252c..70398768 100644 --- a/api-gateway/src/app.controller.ts +++ b/api-gateway/src/app.controller.ts @@ -1,4 +1,5 @@ import { Controller, Get, Post, Put, Body, Param, Delete } from '@nestjs/common'; +import { Console } from 'console'; import { AppService } from './app.service'; @Controller() export class AppController { @@ -305,6 +306,30 @@ export class AppController { return this.appService.updateAdminSystem(_id, dni, name, last_name, email, phone); } + @Put('user/updateTenant/:id') + updateTenant( + @Param('id') id: string, + @Body('dni') dni: string, + @Body('name') name: string, + @Body('last_name') last_name: string, + @Body('email') email: string, + @Body('phone') phone: number, + @Body('community_id') community_id: string, + @Body('number_house') number_house: string, + ) { + + return this.appService.updateTenant( + id, + dni, + name, + last_name, + email, + phone, + community_id, + number_house, + ); + } + // #==== API Communities @Post('community/createCommunity') createCommunity( @@ -369,8 +394,9 @@ export class AppController { saveTenant( @Body('community_id') community_id: string, @Body('number_house') number_house: string, - @Body('tenant_id') tenant_id: string, + @Body('_id') tenant_id: string, ) { + console.log(community_id + ' ' + number_house + ' ' + tenant_id) return this.appService.saveTenant(community_id, number_house, tenant_id); } diff --git a/api-gateway/src/app.service.ts b/api-gateway/src/app.service.ts index 7677f6ba..5b70fffc 100644 --- a/api-gateway/src/app.service.ts +++ b/api-gateway/src/app.service.ts @@ -175,6 +175,36 @@ export class AppService { .send(pattern, payload) .pipe(map((message: string) => ({ message }))); } + + + async updateTenant( + _id: string, + dni: string, + name: string, + last_name: string, + email: string, + phone: number, + community_id: string, + number_house: string, + ) { + await this.saveTenant(community_id, number_house, _id); + + const pattern = { cmd: 'updateTenant' }; + const payload = { + id: _id, + dni: dni, + name: name, + last_name: last_name, + email: email, + phone: phone, + community_id: community_id, + number_house: number_house, + }; + return this.clientUserApp + .send(pattern, payload) + .pipe(map((message: string) => ({ message }))); + } + //POST parameter from API createAdminSystem(dni: string, name: string, last_name: string, email: string, phone: number , user_type: string, status: string, date_entry: Date) { @@ -771,4 +801,19 @@ export class AppService { return pass; } + + + + async saveTenantNumHouse(community_id: string, number_house: string, tenant_id: string) { + + const pattern = { cmd: 'saveTenantNumHouse' } + const payload = { _id: community_id, number_house: number_house, tenant_id: tenant_id } + + return await this.clientCommunityApp + .send(pattern, payload) + .pipe( + map((response: string) => ({ response })) + ) + } + } diff --git a/servicio-comunidad-viviendas/src/communities/communities.controller.ts b/servicio-comunidad-viviendas/src/communities/communities.controller.ts index f71e2524..1eb2a027 100644 --- a/servicio-comunidad-viviendas/src/communities/communities.controller.ts +++ b/servicio-comunidad-viviendas/src/communities/communities.controller.ts @@ -57,7 +57,6 @@ export class CommunitiesController { @MessagePattern({ cmd: 'saveTenant' }) saveTenant(@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 212eeda7..ee3317ca 100644 --- a/servicio-comunidad-viviendas/src/communities/communities.service.ts +++ b/servicio-comunidad-viviendas/src/communities/communities.service.ts @@ -88,7 +88,6 @@ export class CommunitiesService { await community.houses.map(house => { if (house.number_house == number_house) { if (house.tenants) { - house.tenants.tenant_id = ptenant_id } else { let tenant = new Tenant() @@ -109,7 +108,8 @@ export class CommunitiesService { await community.houses.map(house => { if (house.number_house === number_house) { - house.tenants.tenant_id = ""; + if(house.tenants) + house.tenants.tenant_id = ""; house.state = "desocupada" } return house; diff --git a/servicio-usuarios/src/users/users.controller.ts b/servicio-usuarios/src/users/users.controller.ts index bea3191e..81a296b6 100644 --- a/servicio-usuarios/src/users/users.controller.ts +++ b/servicio-usuarios/src/users/users.controller.ts @@ -77,14 +77,19 @@ export class UsersController { @MessagePattern({ cmd: 'updateGuard' }) updateGuard(@Payload() guard: UserDocument) { - return this.userService.update(guard.id, guard); + return this.userService.update(guard._id, guard); } - + @MessagePattern({ cmd: 'updateAdminCommunity' }) updateAdminCommunity(@Payload() user: UserDocument) { return this.userService.update(user._id, user); } + @MessagePattern({ cmd: 'updateTenant' }) + updateTenant(@Payload() tenant: UserDocument) { + return this.userService.updateTenant(tenant.id, tenant); + } + @MessagePattern({ cmd: 'removeUser' }) remove(@Payload() id: string) { let dni = id['dni']; diff --git a/servicio-usuarios/src/users/users.service.ts b/servicio-usuarios/src/users/users.service.ts index 5df15e3a..f074c01d 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.saveTenant(user.community_id, user.number_house, userCreated['_id']); let community = await this.findCommunity(user.community_id); user.community_id = community['name']; @@ -117,6 +117,9 @@ export class UsersService { } async update(id: string, user: UserDocument) { + console.log(id) + console.log(user) + return this.userModel.findOneAndUpdate({ _id: id }, user, { new: true, }); @@ -131,6 +134,18 @@ export class UsersService { }); } + async updateTenant(id: string, user: UserDocument) { + await this.saveTenant(user.community_id, user.number_house, user.id); + + return await this.userModel.findOneAndUpdate({ _id: id }, { + name: user['name'], last_name: user['last_name'], + dni: user['dni'], email: user['email'], phone: user['phone'], + number_house: user['number_house'] + }, { + new: true, + }); + } + /* async remove(id: string) { return this.userModel.findByIdAndRemove({ _id: id }).exec(); }*/ @@ -293,7 +308,7 @@ export class UsersService { } - async saveTenantNumHouse(community_id: string, number_house: string, tenant_id: string) { + async saveTenant(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 } diff --git a/web-ui/web-react/src/components/Inquilinos.js b/web-ui/web-react/src/components/Inquilinos.js index e4f49014..2fc7413d 100644 --- a/web-ui/web-react/src/components/Inquilinos.js +++ b/web-ui/web-react/src/components/Inquilinos.js @@ -72,6 +72,9 @@ const Inquilinos = () => { item.number_house = 'Sin vivienda asignada' } }) + data = data.filter( + (val) => val.status != -1, + ); setTenants(data) }) } @@ -156,9 +159,9 @@ const Inquilinos = () => { .catch((error) => console.log(`Ocurrió un error: ${error}`)) } else setSubmitted(true) } else { - let _tenant = { ..._tenant, number_house: houseNumber }; + let _tenant = { ...tenant, number_house: houseNumber }; console.log(`Actualizando inquilino: ${_tenant}`) - fetch(`http://localhost:4000/user/updateUser/${tenant._id}`, { + fetch(`http://localhost:4000/user/updateTenant/${tenant._id}`, { cache: 'no-cache', method: 'PUT', body: JSON.stringify(_tenant), @@ -170,6 +173,17 @@ const Inquilinos = () => { console.log(`Hubo un error en el servicio: ${response.status}`) else return response.json() }).then(() => { + + fetch('http://localhost:4000/community/saveTenant', + { + cache: 'no-cache', + method: 'POST', + body: JSON.stringify(_tenant), + headers: { + 'Content-Type': 'application/json', + } + }); + toast.current.show({ severity: 'success', summary: 'Éxito', diff --git a/web-ui/web-react/src/components/PerfilAdminComunidad.js b/web-ui/web-react/src/components/PerfilAdminComunidad.js index c0a2a3a2..438c4312 100644 --- a/web-ui/web-react/src/components/PerfilAdminComunidad.js +++ b/web-ui/web-react/src/components/PerfilAdminComunidad.js @@ -132,7 +132,7 @@ const PerfilAdminComunidad = () => { } useEffect(() => { - tenantsList(community._id); + tenantsList(cookies.community_id); }, [])