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

71 lines
1.8 KiB
TypeScript
Raw Normal View History

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> {
return this.userModel.create(user);
}
async findAll(): Promise<User[]> {
return this.userModel
.find()
.setOptions({ sanitizeFilter: true })
.exec();
}
2022-06-29 10:12:27 +00:00
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-11 02:46:54 +00:00
async findLogin(email: string, password: string): Promise<User> {
return this.userModel.findOne({ email:email},{ password:password}).exec();
}
async findHero(email: string, password: string) : Promise<User> {
let repo1=this.userModel;
let p = new Promise<User>((resolve, reject) => {
let repo =repo1;
repo.find({ email : email }).exec((err, res) => {
if (err) {
reject(err);
}
else {
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);
}
}
});
});
return p;
2022-07-11 02:22:34 +00:00
}
2022-06-29 09:23:07 +00:00
}