34 lines
1.0 KiB
TypeScript
34 lines
1.0 KiB
TypeScript
// src/service/deviceWhitelist.service.ts
|
|
import { Provide, Scope, ScopeEnum } from '@midwayjs/core';
|
|
import { InjectEntityModel } from '@midwayjs/typeorm';
|
|
import { Repository } from 'typeorm';
|
|
import { DeviceWhitelist } from '../entity/device_whitelist';
|
|
@Provide()
|
|
@Scope(ScopeEnum.Request, { allowDowngrade: true })
|
|
export class DeviceWhitelistService {
|
|
@InjectEntityModel(DeviceWhitelist)
|
|
whitelistRepo: Repository<DeviceWhitelist>;
|
|
|
|
async isWhitelisted(deviceId: string): Promise<boolean> {
|
|
const found = await this.whitelistRepo.findOne({ where: { deviceId } });
|
|
return !!found;
|
|
}
|
|
|
|
async addToWhitelist(deviceId: string) {
|
|
const exist = await this.whitelistRepo.findOne({ where: { deviceId } });
|
|
if (exist) return exist;
|
|
|
|
const record = new DeviceWhitelist();
|
|
record.deviceId = deviceId;
|
|
return this.whitelistRepo.save(record);
|
|
}
|
|
|
|
async removeFromWhitelist(deviceId: string) {
|
|
return this.whitelistRepo.delete({ deviceId });
|
|
}
|
|
|
|
async getAll() {
|
|
return this.whitelistRepo.find();
|
|
}
|
|
}
|