mirror of
https://github.com/immich-app/immich
synced 2025-10-17 18:19:27 +00:00
* WIP fix user e2e tests The e2e tests still don't seem to work due to migrations not running. Changes: - update user.e2e tests to use new `userService.createUser` method - fix server `typeorm` command to use ORM config - update make test-e2e to re-create database volume every time - add User DTO - update auth.service and user.service to use User DTO - update CreateUserDto making optional properties that are optional * Fix migrations - add missing `.ts` extension to migrations path - update user e2e test for the new returned User resource
152 lines
4.6 KiB
TypeScript
152 lines
4.6 KiB
TypeScript
import { BadRequestException, Injectable, InternalServerErrorException, Logger, 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 './entities/user.entity';
|
|
import * as bcrypt from 'bcrypt';
|
|
import { createReadStream } from 'fs';
|
|
import { Response as Res } from 'express';
|
|
import { mapUser, User } from './response-dto/user';
|
|
|
|
@Injectable()
|
|
export class UserService {
|
|
constructor(
|
|
@InjectRepository(UserEntity)
|
|
private userRepository: Repository<UserEntity>,
|
|
) {}
|
|
|
|
async getAllUsers(authUser: AuthUserDto, isAll: boolean) {
|
|
if (isAll) {
|
|
return await this.userRepository.find();
|
|
}
|
|
|
|
return await this.userRepository.find({
|
|
where: { id: Not(authUser.id) },
|
|
order: {
|
|
createdAt: 'DESC',
|
|
},
|
|
});
|
|
}
|
|
|
|
async getUserCount(isAdmin: boolean) {
|
|
let users;
|
|
|
|
if (isAdmin) {
|
|
users = await this.userRepository.find({ where: { isAdmin: true } });
|
|
} else {
|
|
users = await this.userRepository.find();
|
|
}
|
|
|
|
return {
|
|
userCount: users.length,
|
|
};
|
|
}
|
|
|
|
async createUser(createUserDto: CreateUserDto): Promise<User> {
|
|
const user = await this.userRepository.findOne({ where: { email: 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);
|
|
|
|
return mapUser(savedUser);
|
|
} catch (e) {
|
|
Logger.error(e, 'Create new user');
|
|
throw new InternalServerErrorException('Failed to register new user');
|
|
}
|
|
}
|
|
|
|
private async hashPassword(password: string, salt: string): Promise<string> {
|
|
return bcrypt.hash(password, salt);
|
|
}
|
|
|
|
async updateUser(updateUserDto: UpdateUserDto) {
|
|
const user = await this.userRepository.findOne(updateUserDto.id);
|
|
|
|
user.lastName = updateUserDto.lastName || user.lastName;
|
|
user.firstName = updateUserDto.firstName || user.firstName;
|
|
user.profileImagePath = updateUserDto.profileImagePath || user.profileImagePath;
|
|
user.isFirstLoggedIn = updateUserDto.isFirstLoggedIn || user.isFirstLoggedIn;
|
|
|
|
// 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);
|
|
|
|
return {
|
|
id: updatedUser.id,
|
|
email: updatedUser.email,
|
|
firstName: updatedUser.firstName,
|
|
lastName: updatedUser.lastName,
|
|
isAdmin: updatedUser.isAdmin,
|
|
profileImagePath: updatedUser.profileImagePath,
|
|
};
|
|
} catch (e) {
|
|
Logger.error(e, 'Create new user');
|
|
throw new InternalServerErrorException('Failed to register new user');
|
|
}
|
|
}
|
|
|
|
async createProfileImage(authUser: AuthUserDto, fileInfo: Express.Multer.File) {
|
|
try {
|
|
await this.userRepository.update(authUser.id, {
|
|
profileImagePath: fileInfo.path,
|
|
});
|
|
|
|
return {
|
|
userId: authUser.id,
|
|
profileImagePath: fileInfo.path,
|
|
};
|
|
} catch (e) {
|
|
Logger.error(e, 'Create User Profile Image');
|
|
throw new InternalServerErrorException('Failed to create new user profile image');
|
|
}
|
|
}
|
|
|
|
async getUserProfileImage(userId: string, res: Res) {
|
|
try {
|
|
const user = await this.userRepository.findOne({ id: userId });
|
|
|
|
if (!user.profileImagePath) {
|
|
// throw new BadRequestException('User does not have a profile image');
|
|
res.status(404).send('User does not have a profile image');
|
|
return;
|
|
}
|
|
|
|
res.set({
|
|
'Content-Type': 'image/jpeg',
|
|
});
|
|
const fileStream = createReadStream(user.profileImagePath);
|
|
return new StreamableFile(fileStream);
|
|
} catch (e) {
|
|
console.log('error getting user profile');
|
|
}
|
|
}
|
|
}
|