diff --git a/api-gateway/src/app.controller.ts b/api-gateway/src/app.controller.ts index 94b6fc14..7e6fe785 100644 --- a/api-gateway/src/app.controller.ts +++ b/api-gateway/src/app.controller.ts @@ -160,6 +160,10 @@ export class AppController { return this.appService.deleteAdminCommunity(id); } + @Delete('user/deleteTenant/:id') + deleteTenant(@Param('id') id: string) { + return this.appService.deleteTenant(id); + } @Post('user/changeStatus') changeStatusUser( diff --git a/api-gateway/src/app.service.ts b/api-gateway/src/app.service.ts index 42e9bf22..c3396486 100644 --- a/api-gateway/src/app.service.ts +++ b/api-gateway/src/app.service.ts @@ -54,8 +54,8 @@ export class AppService { } updateUser( - dni: string, - name: string, + dni: string, + name: string, last_name: string, email: string, phone: number, @@ -201,6 +201,14 @@ export class AppService { .pipe(map((message: string) => ({ message }))); } + deleteTenant(id: string) { + const pattern = { cmd: 'deleteTenant' }; + const payload = { id: id }; + return this.clientUserApp + .send(pattern, payload) + .pipe(map((message: string) => ({ message }))); + } + inicioSesion(pEmail: string, pPassword: string) { const pattern = { cmd: 'loginUser' }; const payload = { email: pEmail, password: pPassword }; diff --git a/devshell.toml b/devshell.toml index 4b8c8bff..d7746d94 100644 --- a/devshell.toml +++ b/devshell.toml @@ -2,3 +2,8 @@ [[commands]] package = "devshell.cli" help = "Per project developer environments" + +[[commands]] +package = "nodejs" +help = "Node.js" + diff --git a/servicio-comunidad-viviendas/package-lock.json b/servicio-comunidad-viviendas/package-lock.json index 46dd3dd0..22c95f9b 100644 --- a/servicio-comunidad-viviendas/package-lock.json +++ b/servicio-comunidad-viviendas/package-lock.json @@ -5,6 +5,7 @@ "requires": true, "packages": { "": { + "name": "servicio-comunidad-viviendas", "version": "0.0.1", "license": "UNLICENSED", "dependencies": { diff --git a/servicio-usuarios/src/users/users.controller.ts b/servicio-usuarios/src/users/users.controller.ts index e8f261a4..a13bae7c 100644 --- a/servicio-usuarios/src/users/users.controller.ts +++ b/servicio-usuarios/src/users/users.controller.ts @@ -115,13 +115,15 @@ export class UsersController { return this.userService.deleteAdminSystem(user['id']); } - @MessagePattern({ cmd: 'deleteAdminCommunity' }) deleteAdminCommunity(@Payload() user: any) { return this.userService.deleteAdminCommunity(user['id']); } - + @MessagePattern({ cmd: 'deleteTenant' }) + deleteTenant(@Payload() user: any) { + return this.userService.deleteTenant(user['id']); + } @MessagePattern({ cmd: 'changeStatus' }) changeStatus(@Payload() body: string) { diff --git a/servicio-usuarios/src/users/users.service.ts b/servicio-usuarios/src/users/users.service.ts index 0cd70023..cbe1689a 100644 --- a/servicio-usuarios/src/users/users.service.ts +++ b/servicio-usuarios/src/users/users.service.ts @@ -14,8 +14,8 @@ export class UsersService { @InjectModel(User.name) private readonly userModel: Model, @Inject('SERVICIO_NOTIFICACIONES') private readonly clientNotificationtApp: ClientProxy, @Inject('SERVICIO_COMUNIDADES') private readonly clientCommunityApp: ClientProxy, - ) { } + private publicKey: string; async create(user: UserDocument): Promise { let passwordEncriptada = Md5.init(user.password); @@ -78,14 +78,14 @@ export class UsersService { }); } - /* async remove(id: string) { - return this.userModel.findByIdAndRemove({ _id: id }).exec(); - }*/ + /* async remove(id: string) { + return this.userModel.findByIdAndRemove({ _id: id }).exec(); + }*/ async remove(id: string) { - return this.userModel.findOneAndUpdate({ _id: id }, {status: '-1'}, { + return this.userModel.findOneAndUpdate({ _id: id }, { status: '-1' }, { new: true, - }); + }); } //inicio de sesion @@ -99,8 +99,12 @@ export class UsersService { reject(err); } else { let passwordEncriptada = Md5.init(password); - if (res[0].password == passwordEncriptada) { - resolve(res[0]); + if (res.length > 0) { + if (res[0].password == passwordEncriptada) { + resolve(res[0]); + } else { + resolve(null); + } } else { resolve(null); } @@ -180,16 +184,21 @@ export class UsersService { } async deleteAdminSystem(id: string) { - return this.userModel.findOneAndUpdate({ _id: id }, {status: '-1'}, { + return this.userModel.findOneAndUpdate({ _id: id }, { status: '-1' }, { new: true, - }); + }); } - deleteAdminCommunity(id: string) { - return this.userModel.findOneAndUpdate({ _id: id }, {status: '-1'}, { + return this.userModel.findOneAndUpdate({ _id: id }, { status: '-1' }, { new: true, - }); + }); + } + + async deleteTenant(id: string) { + return this.userModel.findOneAndUpdate({ _id: id }, { status: '-1' }, { + new: true, + }); } async validateEmail(email: string) { @@ -223,17 +232,17 @@ export class UsersService { const houses = response['houses']; let num_house = ""; await houses.forEach(async house => { - if (tenant_id == house.tenants.tenant_id) { - num_house = house.number_house; - } + if (tenant_id == house.tenants.tenant_id) { + num_house = house.number_house; + } }) return num_house; } async changeStatus(id: string, status: string) { - return this.userModel.findOneAndUpdate({ _id: id }, {status: status}, { + return this.userModel.findOneAndUpdate({ _id: id }, { status: status }, { new: true, - }); + }); } } diff --git a/web-ui/web-react/src/App.js b/web-ui/web-react/src/App.js index 2b95c37c..1551381d 100644 --- a/web-ui/web-react/src/App.js +++ b/web-ui/web-react/src/App.js @@ -33,7 +33,6 @@ import AdministradoresComunidad from './components/AdministradoresComunidad'; import GuardasSeguridad from './components/GuardasSeguridad'; import Communities from './components/ComunidadViviendas'; import Inquilinos from './components/Inquilinos'; -import InquilinosCompletar from "./components/InquilinosCompletar.js"; import RegistroComunicado from './components/RegistroComunicado'; import "../node_modules/bootstrap/dist/css/bootstrap.min.css"; diff --git a/web-ui/web-react/src/components/Inquilinos.js b/web-ui/web-react/src/components/Inquilinos.js index 4325bcf5..0b248337 100644 --- a/web-ui/web-react/src/components/Inquilinos.js +++ b/web-ui/web-react/src/components/Inquilinos.js @@ -15,9 +15,10 @@ import { faIdCardAlt } from '@fortawesome/free-solid-svg-icons' import { faHashtag } from '@fortawesome/free-solid-svg-icons' import { faCircleQuestion } from '@fortawesome/free-solid-svg-icons' import { useCookies } from 'react-cookie' +import classNames from 'classnames'; const Inquilinos = () => { - let emptyTenant = { + const emptyTenant = { _id: null, dni: '', name: '', @@ -40,15 +41,14 @@ const Inquilinos = () => { const [globalFilter, setGlobalFilter] = useState(null) const [deleteTenantDialog, setDeleteTenantDialog] = useState(false) const [deleteTenantsDialog, setDeleteTenantsDialog] = useState(false) - const [communitiesList, setCommunitiesList] = useState([]) - const [communityId, setCommunityId] = useState(null) + const [community, setCommunity] = useState([]) + const [houseNumber, setHouseNumber] = useState([]) + const [housesList, setHousesList] = useState([]) const [submitted, setSubmitted] = useState(false) const toast = useRef(null) const dt = useRef(null) - - const [cookies, setCookie] = useCookies() - const [changeStatusTenantDialog, setChangeStatusTenantDialog] = - useState(false) + const [cookies] = useCookies() + const [changeStatusTenantDialog, setChangeStatusTenantDialog] = useState(false) async function tenantsList() { await fetch( @@ -74,15 +74,21 @@ const Inquilinos = () => { }) } - async function getCommunites() { + async function getCommunity() { let response = await fetch( - 'http://localhost:4000/community/allCommunities', + `http://localhost:4000/community/findCommunityName/${cookies.community_id}`, { method: 'GET' }, ) - let resList = await response.json() - let list = await resList.message - list = await list.filter((val) => val.status !== -1) - setCommunitiesList(await list) + const responseJson = await response.json() + const result = await responseJson.message + setCommunity(await result) + const houses = await result.houses.filter((house) => + house.state === "desocupada" + ) + setHousesList(houses.map((house) => ({ + label: house.number_house, value: house.number_house + })) + ) } useEffect(() => { @@ -90,46 +96,45 @@ const Inquilinos = () => { }, [tenantsList]) useEffect(() => { - getCommunites() + getCommunity() }, []) - const cList = communitiesList.map((item) => ({ - label: item.name, - value: item._id, - })) + const saveTenant = () => { + if (tenant.email && tenant.number_house && tenant.dni + && tenant.name && tenant.last_name && tenant.phone) { + let _tenants = [...tenants] + let _tenant = { ...tenant } + _tenant.community_id = cookies.community_id; + _tenant.number_house = houseNumber; + _tenant.password = _tenant.email; + console.log(_tenant) - function registrarInquilino() { - let newTenant = { - _id: null, - dni: '', - name: '', - last_name: '', - email: document.getElementById('correo_electronico').value, - phone: '', - password: '', - community_id: document.getElementById('numero_vivienda').value, - community_name: '', - number_house: 'Sin número de vivienda', - date_entry: new Date(), - user_type: '3', - status: '1', - status_text: '', - } - - fetch('http://localhost:3000/api/createUser', { - method: 'POST', - cache: 'no-cache', - body: JSON.stringify(newTenant), - headers: { - 'Content-Type': 'application/json', - }, - }).then((response) => { - if (response.ok) { - alert('Inquilino registrado correctamente') - } else { - alert('Error al registrar inquilino') - } - }) + fetch(`http://localhost:4000/user/createUser`, { + cache: 'no-cache', + method: 'POST', + body: JSON.stringify(_tenant), + headers: { + 'Content-Type': 'application/json', + }, + }) + .then((response) => { + if (response.status !== 201) + console.log(`Hubo un error en el servicio: ${response.status}`) + else return response.json() + }) + .then(() => { + _tenants.push(_tenant) + toast.current.show({ + severity: 'success', + summary: 'Éxito', + detail: 'Inquilino creado', + life: 3000, + }) + setTenants(_tenants) + setTenant(emptyTenant) + }) + .catch((error) => console.log(`Ocurrió un error: ${error}`)) + } else setSubmitted(true) } const deleteTenant = () => { @@ -418,6 +423,19 @@ const Inquilinos = () => { ) } + const onInputChange = (e, name) => { + const value = (e.target && e.target.value) || '' + let _tenant = { ...tenant } + _tenant[`${name}`] = value + setTenant(_tenant) + } + + const handleHouses = (e) => { + const getHouseNumber = e.target.value; + setHouseNumber(getHouseNumber); + console.log(getHouseNumber); + } + return (
@@ -595,30 +613,96 @@ const Inquilinos = () => {
-
-
-
Registrar Inquilino
-
-
- - +
+
+
Registro de un administrador de una comunidad de viviendas
+
+
+ +
+
+ + + + onInputChange(e, 'name')} required autoFocus className={classNames({ 'p-invalid': submitted && tenant.name === '' })} /> +
+ {submitted && tenant.name === '' && Nombre es requerido.} +
-
- - setCommunityId(e.value)} - /> +
+ +
+
+ + + + onInputChange(e, 'last_name')} required autoFocus className={classNames({ 'p-invalid': submitted && tenant.last_name === '' })} /> +
+ {submitted && tenant.last_name === '' && Apellidos son requeridos.} +
-
diff --git a/web-ui/web-react/src/components/InquilinosCompletar.js b/web-ui/web-react/src/components/InquilinosCompletar.js deleted file mode 100644 index 06a18ca1..00000000 --- a/web-ui/web-react/src/components/InquilinosCompletar.js +++ /dev/null @@ -1,125 +0,0 @@ -import { Button } from 'primereact/button' -import { InputText } from 'primereact/inputtext' -import React, { useState } from 'react' - -import { useCookies } from 'react-cookie' - -const InquilinosCompletar = () => { - let emptyTenant = { - _id: null, - dni: '', - name: '', - last_name: '', - email: '', - phone: '', - password: '', - community_id: '', - community_name: '', - number_house: 'Sin número de vivienda', - user_type: '4', - date_entry: new Date(), - status: '1', - status_text: '', - } - - const [tenant, setTenant] = useState(emptyTenant) - const [selectedTentants, setSelectedTenants] = useState(null) - const [globalFilter, setGlobalFilter] = useState(null) - const [deleteTenantDialog, setDeleteTenantDialog] = useState(false) - const [deleteTenantsDialog, setDeleteTenantsDialog] = useState(false) - const [communitiesList, setCommunitiesList] = useState([]) - const [communityId, setCommunityId] = useState(null) - const [submitted, setSubmitted] = useState(false) - const [cookies, setCookie] = useCookies() - const [changeStatusTenantDialog, setChangeStatusTenantDialog] = - useState(false) - - function finalizarRegistro() { - let data = { - dni: document.getElementById('identificacion').value, - name: document.getElementById('nombre').value, - last_name: document.getElementById('apellidos').value, - phone: document.getElementById('telefono').value, - email: document.getElementById('correo_electronico').value, - community_id: document.getElementById('numero_vivienda').value, - password: document.getElementById('password').value, - user_type: '3', - status: '1', - } - - fetch('http://localhost:3000/api/createUser', { - method: 'PUT', - cache: 'no-cache', - body: JSON.stringify(data), - headers: { - 'Content-Type': 'application/json', - }, - }).then((response) => { - if (response.ok) { - alert('Inquilino registrado correctamente') - } else { - alert('Error al registrar inquilino') - } - }) - } - - return ( -
-
-
-
Finalizar Registro
-
-
- - -
-
- - -
-
- - -
-
- - -
-
- - -
-
-
-
-
- ) -} - -export default React.memo(InquilinosCompletar)