Merge pull request #106 from DeimosPr4/UH-listarComunidadesdeViviendas

listar comunidades de viviendas
This commit is contained in:
Maria Sánchez 2022-07-19 13:58:57 -06:00 committed by GitHub
commit 4df366362d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 172 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

@ -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();

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

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

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

@ -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 = () => {
<Route path="/documentation" component={Documentation} />
<Route path="/administradoresSistema" component={AdministradoresSistema} />
<Route path="/administradoresComunidad" component={AdministradoresComunidad} />
<Route path="/comunidadesViviendas" component={Communities} />
<Route path="/logIn" component={LogIn} />
</div>

View File

@ -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 (
<div className="grid">
<div className="col-12">
<div className="card">
<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="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>
</div>
)
}
export default React.memo(Communities);