diff --git a/server/src/domain/auth/auth.service.ts b/server/src/domain/auth/auth.service.ts index 01cb5adac4..a7e467c5a3 100644 --- a/server/src/domain/auth/auth.service.ts +++ b/server/src/domain/auth/auth.service.ts @@ -75,7 +75,7 @@ export class AuthService { @Inject(IKeyRepository) private keyRepository: IKeyRepository, ) { this.configCore = SystemConfigCore.create(configRepository); - this.userCore = new UserCore(userRepository, libraryRepository, cryptoRepository); + this.userCore = UserCore.create(cryptoRepository, libraryRepository, userRepository); custom.setHttpOptionsDefaults({ timeout: 30000 }); } diff --git a/server/src/domain/user/user.core.ts b/server/src/domain/user/user.core.ts index 2597a2441e..d6cb35b411 100644 --- a/server/src/domain/user/user.core.ts +++ b/server/src/domain/user/user.core.ts @@ -15,13 +15,31 @@ import { ICryptoRepository, ILibraryRepository, IUserRepository, UserListFilter const SALT_ROUNDS = 10; +let instance: UserCore | null; + export class UserCore { - constructor( - private userRepository: IUserRepository, - private libraryRepository: ILibraryRepository, + private constructor( 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): Promise { if (!authUser.isAdmin && authUser.id !== id) { throw new ForbiddenException('You are not allowed to update this user'); diff --git a/server/src/domain/user/user.service.ts b/server/src/domain/user/user.service.ts index 8664bca50d..796a72aa76 100644 --- a/server/src/domain/user/user.service.ts +++ b/server/src/domain/user/user.service.ts @@ -45,7 +45,7 @@ export class UserService { @Inject(IUserRepository) private userRepository: IUserRepository, ) { 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 { diff --git a/server/test/repositories/user.repository.mock.ts b/server/test/repositories/user.repository.mock.ts index 30017e758e..d164bbe10f 100644 --- a/server/test/repositories/user.repository.mock.ts +++ b/server/test/repositories/user.repository.mock.ts @@ -1,6 +1,10 @@ -import { IUserRepository } from '@app/domain'; +import { IUserRepository, UserCore } from '@app/domain'; + +export const newUserRepositoryMock = (reset = true): jest.Mocked => { + if (reset) { + UserCore.reset(); + } -export const newUserRepositoryMock = (): jest.Mocked => { return { get: jest.fn(), getAdmin: jest.fn(),