1
0
Fork 0
mirror of https://github.com/immich-app/immich.git synced 2025-01-01 08:31:59 +00:00

refactor(server): make user core singleton (#4607)

This commit is contained in:
Daniel Dietzler 2023-10-23 14:38:48 +02:00 committed by GitHub
parent 50bc92aac0
commit c6b4bc883b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 7 deletions

View file

@ -75,7 +75,7 @@ export class AuthService {
@Inject(IKeyRepository) private keyRepository: IKeyRepository, @Inject(IKeyRepository) private keyRepository: IKeyRepository,
) { ) {
this.configCore = SystemConfigCore.create(configRepository); this.configCore = SystemConfigCore.create(configRepository);
this.userCore = new UserCore(userRepository, libraryRepository, cryptoRepository); this.userCore = UserCore.create(cryptoRepository, libraryRepository, userRepository);
custom.setHttpOptionsDefaults({ timeout: 30000 }); custom.setHttpOptionsDefaults({ timeout: 30000 });
} }

View file

@ -15,13 +15,31 @@ import { ICryptoRepository, ILibraryRepository, IUserRepository, UserListFilter
const SALT_ROUNDS = 10; const SALT_ROUNDS = 10;
let instance: UserCore | null;
export class UserCore { export class UserCore {
constructor( private constructor(
private userRepository: IUserRepository,
private libraryRepository: ILibraryRepository,
private cryptoRepository: ICryptoRepository, private cryptoRepository: ICryptoRepository,
private libraryRepository: ILibraryRepository,
private userRepository: IUserRepository,
) {} ) {}
static create(
cryptoRepository: ICryptoRepository,
libraryRepository: ILibraryRepository,
userRepository: IUserRepository,
) {
if (!instance) {
instance = new UserCore(cryptoRepository, libraryRepository, userRepository);
}
return instance;
}
static reset() {
instance = null;
}
async updateUser(authUser: AuthUserDto, id: string, dto: Partial<UserEntity>): Promise<UserEntity> { async updateUser(authUser: AuthUserDto, id: string, dto: Partial<UserEntity>): Promise<UserEntity> {
if (!authUser.isAdmin && authUser.id !== id) { if (!authUser.isAdmin && authUser.id !== id) {
throw new ForbiddenException('You are not allowed to update this user'); throw new ForbiddenException('You are not allowed to update this user');

View file

@ -45,7 +45,7 @@ export class UserService {
@Inject(IUserRepository) private userRepository: IUserRepository, @Inject(IUserRepository) private userRepository: IUserRepository,
) { ) {
this.storageCore = new StorageCore(storageRepository, assetRepository, moveRepository, personRepository); this.storageCore = new StorageCore(storageRepository, assetRepository, moveRepository, personRepository);
this.userCore = new UserCore(userRepository, libraryRepository, cryptoRepository); this.userCore = UserCore.create(cryptoRepository, libraryRepository, userRepository);
} }
async getAll(authUser: AuthUserDto, isAll: boolean): Promise<UserResponseDto[]> { async getAll(authUser: AuthUserDto, isAll: boolean): Promise<UserResponseDto[]> {

View file

@ -1,6 +1,10 @@
import { IUserRepository } from '@app/domain'; import { IUserRepository, UserCore } from '@app/domain';
export const newUserRepositoryMock = (reset = true): jest.Mocked<IUserRepository> => {
if (reset) {
UserCore.reset();
}
export const newUserRepositoryMock = (): jest.Mocked<IUserRepository> => {
return { return {
get: jest.fn(), get: jest.fn(),
getAdmin: jest.fn(), getAdmin: jest.fn(),