You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

34 lines
1.2 KiB

import { EntityRepository, Repository } from "typeorm";
import { SprinklersDevice, User } from "../entities";
@EntityRepository(SprinklersDevice)
export class SprinklersDeviceRepository extends Repository<SprinklersDevice> {
findByName(name: string) {
return this.findOne({ name });
}
async userHasAccess(userId: number, deviceId: number): Promise<boolean> {
const count = await this.manager
.createQueryBuilder(User, "user")
.innerJoinAndSelect("user.devices", "sprinklers_device",
"user.id = :userId AND sprinklers_device.id = :deviceId",
{ userId, deviceId })
.getCount();
return count > 0;
}
async findUserDevice(userId: number, deviceId: number): Promise<SprinklersDevice | null> {
const user = await this.manager
.createQueryBuilder(User, "user")
.innerJoinAndSelect("user.devices", "sprinklers_device",
"user.id = :userId AND sprinklers_device.id = :deviceId",
{ userId, deviceId })
.getOne();
if (!user) {
return null;
}
return user.devices![0];
}
}