2022-12-23 21:08:50 +01:00
|
|
|
import { BadRequestException, Inject, Injectable, NotFoundException } from '@nestjs/common';
|
2023-01-16 13:09:04 -05:00
|
|
|
import { randomBytes } from 'crypto';
|
2022-12-23 21:08:50 +01:00
|
|
|
import { ReadStream } from 'fs';
|
2023-01-11 21:34:36 -05:00
|
|
|
import { AuthUserDto } from '../auth';
|
|
|
|
|
import { IUserRepository } from '../user';
|
2022-02-03 10:06:44 -06:00
|
|
|
import { CreateUserDto } from './dto/create-user.dto';
|
|
|
|
|
import { UpdateUserDto } from './dto/update-user.dto';
|
2022-12-09 15:53:11 -05:00
|
|
|
import { UserCountDto } from './dto/user-count.dto';
|
2022-07-08 21:26:50 -05:00
|
|
|
import {
|
|
|
|
|
CreateProfileImageResponseDto,
|
|
|
|
|
mapCreateProfileImageResponse,
|
|
|
|
|
} from './response-dto/create-profile-image-response.dto';
|
2022-11-14 21:24:25 -05:00
|
|
|
import { mapUserCountResponse, UserCountResponseDto } from './response-dto/user-count-response.dto';
|
|
|
|
|
import { mapUser, UserResponseDto } from './response-dto/user-response.dto';
|
2022-12-23 21:08:50 +01:00
|
|
|
import { UserCore } from './user.core';
|
2022-02-03 10:06:44 -06:00
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class UserService {
|
2022-12-23 21:08:50 +01:00
|
|
|
private userCore: UserCore;
|
2022-12-30 08:22:06 -05:00
|
|
|
constructor(@Inject(IUserRepository) userRepository: IUserRepository) {
|
2022-12-23 21:08:50 +01:00
|
|
|
this.userCore = new UserCore(userRepository);
|
|
|
|
|
}
|
2022-05-21 02:23:55 -05:00
|
|
|
|
2022-07-08 21:26:50 -05:00
|
|
|
async getAllUsers(authUser: AuthUserDto, isAll: boolean): Promise<UserResponseDto[]> {
|
2022-05-21 02:23:55 -05:00
|
|
|
if (isAll) {
|
2022-12-23 21:08:50 +01:00
|
|
|
const allUsers = await this.userCore.getList();
|
2022-07-08 21:26:50 -05:00
|
|
|
return allUsers.map(mapUser);
|
2022-05-21 02:23:55 -05:00
|
|
|
}
|
2022-04-23 21:08:45 -05:00
|
|
|
|
2022-12-23 21:08:50 +01:00
|
|
|
const allUserExceptRequestedUser = await this.userCore.getList({ excludeId: authUser.id });
|
2022-07-08 21:26:50 -05:00
|
|
|
return allUserExceptRequestedUser.map(mapUser);
|
2022-04-23 21:08:45 -05:00
|
|
|
}
|
2022-05-21 02:23:55 -05:00
|
|
|
|
2022-11-07 16:53:47 -05:00
|
|
|
async getUserById(userId: string, withDeleted = false): Promise<UserResponseDto> {
|
2022-12-23 21:08:50 +01:00
|
|
|
const user = await this.userCore.get(userId, withDeleted);
|
2022-07-16 23:52:00 -05:00
|
|
|
if (!user) {
|
|
|
|
|
throw new NotFoundException('User not found');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return mapUser(user);
|
|
|
|
|
}
|
2022-07-17 15:09:26 -05:00
|
|
|
|
2022-07-08 21:26:50 -05:00
|
|
|
async getUserInfo(authUser: AuthUserDto): Promise<UserResponseDto> {
|
2022-12-23 21:08:50 +01:00
|
|
|
const user = await this.userCore.get(authUser.id);
|
2022-07-08 21:26:50 -05:00
|
|
|
if (!user) {
|
|
|
|
|
throw new BadRequestException('User not found');
|
|
|
|
|
}
|
|
|
|
|
return mapUser(user);
|
2022-06-27 15:13:07 -05:00
|
|
|
}
|
|
|
|
|
|
2022-12-09 15:53:11 -05:00
|
|
|
async getUserCount(dto: UserCountDto): Promise<UserCountResponseDto> {
|
2022-12-23 21:08:50 +01:00
|
|
|
let users = await this.userCore.getList();
|
2022-12-09 15:53:11 -05:00
|
|
|
|
|
|
|
|
if (dto.admin) {
|
|
|
|
|
users = users.filter((user) => user.isAdmin);
|
|
|
|
|
}
|
2022-05-21 02:23:55 -05:00
|
|
|
|
2022-07-08 21:26:50 -05:00
|
|
|
return mapUserCountResponse(users.length);
|
2022-05-21 02:23:55 -05:00
|
|
|
}
|
|
|
|
|
|
2022-06-25 19:53:06 +02:00
|
|
|
async createUser(createUserDto: CreateUserDto): Promise<UserResponseDto> {
|
2022-12-23 21:08:50 +01:00
|
|
|
const createdUser = await this.userCore.createUser(createUserDto);
|
|
|
|
|
return mapUser(createdUser);
|
2022-05-21 02:23:55 -05:00
|
|
|
}
|
|
|
|
|
|
2022-12-26 10:35:52 -05:00
|
|
|
async updateUser(authUser: AuthUserDto, dto: UpdateUserDto): Promise<UserResponseDto> {
|
|
|
|
|
const user = await this.userCore.get(dto.id);
|
2022-06-25 19:53:06 +02:00
|
|
|
if (!user) {
|
|
|
|
|
throw new NotFoundException('User not found');
|
|
|
|
|
}
|
2022-12-26 10:35:52 -05:00
|
|
|
const updatedUser = await this.userCore.updateUser(authUser, dto.id, dto);
|
2022-12-23 21:08:50 +01:00
|
|
|
return mapUser(updatedUser);
|
2022-05-21 02:23:55 -05:00
|
|
|
}
|
2022-05-27 22:15:35 -05:00
|
|
|
|
2022-11-07 16:53:47 -05:00
|
|
|
async deleteUser(authUser: AuthUserDto, userId: string): Promise<UserResponseDto> {
|
2022-12-23 21:08:50 +01:00
|
|
|
const user = await this.userCore.get(userId);
|
2022-11-07 16:53:47 -05:00
|
|
|
if (!user) {
|
|
|
|
|
throw new BadRequestException('User not found');
|
|
|
|
|
}
|
2022-12-23 21:08:50 +01:00
|
|
|
const deletedUser = await this.userCore.deleteUser(authUser, user);
|
|
|
|
|
return mapUser(deletedUser);
|
2022-11-07 16:53:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async restoreUser(authUser: AuthUserDto, userId: string): Promise<UserResponseDto> {
|
2022-12-23 21:08:50 +01:00
|
|
|
const user = await this.userCore.get(userId, true);
|
2022-11-07 16:53:47 -05:00
|
|
|
if (!user) {
|
|
|
|
|
throw new BadRequestException('User not found');
|
|
|
|
|
}
|
2022-12-23 21:08:50 +01:00
|
|
|
const updatedUser = await this.userCore.restoreUser(authUser, user);
|
|
|
|
|
return mapUser(updatedUser);
|
2022-11-07 16:53:47 -05:00
|
|
|
}
|
|
|
|
|
|
2022-07-08 21:26:50 -05:00
|
|
|
async createProfileImage(
|
|
|
|
|
authUser: AuthUserDto,
|
|
|
|
|
fileInfo: Express.Multer.File,
|
|
|
|
|
): Promise<CreateProfileImageResponseDto> {
|
2022-12-23 21:08:50 +01:00
|
|
|
const updatedUser = await this.userCore.createProfileImage(authUser, fileInfo.path);
|
|
|
|
|
return mapCreateProfileImageResponse(updatedUser.id, updatedUser.profileImagePath);
|
2022-05-27 22:24:58 -05:00
|
|
|
}
|
2022-05-27 22:15:35 -05:00
|
|
|
|
2022-12-23 21:08:50 +01:00
|
|
|
async getUserProfileImage(userId: string): Promise<ReadStream> {
|
|
|
|
|
const user = await this.userCore.get(userId);
|
|
|
|
|
if (!user) {
|
|
|
|
|
throw new NotFoundException('User not found');
|
2022-05-28 22:35:45 -05:00
|
|
|
}
|
2022-12-23 21:08:50 +01:00
|
|
|
return this.userCore.getUserProfileImage(user);
|
2022-05-27 22:15:35 -05:00
|
|
|
}
|
2023-01-16 13:09:04 -05:00
|
|
|
|
|
|
|
|
async resetAdminPassword(ask: (admin: UserResponseDto) => Promise<string | undefined>) {
|
|
|
|
|
const admin = await this.userCore.getAdmin();
|
|
|
|
|
if (!admin) {
|
|
|
|
|
throw new BadRequestException('Admin account does not exist');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const providedPassword = await ask(admin);
|
|
|
|
|
const password = providedPassword || randomBytes(24).toString('base64').replace(/\W/g, '');
|
|
|
|
|
|
|
|
|
|
await this.userCore.updateUser(admin, admin.id, { password });
|
|
|
|
|
|
|
|
|
|
return { admin, password, provided: !!providedPassword };
|
|
|
|
|
}
|
2022-02-03 10:06:44 -06:00
|
|
|
}
|