diff --git a/api-gateway/src/app.controller.ts b/api-gateway/src/app.controller.ts index 510dfeb9..3753431b 100644 --- a/api-gateway/src/app.controller.ts +++ b/api-gateway/src/app.controller.ts @@ -241,6 +241,20 @@ export class AppController { return this.appService.changeStatusUser(pId, pStatus); } + + @Post('user/updateAdminSystem') + updateAdminSystem( + //Nombre, Apellidos, Correo electrónico, Cédula, Teléfono + @Body('_id') _id: string, + @Body('dni') dni: string, + @Body('name') name: string, + @Body('last_name') last_name: string, + @Body('email') email: string, + @Body('phone') phone: number, + ) { + return this.appService.updateAdminSystem(_id, dni, name, last_name, email, phone); + } + // #==== API Communities @Post('community/createCommunity') createCommunity( diff --git a/api-gateway/src/app.service.ts b/api-gateway/src/app.service.ts index 42475489..1e173a1c 100644 --- a/api-gateway/src/app.service.ts +++ b/api-gateway/src/app.service.ts @@ -287,6 +287,19 @@ export class AppService { .pipe(map((message: string) => ({ message }))); } + updateAdminSystem(_id: string, dni: string, name: string, + last_name: string, email: string, phone: number + ) { + const pattern = { cmd: 'updateAdminSystem' }; + const payload = { + _id: _id, dni: dni, name: name, last_name: last_name, + email: email, phone: phone + }; + return this.clientUserApp + .send(pattern, payload) + .pipe(map((message: string) => ({ message }))); + } + //GET parameter from API findCommunityAdmin(community_id: string) { const pattern = { cmd: 'findCommunityAdmin' }; diff --git a/servicio-usuarios/src/users/users.controller.ts b/servicio-usuarios/src/users/users.controller.ts index 07cee85d..be7deb01 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' }) @@ -85,7 +85,10 @@ export class UsersController { let dni = id['dni']; return this.userService.remove(dni); } - + @MessagePattern({ cmd: 'updateAdminSystem' }) + updateAdminSystem(@Payload() user: UserDocument) { + return this.userService.updateAdminSystem(user._id, user); + } //inicio de sesion @MessagePattern({ cmd: 'loginUser' }) findLogin(@Payload() body: string) { diff --git a/servicio-usuarios/src/users/users.service.ts b/servicio-usuarios/src/users/users.service.ts index e5a17fef..1393b2bd 100644 --- a/servicio-usuarios/src/users/users.service.ts +++ b/servicio-usuarios/src/users/users.service.ts @@ -103,6 +103,15 @@ export class UsersService { }); } + async updateAdminSystem(id: string, user: UserDocument) { + 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, + }); + } + /* async remove(id: string) { return this.userModel.findByIdAndRemove({ _id: id }).exec(); }*/ diff --git a/web-ui/web-react/src/components/AdministradoresSistema.js b/web-ui/web-react/src/components/AdministradoresSistema.js index a301c3ed..6fa70310 100644 --- a/web-ui/web-react/src/components/AdministradoresSistema.js +++ b/web-ui/web-react/src/components/AdministradoresSistema.js @@ -12,8 +12,25 @@ import { faPhoneAlt } from '@fortawesome/free-solid-svg-icons'; import { faAt } from '@fortawesome/free-solid-svg-icons'; import { faIdCardAlt } from '@fortawesome/free-solid-svg-icons'; import { faCircleQuestion } from '@fortawesome/free-solid-svg-icons'; +import classNames from 'classnames'; const AdministradoresSistema = () => { + + + let emptySysAdmin = { + _id: null, + dni: '', + name: '', + last_name: '', + email: '', + phone: '', + password: '', + user_type: '1', + status: '1', + status_text: '', + }; + + const [administrators, setAdministrators] = useState([]); const [urlFetch, setUrlFetch] = useState( 'http://localhost:4000/user/findAdminSistema/', @@ -30,19 +47,12 @@ const AdministradoresSistema = () => { const [changeStatusAdminSystemDialog, setChangeStatusAdminSystemDialog] = useState(false); const [changeStatusAdminsSystemDialog, setChangeStatusAdminsSystemDialog] = useState(false); + const [adminDialog, setAdminDialog] = useState(false); + const [submitted, setSubmitted] = useState(false); + + const [editAdminDialog, setEditAdminDialog] = useState(false); + const [saveButtonTitle, setSaveButtonTitle] = useState("Registrar") - let emptySysAdmin = { - _id: null, - dni: '', - name: '', - last_name: '', - email: '', - phone: '', - password: '', - user_type: '1', - status: '1', - status_text: '', - }; async function fetchP() { let nombres = await fetch(urlFetch, { method: 'GET' }); @@ -55,7 +65,7 @@ const AdministradoresSistema = () => { item.status_text = 'Activo'; } else if (item.status == '0') { item.status_text = 'Inactivo'; - } + } }) setAdministrators(await data); } @@ -64,46 +74,123 @@ const AdministradoresSistema = () => { fetchP(); }, []) - function registrarAdmin() { - var data = { - dni: document.getElementById('identificacion').value, - name: document.getElementById('nombre').value, - last_name: document.getElementById('apellidos').value, - email: document.getElementById('correo_electronico').value, - phone: document.getElementById('telefono').value, - password: document.getElementById('correo_electronico').value, - user_type: "1", //1 es admin - status: "1" - }; - setSysAdmin(data) - - fetch('http://localhost:4000/user/createAdminSystem/', { - cache: 'no-cache', - method: 'POST', - body: JSON.stringify(data), - headers: { - 'Content-Type': 'application/json' + const findIndexById = (id) => { + let index = -1; + for (let i = 0; i < administrators.length; i++) { + if (administrators[i]._id === id) { + index = i; + break; } - }) - .then( - function (response) { - if (response.status != 201) - console.log('Ocurrió un error con el servicio: ' + response.status); - else - return response.json(); + } + return index; + + } + + const findRepeated = (name, value) => { + let _administrators = [...administrators]; + let value_filtered = _administrators.filter(item => item[`${name}`] === value); + return value_filtered.length + } + + + function guardarAdmin() { + let _administrators = [...administrators]; + let _admin = { ...sysadmin }; + + if (_admin.name && _admin.dni && _admin.last_name && _admin.email && + _admin.phone) { + + + + if (findRepeated('email', _admin.email) || findRepeated('dni', _admin.dni)) { + setSubmitted(true); + + } else { + if (_admin._id) { + + + + fetch('http://localhost:4000/user/updateAdminSystem/', { + cache: 'no-cache', + method: 'POST', + body: JSON.stringify(_admin), + 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(); + } + ) + .then( + function (response) { + const index = findIndexById(sysadmin._id); + + _administrators[index] = _admin; + toast.current.show({ + severity: 'success', + summary: 'Exito', + detail: 'Administrador Actualizado', + life: 3000, + }); + setAdministrators(_administrators) + setEditAdminDialog(false); + setSysAdmin(emptySysAdmin); + } + ) + .catch( + err => console.log('Ocurrió un error con el fetch', err) + ); + + + + + + + } else { + + + fetch('http://localhost:4000/user/createAdminSystem/', { + cache: 'no-cache', + method: 'POST', + body: JSON.stringify(_admin), + 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(); + } + ) + .then( + function (response) { + _administrators.push(_admin); + setAdministrators(_administrators) + } + ) + .catch( + err => console.log('Ocurrió un error con el fetch', err) + ); } - ) - .then( - function (response) { - let _administrators = [...administrators]; - let _admin = { ...sysadmin }; - _administrators.push(_admin); - setAdministrators(_administrators) - } - ) - .catch( - err => console.log('Ocurrió un error con el fetch', err) - ); + } + + + + + + + } else { + setSubmitted(true); + + } } const cambiarStatusUser = () => { @@ -177,6 +264,31 @@ const AdministradoresSistema = () => { setChangeStatusAdminSystemDialog(true); }; + const hideAdminDialog = () => { + setSubmitted(false); + setAdminDialog(false); + setSysAdmin(emptySysAdmin); + + }; + + const infoAdmin = (sysadmin) => { + setSysAdmin({ ...sysadmin }); + setAdminDialog(true); + + }; + + + const cancelEdit = () => { + setSaveButtonTitle('Registrar'); + setSubmitted(false); + setSysAdmin(emptySysAdmin); + } + + const editAdmin = (sysadmin) => { + setSysAdmin({ ...sysadmin }); + setSaveButtonTitle('Actualizar'); + }; + const deleteSysAdmin = () => { fetch('http://localhost:4000/user/deleteAdminSystem/' + sysadmin._id, { cache: 'no-cache', @@ -259,6 +371,21 @@ const AdministradoresSistema = () => { return (
+
-
Registro de un administrador del sistema
+
Mantenimiento Administrador del Sistema
-
- - +
+ + +
+
+ + + + onInputChange(e, 'name')} + required + autoFocus + className={classNames({ + 'p-invalid': submitted && sysadmin.name === '', + })} + /> +
+ {submitted && sysadmin.name === '' && + Nombre es requirido.} +
-
- - +
+ +
+
+ + + + onInputChange(e, 'last_name')} + required + autoFocus + className={classNames({ + 'p-invalid': submitted && sysadmin.last_name === '', + })} + /> +
+ {submitted && sysadmin.last_name === '' && ( + Apellido(s) es requerido. + )} +
-
+
- +
+
+ + + + onInputChange(e, 'email')} + required + autoFocus + className={classNames({ + 'p-invalid': submitted && (sysadmin.email === '' || findRepeated('email', sysadmin.email) > 0), + })} + /> +
+ {submitted && sysadmin.email === '' && ( + Correo electrónico es requerido. + )} + {submitted && findRepeated('email', sysadmin.email) > 0 && + Correo electrónico se encuentra repetido. + } +
-
- - +
+ +
+
+ + + + onInputChange(e, 'dni')} + required + autoFocus + className={classNames({ + 'p-invalid': submitted && (sysadmin.dni === '' || findRepeated('dni', sysadmin.dni) > 0), + })} + /> +
+ {submitted && sysadmin.dni === '' && ( + Identificación es requerida. + )} + {submitted && findRepeated('dni', sysadmin.dni) > 0 && + Identificación se encuentra repetida. + } +
- - + +
+
+ + + + onInputChange(e, 'phone')} + required + autoFocus + className={classNames({ + 'p-invalid': submitted && sysadmin.phone === '', + })} + /> +
+ {submitted && sysadmin.phone === '' && ( + Teléfono es requerido. + )} +
- +
+
+
+
+
); diff --git a/web-ui/web-react/src/components/Inquilinos.js b/web-ui/web-react/src/components/Inquilinos.js index 82908833..c932116f 100644 --- a/web-ui/web-react/src/components/Inquilinos.js +++ b/web-ui/web-react/src/components/Inquilinos.js @@ -926,7 +926,7 @@ const Inquilinos = () => { /> {saveButtonTitle === 'Actualizar' && (