114 lines
3.0 KiB
TypeScript
114 lines
3.0 KiB
TypeScript
import { Injectable, NotFoundException } from '@nestjs/common';
|
|
import omit from 'lodash.omit';
|
|
import pick from 'lodash.pick';
|
|
import { BuildingService } from 'src/objects/building/building.service';
|
|
import { GroupService } from 'src/objects/group/group.service';
|
|
import { User } from 'src/objects/user/user.entity';
|
|
import { UserService } from 'src/objects/user/user.service';
|
|
import { BuildingFloorUpdateRequestDto } from './dto/building-floor-request.dto';
|
|
import { CreateBuildingRequestDto } from './dto/buildings-create-request.dto';
|
|
|
|
@Injectable()
|
|
export class AppBuildingService {
|
|
constructor(
|
|
private readonly buildingService: BuildingService,
|
|
private readonly userService: UserService,
|
|
private readonly groupService: GroupService,
|
|
) {}
|
|
|
|
async getUserBuildings(user: User) {
|
|
return this.buildingService.getBuildingsByUserSub(user.sub);
|
|
}
|
|
|
|
async createBuilding(user: User, body: CreateBuildingRequestDto) {
|
|
const getGroup = await this.groupService.getByIdAndMemberSub(
|
|
body.group,
|
|
user.sub,
|
|
);
|
|
if (!getGroup) {
|
|
throw new NotFoundException('Group not found');
|
|
}
|
|
|
|
// TODO: validate color
|
|
|
|
const newBuilding = await this.buildingService.saveBuilding({
|
|
...pick(body, ['displayName', 'color', 'address']),
|
|
groups: [getGroup],
|
|
});
|
|
|
|
const newFloor = await this.buildingService.saveFloor({
|
|
displayName: 'Ground Floor',
|
|
number: 0,
|
|
plan: '{}',
|
|
building: newBuilding,
|
|
});
|
|
|
|
delete newFloor.building;
|
|
newBuilding.floors = [newFloor];
|
|
|
|
return omit(newBuilding, ['groups']);
|
|
}
|
|
|
|
async getBuildingById(user: User, id: number) {
|
|
const building = await this.buildingService.getBuildingByIdAndUserSub(
|
|
id,
|
|
user.sub,
|
|
['floors', 'floors.rooms'],
|
|
);
|
|
|
|
if (!building) {
|
|
throw new NotFoundException('Building not found');
|
|
}
|
|
|
|
return omit(building, ['groups']);
|
|
}
|
|
|
|
async getBuildingFloors(user: User, buildingId: number) {
|
|
const floors = await this.buildingService.getFloorsByBuildingAndUserSub(
|
|
buildingId,
|
|
user.sub,
|
|
['rooms'],
|
|
);
|
|
|
|
return floors.map((floor) => omit(floor, ['building']));
|
|
}
|
|
|
|
async getBuildingFloor(user: User, buildingId: number, floorNumber: number) {
|
|
const floor = await this.buildingService.getFloorByBuildingAndUserSub(
|
|
buildingId,
|
|
floorNumber,
|
|
user.sub,
|
|
['rooms'],
|
|
);
|
|
|
|
if (!floor) {
|
|
throw new NotFoundException('Floor not found');
|
|
}
|
|
|
|
return omit(floor, ['building']);
|
|
}
|
|
|
|
async updateBuildingFloor(
|
|
user: User,
|
|
body: BuildingFloorUpdateRequestDto,
|
|
buildingId: number,
|
|
floorNumber: number,
|
|
) {
|
|
const floor = await this.buildingService.getFloorByBuildingAndUserSub(
|
|
buildingId,
|
|
floorNumber,
|
|
user.sub,
|
|
['rooms'],
|
|
);
|
|
|
|
if (!floor) {
|
|
throw new NotFoundException('Floor not found');
|
|
}
|
|
|
|
Object.assign(floor, body);
|
|
|
|
await this.buildingService.saveFloor(floor);
|
|
return omit(floor, ['building']);
|
|
}
|
|
}
|