fix error inquilinos

This commit is contained in:
Mariela 2022-08-31 02:30:55 -06:00
parent 11ac9b0232
commit 1784dd4bbb
8 changed files with 115 additions and 11 deletions

View File

@ -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);
}

View File

@ -175,6 +175,36 @@ export class AppService {
.send<string>(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<string>(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<string>(pattern, payload)
.pipe(
map((response: string) => ({ response }))
)
}
}

View File

@ -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'];

View File

@ -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,6 +108,7 @@ export class CommunitiesService {
await community.houses.map(house => {
if (house.number_house === number_house) {
if(house.tenants)
house.tenants.tenant_id = "";
house.state = "desocupada"
}

View File

@ -77,7 +77,7 @@ 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' })
@ -85,6 +85,11 @@ export class UsersController {
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'];

View File

@ -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 }

View File

@ -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',

View File

@ -132,7 +132,7 @@ const PerfilAdminComunidad = () => {
}
useEffect(() => {
tenantsList(community._id);
tenantsList(cookies.community_id);
}, [])