get user admin comunidad de vivienda desde servicio comunidades

This commit is contained in:
Mariela 2022-07-19 13:56:48 -06:00
parent 17604ace92
commit 31ef827baf
11 changed files with 121 additions and 12 deletions

View File

@ -110,6 +110,14 @@ export class AppController {
}
@Post('community/findCommunityAdmin')
findCommunityAdmin(
@Body('community_id') community_id: string,
) {
return this.appService.findCommunityAdmin(community_id);
}
// #==== API Common Areas
@Post('commonArea/createCommonArea')
createCommonArea(

View File

@ -115,6 +115,18 @@ export class AppService {
);
}
//GET parameter from API
findCommunityAdmin(community_id: string) {
const pattern = { cmd: 'findCommunityAdmin' };
const payload = { community_id: community_id };
return this.clientCommunityApp
.send<string>(pattern, payload)
.pipe(
map((message: string) => ({ message })),
);
}
// ====================== COMMUNITIES ===============================
//POST parameter from API

View File

@ -29,6 +29,12 @@ export class CommunitiesController {
return this.communitiesService.findOneName(_id);
}
/* @MessagePattern({cmd: 'findCommunityAdmin'})
findCommunityAdmin(@Payload() community: any) {
let _community = community['community_id'];
return this.communitiesService.findCommunityAdmin(_community, "2");
}*/
@MessagePattern({cmd: 'updateCommunity'})
update(@Payload() community: CommunityDocument) {
return this.communitiesService.update(community.id, community);

View File

@ -2,12 +2,23 @@ import { Module } from '@nestjs/common';
import { CommunitiesService } from './communities.service';
import { CommunitiesController } from './communities.controller';
import { MongooseModule } from '@nestjs/mongoose';
import { ClientsModule, Transport } from "@nestjs/microservices";
import { Community, CommunitySchema } from '../schemas/community.schema';
@Module({
imports: [
ClientsModule.register([
{
name: "SERVICIO_USUARIOS",
transport: Transport.TCP,
options: {
host: "127.0.0.1",
port: 3001
}
}
]),
MongooseModule.forFeature([{ name: Community.name, schema: CommunitySchema }]),
],
controllers: [CommunitiesController],

View File

@ -1,24 +1,50 @@
import { Injectable } from '@nestjs/common';
import { Injectable, Inject } from '@nestjs/common';
import { Model } from 'mongoose';
import { Community, CommunityDocument } from 'src/schemas/community.schema';
import { InjectModel } from '@nestjs/mongoose';
import { RpcException, ClientProxy } from '@nestjs/microservices';
import { from, lastValueFrom, map, scan, mergeMap } from 'rxjs';
import { Admin } from 'src/schemas/admin.entity';
import { appendFileSync } from 'fs';
@Injectable()
export class CommunitiesService {
constructor(
@InjectModel(Community.name) private readonly communityModel: Model<CommunityDocument>,
) {}
@Inject('SERVICIO_USUARIOS') private readonly clientUserApp: ClientProxy,
) { }
async create(community: CommunityDocument): Promise<Community> {
return this.communityModel.create(community);
}
async findAll(): Promise<Community[]> {
return this.communityModel
return await this.communityModel
.find()
.setOptions({ sanitizeFilter: true })
.exec();
.exec()
.then( async community => {
if(community){
await Promise.all(community.map(async c => {
let admin = await this.findCommunityAdmin(c["_id"], "2")
if(admin){
c["id_admin"] = admin["_id"]
c["name_admin"] = admin["name"]
}
return c
}))
console.log(community)
}
return community;
})
//buscar al usuario con el id de la comunidad anexado
}
findOne(id: string): Promise<Community> {
@ -37,4 +63,19 @@ export class CommunitiesService {
async remove(id: string) {
return this.communityModel.findByIdAndRemove({ _id: id }).exec();
}
async findCommunityAdmin(community: string, user_type: string) {
const pattern = { cmd: 'findOneCommunityUser' }
const payload = { community_id: community, user_type: user_type }
let callback = await this.clientUserApp
.send<string>(pattern, payload)
.pipe(
map((response: string) => ({ response }))
)
const finalValue = await lastValueFrom(callback);
return finalValue['response'];
}
}

View File

@ -0,0 +1,4 @@
export interface Admin {
id_community: string;
user_type: string;
}

View File

@ -7,6 +7,11 @@ export type CommunityDocument = Community & Document;
@Schema({ collection: 'communities' })
export class Community {
@Prop()
id_admin: string;
@Prop({ default: "Sin Administrador" })
name_admin: string ;
@Prop()
name: string;
@ -37,6 +42,7 @@ export class Community {
@Prop({ type: [HouseSchema] })
houses: Array<House>;
}

View File

@ -9,6 +9,9 @@ export class House extends Document {
@Prop({ default: " " })
number_house: string;
@Prop({ default: " " })
description: string;
@Prop({ default: "desocupada" })
state: string;

View File

@ -66,4 +66,13 @@ export class UsersController {
testSendMail(@Payload() user: UserDocument) {
return this.userService.testSendMail(user);
}
//buscar usuario de una comunidad
@MessagePattern({ cmd: 'findOneCommunityUser' })
findCommunityUser(@Payload() user: any) {
return this.userService.findCommunityUser(user["community_id"], user["user_type"]);
}
}

View File

@ -99,4 +99,10 @@ export class UsersService {
);
}
async findCommunityUser(community_id: string, user_type: number): Promise<User> {
return this.userModel.findOne({ community_id: community_id, user_type: user_type }).exec();
}
}

View File

@ -25,13 +25,16 @@ const Communities = () => {
<div className="grid">
<div className="col-12">
<div className="card">
<h5>Administradores de comunidad</h5>
<h5>Comunidades de Viviendas</h5>
<DataTable value={communitiesList} scrollable scrollHeight="400px" scrollDirection="both" className="mt-3">
<Column field="name" header="Nombre" style={{ flexGrow: 1, flexBasis: '160px' }}></Column>
<Column field="last_name" header="Provincia" style={{ flexGrow: 1, flexBasis: '160px' }}></Column>
<Column field="dni" header="Cantón" style={{ flexGrow: 1, flexBasis: '160px' }}></Column>
<Column field="email" header="Distrito" style={{ flexGrow: 1, flexBasis: '160px' }}></Column>
<Column field="province" header="Provincia" style={{ flexGrow: 1, flexBasis: '160px' }}></Column>
<Column field="canton" header="Cantón" style={{ flexGrow: 1, flexBasis: '160px' }}></Column>
<Column field="district" header="Distrito" style={{ flexGrow: 1, flexBasis: '160px' }}></Column>
<Column field="phone" header="Telefóno" style={{ flexGrow: 1, flexBasis: '180px' }}></Column>
<Column field="num_houses" header="Número de viviendas" style={{ flexGrow: 1, flexBasis: '180px' }}></Column>
<Column field="quote" header="Cuota mensual" style={{ flexGrow: 1, flexBasis: '180px' }}></Column>
<Column field="name_admin" header="Administrador" style={{ flexGrow: 1, flexBasis: '180px' }}></Column>
</DataTable>
</div>
</div>