katoikia-app/servicio-usuarios/src/users/users.service.ts

110 lines
3.1 KiB
TypeScript
Raw Normal View History

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';
import { map } from 'rxjs/operators';
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-25 04:38:48 +00:00
@Inject('SERVICIO_NOTIFICACIONES')
private readonly clientNotificationtApp: ClientProxy,
) {}
private publicKey: string;
2022-06-29 09:23:07 +00:00
async create(user: UserDocument): Promise<User> {
let passwordEncriptada = Md5.init(user.password);
user.password = passwordEncriptada;
2022-06-29 09:23:07 +00:00
return this.userModel.create(user);
}
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
async findLogin(email: string, password: string): Promise<User> {
let repo1 = this.userModel;
let userReturn = new Promise<User>((resolve, reject) => {
let repo = repo1;
2022-07-11 02:46:54 +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 {
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);
}
}
});
});
return userReturn;
2022-07-11 02:22:34 +00:00
}
//find admin del sistema
async allUsersAdminSistema(): Promise<User[]> {
return this.userModel.find({ user_type: 1 }).exec();
}
2022-07-19 04:28:56 +00:00
//find admin del sistema
async findGuardsCommunity(pcommunity_id: string): Promise<User[]> {
2022-07-19 04:28:56 +00:00
return this.userModel.find({ user_type: 4 }).exec();
}
//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);
/*.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-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();
}
async deleteAdminSystem(id: string) {
2022-07-25 04:38:48 +00:00
return this.userModel.deleteOne({ _id: id }).exec();
}
2022-06-29 09:23:07 +00:00
}