From b0791c2ed59002867242309ea693436f9150d116 Mon Sep 17 00:00:00 2001 From: Mariela Date: Thu, 1 Sep 2022 01:31:20 -0600 Subject: [PATCH 01/10] editar informacion como admin de vivienda --- api-gateway/src/app.controller.ts | 14 +- api-gateway/src/app.service.ts | 10 + servicio-usuarios/src/schemas/user.schema.ts | 14 +- .../src/users/users.controller.ts | 11 +- servicio-usuarios/src/users/users.service.ts | 23 +- web-ui/web-react/package-lock.json | 47 ++ web-ui/web-react/package.json | 1 + .../src/components/PerfilAdminComunidad.js | 449 ++++++++++++++++-- 8 files changed, 518 insertions(+), 51 deletions(-) diff --git a/api-gateway/src/app.controller.ts b/api-gateway/src/app.controller.ts index 2b5fa6e3..7e9cf593 100644 --- a/api-gateway/src/app.controller.ts +++ b/api-gateway/src/app.controller.ts @@ -329,7 +329,19 @@ export class AppController { number_house, ); } - + + @Put('user/changePassword/:id') + changePassword( + @Param('id') id: string, + @Body('password') password: string, + ) { + + return this.appService.changePassword( + id, + password, + ); + } + // #==== API Communities @Post('community/createCommunity') createCommunity( diff --git a/api-gateway/src/app.service.ts b/api-gateway/src/app.service.ts index 1334ab7d..341efaec 100644 --- a/api-gateway/src/app.service.ts +++ b/api-gateway/src/app.service.ts @@ -396,6 +396,16 @@ export class AppService { } + changePassword(id: string, password: string) { + const pattern = { cmd: 'changePassword' }; + const payload = { id: id, password: password }; + return this.clientUserApp + .send(pattern, payload) + .pipe(map((message: string) => ({ message }))); + } + + + // ====================== COMMUNITIES =============================== changeStatusCommunity(pId: string, pStatus: string) { const pattern = { cmd: 'changeStatus' }; diff --git a/servicio-usuarios/src/schemas/user.schema.ts b/servicio-usuarios/src/schemas/user.schema.ts index f09a3231..38c97157 100644 --- a/servicio-usuarios/src/schemas/user.schema.ts +++ b/servicio-usuarios/src/schemas/user.schema.ts @@ -7,20 +7,20 @@ export type UserDocument = User & Document; @Schema({ collection: 'users'}) export class User { - @Prop({index: true}) - dni!: string; + @Prop() + dni: string; - @Prop({required: true}) + @Prop() name: string; - @Prop({required: true}) + @Prop() last_name: string; - @Prop({required: true, unique: true}) + @Prop() email: string; - @Prop({required: true, unique: true}) - phone: number; + @Prop() + phone: string; @Prop() password: string; diff --git a/servicio-usuarios/src/users/users.controller.ts b/servicio-usuarios/src/users/users.controller.ts index 81a296b6..0bf8f960 100644 --- a/servicio-usuarios/src/users/users.controller.ts +++ b/servicio-usuarios/src/users/users.controller.ts @@ -72,7 +72,7 @@ export class UsersController { @MessagePattern({ cmd: 'updateUser' }) update(@Payload() user: UserDocument) { - return this.userService.update(user._id, user); + return this.userService.update(user['id'], user); } @MessagePattern({ cmd: 'updateGuard' }) @@ -165,4 +165,13 @@ export class UsersController { let pstatus = body['status']; return this.userService.changeStatus(pid, pstatus); } + + @MessagePattern({ cmd: 'changePassword' }) + changePassword(@Payload() body: string) { + let pid = body['id']; + let password = body['password']; + return this.userService.changePassword(pid, password); + } + + } diff --git a/servicio-usuarios/src/users/users.service.ts b/servicio-usuarios/src/users/users.service.ts index f074c01d..0e5a3dff 100644 --- a/servicio-usuarios/src/users/users.service.ts +++ b/servicio-usuarios/src/users/users.service.ts @@ -117,10 +117,10 @@ export class UsersService { } async update(id: string, user: UserDocument) { - console.log(id) - console.log(user) - - return this.userModel.findOneAndUpdate({ _id: id }, user, { + return this.userModel.findOneAndUpdate({ _id: id }, { + name: user['name'], last_name: user['last_name'], + dni: user['dni'], email: user['email'], phone: user['phone'] + }, { new: true, }); } @@ -179,7 +179,6 @@ export class UsersService { } }); }); - return userReturn; } @@ -197,19 +196,14 @@ export class UsersService { return this.userModel.find({ user_type: 2 }).exec(); } - //find inquilinos async findTenants(): Promise { return this.userModel.find({ user_type: 3 }).exec(); } - //find inquilinos async findTenantsCommunity(pcommunity_id: string) { //let tenants = await this.findCommunityTenants(pcommunity_id); - - - return await this.userModel.find({ community_id: pcommunity_id, user_type: 4 }) } @@ -335,5 +329,14 @@ export class UsersService { await this.userModel.updateMany({community_id: community_id, user_type:'3' }, {"$set":{"community_id": '', "status": '-1'} }); return this.userModel.updateMany({ community_id: community_id, user_type: '4' }, { "$set": { "community_id": '', "status": '-1' } }); } + + + async changePassword(id: string, password: string) { + return this.userModel.findOneAndUpdate({ _id: id }, { password: password }, { + new: true, + }); + } + + } diff --git a/web-ui/web-react/package-lock.json b/web-ui/web-react/package-lock.json index 6699ac1c..423a1ef0 100644 --- a/web-ui/web-react/package-lock.json +++ b/web-ui/web-react/package-lock.json @@ -22,6 +22,7 @@ "chart.js": "3.3.2", "classnames": "^2.2.6", "cors": "^2.8.5", + "md5": "^2.3.0", "primeflex": "3.1.0", "primeicons": "^5.0.0", "primereact": "7.2.0", @@ -4677,6 +4678,14 @@ "resolved": "https://registry.npmjs.org/chardet/-/chardet-0.7.0.tgz", "integrity": "sha512-mT8iDcrh03qDGRRmoA2hmBJnxpllMR+0/0qlzjqZES6NdiWDcZkCNAk4rPFZ9Q85r27unkiNNg8ZOiwZXBHwcA==" }, + "node_modules/charenc": { + "version": "0.0.2", + "resolved": "https://registry.npmjs.org/charenc/-/charenc-0.0.2.tgz", + "integrity": "sha512-yrLQ/yVUFXkzg7EDQsPieE/53+0RlaWTs+wBrvW36cyilJ2SaDWfl4Yj7MtLTXleV9uEKefbAGUPv2/iWSooRA==", + "engines": { + "node": "*" + } + }, "node_modules/chart.js": { "version": "3.3.2", "resolved": "https://registry.npmjs.org/chart.js/-/chart.js-3.3.2.tgz", @@ -5384,6 +5393,14 @@ "semver": "bin/semver" } }, + "node_modules/crypt": { + "version": "0.0.2", + "resolved": "https://registry.npmjs.org/crypt/-/crypt-0.0.2.tgz", + "integrity": "sha512-mCxBlsHFYh9C+HVpiEacem8FEBnMXgU9gy4zmNC+SXAZNB/1idgp/aulFJ4FgCi7GPEVbfyng092GqL2k2rmow==", + "engines": { + "node": "*" + } + }, "node_modules/crypto-browserify": { "version": "3.12.0", "resolved": "https://registry.npmjs.org/crypto-browserify/-/crypto-browserify-3.12.0.tgz", @@ -10589,6 +10606,16 @@ "node": ">=0.10.0" } }, + "node_modules/md5": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/md5/-/md5-2.3.0.tgz", + "integrity": "sha512-T1GITYmFaKuO91vxyoQMFETst+O71VUPEU3ze5GNzDm0OWdP8v1ziTaAEPUr/3kLsY3Sftgz242A1SetQiDL7g==", + "dependencies": { + "charenc": "0.0.2", + "crypt": "0.0.2", + "is-buffer": "~1.1.6" + } + }, "node_modules/md5.js": { "version": "1.3.5", "resolved": "https://registry.npmjs.org/md5.js/-/md5.js-1.3.5.tgz", @@ -21618,6 +21645,11 @@ "resolved": "https://registry.npmjs.org/chardet/-/chardet-0.7.0.tgz", "integrity": "sha512-mT8iDcrh03qDGRRmoA2hmBJnxpllMR+0/0qlzjqZES6NdiWDcZkCNAk4rPFZ9Q85r27unkiNNg8ZOiwZXBHwcA==" }, + "charenc": { + "version": "0.0.2", + "resolved": "https://registry.npmjs.org/charenc/-/charenc-0.0.2.tgz", + "integrity": "sha512-yrLQ/yVUFXkzg7EDQsPieE/53+0RlaWTs+wBrvW36cyilJ2SaDWfl4Yj7MtLTXleV9uEKefbAGUPv2/iWSooRA==" + }, "chart.js": { "version": "3.3.2", "resolved": "https://registry.npmjs.org/chart.js/-/chart.js-3.3.2.tgz", @@ -22179,6 +22211,11 @@ } } }, + "crypt": { + "version": "0.0.2", + "resolved": "https://registry.npmjs.org/crypt/-/crypt-0.0.2.tgz", + "integrity": "sha512-mCxBlsHFYh9C+HVpiEacem8FEBnMXgU9gy4zmNC+SXAZNB/1idgp/aulFJ4FgCi7GPEVbfyng092GqL2k2rmow==" + }, "crypto-browserify": { "version": "3.12.0", "resolved": "https://registry.npmjs.org/crypto-browserify/-/crypto-browserify-3.12.0.tgz", @@ -26217,6 +26254,16 @@ "object-visit": "^1.0.0" } }, + "md5": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/md5/-/md5-2.3.0.tgz", + "integrity": "sha512-T1GITYmFaKuO91vxyoQMFETst+O71VUPEU3ze5GNzDm0OWdP8v1ziTaAEPUr/3kLsY3Sftgz242A1SetQiDL7g==", + "requires": { + "charenc": "0.0.2", + "crypt": "0.0.2", + "is-buffer": "~1.1.6" + } + }, "md5.js": { "version": "1.3.5", "resolved": "https://registry.npmjs.org/md5.js/-/md5.js-1.3.5.tgz", diff --git a/web-ui/web-react/package.json b/web-ui/web-react/package.json index 4cb7843c..cc71cde5 100644 --- a/web-ui/web-react/package.json +++ b/web-ui/web-react/package.json @@ -22,6 +22,7 @@ "chart.js": "3.3.2", "classnames": "^2.2.6", "cors": "^2.8.5", + "md5": "^2.3.0", "primeflex": "3.1.0", "primeicons": "^5.0.0", "primereact": "7.2.0", diff --git a/web-ui/web-react/src/components/PerfilAdminComunidad.js b/web-ui/web-react/src/components/PerfilAdminComunidad.js index 438c4312..cc73e17f 100644 --- a/web-ui/web-react/src/components/PerfilAdminComunidad.js +++ b/web-ui/web-react/src/components/PerfilAdminComunidad.js @@ -8,6 +8,13 @@ import { faMapLocationDot } from '@fortawesome/free-solid-svg-icons'; import { faPhoneAlt } from '@fortawesome/free-solid-svg-icons'; import { faHashtag } from '@fortawesome/free-solid-svg-icons'; import { faCircleQuestion } from '@fortawesome/free-solid-svg-icons'; +import { Button } from 'primereact/button'; +import { Toast } from 'primereact/toast'; +import { Dialog } from 'primereact/dialog'; +import { Toolbar } from 'primereact/toolbar'; +import { InputText } from 'primereact/inputtext'; +import classNames from 'classnames'; +import md5 from 'md5'; const PerfilAdminComunidad = () => { @@ -42,6 +49,13 @@ const PerfilAdminComunidad = () => { houses: [], }; + let emptyNewPassword = { + _id: null, + passwordOld: '', + passwordNew: '', + passwordConfirm: '' + } + const [admin, setAdmin] = useState(emptyAdminCommunity); const [community, setCommunity] = useState(emptyCommunity); const [cookies, setCookie] = useCookies(); @@ -52,34 +66,36 @@ const PerfilAdminComunidad = () => { const [provincesList, setProvincesList] = useState([]); const [cantonsList, setCantonsList] = useState([]); const [districtsList, setDistrictsList] = useState([]); + const [editAdminDialog, setEditAdminDialog] = useState(false); + const [editPasswordDialog, setEditPasswordDialog] = useState(false); + const [submitted, setSubmitted] = useState(false); + const toast = useRef(null); + const [newPassword, setNewPassword] = useState(emptyNewPassword); + + async function getProvinces() { const response = await fetch('assets/demo/data/provincias.json', { - method: 'GET', + method: 'GET', }); return await response.json(); - } - - - async function getCantons() { - const response = await fetch('assets/demo/data/cantones.json', { - method: 'GET', - }); - return await response.json(); - } - - - async function getDistricts() { - const response = await fetch('assets/demo/data/distritos.json', { - method: 'GET', - }); - return await response.json(); - } - - - + } + + + async function getCantons() { + const response = await fetch('assets/demo/data/cantones.json', { + method: 'GET', + }); + return await response.json(); + } + + async function getDistricts() { + const response = await fetch('assets/demo/data/distritos.json', { + method: 'GET', + }); + return await response.json(); + } - async function getAdmin() { await fetch('http://localhost:4000/user/findUserById/' + cookies.id, { method: 'GET' }) .then((response) => response.json()) @@ -100,6 +116,7 @@ const PerfilAdminComunidad = () => { getAdmin(); }, []) + async function getCommunity() { let pList = await getProvinces(); let cList = await getCantons(); @@ -135,8 +152,107 @@ const PerfilAdminComunidad = () => { tenantsList(cookies.community_id); }, []) + const saveAdmin = () => { + let _admin = { ...admin }; + _admin.community_id = cookies.community_id; + + if (_admin.name && _admin.dni && + _admin.last_name && _admin.email && + _admin.phone) { + + console.log(`Actualizando admnistrador de comunidad: ${_admin}`) + _admin.community_id = cookies.community_id; + console.log(`Actualizando admnistrador de comunidad: ${_admin}`) + + fetch(`http://localhost:4000/user/updateAdminCommunity/${_admin._id}`, { + cache: 'no-cache', + method: 'PUT', + body: JSON.stringify(_admin), + headers: { + 'Content-Type': 'application/json', + }, + }).then((response) => { + if (response.status !== 200) + console.log(`Hubo un error en el servicio: ${response.status}`) + else return response.json() + }).then((response) => { + + toast.current.show({ + severity: 'success', + summary: 'Éxito', + detail: 'Administrador de comunidad actualizado', + life: 3000, + }) + + setAdmin(response.message); + setEditAdminDialog(false); + + }) + + + } else { + setSubmitted(true); + } + + + } + + const savePassword = () => { + let _admin = { ...admin }; + let _newPassword = { ...newPassword }; + + if (_newPassword.passwordOld && _newPassword.passwordNew && + _newPassword.passwordNew === _newPassword.passwordConfirm) { + + _admin.password = md5(_newPassword.passwordNew); + console.log(`Actualizando admnistrador de comunidad: ${_admin}`) + + fetch(`http://localhost:4000/user/changePassword/${_admin._id}`, { + cache: 'no-cache', + method: 'PUT', + body: JSON.stringify(_admin), + headers: { + 'Content-Type': 'application/json', + }, + }).then((response) => { + if (response.status !== 200) + console.log(`Hubo un error en el servicio: ${response.status}`) + else return response.json() + }).then((response) => { + + toast.current.show({ + severity: 'success', + summary: 'Éxito', + detail: 'Administrador de comunidad actualizado', + life: 3000, + }) + setAdmin(_admin); + setEditPasswordDialog(false); + }) + + + } else { + setSubmitted(true); + } + + + } + + const findRepeated = async (name, value) => { + let _administrators; + await fetch('http://localhost:4000/user/findAdminComunidad/', { method: 'GET' }) + .then((response) => response.json()) + .then((data) => data.message) + .then(data => { + data = data.filter( + (val) => val.status != -1, + ); + _administrators = data; + }); + let value_filtered = await _administrators.filter(item => item[`${name}`] === value); + return value_filtered.length; + } - function findNameTenant(tenant_id) { let name = ''; if (tenant_id == '') { @@ -148,6 +264,105 @@ const PerfilAdminComunidad = () => { return name; } + const onInputChange = (e, name) => { + const val = (e.target && e.target.value) || ''; + let _admin = { ...admin }; + _admin[`${name}`] = val; + setAdmin(_admin); + } + + + const onInputChangePassword = (e, name) => { + const val = (e.target && e.target.value) || ''; + let _pass = { ...newPassword }; + _pass[`${name}`] = val; + setNewPassword(_pass); + } + + + const editAdmin = (admin) => { + setAdmin(admin); + setEditAdminDialog(true); + } + + const editPassword = () => { + setNewPassword(emptyNewPassword); + setEditPasswordDialog(true); + } + + const hideEditAdminDialog = () => { + setSubmitted(false); + setEditAdminDialog(false); + } + + const hideEditPasswordDialog = () => { + setSubmitted(false); + setEditPasswordDialog(false); + } + + const actionsAdminCommunity = (rowData) => { + return ( + <> +
+
+
+
+
+
+
+
+ + ) + } + + + const editAdminDialogFooter = ( + <> + From ca2015ee0b70ac5f0af14246d3067079317a1930 Mon Sep 17 00:00:00 2001 From: Eduardo Quiros Date: Thu, 1 Sep 2022 10:10:55 -0600 Subject: [PATCH 04/10] remove unnecessary console log --- web-ui/web-react/src/components/ComunidadViviendas.js | 1 - 1 file changed, 1 deletion(-) diff --git a/web-ui/web-react/src/components/ComunidadViviendas.js b/web-ui/web-react/src/components/ComunidadViviendas.js index 515142f0..e3b1c4ce 100644 --- a/web-ui/web-react/src/components/ComunidadViviendas.js +++ b/web-ui/web-react/src/components/ComunidadViviendas.js @@ -456,7 +456,6 @@ const Communities = () => { }; const updateCommunity = (community) => { - console.log(community); setCommunity(community); setSaveButtonLabel('Actualizar'); setHousesList(community.houses); From 0321b6ad006d188e15771aed2fc98db07e1f94c0 Mon Sep 17 00:00:00 2001 From: Eduardo Quiros Date: Thu, 1 Sep 2022 10:12:22 -0600 Subject: [PATCH 05/10] formatting --- .../src/components/ComunidadViviendas.js | 282 ++++++++++-------- 1 file changed, 150 insertions(+), 132 deletions(-) diff --git a/web-ui/web-react/src/components/ComunidadViviendas.js b/web-ui/web-react/src/components/ComunidadViviendas.js index e3b1c4ce..f80f1157 100644 --- a/web-ui/web-react/src/components/ComunidadViviendas.js +++ b/web-ui/web-react/src/components/ComunidadViviendas.js @@ -41,7 +41,7 @@ const Communities = () => { const [districtId, setDistrictId] = useState(null); const [codeHouses, setCodeHouses] = useState(''); const [submitted, setSubmitted] = useState(false); - const [saveButtonLabel, setSaveButtonLabel] = useState('Registrar') + const [saveButtonLabel, setSaveButtonLabel] = useState('Registrar'); const [selectedCommunities, setSelectedCommunities] = useState(null); const [globalFilter, setGlobalFilter] = useState(null); const [deleteCommunityDialog, setDeleteCommunityDialog] = useState(false); @@ -50,15 +50,11 @@ const Communities = () => { const toast = useRef(null); const dt = useRef(null); - - //para el perfil de la comunidad const [tenants, setTenants] = useState([]); const [communityDialog, setCommunityDialog] = useState(false); - - const p = provincesList.map((item) => ({ label: item.name, value: item.code, @@ -76,7 +72,6 @@ const Communities = () => { parent: item.parentCode, })); - async function getProvinces() { const response = await fetch('assets/demo/data/provincias.json', { method: 'GET', @@ -119,7 +114,6 @@ const Communities = () => { setDistrictsList(await districts); } - useEffect(() => { fillProvinces(); }, []); @@ -132,7 +126,6 @@ const Communities = () => { fillDistricts(); }, [cantonId]); - const handleProvinces = (event) => { const getprovinciaId = event.target.value; setProvinciaId(getprovinciaId); @@ -162,9 +155,7 @@ const Communities = () => { let pList = await getProvinces(); let cList = await getCantons(); let dList = await getDistricts(); - let data = await resJson.message.filter( - (val) => val.status != -1, - ) + let data = await resJson.message.filter((val) => val.status != -1); await data.map((item) => { if (item.status == '1') { item.status_text = 'Activo'; @@ -188,21 +179,20 @@ const Communities = () => { }, []); async function tenantsList(id) { - await fetch(`http://localhost:4000/user/findTenants/${id}`, { method: 'GET' }) + await fetch(`http://localhost:4000/user/findTenants/${id}`, { + method: 'GET', + }) .then((response) => response.json()) - .then(data => data.message) - .then(data => { - data = data.filter( - (val) => val.status != -1, - ) - setTenants(data) + .then((data) => data.message) + .then((data) => { + data = data.filter((val) => val.status != -1); + setTenants(data); }); } useEffect(() => { tenantsList(community._id); - }, []) - + }, []); const saveCommunity = () => { if ( @@ -272,14 +262,12 @@ const Communities = () => { } }; - - function findNameTenant(tenant_id) { let name = ''; if (tenant_id == '') { name = 'Sin inquilino'; } else { - let tenant = tenants.find(t => t._id == tenant_id) + let tenant = tenants.find((t) => t._id == tenant_id); name = tenant['name'] + ' ' + tenant['last_name']; } return name; @@ -340,7 +328,6 @@ const Communities = () => { if (community.status == '1') { community.status = '0'; community.status_text = 'Inactivo'; - } else if (community.status == '0') { community.status = '1'; community.status_text = 'Activo'; @@ -354,69 +341,66 @@ const Communities = () => { method: 'POST', body: JSON.stringify(data), headers: { - 'Content-Type': 'application/json' - } + '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(); - } - ) - .then( - function(response) { - setEditCommunityDialog(false); - toast.current.show({ - severity: 'success', - summary: 'Éxito', - detail: 'Comunidad de Viviendas Actualizada', - life: 3000, - }); - } - ) - .catch( - err => console.log('Ocurrió un error con el fetch', err) - ); - } + .then(function(response) { + if (response.status != 201) + console.log('Ocurrió un error con el servicio: ' + response.status); + else return response.json(); + }) + .then(function(response) { + setEditCommunityDialog(false); + toast.current.show({ + severity: 'success', + summary: 'Éxito', + detail: 'Comunidad de Viviendas Actualizada', + life: 3000, + }); + }) + .catch((err) => console.log('Ocurrió un error con el fetch', err)); + }; const deleteCommunity = () => { fetch('http://localhost:4000/community/deleteCommunity/' + community._id, { cache: 'no-cache', method: 'DELETE', headers: { - 'Content-Type': 'application/json' - } + '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(); - } - ) - .then( - function(response) { - - let _community = communitiesList.filter(val => val._id !== community._id); - setCommunitiesList(_community); - setDeleteCommunityDialog(false); - setCommunity(emptyCommunity); - toast.current.show({ severity: 'success', summary: 'Exito', detail: 'Comunidad de Viviendas Eliminada', life: 3000 }); - } - ) - .catch( - err => { - console.log('Ocurrió un error con el fetch', err) - toast.current.show({ severity: 'danger', summary: 'Error', detail: 'Comunidad de Viviendas no se pudo eliminar', life: 3000 }); - } - ); - let _communities = communitiesList.filter((val) => val._id !== community._id); - _communities = _communities.filter( - (val) => val.status != -1, - ) + .then(function(response) { + if (response.status != 201) + console.log('Ocurrió un error con el servicio: ' + response.status); + else return response.json(); + }) + .then(function(response) { + let _community = communitiesList.filter( + (val) => val._id !== community._id, + ); + setCommunitiesList(_community); + setDeleteCommunityDialog(false); + setCommunity(emptyCommunity); + toast.current.show({ + severity: 'success', + summary: 'Exito', + detail: 'Comunidad de Viviendas Eliminada', + life: 3000, + }); + }) + .catch((err) => { + console.log('Ocurrió un error con el fetch', err); + toast.current.show({ + severity: 'danger', + summary: 'Error', + detail: 'Comunidad de Viviendas no se pudo eliminar', + life: 3000, + }); + }); + let _communities = communitiesList.filter( + (val) => val._id !== community._id, + ); + _communities = _communities.filter((val) => val.status != -1); setCommunitiesList(_communities); setDeleteCommunityDialog(false); setCommunity(emptyCommunity); @@ -441,9 +425,7 @@ const Communities = () => { } }) })*/ - _communities = _communities.filter( - (val) => val.status != -1, - ) + _communities = _communities.filter((val) => val.status != -1); setCommunitiesList(_communities); setDeleteCommunitiesDialog(false); setSelectedCommunities(null); @@ -462,15 +444,23 @@ const Communities = () => { setProvinciaId(community.provincia); setCantonId(community.canton); setDistrictId(community.district); + }; + + const cancelEdit = () => { + setCommunity(emptyCommunity); + setCantonId(''); + setHousesList([]); + setProvinciaId(''); + setDistrictId(''); + setSaveButtonLabel('Registrar'); } const actionsCommunity = (rowData) => { - let icono = ''; if (rowData.status == '0') { - icono = "pi pi-eye"; + icono = 'pi pi-eye'; } else if (rowData.status == '1') { - icono = "pi pi-eye-slash"; + icono = 'pi pi-eye-slash'; } return ( @@ -550,7 +540,6 @@ const Communities = () => { className="p-button-text" onClick={hideCommunityDialog} /> - ); @@ -604,8 +593,6 @@ const Communities = () => { ); - - const headerName = ( <>

@@ -668,8 +655,8 @@ const Communities = () => { <>

{' '} - {' '} - Número de viviendas + Número + de viviendas

); @@ -686,12 +673,16 @@ const Communities = () => { const headerStatus = ( <> -

{' '} - {' '} +

+ {' '} + {' '} Estado

- ) + ); //ver perfil comunidad const headerTenant = ( @@ -701,16 +692,13 @@ const Communities = () => { {' '} Inquilinos

- ); const statusBodyTemplate = (rowData) => { return ( <> - + {rowData.status_text} @@ -724,11 +712,7 @@ const Communities = () => { name = findNameTenant(tenants.tenant_id); } - return ( - <> - {name} - - ) + return <>{name}; }; return ( @@ -810,15 +794,19 @@ const Communities = () => { sortable header={headerStatus} body={statusBodyTemplate} - style={{ flexGrow: 1, flexBasis: '160px', minWidth: '160px', wordBreak: 'break-word' }}> - + style={{ + flexGrow: 1, + flexBasis: '160px', + minWidth: '160px', + wordBreak: 'break-word', + }} + > - { modal className="p-fluid" footer={communityDialogFooter} - onHide={hideCommunityDialog}> -
-
+ onHide={hideCommunityDialog} + > +
+

Nombre

-
+

{community.name}

@@ -839,30 +831,34 @@ const Communities = () => {
-
+

Administrador

-
+

{community.name_admin}

-

Teléfono Administrativo

-
+

{community.phone}

-
-
+

Provincia

@@ -870,7 +866,6 @@ const Communities = () => {

{community.province}

-
@@ -880,7 +875,6 @@ const Communities = () => {

{community.canton}

-
@@ -890,14 +884,16 @@ const Communities = () => {

{community.district}

-
-
+

Número de Viviendas

-
+

{community.num_houses}

@@ -905,12 +901,16 @@ const Communities = () => {
-
+
- - -

Viviendas

-
+

+ {' '} + Viviendas +

+
{
@@ -942,7 +950,6 @@ const Communities = () => {
-
{ /> {community && ( - ¿Estás seguro que desea cambiar estado a {community.name}? + ¿Estás seguro que desea cambiar estado a{' '} + {community.name}? )} @@ -1183,12 +1191,22 @@ const Communities = () => { )} -
+
+ /> + {saveButtonLabel === 'Actualizar' && ( +
From ad16f8759dd001b960a252985ae04897da0faa1d Mon Sep 17 00:00:00 2001 From: Eduardo Quiros Date: Thu, 1 Sep 2022 10:25:44 -0600 Subject: [PATCH 06/10] switch to arrow functions --- .../web-react/src/components/ComunidadViviendas.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/web-ui/web-react/src/components/ComunidadViviendas.js b/web-ui/web-react/src/components/ComunidadViviendas.js index f80f1157..cf278fc2 100644 --- a/web-ui/web-react/src/components/ComunidadViviendas.js +++ b/web-ui/web-react/src/components/ComunidadViviendas.js @@ -93,7 +93,7 @@ const Communities = () => { async function fillCantons() { const resJson = await getCantons(); - const cantones = await resJson.filter(function(i, n) { + const cantones = await resJson.filter((i, n) => { return i.parentCode === provinciaId; }); setCantonsList(await cantones); @@ -108,7 +108,7 @@ const Communities = () => { async function fillDistricts() { const resJson = await getDistricts(); - const districts = await resJson.filter(function(i, n) { + const districts = await resJson.filter((i, n) => { return i.parentCode === cantonId; }); setDistrictsList(await districts); @@ -223,7 +223,7 @@ const Communities = () => { 'Content-Type': 'application/json', }, }) - .then(function(response) { + .then((response) => { if (response.status != 201) console.log('Ocurrió un error con el servicio: ' + response.status); else return response.json(); @@ -344,12 +344,12 @@ const Communities = () => { 'Content-Type': 'application/json', }, }) - .then(function(response) { + .then((response) => { if (response.status != 201) console.log('Ocurrió un error con el servicio: ' + response.status); else return response.json(); }) - .then(function(response) { + .then((response) => { setEditCommunityDialog(false); toast.current.show({ severity: 'success', @@ -369,12 +369,12 @@ const Communities = () => { 'Content-Type': 'application/json', }, }) - .then(function(response) { + .then((response) => { if (response.status != 201) console.log('Ocurrió un error con el servicio: ' + response.status); else return response.json(); }) - .then(function(response) { + .then((response) => { let _community = communitiesList.filter( (val) => val._id !== community._id, ); From a089525afcf1fe2215b37da41f5bd28d84a80dda Mon Sep 17 00:00:00 2001 From: Eduardo Quiros Date: Thu, 1 Sep 2022 10:26:50 -0600 Subject: [PATCH 07/10] underscore unused params --- web-ui/web-react/src/components/ComunidadViviendas.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/web-ui/web-react/src/components/ComunidadViviendas.js b/web-ui/web-react/src/components/ComunidadViviendas.js index cf278fc2..dff13e13 100644 --- a/web-ui/web-react/src/components/ComunidadViviendas.js +++ b/web-ui/web-react/src/components/ComunidadViviendas.js @@ -93,7 +93,7 @@ const Communities = () => { async function fillCantons() { const resJson = await getCantons(); - const cantones = await resJson.filter((i, n) => { + const cantones = await resJson.filter((i, _n) => { return i.parentCode === provinciaId; }); setCantonsList(await cantones); @@ -108,7 +108,7 @@ const Communities = () => { async function fillDistricts() { const resJson = await getDistricts(); - const districts = await resJson.filter((i, n) => { + const districts = await resJson.filter((i, _n) => { return i.parentCode === cantonId; }); setDistrictsList(await districts); @@ -349,7 +349,7 @@ const Communities = () => { console.log('Ocurrió un error con el servicio: ' + response.status); else return response.json(); }) - .then((response) => { + .then((_response) => { setEditCommunityDialog(false); toast.current.show({ severity: 'success', @@ -374,7 +374,7 @@ const Communities = () => { console.log('Ocurrió un error con el servicio: ' + response.status); else return response.json(); }) - .then((response) => { + .then((_response) => { let _community = communitiesList.filter( (val) => val._id !== community._id, ); From 1d684d74fdbaf076cde966fd5591c2a069f638d9 Mon Sep 17 00:00:00 2001 From: Eduardo Quiros Date: Thu, 1 Sep 2022 10:36:20 -0600 Subject: [PATCH 08/10] change empty to const --- web-ui/web-react/src/components/ComunidadViviendas.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/web-ui/web-react/src/components/ComunidadViviendas.js b/web-ui/web-react/src/components/ComunidadViviendas.js index dff13e13..d3d4f23c 100644 --- a/web-ui/web-react/src/components/ComunidadViviendas.js +++ b/web-ui/web-react/src/components/ComunidadViviendas.js @@ -16,7 +16,7 @@ import { faHashtag } from '@fortawesome/free-solid-svg-icons'; import { faCircleQuestion } from '@fortawesome/free-solid-svg-icons'; const Communities = () => { - let emptyCommunity = { + const emptyCommunity = { _id: null, name: '', province: provinciaId, @@ -214,7 +214,6 @@ const Communities = () => { number_house: codeHouses + (i + 1), }); } - // console.log(houses) fetch('http://localhost:4000/community/createCommunity', { cache: 'no-cache', method: 'POST', From eb55195783849de7081fbc3affe830ce563e396b Mon Sep 17 00:00:00 2001 From: Eduardo Quiros Date: Thu, 1 Sep 2022 12:39:01 -0600 Subject: [PATCH 09/10] remover set ids --- web-ui/web-react/src/components/ComunidadViviendas.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/web-ui/web-react/src/components/ComunidadViviendas.js b/web-ui/web-react/src/components/ComunidadViviendas.js index d3d4f23c..36f22a48 100644 --- a/web-ui/web-react/src/components/ComunidadViviendas.js +++ b/web-ui/web-react/src/components/ComunidadViviendas.js @@ -440,9 +440,6 @@ const Communities = () => { setCommunity(community); setSaveButtonLabel('Actualizar'); setHousesList(community.houses); - setProvinciaId(community.provincia); - setCantonId(community.canton); - setDistrictId(community.district); }; const cancelEdit = () => { From 1a7edfc76e89fda1218739b4d866e7f34cc6f179 Mon Sep 17 00:00:00 2001 From: Eduardo Quiros Date: Thu, 1 Sep 2022 14:27:11 -0600 Subject: [PATCH 10/10] finish functionality --- api-gateway/src/app.controller.ts | 27 ++++ api-gateway/src/app.service.ts | 19 +++ .../src/components/ComunidadViviendas.js | 122 +++++++++++------- 3 files changed, 118 insertions(+), 50 deletions(-) diff --git a/api-gateway/src/app.controller.ts b/api-gateway/src/app.controller.ts index d9b740b9..e8e98459 100644 --- a/api-gateway/src/app.controller.ts +++ b/api-gateway/src/app.controller.ts @@ -353,6 +353,33 @@ export class AppController { ); } + @Put('community/updateCommunity/:id') + updateCommunity( + @Param('id') id: string, + @Body('name') name: string, + @Body('province') province: string, + @Body('canton') canton: string, + @Body('district') district: string, + @Body('num_houses') num_houses: number, + @Body('phone') phone: string, + @Body('status') status: string, + @Body('date_entry') date_entry: Date, + @Body('houses') houses: [], + ) { + return this.appService.updateCommunity( + id, + name, + province, + canton, + district, + num_houses, + phone, + status, + date_entry, + houses, + ); + } + @Get('community/allCommunities') allcommunities() { return this.appService.allCommunities(); diff --git a/api-gateway/src/app.service.ts b/api-gateway/src/app.service.ts index 235090ec..f8a24bbe 100644 --- a/api-gateway/src/app.service.ts +++ b/api-gateway/src/app.service.ts @@ -426,6 +426,25 @@ export class AppService { .pipe(map((message: string) => ({ message }))); } + updateCommunity(id: string, name: string, province: string, canton: string, district: string, num_houses: number, phone: string, status: string, date_entry: Date, houses: unknown) { + const pattern = { cmd: 'updateCommunity' }; + const payload = { + id: id, + name: name, + province: province, + canton: canton, + district: district, + num_houses: num_houses, + phone: phone, + status: status, + date_entry: date_entry, + houses: houses, + }; + return this.clientCommunityApp + .send(pattern, payload) + .pipe(map((message: string) => ({ message }))); + } + allCommunities() { const pattern = { cmd: 'findAllCommunities' }; const payload = {}; diff --git a/web-ui/web-react/src/components/ComunidadViviendas.js b/web-ui/web-react/src/components/ComunidadViviendas.js index 36f22a48..548388fb 100644 --- a/web-ui/web-react/src/components/ComunidadViviendas.js +++ b/web-ui/web-react/src/components/ComunidadViviendas.js @@ -203,59 +203,80 @@ const Communities = () => { districtId && community.phone ) { - let _communities = [...communitiesList]; - let _community = { ...community }; - _community.province = provinciaId; - _community.canton = cantonId; - _community.district = districtId; + if (saveButtonLabel === 'Registrar') { + 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: codeHouses + (i + 1), - }); - } - fetch('http://localhost:4000/community/createCommunity', { - cache: 'no-cache', - method: 'POST', - body: JSON.stringify(_community), - headers: { - 'Content-Type': 'application/json', - }, - }) - .then((response) => { - if (response.status != 201) + for (let i = 0; i < _community.num_houses; i++) { + _community.houses.push({ + number_house: codeHouses + (i + 1), + }); + } + fetch('http://localhost:4000/community/createCommunity', { + cache: 'no-cache', + method: 'POST', + body: JSON.stringify(_community), + headers: { + 'Content-Type': 'application/json', + }, + }) + .then((response) => { + if (response.status != 201) + console.log('Ocurrió un error con el servicio: ' + response.status); + else return response.json(); + }) + .then(() => { + _community.province = provincesList.find( + (p) => p.code === _community.province, + ).name; + _community.canton = cantonsList.find( + (p) => p.code === _community.canton, + ).name; + _community.district = districtsList.find( + (p) => p.code === _community.district, + ).name; + + _communities.push(_community); + toast.current.show({ + severity: 'success', + summary: 'Registro exitoso', + detail: 'Comunidad de vivienda Creada', + life: 3000, + }); + setCommunitiesList(_communities); + setProvinciaId(''); + setCantonId(''); + setDistrictId(''); + setCodeHouses(''); + getCommunites(); + setCommunity(emptyCommunity); + }) + .catch((err) => console.log('Ocurrió un error con el fetch', err)); + } else { + let _community = { ...community }; + _community.province = provinciaId; + _community.canton = cantonId; + _community.district = districtId; + console.log(`Actualizando comunidad: ${_community}`); + fetch(`http://localhost:4000/community/updateCommunity/${community._id}`, { + method: 'PUT', + cache: 'no-cache', + body: JSON.stringify(_community), + headers: { + 'Content-Type': 'application/json', + } + }).then((response) => { + getCommunites(); + if (response.status != 200) console.log('Ocurrió un error con el servicio: ' + response.status); else return response.json(); - }) - .then(() => { - _community.province = provincesList.find( - (p) => p.code === _community.province, - ).name; - _community.canton = cantonsList.find( - (p) => p.code === _community.canton, - ).name; - _community.district = districtsList.find( - (p) => p.code === _community.district, - ).name; - - _communities.push(_community); - toast.current.show({ - severity: 'success', - summary: 'Registro exitoso', - detail: 'Comunidad de vivienda Creada', - life: 3000, - }); - - setCommunitiesList(_communities); - - setProvinciaId(''); - setCantonId(''); - setDistrictId(''); - setCodeHouses(''); - - setCommunity(emptyCommunity); - }) - .catch((err) => console.log('Ocurrió un error con el fetch', err)); + }).catch((err) => console.log('Ocurrió un error con el fetch', err)); + setSaveButtonLabel('Registrar'); + setCommunity(emptyCommunity); + } } else { setSubmitted(true); } @@ -356,6 +377,7 @@ const Communities = () => { detail: 'Comunidad de Viviendas Actualizada', life: 3000, }); + getCommunites(); }) .catch((err) => console.log('Ocurrió un error con el fetch', err)); };