|
@@ -1,18 +1,15 @@
|
|
|
import {
|
|
|
BadRequestException,
|
|
|
+ Inject,
|
|
|
Injectable,
|
|
|
InternalServerErrorException,
|
|
|
Logger,
|
|
|
NotFoundException,
|
|
|
StreamableFile,
|
|
|
} from '@nestjs/common';
|
|
|
-import { InjectRepository } from '@nestjs/typeorm';
|
|
|
-import { Not, Repository } from 'typeorm';
|
|
|
import { AuthUserDto } from '../../decorators/auth-user.decorator';
|
|
|
import { CreateUserDto } from './dto/create-user.dto';
|
|
|
import { UpdateUserDto } from './dto/update-user.dto';
|
|
|
-import { UserEntity } from '@app/database/entities/user.entity';
|
|
|
-import * as bcrypt from 'bcrypt';
|
|
|
import { createReadStream } from 'fs';
|
|
|
import { Response as Res } from 'express';
|
|
|
import { mapUser, UserResponseDto } from './response-dto/user-response.dto';
|
|
@@ -21,32 +18,28 @@ import {
|
|
|
CreateProfileImageResponseDto,
|
|
|
mapCreateProfileImageResponse,
|
|
|
} from './response-dto/create-profile-image-response.dto';
|
|
|
+import { IUserRepository, USER_REPOSITORY } from './user-repository';
|
|
|
|
|
|
@Injectable()
|
|
|
export class UserService {
|
|
|
constructor(
|
|
|
- @InjectRepository(UserEntity)
|
|
|
- private userRepository: Repository<UserEntity>,
|
|
|
+ @Inject(USER_REPOSITORY)
|
|
|
+ private userRepository: IUserRepository,
|
|
|
) {}
|
|
|
|
|
|
async getAllUsers(authUser: AuthUserDto, isAll: boolean): Promise<UserResponseDto[]> {
|
|
|
if (isAll) {
|
|
|
- const allUsers = await this.userRepository.find();
|
|
|
+ const allUsers = await this.userRepository.getList();
|
|
|
return allUsers.map(mapUser);
|
|
|
}
|
|
|
|
|
|
- const allUserExceptRequestedUser = await this.userRepository.find({
|
|
|
- where: { id: Not(authUser.id) },
|
|
|
- order: {
|
|
|
- createdAt: 'DESC',
|
|
|
- },
|
|
|
- });
|
|
|
+ const allUserExceptRequestedUser = await this.userRepository.getList({ excludeId: authUser.id });
|
|
|
|
|
|
return allUserExceptRequestedUser.map(mapUser);
|
|
|
}
|
|
|
|
|
|
async getUserInfo(authUser: AuthUserDto): Promise<UserResponseDto> {
|
|
|
- const user = await this.userRepository.findOne({ where: { id: authUser.id } });
|
|
|
+ const user = await this.userRepository.get(authUser.id);
|
|
|
if (!user) {
|
|
|
throw new BadRequestException('User not found');
|
|
|
}
|
|
@@ -54,28 +47,20 @@ export class UserService {
|
|
|
}
|
|
|
|
|
|
async getUserCount(): Promise<UserCountResponseDto> {
|
|
|
- const users = await this.userRepository.find();
|
|
|
+ const users = await this.userRepository.getList();
|
|
|
|
|
|
return mapUserCountResponse(users.length);
|
|
|
}
|
|
|
|
|
|
async createUser(createUserDto: CreateUserDto): Promise<UserResponseDto> {
|
|
|
- const user = await this.userRepository.findOne({ where: { email: createUserDto.email } });
|
|
|
+ const user = await this.userRepository.getByEmail(createUserDto.email);
|
|
|
|
|
|
if (user) {
|
|
|
throw new BadRequestException('User exists');
|
|
|
}
|
|
|
|
|
|
- const newUser = new UserEntity();
|
|
|
- newUser.email = createUserDto.email;
|
|
|
- newUser.salt = await bcrypt.genSalt();
|
|
|
- newUser.password = await this.hashPassword(createUserDto.password, newUser.salt);
|
|
|
- newUser.firstName = createUserDto.firstName;
|
|
|
- newUser.lastName = createUserDto.lastName;
|
|
|
- newUser.isAdmin = false;
|
|
|
-
|
|
|
try {
|
|
|
- const savedUser = await this.userRepository.save(newUser);
|
|
|
+ const savedUser = await this.userRepository.create(createUserDto);
|
|
|
|
|
|
return mapUser(savedUser);
|
|
|
} catch (e) {
|
|
@@ -84,40 +69,13 @@ export class UserService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private async hashPassword(password: string, salt: string): Promise<string> {
|
|
|
- return bcrypt.hash(password, salt);
|
|
|
- }
|
|
|
-
|
|
|
async updateUser(updateUserDto: UpdateUserDto): Promise<UserResponseDto> {
|
|
|
- const user = await this.userRepository.findOne({ where: { id: updateUserDto.id } });
|
|
|
+ const user = await this.userRepository.get(updateUserDto.id);
|
|
|
if (!user) {
|
|
|
throw new NotFoundException('User not found');
|
|
|
}
|
|
|
-
|
|
|
- user.lastName = updateUserDto.lastName || user.lastName;
|
|
|
- user.firstName = updateUserDto.firstName || user.firstName;
|
|
|
- user.profileImagePath = updateUserDto.profileImagePath || user.profileImagePath;
|
|
|
- user.shouldChangePassword =
|
|
|
- updateUserDto.shouldChangePassword != undefined ? updateUserDto.shouldChangePassword : user.shouldChangePassword;
|
|
|
-
|
|
|
- // If payload includes password - Create new password for user
|
|
|
- if (updateUserDto.password) {
|
|
|
- user.salt = await bcrypt.genSalt();
|
|
|
- user.password = await this.hashPassword(updateUserDto.password, user.salt);
|
|
|
- }
|
|
|
-
|
|
|
- if (updateUserDto.isAdmin) {
|
|
|
- const adminUser = await this.userRepository.findOne({ where: { isAdmin: true } });
|
|
|
-
|
|
|
- if (adminUser) {
|
|
|
- throw new BadRequestException('Admin user exists');
|
|
|
- }
|
|
|
-
|
|
|
- user.isAdmin = true;
|
|
|
- }
|
|
|
-
|
|
|
try {
|
|
|
- const updatedUser = await this.userRepository.save(user);
|
|
|
+ const updatedUser = await this.userRepository.update(user, updateUserDto);
|
|
|
|
|
|
return mapUser(updatedUser);
|
|
|
} catch (e) {
|
|
@@ -130,10 +88,13 @@ export class UserService {
|
|
|
authUser: AuthUserDto,
|
|
|
fileInfo: Express.Multer.File,
|
|
|
): Promise<CreateProfileImageResponseDto> {
|
|
|
+ const user = await this.userRepository.get(authUser.id);
|
|
|
+ if (!user) {
|
|
|
+ throw new NotFoundException('User not found');
|
|
|
+ }
|
|
|
+
|
|
|
try {
|
|
|
- await this.userRepository.update(authUser.id, {
|
|
|
- profileImagePath: fileInfo.path,
|
|
|
- });
|
|
|
+ await this.userRepository.createProfileImage(user, fileInfo)
|
|
|
|
|
|
return mapCreateProfileImageResponse(authUser.id, fileInfo.path);
|
|
|
} catch (e) {
|
|
@@ -144,7 +105,7 @@ export class UserService {
|
|
|
|
|
|
async getUserProfileImage(userId: string, res: Res) {
|
|
|
try {
|
|
|
- const user = await this.userRepository.findOne({ where: { id: userId } });
|
|
|
+ const user = await this.userRepository.get(userId);
|
|
|
if (!user) {
|
|
|
throw new NotFoundException('User not found');
|
|
|
}
|