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'; import { AppService } from './app.service';
@Controller() @Controller()
export class AppController { export class AppController {
constructor(private readonly appService: AppService) {} constructor(private readonly appService: AppService) { }
// #==== API Users // #==== API Users
@Post('user/createAdminSystem') @Post('user/createAdminSystem')
createAdminSystem( createAdminSystem(
@ -15,8 +15,8 @@ export class AppController {
@Body('status') status: string, @Body('status') status: string,
@Body('date_entry') date_entry: Date, @Body('date_entry') date_entry: Date,
) { ) {
return this.appService.createAdminSystem(dni, name, last_name, email, phone, return this.appService.createAdminSystem(dni, name, last_name, email, phone,
user_type, status, date_entry); user_type, status, date_entry);
} }
@Post('user/createGuard') @Post('user/createGuard')
@ -33,7 +33,7 @@ export class AppController {
@Body('community_id') community_id: string, @Body('community_id') community_id: string,
) { ) {
return this.appService.createGuard(dni, name, last_name, email, phone, 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') @Post('user/createAdminCommunity')
@ -47,10 +47,10 @@ export class AppController {
@Body('user_type') user_type: string, @Body('user_type') user_type: string,
@Body('status') status: string, @Body('status') status: string,
@Body('date_entry') date_entry: Date, @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, 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') @Post('user/createUser')
@ -82,7 +82,7 @@ export class AppController {
); );
} }
@Post('user/createTenant') @Post('user/createTenant')
createTenant( createTenant(
@Body('dni') dni: string, @Body('dni') dni: string,
@ -203,7 +203,7 @@ export class AppController {
allUsersTenants(@Param('community_id') paramCommunity_id: string) { allUsersTenants(@Param('community_id') paramCommunity_id: string) {
return this.appService.findTenantsCommunity(paramCommunity_id); return this.appService.findTenantsCommunity(paramCommunity_id);
} }
@Get('user/find/:dni') @Get('user/find/:dni')
findUser(@Param('dni') paramUserDNI: string) { findUser(@Param('dni') paramUserDNI: string) {
return this.appService.findUser(paramUserDNI); return this.appService.findUser(paramUserDNI);
@ -224,9 +224,13 @@ export class AppController {
return this.appService.deleteAdminCommunity(id); return this.appService.deleteAdminCommunity(id);
} }
@Delete('user/deleteTenant/:id') @Put('user/deleteTenant/:id')
deleteTenant(@Param('id') id: string) { deleteTenant(
return this.appService.deleteTenant(id); @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') @Post('user/changeStatus')

View File

@ -271,9 +271,9 @@ export class AppService {
.pipe(map((message: string) => ({ message }))); .pipe(map((message: string) => ({ message })));
} }
deleteTenant(id: string) { deleteTenant(id: string, community_id: string, number_house: string) {
const pattern = { cmd: 'deleteTenant' }; const pattern = { cmd: 'deleteTenant' };
const payload = { id: id }; const payload = { _id: id, community_id: community_id, number_house: number_house };
return this.clientUserApp return this.clientUserApp
.send<string>(pattern, payload) .send<string>(pattern, payload)
.pipe(map((message: string) => ({ message }))); .pipe(map((message: string) => ({ message })));
@ -382,7 +382,7 @@ export class AppService {
const finalValue = await lastValueFrom(callback); const finalValue = await lastValueFrom(callback);
const response = finalValue['response']; const response = finalValue['response'];
const houses = response['houses']; const houses = response['houses'];
return houses; return houses;
} }
@ -393,7 +393,7 @@ export class AppService {
.send<string>(pattern, payload) .send<string>(pattern, payload)
.pipe(map((message: string) => ({ message }))); .pipe(map((message: string) => ({ message })));
} }
// ====================== COMMON AREAS =============================== // ====================== COMMON AREAS ===============================
//POST parameter from API //POST parameter from API
createCommonArea( createCommonArea(

View File

@ -66,7 +66,6 @@ export class CommunitiesController {
@MessagePattern({ cmd: 'deleteTenant' }) @MessagePattern({ cmd: 'deleteTenant' })
deleteTenant(@Payload() body: string) { deleteTenant(@Payload() body: string) {
let id = body['_id']; let id = body['_id'];
let tenant_id = body['tenant_id']; let tenant_id = body['tenant_id'];
let number_house = body['number_house']; 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) { async deleteTenant(id: string, number_house: string, tenant_id: string) {
let community = await this.findOne(id); let community = await this.findOne(id);
await community.houses.map(house => { await community.houses.map(house => {
if (house.number_house == number_house && if (house.number_house === number_house) {
house.tenants.tenant_id == tenant_id) {
house.tenants.tenant_id = ""; house.tenants.tenant_id = "";
house.state = "desocupada" house.state = "desocupada"
} }

View File

@ -133,7 +133,10 @@ export class UsersController {
@MessagePattern({ cmd: 'deleteTenant' }) @MessagePattern({ cmd: 'deleteTenant' })
deleteTenant(@Payload() user: any) { 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' }) @MessagePattern({ cmd: 'changeStatus' })

View File

@ -29,7 +29,7 @@ export class UsersService {
let passwordEncriptada = Md5.init(user.password); let passwordEncriptada = Md5.init(user.password);
user.password = passwordEncriptada; user.password = passwordEncriptada;
let userCreated = await this.userModel.create(user); 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); let community = await this.findCommunity(user.community_id);
user.community_id = community['name']; user.community_id = community['name'];
@ -195,7 +195,7 @@ export class UsersService {
} }
async deleteAdminSystem(id: string) { async deleteAdminSystem(id: string) {
return this.userModel.findOneAndUpdate({ _id: id }, { status: '-1' }, { return this.userModel.findOneAndUpdate({ _id: id }, { status: '-1', number_house:'' }, {
new: true, new: true,
}); });
} }
@ -206,10 +206,18 @@ export class UsersService {
}); });
} }
async deleteTenant(id: string) { async deleteTenant(tenant_id: string, community_id: string, number_house: string) {
return this.userModel.findOneAndUpdate({ _id: id }, { status: '-1' }, {
new: true, 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) { 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 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 return await this.clientCommunityApp
.send<string>(pattern, payload) .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 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 return await this.clientCommunityApp
.send<string>(pattern, payload) .send<string>(pattern, payload)
.pipe( .pipe(

View File

@ -184,15 +184,49 @@ const Inquilinos = () => {
} }
const deleteTenant = () => { const deleteTenant = () => {
let _tenants = tenants.filter((val) => val._id !== tenant._id)
setTenants(_tenants) let _tenant = {
setDeleteTenantDialog(false) community_id: tenant.community_id,
setTenant(emptyTenant) number_house: tenant.number_house
toast.current.show({ };
severity: 'success',
summary: 'Inquilino Eliminado', fetch('http://localhost:4000/user/deleteTenant/' + tenant._id, {
life: 3000, 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 = () => { const deleteSelectedTenants = () => {