2022-07-16 01:02:17 +00:00
|
|
|
import { Injectable, Inject } from '@nestjs/common';
|
2022-06-29 09:23:07 +00:00
|
|
|
import { Model } from 'mongoose';
|
|
|
|
import { User, UserDocument } from '../schemas/user.schema';
|
|
|
|
import { InjectModel } from '@nestjs/mongoose';
|
2022-07-25 04:38:48 +00:00
|
|
|
import { Md5 } from 'md5-typescript';
|
2022-07-16 01:02:17 +00:00
|
|
|
import { map } from 'rxjs/operators';
|
2022-07-26 22:59:29 +00:00
|
|
|
import { lastValueFrom } from 'rxjs';
|
2022-07-16 01:02:17 +00:00
|
|
|
|
|
|
|
import { RpcException, ClientProxy } from '@nestjs/microservices';
|
|
|
|
|
2022-06-29 09:23:07 +00:00
|
|
|
@Injectable()
|
|
|
|
export class UsersService {
|
|
|
|
constructor(
|
|
|
|
@InjectModel(User.name) private readonly userModel: Model<UserDocument>,
|
2022-07-16 01:02:17 +00:00
|
|
|
@Inject('SERVICIO_NOTIFICACIONES') private readonly clientNotificationtApp: ClientProxy,
|
2022-07-26 22:59:29 +00:00
|
|
|
@Inject('SERVICIO_COMUNIDADES') private readonly clientCommunityApp: ClientProxy,
|
2022-07-16 01:02:17 +00:00
|
|
|
|
|
|
|
) { }
|
2022-07-22 07:22:13 +00:00
|
|
|
private publicKey: string;
|
2022-06-29 09:23:07 +00:00
|
|
|
async create(user: UserDocument): Promise<User> {
|
2022-07-16 01:02:17 +00:00
|
|
|
let passwordEncriptada = Md5.init(user.password);
|
|
|
|
user.password = passwordEncriptada;
|
2022-06-29 09:23:07 +00:00
|
|
|
return this.userModel.create(user);
|
|
|
|
}
|
|
|
|
|
2022-07-26 22:59:29 +00:00
|
|
|
|
|
|
|
async createAdminCommunity(user: UserDocument) {
|
2022-07-27 21:54:31 +00:00
|
|
|
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'], community_name: community['name'] };
|
|
|
|
return this.clientNotificationtApp
|
|
|
|
.send<string>(pattern, payload)
|
|
|
|
.pipe(
|
|
|
|
map((message: string) => ({ message })),
|
|
|
|
);
|
2022-07-26 22:59:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async findCommunity(community_id: string) {
|
|
|
|
const pattern = { cmd: 'findOneCommunity' }
|
|
|
|
const payload = { _id: community_id }
|
|
|
|
|
|
|
|
let callback = await this.clientCommunityApp
|
|
|
|
.send<string>(pattern, payload)
|
|
|
|
.pipe(
|
|
|
|
map((response: string) => ({ response }))
|
|
|
|
)
|
|
|
|
const finalValue = await lastValueFrom(callback);
|
|
|
|
return finalValue['response'];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-07-16 01:02:17 +00:00
|
|
|
async findAll(): Promise<User[]> {
|
2022-07-25 04:38:48 +00:00
|
|
|
return this.userModel.find().setOptions({ sanitizeFilter: true }).exec();
|
2022-06-29 09:23:07 +00:00
|
|
|
}
|
|
|
|
async findOne(id: string): Promise<User> {
|
|
|
|
return this.userModel.findOne({ _id: id }).exec();
|
|
|
|
}
|
|
|
|
|
2022-06-29 10:12:27 +00:00
|
|
|
async findOneByDNI(dni: string): Promise<User> {
|
|
|
|
return this.userModel.findOne({ dni: dni }).exec();
|
|
|
|
}
|
|
|
|
|
2022-06-29 09:23:07 +00:00
|
|
|
async update(id: string, user: UserDocument) {
|
|
|
|
return this.userModel.findOneAndUpdate({ _id: id }, user, {
|
|
|
|
new: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async remove(id: string) {
|
|
|
|
return this.userModel.findByIdAndRemove({ _id: id }).exec();
|
|
|
|
}
|
2022-07-11 02:22:34 +00:00
|
|
|
|
|
|
|
//inicio de sesion
|
2022-07-16 01:02:17 +00:00
|
|
|
async findLogin(email: string, password: string): Promise<User> {
|
|
|
|
let repo1 = this.userModel;
|
2022-07-11 03:48:25 +00:00
|
|
|
let userReturn = new Promise<User>((resolve, reject) => {
|
2022-07-16 01:02:17 +00:00
|
|
|
let repo = repo1;
|
2022-07-11 02:46:54 +00:00
|
|
|
|
2022-07-16 01:02:17 +00:00
|
|
|
repo.find({ email: email }).exec((err, res) => {
|
2022-07-11 02:46:54 +00:00
|
|
|
if (err) {
|
|
|
|
reject(err);
|
2022-07-25 04:38:48 +00:00
|
|
|
} else {
|
2022-07-16 01:02:17 +00:00
|
|
|
let passwordEncriptada = Md5.init(password);
|
|
|
|
if (res[0].password == passwordEncriptada) {
|
2022-07-11 02:46:54 +00:00
|
|
|
resolve(res[0]);
|
2022-07-25 04:38:48 +00:00
|
|
|
} else {
|
2022-07-11 02:46:54 +00:00
|
|
|
resolve(null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2022-07-16 01:02:17 +00:00
|
|
|
|
|
|
|
return userReturn;
|
2022-07-11 02:22:34 +00:00
|
|
|
}
|
2022-07-15 01:01:02 +00:00
|
|
|
|
|
|
|
//find admin del sistema
|
2022-07-16 01:02:17 +00:00
|
|
|
async allUsersAdminSistema(): Promise<User[]> {
|
2022-07-15 01:01:02 +00:00
|
|
|
return this.userModel.find({ user_type: 1 }).exec();
|
|
|
|
}
|
2022-07-15 03:16:02 +00:00
|
|
|
|
2022-07-19 04:28:56 +00:00
|
|
|
//find admin del sistema
|
2022-07-22 07:22:13 +00:00
|
|
|
async findGuardsCommunity(pcommunity_id: string): Promise<User[]> {
|
2022-07-19 04:28:56 +00:00
|
|
|
return this.userModel.find({ user_type: 4 }).exec();
|
|
|
|
}
|
2022-07-16 01:02:17 +00:00
|
|
|
//find admin de comunidad
|
|
|
|
async allUsersAdminComunidad(): Promise<User[]> {
|
|
|
|
return this.userModel.find({ user_type: 2 }).exec();
|
|
|
|
}
|
|
|
|
|
|
|
|
async testSendMail(user: UserDocument) {
|
|
|
|
let passwordEncriptada = Md5.init(user.password);
|
|
|
|
user.password = passwordEncriptada;
|
2022-07-25 04:38:48 +00:00
|
|
|
this.userModel.create(user);
|
2022-07-16 01:02:17 +00:00
|
|
|
/*.then(() => {
|
|
|
|
|
|
|
|
} );*/
|
|
|
|
|
|
|
|
const pattern = { cmd: 'html' };
|
|
|
|
const payload = { email: user['email'], name: user['name'] };
|
|
|
|
return this.clientNotificationtApp
|
|
|
|
.send<string>(pattern, payload)
|
2022-07-25 04:38:48 +00:00
|
|
|
.pipe(map((message: string) => ({ message })));
|
2022-07-16 01:02:17 +00:00
|
|
|
}
|
2022-07-15 03:16:02 +00:00
|
|
|
|
2022-07-25 04:38:48 +00:00
|
|
|
async findCommunityUser(
|
|
|
|
community_id: string,
|
|
|
|
user_type: number,
|
|
|
|
): Promise<User> {
|
|
|
|
return this.userModel
|
|
|
|
.findOne({ community_id: community_id, user_type: user_type })
|
|
|
|
.exec();
|
2022-07-19 19:56:48 +00:00
|
|
|
}
|
|
|
|
|
2022-07-22 07:22:13 +00:00
|
|
|
async deleteAdminSystem(id: string) {
|
2022-07-26 22:59:29 +00:00
|
|
|
return this.userModel.deleteOne({ _id: id }).exec();
|
2022-07-22 07:22:13 +00:00
|
|
|
}
|
2022-07-22 05:10:36 +00:00
|
|
|
|
2022-07-27 21:54:31 +00:00
|
|
|
async validateEmail(email: string) {
|
|
|
|
let repo1 = this.userModel;
|
|
|
|
return new Promise<User>((resolve, reject) => {
|
|
|
|
let repo = repo1;
|
2022-07-26 22:59:29 +00:00
|
|
|
|
2022-07-27 21:54:31 +00:00
|
|
|
repo.find({ email: email }).exec((err, res) => {
|
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
} else {
|
|
|
|
if (res.length > 0) {
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2022-07-26 22:59:29 +00:00
|
|
|
|
2022-06-29 09:23:07 +00:00
|
|
|
}
|
2022-07-26 22:59:29 +00:00
|
|
|
|