immich/server/apps/immich/src/api-v1/device-info/device-info.service.ts
Jaime Baez c918f5b001
Set TypeScript to strict mode and fix issues related to server types (#261)
* Fix lint issues and some other TS issues

- set TypeScript in strict mode
- add npm commands to lint / check code
- fix all lint issues
- fix some TS issues
- rename User reponse DTO to make it consistent with the other ones
- override Express/User interface to use UserResponseDto interface
 This is for when the accessing the `user` from a Express Request,
 like in `asset-upload-config`

* Fix the rest of TS issues

- fix all the remaining TypeScript errors
- add missing `@types/mapbox__mapbox-sdk` package

* Move global.d.ts to server `src` folder

* Update AssetReponseDto duration type

This is now of type `string` that defaults to '0:00:00.00000' if not set
which is what the mobile app currently expects

* Set context when logging error in asset.service

Use `ServeFile` as the context for logging an error when
asset.resizePath is not set

* Fix wrong AppController merge conflict resolution

`redirectToWebpage` was removed in main as is no longer used.
2022-06-25 12:53:06 -05:00

63 lines
1.9 KiB
TypeScript

import { BadRequestException, Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { AuthUserDto } from '../../decorators/auth-user.decorator';
import { CreateDeviceInfoDto } from './dto/create-device-info.dto';
import { UpdateDeviceInfoDto } from './dto/update-device-info.dto';
import { DeviceInfoEntity } from '@app/database/entities/device-info.entity';
@Injectable()
export class DeviceInfoService {
constructor(
@InjectRepository(DeviceInfoEntity)
private deviceRepository: Repository<DeviceInfoEntity>,
) {}
async create(createDeviceInfoDto: CreateDeviceInfoDto, authUser: AuthUserDto) {
const res = await this.deviceRepository.findOne({
deviceId: createDeviceInfoDto.deviceId,
userId: authUser.id,
});
if (res) {
Logger.log('Device Info Exist', 'createDeviceInfo');
return res;
}
const deviceInfo = new DeviceInfoEntity();
deviceInfo.deviceId = createDeviceInfoDto.deviceId;
deviceInfo.deviceType = createDeviceInfoDto.deviceType;
deviceInfo.userId = authUser.id;
try {
return await this.deviceRepository.save(deviceInfo);
} catch (e) {
Logger.error('Error creating new device info', 'createDeviceInfo');
}
}
async update(userId: string, updateDeviceInfoDto: UpdateDeviceInfoDto) {
const deviceInfo = await this.deviceRepository.findOne({
where: { deviceId: updateDeviceInfoDto.deviceId, userId: userId },
});
if (!deviceInfo) {
throw new BadRequestException('Device Not Found');
}
const res = await this.deviceRepository.update(
{
id: deviceInfo.id,
},
updateDeviceInfoDto,
);
if (res.affected == 1) {
return await this.deviceRepository.findOne({
where: { deviceId: updateDeviceInfoDto.deviceId, userId: userId },
});
} else {
throw new BadRequestException('Bad Request');
}
}
}