diff --git a/api-gateway/src/app.controller.ts b/api-gateway/src/app.controller.ts index 2d02bfe4..e0a5eb1d 100644 --- a/api-gateway/src/app.controller.ts +++ b/api-gateway/src/app.controller.ts @@ -11,13 +11,12 @@ export class AppController { @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.createAdminSystem(dni, name, last_name, email, phone, password, - user_type, status, date_entry); + return this.appService.createAdminSystem(dni, name, last_name, email, phone, + user_type, status, date_entry); } @Post('user/createGuard') @@ -28,13 +27,29 @@ export class AppController { @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, @Body('community_id') community_id:string ) { - return this.appService.createGuard(dni, name, last_name, email, phone, password, + return this.appService.createGuard(dni, name, last_name, email, phone, + user_type, status, date_entry,community_id); + } + + @Post('user/createAdminCommunity') + createAdminCommunity( + //Nombre, Apellidos, Correo electrónico, Cédula, Teléfono, Contraseña + @Body('dni') dni: string, + @Body('name') name: string, + @Body('last_name') last_name: string, + @Body('email') email: string, + @Body('phone') phone: number, + @Body('user_type') user_type: string, + @Body('status') status: string, + @Body('date_entry') date_entry: Date, + @Body('community_id') community_id:string + ) { + return this.appService.createAdminCommunity(dni, name, last_name, email, phone, user_type, status, date_entry,community_id); } @@ -349,4 +364,8 @@ export class AppController { return this.appService.html(email, name); } + + + + } \ No newline at end of file diff --git a/api-gateway/src/app.service.ts b/api-gateway/src/app.service.ts index 65ebd4f8..c55021d1 100644 --- a/api-gateway/src/app.service.ts +++ b/api-gateway/src/app.service.ts @@ -37,11 +37,11 @@ export class AppService { //POST parameter from API createAdminSystem(dni: string, name: string, last_name: string, email: string, phone: number - , password: string, user_type: string, status: string, date_entry: Date) { + , user_type: string, status: string, date_entry: Date) { const pattern = { cmd: 'createAdminSystem' }; const payload = { dni: dni, name: name, last_name: last_name, email: email, phone: phone, - password: password, user_type: user_type, status: status, date_entry: date_entry + password: this.generatePassword(), user_type: user_type, status: status, date_entry: date_entry }; return this.clientUserApp .send(pattern, payload) @@ -51,11 +51,27 @@ export class AppService { } createGuard(dni: string, name: string, last_name: string, email: string, phone: number - , password: string, user_type: string, status: string, date_entry: Date, community_id: string) { + , user_type: string, status: string, date_entry: Date, community_id: string) { const pattern = { cmd: 'createGuard' }; const payload = { dni: dni, name: name, last_name: last_name, email: email, phone: phone, - password: password, user_type: user_type, status: status, date_entry: date_entry, community_id + password: this.generatePassword(), user_type: user_type, status: status, date_entry: date_entry, community_id + + }; + return this.clientUserApp + .send(pattern, payload) + .pipe( + map((message: string) => ({ message })), + ); + } + + + createAdminCommunity(dni: string, name: string, last_name: string, email: string, phone: number + , user_type: string, status: string, date_entry: Date, community_id: string) { + const pattern = { cmd: 'createAdminCommunity' }; + const payload = { + dni: dni, name: name, last_name: last_name, email: email, phone: phone, + password: this.generatePassword(), user_type: user_type, status: status, date_entry: date_entry, community_id }; return this.clientUserApp @@ -116,7 +132,7 @@ export class AppService { ); } - + deleteAdminSystem(id: string) { const pattern = { cmd: 'deleteAdminSystem' }; const payload = { id: id }; @@ -129,7 +145,7 @@ export class AppService { inicioSesion(pEmail: string, pPassword: string) { const pattern = { cmd: 'loginUser' }; - const payload = { email: pEmail, password: pPassword}; + const payload = { email: pEmail, password: pPassword }; return this.clientUserApp .send(pattern, payload) .pipe( @@ -153,7 +169,7 @@ export class AppService { //POST parameter from API createCommunity(name: string, province: string, canton: string, district: string - , num_houses: number, phone: string, status: string, date_entry: Date, houses: []) { + , num_houses: number, phone: string, status: string, date_entry: Date, houses: []) { const pattern = { cmd: 'createCommunity' }; const payload = { name: name, province: province, canton: canton, district: district, num_houses: num_houses, @@ -245,7 +261,7 @@ export class AppService { , status: string, date_entry: Date) { const pattern = { cmd: 'createGuest' }; const payload = { - name: name, last_name: last_name, dni: dni, number_plate: number_plate, phone: phone, + name: name, last_name: last_name, dni: dni, number_plate: number_plate, phone: phone, status: status, date_entry: date_entry }; return this.clientGuestApp @@ -276,15 +292,15 @@ export class AppService { ); } - // ====================== PAYMENTS =============================== + // ====================== PAYMENTS =============================== //POST parameter from API createPayment(date_payment: Date, mount: number, description: string, period: string , status: string, user_id: string, communty_id: string) { const pattern = { cmd: 'createPayment' }; const payload = { - date_payment: date_payment, mount: mount, description: description, - period: period, status: status, user_id: user_id, communty_id: communty_id + date_payment: date_payment, mount: mount, description: description, + period: period, status: status, user_id: user_id, communty_id: communty_id }; return this.clientPaymentApp .send(pattern, payload) @@ -318,12 +334,12 @@ export class AppService { // ====================== RESERVATIONS =============================== //POST parameter from API - createReservation(start_time: string, finish_time: string, status: string, + createReservation(start_time: string, finish_time: string, status: string, date_entry: Date, user_id: string, common_area_id: string) { const pattern = { cmd: 'createReservation' }; const payload = { - start_time: start_time, finish_time: finish_time, status: status, - date_entry: date_entry, user_id: user_id, common_area_id: common_area_id + start_time: start_time, finish_time: finish_time, status: status, + date_entry: date_entry, user_id: user_id, common_area_id: common_area_id }; return this.clientReservationApp .send(pattern, payload) @@ -357,11 +373,11 @@ export class AppService { // ====================== POSTS =============================== //POST parameter from API - createPost(post: string, date_entry: Date, user_id: string, + createPost(post: string, date_entry: Date, user_id: string, community_id: string) { const pattern = { cmd: 'createPost' }; const payload = { - post: post, date_entry: date_entry, user_id: user_id, + post: post, date_entry: date_entry, user_id: user_id, community_id: community_id }; return this.clientPostApp @@ -395,11 +411,11 @@ export class AppService { // ====================== COMMNENT POSTS =============================== //Comment parameter from API - createComment(comment: string, date_entry: Date, user_id: string, + createComment(comment: string, date_entry: Date, user_id: string, post_id: string) { const pattern = { cmd: 'createComment' }; const payload = { - comment: comment, date_entry: date_entry, user_id: user_id, + comment: comment, date_entry: date_entry, user_id: user_id, post_id: post_id }; return this.clientPostApp @@ -433,11 +449,11 @@ export class AppService { // ====================== REPORTS =============================== //Report parameter from API - createReport(action: string, description: string, date_entry: Date, + createReport(action: string, description: string, date_entry: Date, user_id: string) { const pattern = { cmd: 'createReport' }; const payload = { - action: action, description: description, date_entry: date_entry, + action: action, description: description, date_entry: date_entry, user_id: user_id }; return this.clientReportApp @@ -471,7 +487,7 @@ export class AppService { sendMail(email: string) { const pattern = { cmd: 'sendMail' }; - const payload = { email: email}; + const payload = { email: email }; return this.clientNotificationtApp .send(pattern, payload) .pipe( @@ -481,11 +497,29 @@ export class AppService { html(email: string, name: string) { const pattern = { cmd: 'html' }; - const payload = { email: email, name: name}; + const payload = { email: email, name: name }; return this.clientNotificationtApp .send(pattern, payload) .pipe( map((message: string) => ({ message })), ); } + + + + /* Function to generate combination of password */ + generatePassword() { + var pass = ''; + var str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' + + 'abcdefghijklmnopqrstuvwxyz0123456789@#$'; + + for (let i = 1; i <= 8; i++) { + var char = Math.floor(Math.random() + * str.length + 1); + + pass += str.charAt(char) + } + + return pass; + } } \ No newline at end of file diff --git a/servicio-notificaciones/src/email.controller.ts b/servicio-notificaciones/src/email.controller.ts index c18340a0..882ebccf 100644 --- a/servicio-notificaciones/src/email.controller.ts +++ b/servicio-notificaciones/src/email.controller.ts @@ -22,8 +22,8 @@ export class EmailController { @MessagePattern({ cmd: 'html' }) async postHTMLEmail(@Payload() user: any) { - const url = "http://localhost:3000/"; - const image = "images/email.ong"; + const url = "http://localhost:3000/"; + const image = "images/email.ong"; var response = await this.mailService.sendMail({ to: user["email"], from: "mbonilla.guti@gmail.com", @@ -36,9 +36,44 @@ export class EmailController { attachments: [ { filename: 'email.png', - path: __dirname +'/mails/images/email.png', + path: __dirname + '/mails/images/email.png', cid: 'logo' //my mistake was putting "cid:logo@cid" here! - } + } + ] + }); + return response; + } + + + @MessagePattern({ cmd: 'emailCreateUserAdminCommunity' }) + async emailCreateUserAdmin(@Payload() user: any) { + const url = "http://localhost:3000/"; + const image = "images/email.png"; + const logo = "images/Logo Katoikia.svg"; + var response = await this.mailService.sendMail({ + to: user["email"], + from: "mbonilla.guti@gmail.com", + subject: 'Usuario registrado', + template: 'emailCreateUserAdmin', + context: { + name: user["name"], + password: user["password"], + date_entry: user["date_entry"], + email: user["email"], + community_name: user['community_name'], + url + }, + attachments: [ + { + filename: 'email.png', + path: __dirname + '/mails/images/email.png', + cid: 'image_email' //my mistake was putting "cid:logo@cid" here! + }, + { + filename: 'Logo Katoikia.svg', + path: __dirname + '/mails/images/Logo Katoikia.svg', + cid: 'logo' //my mistake was putting "cid:logo@cid" here! + } ] }); return response; diff --git a/servicio-notificaciones/src/mails/emailCreateUserAdmin.hbs b/servicio-notificaciones/src/mails/emailCreateUserAdmin.hbs new file mode 100644 index 00000000..e14f72e0 --- /dev/null +++ b/servicio-notificaciones/src/mails/emailCreateUserAdmin.hbs @@ -0,0 +1,442 @@ + + + + + + + + + + + + + + + + + + + + + + + + +
+
+ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌  +
+ +
+ + + \ No newline at end of file diff --git a/servicio-notificaciones/src/mails/emailCreateUserAdminCommunity.hbs b/servicio-notificaciones/src/mails/emailCreateUserAdminCommunity.hbs new file mode 100644 index 00000000..04cd0f59 --- /dev/null +++ b/servicio-notificaciones/src/mails/emailCreateUserAdminCommunity.hbs @@ -0,0 +1,442 @@ + + + + + + + + + + + + + + + + + + + + + + + + +
+
+ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌ ‌  +
+ +
+ + + \ No newline at end of file diff --git a/servicio-notificaciones/src/mails/images/Logo Katoikia.png b/servicio-notificaciones/src/mails/images/Logo Katoikia.png new file mode 100644 index 00000000..d59e2c9a Binary files /dev/null and b/servicio-notificaciones/src/mails/images/Logo Katoikia.png differ diff --git a/servicio-notificaciones/src/mails/images/Logo Katoikia.svg b/servicio-notificaciones/src/mails/images/Logo Katoikia.svg new file mode 100644 index 00000000..1416dbb0 --- /dev/null +++ b/servicio-notificaciones/src/mails/images/Logo Katoikia.svg @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/servicio-usuarios/src/users/users.controller.ts b/servicio-usuarios/src/users/users.controller.ts index ea3c70a9..a059227e 100644 --- a/servicio-usuarios/src/users/users.controller.ts +++ b/servicio-usuarios/src/users/users.controller.ts @@ -22,6 +22,10 @@ export class UsersController { return this.userService.create(user); } + @MessagePattern({ cmd: 'createAdminCommunity' }) + createAdminCommunity(@Payload() user: UserDocument) { + return this.userService.createAdminCommunity(user); + } @MessagePattern({ cmd: 'findAllUsers' }) findAll() { @@ -91,5 +95,7 @@ export class UsersController { return this.userService.deleteAdminSystem(user["id"]); } + + } diff --git a/servicio-usuarios/src/users/users.module.ts b/servicio-usuarios/src/users/users.module.ts index 75fc3bd5..79cad874 100644 --- a/servicio-usuarios/src/users/users.module.ts +++ b/servicio-usuarios/src/users/users.module.ts @@ -18,6 +18,16 @@ import { ClientsModule, Transport } from "@nestjs/microservices"; } } ]), + ClientsModule.register([ + { + name: "SERVICIO_COMUNIDADES", + transport: Transport.TCP, + options: { + host: "127.0.0.1", + port: 3002 + } + } + ]), MongooseModule.forFeature([{ name: User.name, schema: UserSchema }]), ], controllers: [UsersController], diff --git a/servicio-usuarios/src/users/users.service.ts b/servicio-usuarios/src/users/users.service.ts index edaf2ed0..7119c469 100644 --- a/servicio-usuarios/src/users/users.service.ts +++ b/servicio-usuarios/src/users/users.service.ts @@ -4,6 +4,7 @@ import { User, UserDocument } from '../schemas/user.schema'; import { InjectModel } from '@nestjs/mongoose'; import { Md5 } from "md5-typescript"; import { map } from 'rxjs/operators'; +import { lastValueFrom } from 'rxjs'; import { RpcException, ClientProxy } from '@nestjs/microservices'; @@ -14,6 +15,7 @@ export class UsersService { constructor( @InjectModel(User.name) private readonly userModel: Model, @Inject('SERVICIO_NOTIFICACIONES') private readonly clientNotificationtApp: ClientProxy, + @Inject('SERVICIO_COMUNIDADES') private readonly clientCommunityApp: ClientProxy, ) { } private publicKey: string; @@ -23,6 +25,38 @@ export class UsersService { return this.userModel.create(user); } + + async createAdminCommunity(user: UserDocument) { + let password = user.password; + let passwordEncriptada = Md5.init(user.password); + user.password = passwordEncriptada; + this.userModel.create(user); + let community = await this.findCommunity(user.community_id); + user.community_id = community['name']; + + const pattern = { cmd: 'emailCreateUserAdminCommunity' }; + const payload = { email: user['email'], password: password, name: user['name'], date_entry: user['date_entry'] }; + return this.clientNotificationtApp + .send(pattern, payload) + .pipe( + map((message: string) => ({ message })), + ); + } + + async findCommunity(community_id: string) { + const pattern = { cmd: 'findOneCommunity' } + const payload = { _id: community_id } + + let callback = await this.clientCommunityApp + .send(pattern, payload) + .pipe( + map((response: string) => ({ response })) + ) + const finalValue = await lastValueFrom(callback); + return finalValue['response']; + + } + async findAll(): Promise { return this.userModel .find() @@ -110,7 +144,11 @@ export class UsersService { } async deleteAdminSystem(id: string) { - return this.userModel.deleteOne({_id: id}).exec(); + return this.userModel.deleteOne({ _id: id }).exec(); } + + + } + diff --git a/web-ui/web-react/src/components/AdministradoresComunidad.js b/web-ui/web-react/src/components/AdministradoresComunidad.js index 7a203a1e..a5ab7f0a 100644 --- a/web-ui/web-react/src/components/AdministradoresComunidad.js +++ b/web-ui/web-react/src/components/AdministradoresComunidad.js @@ -31,6 +31,7 @@ const AdministradoresComunidad = () => { community_id: '', community_name: '', user_type: '2', + date_entry: new Date(), status: '1' }; @@ -48,8 +49,6 @@ const AdministradoresComunidad = () => { const dt = useRef(null); - - async function listaAdmin() { let nombres = await fetch('http://localhost:4000/user/findAdminComunidad/', { method: 'GET' }) .then((response) => response.json()) @@ -58,14 +57,14 @@ const AdministradoresComunidad = () => { //item.full_name returns the repositorie name return fetch(`http://localhost:4000/community/findCommunityName/${item.community_id}`, { method: 'GET' }) .then((response2) => response2.json()) - .then(data => { - console.log(data.message.name); - item.community_name = data.message.name + .then(data => data.message) + .then( data => { + item.community_name = data['name'] return item }) })); - }).then(data => setListaAdmins(data) - ); + }) + .then(data => setListaAdmins(data)); } @@ -90,7 +89,7 @@ const AdministradoresComunidad = () => { const cList = communitiesList.map((item) => ({ label: item.name, - value: item.id, + value: item._id, })) @@ -153,9 +152,46 @@ const AdministradoresComunidad = () => { const saveAdminCommunity = () => { - if (adminCommunity.password !== adminCommunity.confirmPassword) { - setSubmitted(true); - } else if (adminCommunity.name && adminCommunity.dni && adminCommunity.last_name && adminCommunity.email && adminCommunity.password && adminCommunity.phone) { + if (adminCommunity.name && adminCommunity.dni && adminCommunity.last_name && adminCommunity.email && adminCommunity.phone) { + + let _administrators= [...listaAdmins]; + let _adminCommunity = { ...adminCommunity }; + _adminCommunity.community_id = communityId; + console.log(_adminCommunity) + console.log(communityId) + + fetch('http://localhost:4000/user/createAdminCommunity', { + cache: 'no-cache', + method: 'POST', + body: JSON.stringify(_adminCommunity), + 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(() => { + + // _adminCommunity.community_id = communitiesList.find(c => c._id === _adminCommunity.community_id).name + + _administrators.push(_adminCommunity); + toast.current.show({ severity: 'success', summary: 'Registro exitoso', detail: 'Comunidad de vivienda Creada', life: 3000 }); + + setListaAdmins(_administrators); + + setAdminCommunity(emptyAdminCommunity); + + }) + .catch( + err => console.log('Ocurrió un error con el fetch', err) + ); + toast.current.show({ severity: 'success', summary: 'Registro exitoso', detail: 'Administrador de Comunidad de vivienda Creada', life: 3000 }); } else { @@ -286,8 +322,9 @@ const AdministradoresComunidad = () => { } const handleCommunities = (event) => { - const getCommunity = event.target.value; - setCommunityId(getCommunity); + const getCommunityValue = event.target.value; + setCommunityId(getCommunityValue); + console.log(getCommunityValue) } return (
@@ -397,8 +434,8 @@ const AdministradoresComunidad = () => { - +
{submitted && !communityId && Comunidad es requirida.} diff --git a/web-ui/web-react/src/components/AdministradoresSistema.js b/web-ui/web-react/src/components/AdministradoresSistema.js index 68c29f1f..0991b828 100644 --- a/web-ui/web-react/src/components/AdministradoresSistema.js +++ b/web-ui/web-react/src/components/AdministradoresSistema.js @@ -59,6 +59,7 @@ const AdministradoresSistema = () => { user_type: "1", //1 es admin status: "1" }; + setSysAdmin(data) // console.log(data); fetch('http://localhost:4000/user/createAdminSystem/', { @@ -80,7 +81,7 @@ const AdministradoresSistema = () => { .then( function (response) { let _administrators = [...administrators]; - let _admin = { ...response.message };; + let _admin = { ...sysadmin }; _administrators.push(_admin); setAdministrators(_administrators) }