| 14 | @injectable() |
| 15 | @routerName('device') |
| 16 | export class DevicesService extends DBService<Device> |
| 17 | implements IDeviceRouter, IService { |
| 18 | public readonly DBObject: any = Device; |
| 19 | |
| 20 | protected readonly log: Logger = createEverLogger({ |
| 21 | name: 'devicesService', |
| 22 | }); |
| 23 | |
| 24 | @observableListener() |
| 25 | get(id: string): Observable<Device | null> { |
| 26 | return super.get(id).pipe( |
| 27 | map(async (device) => { |
| 28 | await this.throwIfNotExists(id); |
| 29 | return device; |
| 30 | }), |
| 31 | switchMap((device) => device) |
| 32 | ); |
| 33 | } |
| 34 | |
| 35 | async getMultipleDevices( |
| 36 | ids: Array<Device['id']> |
| 37 | ): Promise<Observable<Device[]>> { |
| 38 | const devices = await this.find({ |
| 39 | _id: { $in: ids }, |
| 40 | isDeleted: { $eq: false }, |
| 41 | }); |
| 42 | |
| 43 | const devicesIds = devices.map((device) => device.id); |
| 44 | |
| 45 | return super.getMultiple(devicesIds); |
| 46 | } |
| 47 | |
| 48 | @asyncListener() |
| 49 | async create(device: IDeviceCreateObject): Promise<Device> { |
| 50 | return super.create(device); |
| 51 | } |
| 52 | |
| 53 | @asyncListener() |
| 54 | async updateLanguage( |
| 55 | deviceId: string, |
| 56 | language: ILanguage |
| 57 | ): Promise<Device> { |
| 58 | await this.throwIfNotExists(deviceId); |
| 59 | |
| 60 | return this.update(deviceId, { |
| 61 | language, |
| 62 | }); |
| 63 | } |
| 64 | |
| 65 | async throwIfNotExists(deviceId: string) { |
| 66 | const device = await super.get(deviceId).pipe(first()).toPromise(); |
| 67 | |
| 68 | if (!device || device.isDeleted) { |
| 69 | throw Error(`Device with id '${deviceId}' does not exists!`); |
| 70 | } |
| 71 | } |
| 72 | } |
nothing calls this directly
no test coverage detected