mirror of
https://github.com/KevinMidboe/immich.git
synced 2025-10-29 17:40:28 +00:00
refactor(server): device info (#1490)
* refactor(server): device info * fix: export device service --------- Co-authored-by: Alex Tran <alex.tran1502@gmail.com>
This commit is contained in:
23
server/libs/domain/src/device-info/device-info.core.ts
Normal file
23
server/libs/domain/src/device-info/device-info.core.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { DeviceInfoEntity } from '@app/infra/db/entities';
|
||||
import { IDeviceInfoRepository } from './device-info.repository';
|
||||
|
||||
type UpsertKeys = Pick<DeviceInfoEntity, 'deviceId' | 'userId'>;
|
||||
type UpsertEntity = UpsertKeys & Partial<DeviceInfoEntity>;
|
||||
|
||||
export class DeviceInfoCore {
|
||||
constructor(private repository: IDeviceInfoRepository) {}
|
||||
|
||||
async upsert(entity: UpsertEntity) {
|
||||
const exists = await this.repository.get(entity.userId, entity.deviceId);
|
||||
if (!exists) {
|
||||
if (!entity.isAutoBackup) {
|
||||
entity.isAutoBackup = false;
|
||||
}
|
||||
return this.repository.save(entity);
|
||||
}
|
||||
|
||||
exists.isAutoBackup = entity.isAutoBackup ?? exists.isAutoBackup;
|
||||
exists.deviceType = entity.deviceType ?? exists.deviceType;
|
||||
return this.repository.save(exists);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
import { DeviceInfoEntity } from '@app/infra/db/entities';
|
||||
|
||||
export const IDeviceInfoRepository = 'IDeviceInfoRepository';
|
||||
|
||||
export interface IDeviceInfoRepository {
|
||||
get(userId: string, deviceId: string): Promise<DeviceInfoEntity | null>;
|
||||
save(entity: Partial<DeviceInfoEntity>): Promise<DeviceInfoEntity>;
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
import { DeviceInfoEntity, DeviceType } from '@app/infra';
|
||||
import { authStub, newDeviceInfoRepositoryMock } from '../../test';
|
||||
import { IDeviceInfoRepository } from './device-info.repository';
|
||||
import { DeviceInfoService } from './device-info.service';
|
||||
|
||||
const deviceId = 'device-123';
|
||||
const userId = 'user-123';
|
||||
|
||||
describe('DeviceInfoService', () => {
|
||||
let sut: DeviceInfoService;
|
||||
let repositoryMock: jest.Mocked<IDeviceInfoRepository>;
|
||||
|
||||
beforeEach(async () => {
|
||||
repositoryMock = newDeviceInfoRepositoryMock();
|
||||
|
||||
sut = new DeviceInfoService(repositoryMock);
|
||||
});
|
||||
|
||||
it('should be defined', () => {
|
||||
expect(sut).toBeDefined();
|
||||
});
|
||||
|
||||
describe('upsert', () => {
|
||||
it('should create a new record', async () => {
|
||||
const request = { deviceId, userId, deviceType: DeviceType.IOS } as DeviceInfoEntity;
|
||||
const response = { ...request, id: 1 } as DeviceInfoEntity;
|
||||
|
||||
repositoryMock.get.mockResolvedValue(null);
|
||||
repositoryMock.save.mockResolvedValue(response);
|
||||
|
||||
await expect(sut.upsert(authStub.user1, request)).resolves.toEqual(response);
|
||||
|
||||
expect(repositoryMock.get).toHaveBeenCalledTimes(1);
|
||||
expect(repositoryMock.save).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('should update an existing record', async () => {
|
||||
const request = { deviceId, userId, deviceType: DeviceType.IOS, isAutoBackup: true } as DeviceInfoEntity;
|
||||
const response = { ...request, id: 1 } as DeviceInfoEntity;
|
||||
|
||||
repositoryMock.get.mockResolvedValue(response);
|
||||
repositoryMock.save.mockResolvedValue(response);
|
||||
|
||||
await expect(sut.upsert(authStub.user1, request)).resolves.toEqual(response);
|
||||
|
||||
expect(repositoryMock.get).toHaveBeenCalledTimes(1);
|
||||
expect(repositoryMock.save).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('should keep properties that were not updated', async () => {
|
||||
const request = { deviceId, userId } as DeviceInfoEntity;
|
||||
const response = { id: 1, isAutoBackup: true, deviceId, userId, deviceType: DeviceType.WEB } as DeviceInfoEntity;
|
||||
|
||||
repositoryMock.get.mockResolvedValue(response);
|
||||
repositoryMock.save.mockResolvedValue(response);
|
||||
|
||||
await expect(sut.upsert(authStub.user1, request)).resolves.toEqual(response);
|
||||
|
||||
expect(repositoryMock.get).toHaveBeenCalledTimes(1);
|
||||
expect(repositoryMock.save).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
});
|
||||
20
server/libs/domain/src/device-info/device-info.service.ts
Normal file
20
server/libs/domain/src/device-info/device-info.service.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import { Inject, Injectable } from '@nestjs/common';
|
||||
import { AuthUserDto } from '../auth';
|
||||
import { DeviceInfoCore } from './device-info.core';
|
||||
import { IDeviceInfoRepository } from './device-info.repository';
|
||||
import { UpsertDeviceInfoDto } from './dto';
|
||||
import { DeviceInfoResponseDto, mapDeviceInfoResponse } from './response-dto';
|
||||
|
||||
@Injectable()
|
||||
export class DeviceInfoService {
|
||||
private core: DeviceInfoCore;
|
||||
|
||||
constructor(@Inject(IDeviceInfoRepository) repository: IDeviceInfoRepository) {
|
||||
this.core = new DeviceInfoCore(repository);
|
||||
}
|
||||
|
||||
public async upsert(authUser: AuthUserDto, dto: UpsertDeviceInfoDto): Promise<DeviceInfoResponseDto> {
|
||||
const deviceInfo = await this.core.upsert({ ...dto, userId: authUser.id });
|
||||
return mapDeviceInfoResponse(deviceInfo);
|
||||
}
|
||||
}
|
||||
1
server/libs/domain/src/device-info/dto/index.ts
Normal file
1
server/libs/domain/src/device-info/dto/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from './upsert-device-info.dto';
|
||||
@@ -0,0 +1,15 @@
|
||||
import { IsNotEmpty, IsOptional } from 'class-validator';
|
||||
import { DeviceType } from '@app/infra/db/entities';
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
|
||||
export class UpsertDeviceInfoDto {
|
||||
@IsNotEmpty()
|
||||
deviceId!: string;
|
||||
|
||||
@IsNotEmpty()
|
||||
@ApiProperty({ enumName: 'DeviceTypeEnum', enum: DeviceType })
|
||||
deviceType!: DeviceType;
|
||||
|
||||
@IsOptional()
|
||||
isAutoBackup?: boolean;
|
||||
}
|
||||
4
server/libs/domain/src/device-info/index.ts
Normal file
4
server/libs/domain/src/device-info/index.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export * from './device-info.repository';
|
||||
export * from './device-info.service';
|
||||
export * from './dto';
|
||||
export * from './response-dto';
|
||||
@@ -0,0 +1,26 @@
|
||||
import { DeviceInfoEntity, DeviceType } from '@app/infra/db/entities';
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
|
||||
export class DeviceInfoResponseDto {
|
||||
@ApiProperty({ type: 'integer' })
|
||||
id!: number;
|
||||
userId!: string;
|
||||
deviceId!: string;
|
||||
|
||||
@ApiProperty({ enumName: 'DeviceTypeEnum', enum: DeviceType })
|
||||
deviceType!: DeviceType;
|
||||
|
||||
createdAt!: string;
|
||||
isAutoBackup!: boolean;
|
||||
}
|
||||
|
||||
export function mapDeviceInfoResponse(entity: DeviceInfoEntity): DeviceInfoResponseDto {
|
||||
return {
|
||||
id: entity.id,
|
||||
userId: entity.userId,
|
||||
deviceId: entity.deviceId,
|
||||
deviceType: entity.deviceType,
|
||||
createdAt: entity.createdAt,
|
||||
isAutoBackup: entity.isAutoBackup,
|
||||
};
|
||||
}
|
||||
1
server/libs/domain/src/device-info/response-dto/index.ts
Normal file
1
server/libs/domain/src/device-info/response-dto/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from './device-info-response.dto';
|
||||
@@ -1,6 +1,7 @@
|
||||
import { DynamicModule, Global, Module, ModuleMetadata, Provider } from '@nestjs/common';
|
||||
import { APIKeyService } from './api-key';
|
||||
import { AuthService } from './auth';
|
||||
import { DeviceInfoService } from './device-info';
|
||||
import { JobService } from './job';
|
||||
import { OAuthService } from './oauth';
|
||||
import { ShareService } from './share';
|
||||
@@ -10,6 +11,7 @@ import { UserService } from './user';
|
||||
const providers: Provider[] = [
|
||||
APIKeyService,
|
||||
AuthService,
|
||||
DeviceInfoService,
|
||||
JobService,
|
||||
OAuthService,
|
||||
SystemConfigService,
|
||||
|
||||
@@ -3,6 +3,7 @@ export * from './api-key';
|
||||
export * from './asset';
|
||||
export * from './auth';
|
||||
export * from './crypto';
|
||||
export * from './device-info';
|
||||
export * from './domain.module';
|
||||
export * from './job';
|
||||
export * from './oauth';
|
||||
|
||||
8
server/libs/domain/test/device-info.repository.mock.ts
Normal file
8
server/libs/domain/test/device-info.repository.mock.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { IDeviceInfoRepository } from '../src';
|
||||
|
||||
export const newDeviceInfoRepositoryMock = (): jest.Mocked<IDeviceInfoRepository> => {
|
||||
return {
|
||||
get: jest.fn(),
|
||||
save: jest.fn(),
|
||||
};
|
||||
};
|
||||
@@ -1,5 +1,6 @@
|
||||
export * from './api-key.repository.mock';
|
||||
export * from './crypto.repository.mock';
|
||||
export * from './device-info.repository.mock';
|
||||
export * from './fixtures';
|
||||
export * from './job.repository.mock';
|
||||
export * from './shared-link.repository.mock';
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
import { IDeviceInfoRepository } from '@app/domain';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { DeviceInfoEntity } from '../entities';
|
||||
|
||||
export class DeviceInfoRepository implements IDeviceInfoRepository {
|
||||
constructor(@InjectRepository(DeviceInfoEntity) private repository: Repository<DeviceInfoEntity>) {}
|
||||
|
||||
get(userId: string, deviceId: string): Promise<DeviceInfoEntity | null> {
|
||||
return this.repository.findOne({ where: { userId, deviceId } });
|
||||
}
|
||||
|
||||
save(entity: Partial<DeviceInfoEntity>): Promise<DeviceInfoEntity> {
|
||||
return this.repository.save(entity);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,6 @@
|
||||
export * from './api-key.repository';
|
||||
export * from './device-info.repository';
|
||||
export * from './shared-link.repository';
|
||||
export * from './user.repository';
|
||||
export * from './system-config.repository';
|
||||
export * from './user-token.repository';
|
||||
export * from './user.repository';
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import {
|
||||
ICryptoRepository,
|
||||
IDeviceInfoRepository,
|
||||
IJobRepository,
|
||||
IKeyRepository,
|
||||
ISharedLinkRepository,
|
||||
@@ -7,20 +8,31 @@ import {
|
||||
IUserRepository,
|
||||
QueueName,
|
||||
} from '@app/domain';
|
||||
import { databaseConfig, UserEntity, UserTokenEntity } from './db';
|
||||
import { IUserTokenRepository } from '@app/domain/user-token';
|
||||
import { UserTokenRepository } from '@app/infra/db/repository/user-token.repository';
|
||||
import { BullModule } from '@nestjs/bull';
|
||||
import { Global, Module, Provider } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { APIKeyEntity, SharedLinkEntity, SystemConfigEntity, UserRepository } from './db';
|
||||
import { APIKeyRepository, SharedLinkRepository } from './db/repository';
|
||||
import { CryptoRepository } from './auth/crypto.repository';
|
||||
import { SystemConfigRepository } from './db/repository/system-config.repository';
|
||||
import {
|
||||
APIKeyEntity,
|
||||
APIKeyRepository,
|
||||
databaseConfig,
|
||||
DeviceInfoEntity,
|
||||
DeviceInfoRepository,
|
||||
SharedLinkEntity,
|
||||
SharedLinkRepository,
|
||||
SystemConfigEntity,
|
||||
SystemConfigRepository,
|
||||
UserEntity,
|
||||
UserRepository,
|
||||
UserTokenEntity,
|
||||
} from './db';
|
||||
import { JobRepository } from './job';
|
||||
import { IUserTokenRepository } from '@app/domain/user-token';
|
||||
import { UserTokenRepository } from '@app/infra/db/repository/user-token.repository';
|
||||
|
||||
const providers: Provider[] = [
|
||||
{ provide: ICryptoRepository, useClass: CryptoRepository },
|
||||
{ provide: IDeviceInfoRepository, useClass: DeviceInfoRepository },
|
||||
{ provide: IKeyRepository, useClass: APIKeyRepository },
|
||||
{ provide: IJobRepository, useClass: JobRepository },
|
||||
{ provide: ISharedLinkRepository, useClass: SharedLinkRepository },
|
||||
@@ -33,7 +45,14 @@ const providers: Provider[] = [
|
||||
@Module({
|
||||
imports: [
|
||||
TypeOrmModule.forRoot(databaseConfig),
|
||||
TypeOrmModule.forFeature([APIKeyEntity, UserEntity, SharedLinkEntity, SystemConfigEntity, UserTokenEntity]),
|
||||
TypeOrmModule.forFeature([
|
||||
APIKeyEntity,
|
||||
DeviceInfoEntity,
|
||||
UserEntity,
|
||||
SharedLinkEntity,
|
||||
SystemConfigEntity,
|
||||
UserTokenEntity,
|
||||
]),
|
||||
BullModule.forRootAsync({
|
||||
useFactory: async () => ({
|
||||
prefix: 'immich_bull',
|
||||
|
||||
Reference in New Issue
Block a user