mirror of
https://github.com/KevinMidboe/immich.git
synced 2025-10-29 17:40:28 +00:00
32 lines
1.0 KiB
TypeScript
32 lines
1.0 KiB
TypeScript
import { DeviceInfoEntity } from '@app/database/entities/device-info.entity';
|
|
import { Injectable } from '@nestjs/common';
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
import { Repository } from 'typeorm';
|
|
|
|
type EntityKeys = Pick<DeviceInfoEntity, 'deviceId' | 'userId'>;
|
|
type Entity = EntityKeys & Partial<DeviceInfoEntity>;
|
|
|
|
@Injectable()
|
|
export class DeviceInfoService {
|
|
constructor(
|
|
@InjectRepository(DeviceInfoEntity)
|
|
private repository: Repository<DeviceInfoEntity>,
|
|
) {}
|
|
|
|
public async upsert(entity: Entity): Promise<DeviceInfoEntity> {
|
|
const { deviceId, userId } = entity;
|
|
const exists = await this.repository.findOne({ where: { userId, deviceId } });
|
|
|
|
if (!exists) {
|
|
if (!entity.isAutoBackup) {
|
|
entity.isAutoBackup = false;
|
|
}
|
|
return await this.repository.save(entity);
|
|
}
|
|
|
|
exists.isAutoBackup = entity.isAutoBackup ?? exists.isAutoBackup;
|
|
exists.deviceType = entity.deviceType ?? exists.deviceType;
|
|
return await this.repository.save(exists);
|
|
}
|
|
}
|