2023-01-12 18:10:36 +00:00
|
|
|
import { Injectable } from '@nestjs/common';
|
|
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
2023-01-27 16:26:32 +00:00
|
|
|
import { ILike, Repository } from 'typeorm';
|
2023-01-12 18:10:36 +00:00
|
|
|
import { GroupService } from '../group/group.service';
|
2023-01-26 18:30:39 +00:00
|
|
|
import { UserBuildingAccessControl } from './entities/acl.entity';
|
2023-01-12 18:10:36 +00:00
|
|
|
import { Building } from './entities/building.entity';
|
|
|
|
import { Floor } from './entities/floor.entity';
|
|
|
|
import { Room } from './entities/room.entity';
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class BuildingService {
|
|
|
|
constructor(
|
|
|
|
@InjectRepository(Building)
|
|
|
|
private readonly buildingRepository: Repository<Building>,
|
|
|
|
@InjectRepository(Floor)
|
|
|
|
private readonly floorRepository: Repository<Floor>,
|
|
|
|
@InjectRepository(Room)
|
|
|
|
private readonly roomRepository: Repository<Room>,
|
2023-01-26 18:30:39 +00:00
|
|
|
@InjectRepository(UserBuildingAccessControl)
|
|
|
|
private readonly aclRepository: Repository<UserBuildingAccessControl>,
|
2023-01-12 18:10:36 +00:00
|
|
|
private readonly groupService: GroupService,
|
|
|
|
) {}
|
|
|
|
|
2023-01-24 19:03:43 +00:00
|
|
|
async getStorageBuilding(storageId: number) {
|
|
|
|
return this.buildingRepository.findOne({
|
|
|
|
where: {
|
|
|
|
floors: {
|
|
|
|
rooms: {
|
|
|
|
storages: {
|
|
|
|
id: storageId,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-01-12 18:10:36 +00:00
|
|
|
async getBuildingsByUserSub(sub: string, relations = []) {
|
|
|
|
return this.buildingRepository.find({
|
|
|
|
where: {
|
|
|
|
groups: {
|
|
|
|
members: {
|
|
|
|
sub,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
relations,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async getBuildingByIdAndUserSub(id: number, sub: string, relations = []) {
|
|
|
|
return this.buildingRepository.findOne({
|
|
|
|
where: {
|
|
|
|
id,
|
|
|
|
groups: {
|
|
|
|
members: {
|
|
|
|
sub,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
relations,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-01-27 16:26:32 +00:00
|
|
|
async searchBuilding(searchTerm: string, sub: string, relations = []) {
|
|
|
|
const search = ILike(`%${searchTerm}%`);
|
|
|
|
return this.buildingRepository.find({
|
|
|
|
where: [
|
|
|
|
{
|
|
|
|
displayName: search,
|
|
|
|
groups: {
|
|
|
|
members: {
|
|
|
|
sub,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
address: search,
|
|
|
|
groups: {
|
|
|
|
members: {
|
|
|
|
sub,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
relations,
|
|
|
|
take: 16,
|
|
|
|
select: ['id', 'displayName', 'address', 'color'],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-01-12 18:10:36 +00:00
|
|
|
async getFloorByBuildingAndUserSub(
|
|
|
|
buildingId: number,
|
|
|
|
floorNumber: number,
|
|
|
|
sub: string,
|
|
|
|
relations = [],
|
|
|
|
) {
|
|
|
|
return this.floorRepository.findOne({
|
|
|
|
where: {
|
|
|
|
number: floorNumber,
|
|
|
|
building: {
|
|
|
|
id: buildingId,
|
|
|
|
groups: {
|
|
|
|
members: {
|
|
|
|
sub,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
relations,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-01-12 18:53:24 +00:00
|
|
|
async getRoomByBuildingAndUserSub(
|
|
|
|
buildingId: number,
|
|
|
|
roomId: number,
|
|
|
|
sub: string,
|
|
|
|
relations = [],
|
|
|
|
) {
|
|
|
|
return this.roomRepository.findOne({
|
|
|
|
where: {
|
|
|
|
id: roomId,
|
|
|
|
building: {
|
|
|
|
id: buildingId,
|
|
|
|
groups: {
|
|
|
|
members: {
|
|
|
|
sub,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
relations,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-01-12 20:24:09 +00:00
|
|
|
async getRoomByIdAndUserSub(roomId: number, sub: string, relations = []) {
|
|
|
|
return this.roomRepository.findOne({
|
|
|
|
where: {
|
|
|
|
id: roomId,
|
|
|
|
building: {
|
|
|
|
groups: {
|
|
|
|
members: {
|
|
|
|
sub,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
relations,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async getRoomByBuilding(buildingId: number, roomId: number, relations = []) {
|
|
|
|
return this.roomRepository.findOne({
|
|
|
|
where: {
|
|
|
|
id: roomId,
|
|
|
|
building: {
|
|
|
|
id: buildingId,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
relations,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-01-12 18:53:24 +00:00
|
|
|
async getRoomsOnFloorByBuildingAndUserSub(
|
|
|
|
buildingId: number,
|
|
|
|
floorNumber: number,
|
|
|
|
sub: string,
|
|
|
|
relations = [],
|
|
|
|
) {
|
|
|
|
return this.roomRepository.find({
|
|
|
|
where: {
|
|
|
|
floor: {
|
|
|
|
number: floorNumber,
|
|
|
|
building: {
|
|
|
|
id: buildingId,
|
|
|
|
groups: {
|
|
|
|
members: {
|
|
|
|
sub,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
relations,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async getRoomsByBuildingAndUserSub(
|
|
|
|
buildingId: number,
|
|
|
|
sub: string,
|
|
|
|
relations = [],
|
|
|
|
) {
|
|
|
|
return this.roomRepository.find({
|
|
|
|
where: {
|
|
|
|
floor: {
|
|
|
|
building: {
|
|
|
|
id: buildingId,
|
|
|
|
groups: {
|
|
|
|
members: {
|
|
|
|
sub,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
relations,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-01-12 18:10:36 +00:00
|
|
|
async getFloorsByBuildingAndUserSub(
|
|
|
|
buildingId: number,
|
|
|
|
sub: string,
|
|
|
|
relations = [],
|
|
|
|
) {
|
|
|
|
return this.floorRepository.find({
|
|
|
|
where: {
|
|
|
|
building: {
|
|
|
|
id: buildingId,
|
|
|
|
groups: {
|
|
|
|
members: {
|
|
|
|
sub,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
relations,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async saveBuilding(data: Partial<Building>) {
|
|
|
|
const newBuilding = new Building();
|
|
|
|
Object.assign(newBuilding, data);
|
|
|
|
return this.buildingRepository.save(newBuilding);
|
|
|
|
}
|
|
|
|
|
|
|
|
async saveFloor(data: Partial<Floor>) {
|
|
|
|
const newFloor = new Floor();
|
|
|
|
Object.assign(newFloor, data);
|
|
|
|
return this.floorRepository.save(newFloor);
|
|
|
|
}
|
|
|
|
|
|
|
|
async saveRoom(data: Partial<Room>) {
|
|
|
|
const newRoom = new Room();
|
|
|
|
Object.assign(newRoom, data);
|
|
|
|
return this.roomRepository.save(newRoom);
|
|
|
|
}
|
2023-01-19 15:08:17 +00:00
|
|
|
|
|
|
|
async deleteRoom(room: Room) {
|
|
|
|
return this.roomRepository.remove(room);
|
|
|
|
}
|
2023-01-25 17:01:34 +00:00
|
|
|
|
|
|
|
async deleteFloor(floor: Floor) {
|
|
|
|
return this.floorRepository.remove(floor);
|
|
|
|
}
|
2023-01-12 18:10:36 +00:00
|
|
|
}
|