import { Injectable , Logger } from '@nestjs/common' ; import { InjectRepository } from '@nestjs/typeorm' ; import { Repository } from 'typeorm' ; import { User } from './user.entity' ; import { UserCreateDto } from './dto/user.create.dto' ; @ Injectable ( ) export class UserService { private readonly logger = new Logger ( UserService . name ) ; constructor ( @ InjectRepository ( User ) private readonly repo ) { } async findAll ( ) { return this . repo . find ( ) ; } findOneById ( id , role ? ) { const additionalOptions = role ? { role } : { } ; return this . repo . findOne ( { id , ... additionalOptions } ) ; } findOneByEmail ( email ) { return this . repo . findOne ( { select : [ 'id' , 'role' , 'firstname' , 'lastname' , 'email' , 'password' , 'verified' ] , where : { email } , } ) ; } findOneBySocialUid ( provider , uid ) { return this . repo . findOne ( { select : [ 'id' , 'role' , 'firstname' , 'lastname' , 'email' , 'password' ] , where : { [ `${ provider }Uid` ] : uid } , } ) ; } createAndSave ( userCreateDto ) { const newUser = this . repo . create ( userCreateDto ) ; return this . repo . save ( newUser ) ; } async addSocialUid ( user , provider , uid ) { await this . repo . update ( user . id , { [ `${ provider }Uid` ] : uid } ) ; } async update ( id , user ) { this . repo . update ( id , user ) ; } async delete ( id ) { this . repo . delete ( id ) ; } async findOneByConfirmationToken ( confirmationToken ) { return this . repo . findOne ( { select : [ 'id' , 'email' ] , where : { confirmationToken } , } ) ; } async verify ( user ) { await this . repo . update ( user , { verified : true , confirmationToken : null } ) ; this . logger . log ( `User with id = ${ user . id } successfully verified!` ) ; } getInterviewers ( ) { return this . repo . find ( { where : { role : 'interviewer' } , } ) ; } getInterviewees ( ) { return this . repo . find ( { where : { role : 'user' } , } ) ; } }