import { Injectable , NotAcceptableException } from '@nestjs/common' ; import { InjectRepository } from '@nestjs/typeorm' ; import { Repository } from 'typeorm' ; import { createHmac } from 'crypto' ; import { Roles } from '../common/decorators/roles.decorator' ; import { User , UserFillableFields } from './user.entity' ; @ Injectable ( ) @ Roles ( 'admin' ) export class UsersService { constructor ( @ InjectRepository ( User ) private readonly userRepository ) { } async get ( id ) { return this . userRepository . findOne ( id ) ; } async getByEmail ( email ) { return await this . userRepository . findOne ( { email } ) ; } async getByEmailAndPass ( email , password ) { const passHash = createHmac ( 'sha256' , password ) . digest ( 'hex' ) ; return await this . userRepository . findOne ( { email , password : passHash } ) ; } async create ( payload ) { const checkUserExistence = await this . getByEmail ( payload . email ) ; if ( checkUserExistence ) { throw new NotAcceptableException ( 'Another user with provided email already exists.' ) ; } const newUser = this . userRepository . create ( payload ) ; return await this . userRepository . save ( newUser ) ; } }