finalizar eliminar inquilino

This commit is contained in:
Mariela 2022-08-22 16:39:20 -06:00
parent 456d9ce4d0
commit a9c02244a8
7 changed files with 85 additions and 40 deletions

View File

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

View File

@ -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<string>(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<string>(pattern, payload)
.pipe(map((message: string) => ({ message })));
}
// ====================== COMMON AREAS ===============================
//POST parameter from API
createCommonArea(

View File

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

View File

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

View File

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

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.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<string>(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<string>(pattern, payload)
.pipe(

View File

@ -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 = () => {