From df2c9326463461cb0823d29b2811c1715cabcdd2 Mon Sep 17 00:00:00 2001 From: Mariela Date: Thu, 21 Jul 2022 04:27:39 -0600 Subject: [PATCH] registro de comunidad --- api-gateway/src/app.controller.ts | 4 +- api-gateway/src/app.service.ts | 4 +- .../src/schemas/community.schema.ts | 4 +- .../src/schemas/house.schema.ts | 4 +- .../src/schemas/tenant.schema.ts | 2 +- .../src/components/ComunidadViviendas.js | 229 +++++++++++++----- 6 files changed, 183 insertions(+), 64 deletions(-) diff --git a/api-gateway/src/app.controller.ts b/api-gateway/src/app.controller.ts index b2b65e09..c71d9b54 100644 --- a/api-gateway/src/app.controller.ts +++ b/api-gateway/src/app.controller.ts @@ -78,11 +78,11 @@ export class AppController { @Body('canton') canton: string, @Body('district') district: string, @Body('num_houses') num_houses: number, - @Body('phone') phone: number, + @Body('phone') phone: string, @Body('quote') quote: number, @Body('status') status: string, @Body('date_entry') date_entry: Date, - @Body('houses') houses: [{}], + @Body('houses') houses: [], ) { return this.appService.createCommunity(name, province, canton, diff --git a/api-gateway/src/app.service.ts b/api-gateway/src/app.service.ts index badc59ab..976807ed 100644 --- a/api-gateway/src/app.service.ts +++ b/api-gateway/src/app.service.ts @@ -131,11 +131,11 @@ export class AppService { //POST parameter from API createCommunity(name: string, province: string, canton: string, district: string - , num_houses: number, phone: number, quote: number, status: string, date_entry: Date, houses: [{}]) { + , num_houses: number, phone: string, quote: number, status: string, date_entry: Date, houses: []) { const pattern = { cmd: 'createCommunity' }; const payload = { name: name, province: province, canton: canton, district: district, num_houses: num_houses, - phone: phone, quote: quote, status: status, date_entry: date_entry, houses + phone: phone, quote: quote, status: status, date_entry: date_entry, houses: houses }; return this.clientCommunityApp .send(pattern, payload) diff --git a/servicio-comunidad-viviendas/src/schemas/community.schema.ts b/servicio-comunidad-viviendas/src/schemas/community.schema.ts index bc00ed43..92c57eb0 100644 --- a/servicio-comunidad-viviendas/src/schemas/community.schema.ts +++ b/servicio-comunidad-viviendas/src/schemas/community.schema.ts @@ -10,7 +10,7 @@ export class Community { @Prop() id_admin: string; - @Prop({ default: "Sin Administrador" }) + @Prop() name_admin: string ; @Prop() @@ -29,7 +29,7 @@ export class Community { num_houses: number; @Prop() - phone: number; + phone: string; @Prop() quote: number; diff --git a/servicio-comunidad-viviendas/src/schemas/house.schema.ts b/servicio-comunidad-viviendas/src/schemas/house.schema.ts index 905f8664..dbb542be 100644 --- a/servicio-comunidad-viviendas/src/schemas/house.schema.ts +++ b/servicio-comunidad-viviendas/src/schemas/house.schema.ts @@ -1,5 +1,7 @@ import { Schema, Prop, SchemaFactory } from '@nestjs/mongoose'; +import e from 'express'; import { Document } from 'mongoose'; +import { empty } from 'rxjs'; import { Tenant, TenantSchema } from './tenant.schema'; @@ -15,7 +17,7 @@ export class House extends Document { @Prop({ default: "desocupada" }) state: string; - @Prop({ type: TenantSchema, default: " " }) + @Prop({ type: TenantSchema }) tenants: Tenant; } export const HouseSchema = SchemaFactory.createForClass(House); \ No newline at end of file diff --git a/servicio-comunidad-viviendas/src/schemas/tenant.schema.ts b/servicio-comunidad-viviendas/src/schemas/tenant.schema.ts index e0d66fa4..c0e66ec5 100644 --- a/servicio-comunidad-viviendas/src/schemas/tenant.schema.ts +++ b/servicio-comunidad-viviendas/src/schemas/tenant.schema.ts @@ -4,7 +4,7 @@ import { Schema, Prop, SchemaFactory } from '@nestjs/mongoose'; @Schema() export class Tenant { - @Prop() + @Prop( {default: ''}) tenant_id: string; } diff --git a/web-ui/web-react/src/components/ComunidadViviendas.js b/web-ui/web-react/src/components/ComunidadViviendas.js index b3841e64..b436bbbf 100644 --- a/web-ui/web-react/src/components/ComunidadViviendas.js +++ b/web-ui/web-react/src/components/ComunidadViviendas.js @@ -1,20 +1,44 @@ -import React, { useEffect, useState } from 'react'; +import React, { useEffect, useState, useRef } from 'react'; import { InputText } from 'primereact/inputtext'; import { Button } from 'primereact/button'; import { DataTable } from 'primereact/datatable'; import { Column } from 'primereact/column'; import { Dropdown } from 'primereact/dropdown'; +import { Toast } from 'primereact/toast'; +import classNames from 'classnames'; const Communities = () => { + + let emptyCommunity = { + name: '', + province: provinciaId, + canton: cantonId, + district: districtId, + phone: '', + num_houses: 0, + status: 'activo', + date_entry: new Date(), + houses: [], + quote: 0 + }; + + const [communitiesList, setCommunitiesList] = useState([]); + const [community, setCommunity] = useState(emptyCommunity); + const [housesList, setHousesList] = useState([]); const [provincesList, setProvincesList] = useState([]); - const [provinciaId, setProvinciaId] = useState(); + const [provinciaId, setProvinciaId] = useState(null); const [cantonsList, setCantonsList] = useState([]); const [cantonId, setCantonId] = useState(null); const [districtsList, setDistrictsList] = useState([]); const [districtId, setDistrictId] = useState(null); + const [submitted, setSubmitted] = useState(false); + const toast = useRef(null); + const dt = useRef(null); + + const p = provincesList.map((item) => ({ label: item.name, @@ -35,36 +59,56 @@ const Communities = () => { })) - async function getCommunites() { - let response = await fetch('http://localhost:4000/community/allCommunities', { method: 'GET' }); - let list = await response.json(); - setCommunitiesList(list.message); - } + useEffect(() => { - getCommunites(); + fillProvinces(); }, []) useEffect(() => { - getProvinces(); - - }, []) - - useEffect(() => { - getCantons(); + fillCantons(); }, [provinciaId]) useEffect(() => { - getDistricts(); + fillDistricts(); }, [cantonId]) - async function getProvinces() { const response = await fetch('assets/demo/data/provincias.json', { method: 'GET' }); - const getP = await response.json(); - setProvincesList(await getP) + return await response.json(); + } + + async function fillProvinces() { + const getP = await getProvinces(); + setProvincesList(await getP); + } + + async function getCantons() { + const response = await fetch('assets/demo/data/cantones.json', { method: 'GET' }); + return await response.json(); + } + + async function fillCantons() { + const resJson = await getCantons(); + const cantones = await resJson.filter(function (i, n) { + return i.parentCode === provinciaId; + }); + setCantonsList(await cantones); + } + + async function getDistricts() { + const response = await fetch('assets/demo/data/distritos.json', { method: 'GET' }); + return await response.json(); + } + + async function fillDistricts() { + const resJson = await getDistricts(); + const cantones = await resJson.filter(function (i, n) { + return i.parentCode === provinciaId; + }); + setCantonsList(await cantones); } const handleProvinces = (event) => { @@ -72,36 +116,106 @@ const Communities = () => { setProvinciaId(getprovinciaId); } - - async function getCantons() { - const response = await fetch('assets/demo/data/cantones.json', { method: 'GET' }); - const resJson = await response.json(); - const cantones = await resJson.filter(function (i, n) { - return i.parentCode === provinciaId; - }); - setCantonsList(await cantones); - } - const handleCanton = (event) => { const getCantonId = event.target.value; setCantonId(getCantonId); } - async function getDistricts() { - const response = await fetch('assets/demo/data/distritos.json', { method: 'GET' }); - const resJson = await response.json(); - const distrits = await resJson.filter(function (i, n) { - return i.parentCode === cantonId; - }); - setDistrictsList(await distrits); + const handleDistrict = (event) => { + const getDistrictId = event.target.value; + setDistrictId(getDistrictId); } + async function getCommunites() { + let response = await fetch('http://localhost:4000/community/allCommunities', { method: 'GET' }); + let resJson = await response.json(); + let pList = await getProvinces(); + let cList = await getCantons(); + let dList = await getDistricts(); + await resJson.message.map((item) => { + item.province = pList.find(p => p.code === item.province).name + item.canton = cList.find(p => p.code === item.canton).name + item.district = dList.find(p => p.code === item.district).name + }) + setCommunitiesList(await resJson.message); + } + + useEffect(() => { + getCommunites(); + + }, []) + + + const saveCommunity = () => { + setSubmitted(true); + + if (community.name.trim()) { + let _communities = [...communitiesList]; + let _community = { ...community }; + _community.province = provinciaId; + _community.canton = cantonId; + _community.district = districtId; + + + + for (let i = 0; i < _community.num_houses; i++){ + _community.houses.push({ + number_house: i+1, + description: "es esta descripcion", + }) + } + + + _communities.push(_community); + toast.current.show({ severity: 'success', summary: 'Successful', detail: 'Community Created', life: 3000 }); + + setCommunitiesList(_communities); + + setProvinciaId(''); + setCantonId(''); + setDistrictId(''); + setCommunity(emptyCommunity); + + + + // console.log(houses) + fetch('http://localhost:4000/community/createCommunity', { + cache: 'no-cache', + method: 'POST', + body: JSON.stringify(_community), + 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(); + } + ) + .catch( + err => console.log('Ocurrió un error con el fetch', err) + ); + } + + } + + const onInputChange = (e, name) => { + const val = (e.target && e.target.value) || ''; + let _community = { ...community }; + _community[`${name}`] = val; + + setCommunity(_community); + } return (
Comunidades de Viviendas
+ @@ -116,81 +230,84 @@ const Communities = () => {
+ +
Registro de comunidad de viviendas
- +
- + - + onInputChange(e, 'name')} required autoFocus className={classNames({ 'p-invalid': submitted && !community.name } )} />
+ {submitted && community.name==='' && Nombre es requirido.}
- +
- + - +
+ {submitted && !community.province && Provincia es requirido.}
- + - +
+ {submitted && !community.canton && Cantón es requirido.}
- + - +
+ {submitted && !community.district && Distrito es requirido.}
-
-
- +
- + - - + onInputChange(e, 'phone')} required autoFocus className={classNames({ 'p-invalid': submitted && !community.phone } )} />
+ {submitted && community.phone==='' && Número de teléfono es requirido.}
-
- + - - + onInputChange(e, 'num_houses')} required autoFocus className={classNames({ 'p-invalid': submitted && community.num_houses < 1 } )} />
+ {submitted && community.num_houses < 1 && Número de viviendas es requirido y debe ser mayor que 0.}
- +