Skip to content

Commit

Permalink
refactor(server): make user core singleton (immich-app#4607)
Browse files Browse the repository at this point in the history
  • Loading branch information
danieldietzler authored Oct 23, 2023
1 parent 50bc92a commit c6b4bc8
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 7 deletions.
2 changes: 1 addition & 1 deletion server/src/domain/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
Expand Down
24 changes: 21 additions & 3 deletions server/src/domain/user/user.core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<UserEntity>): Promise<UserEntity> {
if (!authUser.isAdmin && authUser.id !== id) {
throw new ForbiddenException('You are not allowed to update this user');
Expand Down
2 changes: 1 addition & 1 deletion server/src/domain/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<UserResponseDto[]> {
Expand Down
8 changes: 6 additions & 2 deletions server/test/repositories/user.repository.mock.ts
Original file line number Diff line number Diff line change
@@ -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 {
get: jest.fn(),
getAdmin: jest.fn(),
Expand Down

0 comments on commit c6b4bc8

Please sign in to comment.