diff --git a/api-gateway/src/app.controller.ts b/api-gateway/src/app.controller.ts index 3a559841..2d02bfe4 100644 --- a/api-gateway/src/app.controller.ts +++ b/api-gateway/src/app.controller.ts @@ -1,4 +1,4 @@ -import { Controller, Get, Post, Body, Param } from '@nestjs/common'; +import { Controller, Get, Post, Body, Param, Delete } from '@nestjs/common'; import { AppService } from "./app.service"; @Controller() export class AppController { @@ -92,6 +92,13 @@ export class AppController { } + @Delete('user/deleteAdminSystem/:id') + deleteAdminSystem( + @Param('id') id: string, + ) { + return this.appService.deleteAdminSystem(id); + } + // #==== API Communities @Post('community/createCommunity') createCommunity( @@ -342,21 +349,4 @@ export class AppController { return this.appService.html(email, name); } - - // #==== API Users - @Post('user/testSendMail') - testSendMail( - @Body('dni') dni: string, - @Body('name') name: string, - @Body('last_name') last_name: string, - @Body('email') email: string, - @Body('phone') phone: number, - @Body('password') password: string, - @Body('user_type') user_type: string, - @Body('status') status: string, - @Body('date_entry') date_entry: Date, - ) { - return this.appService.testSendMail(dni, name, last_name, email, phone, password, - user_type, status, date_entry); - } } \ No newline at end of file diff --git a/api-gateway/src/app.service.ts b/api-gateway/src/app.service.ts index 592117eb..65ebd4f8 100644 --- a/api-gateway/src/app.service.ts +++ b/api-gateway/src/app.service.ts @@ -116,6 +116,17 @@ export class AppService { ); } + + deleteAdminSystem(id: string) { + const pattern = { cmd: 'deleteAdminSystem' }; + 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/servicio-usuarios/src/users/users.controller.ts b/servicio-usuarios/src/users/users.controller.ts index 1fbe7658..ea3c70a9 100644 --- a/servicio-usuarios/src/users/users.controller.ts +++ b/servicio-usuarios/src/users/users.controller.ts @@ -84,6 +84,12 @@ export class UsersController { return this.userService.findCommunityUser(user["community_id"], user["user_type"]); } + @MessagePattern({ cmd: 'deleteAdminSystem' }) + deleteAdminSystem(@Payload() user: any) { + console.log("entró") + + return this.userService.deleteAdminSystem(user["id"]); + } } diff --git a/servicio-usuarios/src/users/users.service.ts b/servicio-usuarios/src/users/users.service.ts index 18b219cb..edaf2ed0 100644 --- a/servicio-usuarios/src/users/users.service.ts +++ b/servicio-usuarios/src/users/users.service.ts @@ -16,7 +16,7 @@ export class UsersService { @Inject('SERVICIO_NOTIFICACIONES') private readonly clientNotificationtApp: ClientProxy, ) { } - private publicKey: string; + private publicKey: string; async create(user: UserDocument): Promise { let passwordEncriptada = Md5.init(user.password); user.password = passwordEncriptada; @@ -79,19 +79,15 @@ export class UsersService { //find admin del sistema - async findGuardsCommunity(pcommunity_id: string): Promise { + async findGuardsCommunity(pcommunity_id: string): Promise { return this.userModel.find({ user_type: 4 }).exec(); } - //find admin de comunidad - async allUsersAdminComunidad(): Promise { - return this.userModel.find({ user_type: 2 }).exec(); - } - //find admin de comunidad async allUsersAdminComunidad(): Promise { return this.userModel.find({ user_type: 2 }).exec(); } + async testSendMail(user: UserDocument) { let passwordEncriptada = Md5.init(user.password); user.password = passwordEncriptada; @@ -109,11 +105,12 @@ export class UsersService { ); } - - async findCommunityUser(community_id: string, user_type: number): Promise { return this.userModel.findOne({ community_id: community_id, user_type: user_type }).exec(); } + async deleteAdminSystem(id: string) { + return this.userModel.deleteOne({_id: id}).exec(); + } } diff --git a/web-ui/web-react/src/components/AdministradoresSistema.js b/web-ui/web-react/src/components/AdministradoresSistema.js index 02ad4fb9..7cc7688a 100644 --- a/web-ui/web-react/src/components/AdministradoresSistema.js +++ b/web-ui/web-react/src/components/AdministradoresSistema.js @@ -1,22 +1,43 @@ -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 { Toast } from 'primereact/toast'; +import { Dialog } from 'primereact/dialog'; const AdministradoresSistema = () => { - const [pokemones,setPokemones]=useState([]); - const [urlFetch,setUrlFetch]=useState('http://localhost:4000/user/findAdminSistema/'); - async function fetchP(){ - let nombres=await fetch(urlFetch, {method:'GET'}); - let pokemonesRes= await nombres.json(); - setPokemones(pokemonesRes.message); - console.log(pokemones); - } - useEffect(()=>{ - fetchP(); - },[]) + const [administrators, setAdministrators] = useState([]); + const [urlFetch, setUrlFetch] = useState('http://localhost:4000/user/findAdminSistema/'); + const [sysadmin, setSysAdmin] = useState(emptySysAdmin); + + const [deleteAdminSystemDialog, setDeleteAdminSystemDialog] = useState(false); + const [deleteAdminsSystemDialog, setDeleteAdminsSystemDialog] = useState(false); + const toast = useRef(null); + + + let emptySysAdmin = { + _id: null, + dni: '', + name: '', + last_name: '', + email: '', + phone: '', + password: '', + user_type: '1', + status: '' + }; + + async function fetchP() { + let nombres = await fetch(urlFetch, { method: 'GET' }); + let adminRes = await nombres.json(); + setAdministrators(adminRes.message); + console.log(administrators); + } + useEffect(() => { + fetchP(); + }, []) function registrarAdmin() { var data = { @@ -29,9 +50,9 @@ const AdministradoresSistema = () => { user_type: "1", //1 es admin status: "1" }; - // console.log(data); + // console.log(data); - fetch('http://localhost:4000/user/createAdminSystem/', { + fetch('http://localhost:4000/user/createAdminSystem/', { cache: 'no-cache', method: 'POST', body: JSON.stringify(data), @@ -39,36 +60,121 @@ const AdministradoresSistema = () => { '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) { + if (response.status != 201) + console.log('Ocurrió un error con el servicio: ' + response.status); + else + return response.json(); + } + ) + .then( + function (response) { + let _administrators = [...administrators]; + let _admin = { ...response.message };; + _administrators.push(_admin); + setAdministrators(_administrators) + } + ) + .catch( + err => console.log('Ocurrió un error con el fetch', err) + ); + } + + + const hideDeleteAdminSystemDialog = () => { + setDeleteAdminSystemDialog(false); + } + + const hideDeleteAdminsSystemsDialog = () => { + setDeleteAdminsSystemDialog(false); + } + + const confirmDeleteAdminSystem = (sysAdmin) => { + setSysAdmin(sysAdmin); + setDeleteAdminSystemDialog(true); + } + + const deleteSysAdmin = () => { + + var data = { + id: sysadmin._id + } + fetch('http://localhost:4000/user/deleteAdminSystem/' + sysadmin._id, { + cache: 'no-cache', + method: 'DELETE', + headers: { + 'Content-Type': 'application/json' } - ) - .then( - function (response) { - fetchP(); - } - ) - .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) { + let _sysadmin = administrators.filter(val => val._id !== sysadmin._id); + setAdministrators(_sysadmin); + + setDeleteAdminSystemDialog(false); + setSysAdmin(emptySysAdmin); + + toast.current.show({ severity: 'success', summary: 'Exito', detail: 'Administrador del Sistema Eliminado', life: 3000 }); + } + ) + .catch( + err => { + console.log('Ocurrió un error con el fetch', err) + toast.current.show({ severity: 'danger', summary: 'Error', detail: 'Administrador del Sistema no se pudo eliminar', life: 3000 }); + } + ); + + + + } + + const actionsAdmin = (rowData) => { + return ( +
+
); } + const deleteAdminSystemDialogFooter = ( + <> +