mirror of
				https://github.com/KevinMidboe/immich.git
				synced 2025-10-29 17:40:28 +00:00 
			
		
		
		
	refactor(server): make user core singleton (#4607)
This commit is contained in:
		| @@ -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 }); | ||||||
|   } |   } | ||||||
|   | |||||||
| @@ -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'); | ||||||
|   | |||||||
| @@ -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[]> { | ||||||
|   | |||||||
| @@ -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(), | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user