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/api-gateway/src/main.ts b/api-gateway/src/main.ts index 3bea0f30..ead48887 100644 --- a/api-gateway/src/main.ts +++ b/api-gateway/src/main.ts @@ -1,9 +1,13 @@ import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module'; -const cors= require('cors'); +const cors = require('cors'); async function bootstrap() { const app = await NestFactory.create(AppModule); + app.enableCors({ + origin: 'http://localhost:3000', + methods: 'GET, PUT, POST, DELETE', + allowedHeaders: 'Content-Type, Authorization', + }); await app.listen(4000); - app.use(cors()); } bootstrap(); 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 0ee35e1d..905f8664 100644 --- a/servicio-comunidad-viviendas/src/schemas/house.schema.ts +++ b/servicio-comunidad-viviendas/src/schemas/house.schema.ts @@ -7,11 +7,14 @@ import { Tenant, TenantSchema } from './tenant.schema'; @Schema() export class House extends Document { @Prop({ default: " " }) - number: string; + number_house: string; @Prop({ default: " " }) description: string; + @Prop({ default: "desocupada" }) + state: string; + @Prop({ type: TenantSchema, default: " " }) tenants: Tenant; } 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 63438e01..2040c3b5 100644 --- a/servicio-usuarios/src/users/users.service.ts +++ b/servicio-usuarios/src/users/users.service.ts @@ -16,7 +16,7 @@ export class UsersService { @Inject('SERVICIO_NOTIFICACIONES') private readonly clientNotificationtApp: ClientProxy, ) { } - private publicKey: string; + private publicKey: string; async create(user: UserDocument): Promise { let passwordEncriptada = Md5.init(user.password); user.password = passwordEncriptada; @@ -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/App.js b/web-ui/web-react/src/App.js index 83530986..a98659e9 100644 --- a/web-ui/web-react/src/App.js +++ b/web-ui/web-react/src/App.js @@ -30,6 +30,7 @@ import BlocksDemo from './templates/BlocksDemo'; import IconsDemo from './templates/IconsDemo'; import AdministradoresSistema from './components/AdministradoresSistema'; import AdministradoresComunidad from './components/AdministradoresComunidad'; +import Communities from './components/ComunidadViviendas'; import Crud from './pages/Crud'; import EmptyPage from './pages/EmptyPage'; @@ -165,6 +166,7 @@ const App = () => { {label: 'Dashboard', icon: 'pi pi-fw pi-home', to: '/'}, {label: 'Administradores del sistema', icon: 'pi pi-fw pi-id-card', to: '/administradoresSistema'}, {label: 'Administradores de comunidad', icon: 'pi pi-fw pi-id-card', to: '/administradoresComunidad'}, + {label: 'Comunidadades', icon: 'pi pi-fw pi-id-card', to: '/comunidadesViviendas'}, {label: 'Log in', icon: 'pi pi-fw pi-id-card', to: '/logIn'} ] }, @@ -320,6 +322,7 @@ const App = () => { + diff --git a/web-ui/web-react/src/components/ComunidadViviendas.js b/web-ui/web-react/src/components/ComunidadViviendas.js new file mode 100644 index 00000000..54ceef00 --- /dev/null +++ b/web-ui/web-react/src/components/ComunidadViviendas.js @@ -0,0 +1,47 @@ +import React, { useEffect, useState } from 'react'; +import { InputText } from 'primereact/inputtext'; +import { Button } from 'primereact/button'; +import { DataTable } from 'primereact/datatable'; +import { Column } from 'primereact/column'; + + +const Communities = () => { + const [communitiesList, setCommunitiesList] = useState([]); + const [housesList, setHousesList] = useState([]); + + + async function getCommunites() { + let response = await fetch('http://localhost:4000/community/allCommunities', { method: 'GET' }); + let list = await response.json(); + setCommunitiesList(list.message); + } + + useEffect(() => { + getCommunites(); + }, []) + + + return ( +
+
+
+
Comunidades de Viviendas
+ + + + + + + + + + +
+
+
+ + +) +} + +export default React.memo(Communities); \ No newline at end of file