You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
64 lines
1.7 KiB
64 lines
1.7 KiB
import { Injectable, Inject } from '@nestjs/common'; |
|
import { User } from './entities/user.entity'; |
|
import { CreateUserDto, UpdateUserDto } from './dto'; |
|
import { USER_REPOSITORY } from '../../core/constants'; |
|
import * as argon from 'argon2'; |
|
import * as _ from 'lodash'; |
|
import { UUID } from 'crypto'; |
|
|
|
@Injectable() |
|
export class UsersService { |
|
constructor( |
|
@Inject(USER_REPOSITORY) private readonly userRepository: typeof User, |
|
) {} |
|
|
|
async findAll() { |
|
return await this.userRepository.findAll(); |
|
} |
|
|
|
async findOne(id: UUID) { |
|
return await this.userRepository.findAll({ where: { uuid: id } }); |
|
} |
|
|
|
async create(createUserDto: CreateUserDto) { |
|
const hashedPassword = await argon.hash(createUserDto.password); |
|
createUserDto.password = hashedPassword; |
|
|
|
const newUser = await this.userRepository.create(createUserDto); |
|
|
|
return _.pick(newUser, [ |
|
'firstName', |
|
'lastName', |
|
'email', |
|
'uuid', |
|
'createdAt', |
|
'updatedAt', |
|
]); |
|
} |
|
|
|
// async update(id: UUID, updateUserDto: UpdateUserDto) { |
|
// const [numberOfAffectedRows, [updatedUser]] = |
|
// await this.userRepository.update( |
|
// { ...updateUserDto }, |
|
// { where: { uuid: id }, returning: true }, |
|
// ); |
|
// return { numberOfAffectedRows, updatedUser }; |
|
// } |
|
|
|
async update(id: UUID, updateUserDto: UpdateUserDto) { |
|
const [numberOfAffectedRows, [updatedUser]] = |
|
await this.userRepository.update( |
|
{ ...updateUserDto }, |
|
{ where: { uuid: id }, returning: true }, |
|
); |
|
return { numberOfAffectedRows, updatedUser }; |
|
} |
|
|
|
async remove(id: UUID) { |
|
const deletedUser = await this.findOne(id); |
|
|
|
await this.userRepository.destroy({ where: { uuid: id } }); |
|
|
|
return deletedUser; |
|
} |
|
}
|
|
|