From 31ef827baf30d9b9eda6c21b91419de838fe0584 Mon Sep 17 00:00:00 2001 From: Mariela Date: Tue, 19 Jul 2022 13:56:48 -0600 Subject: [PATCH] get user admin comunidad de vivienda desde servicio comunidades --- api-gateway/src/app.controller.ts | 8 +++ api-gateway/src/app.service.ts | 12 ++++ .../src/communities/communities.controller.ts | 6 ++ .../src/communities/communities.module.ts | 11 ++++ .../src/communities/communities.service.ts | 57 ++++++++++++++++--- .../src/schemas/admin.entity.ts | 4 ++ .../src/schemas/community.schema.ts | 6 ++ .../src/schemas/house.schema.ts | 3 + .../src/users/users.controller.ts | 9 +++ servicio-usuarios/src/users/users.service.ts | 6 ++ .../src/components/ComunidadViviendas.js | 11 ++-- 11 files changed, 121 insertions(+), 12 deletions(-) create mode 100644 servicio-comunidad-viviendas/src/schemas/admin.entity.ts diff --git a/api-gateway/src/app.controller.ts b/api-gateway/src/app.controller.ts index d3a45f7e..b2b65e09 100644 --- a/api-gateway/src/app.controller.ts +++ b/api-gateway/src/app.controller.ts @@ -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( diff --git a/api-gateway/src/app.service.ts b/api-gateway/src/app.service.ts index 8af78e63..badc59ab 100644 --- a/api-gateway/src/app.service.ts +++ b/api-gateway/src/app.service.ts @@ -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(pattern, payload) + .pipe( + map((message: string) => ({ message })), + ); + } + // ====================== COMMUNITIES =============================== //POST parameter from API diff --git a/servicio-comunidad-viviendas/src/communities/communities.controller.ts b/servicio-comunidad-viviendas/src/communities/communities.controller.ts index 457f4de6..159c67fa 100644 --- a/servicio-comunidad-viviendas/src/communities/communities.controller.ts +++ b/servicio-comunidad-viviendas/src/communities/communities.controller.ts @@ -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); diff --git a/servicio-comunidad-viviendas/src/communities/communities.module.ts b/servicio-comunidad-viviendas/src/communities/communities.module.ts index e515e738..30c0d9b0 100644 --- a/servicio-comunidad-viviendas/src/communities/communities.module.ts +++ b/servicio-comunidad-viviendas/src/communities/communities.module.ts @@ -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], diff --git a/servicio-comunidad-viviendas/src/communities/communities.service.ts b/servicio-comunidad-viviendas/src/communities/communities.service.ts index 58b83794..a6679581 100644 --- a/servicio-comunidad-viviendas/src/communities/communities.service.ts +++ b/servicio-comunidad-viviendas/src/communities/communities.service.ts @@ -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 { 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, - ) {} + @Inject('SERVICIO_USUARIOS') private readonly clientUserApp: ClientProxy, + + ) { } async create(community: CommunityDocument): Promise { return this.communityModel.create(community); } - async findAll(): Promise { - return this.communityModel - .find() - .setOptions({ sanitizeFilter: true }) - .exec(); + async findAll(): Promise { + + return await this.communityModel + .find() + .setOptions({ sanitizeFilter: true }) + .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 { @@ -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(pattern, payload) + .pipe( + map((response: string) => ({ response })) + ) + + const finalValue = await lastValueFrom(callback); + return finalValue['response']; + + } } diff --git a/servicio-comunidad-viviendas/src/schemas/admin.entity.ts b/servicio-comunidad-viviendas/src/schemas/admin.entity.ts new file mode 100644 index 00000000..c9648048 --- /dev/null +++ b/servicio-comunidad-viviendas/src/schemas/admin.entity.ts @@ -0,0 +1,4 @@ +export interface Admin { + id_community: string; + user_type: string; +} \ No newline at end of file diff --git a/servicio-comunidad-viviendas/src/schemas/community.schema.ts b/servicio-comunidad-viviendas/src/schemas/community.schema.ts index c1b6ecf3..bc00ed43 100644 --- a/servicio-comunidad-viviendas/src/schemas/community.schema.ts +++ b/servicio-comunidad-viviendas/src/schemas/community.schema.ts @@ -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; + } diff --git a/servicio-comunidad-viviendas/src/schemas/house.schema.ts b/servicio-comunidad-viviendas/src/schemas/house.schema.ts index f8687b54..905f8664 100644 --- a/servicio-comunidad-viviendas/src/schemas/house.schema.ts +++ b/servicio-comunidad-viviendas/src/schemas/house.schema.ts @@ -9,6 +9,9 @@ export class House extends Document { @Prop({ default: " " }) number_house: string; + @Prop({ default: " " }) + description: string; + @Prop({ default: "desocupada" }) state: string; diff --git a/servicio-usuarios/src/users/users.controller.ts b/servicio-usuarios/src/users/users.controller.ts index 87ed3541..7f4718dd 100644 --- a/servicio-usuarios/src/users/users.controller.ts +++ b/servicio-usuarios/src/users/users.controller.ts @@ -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"]); + } + + } + diff --git a/servicio-usuarios/src/users/users.service.ts b/servicio-usuarios/src/users/users.service.ts index 5e506697..2040c3b5 100644 --- a/servicio-usuarios/src/users/users.service.ts +++ b/servicio-usuarios/src/users/users.service.ts @@ -99,4 +99,10 @@ export class UsersService { ); } + + + async findCommunityUser(community_id: string, user_type: number): Promise { + return this.userModel.findOne({ community_id: community_id, user_type: user_type }).exec(); + } + } diff --git a/web-ui/web-react/src/components/ComunidadViviendas.js b/web-ui/web-react/src/components/ComunidadViviendas.js index c088d151..54ceef00 100644 --- a/web-ui/web-react/src/components/ComunidadViviendas.js +++ b/web-ui/web-react/src/components/ComunidadViviendas.js @@ -25,13 +25,16 @@ const Communities = () => {
-
Administradores de comunidad
+
Comunidades de Viviendas
- - - + + + + + +