2022-06-29 09:23:07 +00:00
|
|
|
import { Injectable } from '@nestjs/common';
|
|
|
|
import { Model } from 'mongoose';
|
|
|
|
import { User, UserDocument } from '../schemas/user.schema';
|
|
|
|
import { InjectModel } from '@nestjs/mongoose';
|
2022-07-11 03:42:06 +00:00
|
|
|
import {Md5} from "md5-typescript";
|
2022-06-29 09:23:07 +00:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class UsersService {
|
|
|
|
constructor(
|
|
|
|
@InjectModel(User.name) private readonly userModel: Model<UserDocument>,
|
|
|
|
) {}
|
2022-07-11 03:42:06 +00:00
|
|
|
private publicKey: string;
|
2022-06-29 09:23:07 +00:00
|
|
|
async create(user: UserDocument): Promise<User> {
|
2022-07-11 04:21:52 +00:00
|
|
|
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[]> {
|
|
|
|
return this.userModel
|
|
|
|
.find()
|
|
|
|
.setOptions({ sanitizeFilter: true })
|
|
|
|
.exec();
|
|
|
|
}
|
|
|
|
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-11 03:49:45 +00:00
|
|
|
async findLogin(email: string, password: string) : Promise<User> {
|
2022-07-11 02:46:54 +00:00
|
|
|
let repo1=this.userModel;
|
2022-07-11 03:48:25 +00:00
|
|
|
let userReturn = new Promise<User>((resolve, reject) => {
|
2022-07-11 02:46:54 +00:00
|
|
|
let repo =repo1;
|
|
|
|
|
|
|
|
repo.find({ email : email }).exec((err, res) => {
|
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
}
|
|
|
|
else {
|
2022-07-11 03:45:08 +00:00
|
|
|
let passwordEncriptada=Md5.init(password);
|
2022-07-11 03:42:06 +00:00
|
|
|
if (res[0].password==passwordEncriptada) {
|
2022-07-11 02:46:54 +00:00
|
|
|
resolve(res[0]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
resolve(null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-07-11 03:48:25 +00:00
|
|
|
return userReturn;
|
2022-07-11 02:22:34 +00:00
|
|
|
}
|
2022-07-15 01:01:02 +00:00
|
|
|
|
|
|
|
//find admin del sistema
|
|
|
|
async allUsersAdminSistema(): Promise<User[]> {
|
|
|
|
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
|
|
|
|
async findGuardsCommunity(pcommunity_id: string): Promise<User[]> {
|
|
|
|
return this.userModel.find({ user_type: 4 }).exec();
|
|
|
|
}
|
2022-07-15 03:16:02 +00:00
|
|
|
//find admin de comunidad
|
|
|
|
async allUsersAdminComunidad(): Promise<User[]> {
|
|
|
|
return this.userModel.find({ user_type: 2 }).exec();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-06-29 09:23:07 +00:00
|
|
|
}
|