mirror of
https://github.com/SyncrowIOT/backend.git
synced 2026-10-25 18:37:54 +00:00
Add building service and check building middleware
This commit is contained in:
@@ -0,0 +1,182 @@
|
||||
import { GetBuildingChildDto } from '../dtos/get.building.dto';
|
||||
import { SpaceTypeRepository } from '../../../libs/common/src/modules/space-type/repositories/space.type.repository';
|
||||
import { Injectable, HttpException, HttpStatus } from '@nestjs/common';
|
||||
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||
import { AddBuildingDto } from '../dtos';
|
||||
import {
|
||||
BuildingChildInterface,
|
||||
BuildingParentInterface,
|
||||
GetBuildingByUuidInterface,
|
||||
} from '../interface/building.interface';
|
||||
import { SpaceEntity } from '@app/common/modules/space/entities';
|
||||
|
||||
@Injectable()
|
||||
export class BuildingService {
|
||||
constructor(
|
||||
private readonly spaceRepository: SpaceRepository,
|
||||
private readonly spaceTypeRepository: SpaceTypeRepository,
|
||||
) {}
|
||||
|
||||
async addBuilding(addBuildingDto: AddBuildingDto) {
|
||||
try {
|
||||
const spaceType = await this.spaceTypeRepository.findOne({
|
||||
where: {
|
||||
type: 'building',
|
||||
},
|
||||
});
|
||||
|
||||
await this.spaceRepository.save({
|
||||
spaceName: addBuildingDto.buildingName,
|
||||
parent: { uuid: addBuildingDto.communityUuid },
|
||||
spaceType: { uuid: spaceType.uuid },
|
||||
});
|
||||
} catch (err) {
|
||||
throw new HttpException(err.message, HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
async getBuildingByUuid(
|
||||
buildingUuid: string,
|
||||
): Promise<GetBuildingByUuidInterface> {
|
||||
try {
|
||||
const building = await this.spaceRepository.findOne({
|
||||
where: {
|
||||
uuid: buildingUuid,
|
||||
spaceType: {
|
||||
type: 'building',
|
||||
},
|
||||
},
|
||||
relations: ['spaceType'],
|
||||
});
|
||||
if (!building) {
|
||||
throw new HttpException('Building not found', HttpStatus.NOT_FOUND);
|
||||
}
|
||||
return {
|
||||
uuid: building.uuid,
|
||||
createdAt: building.createdAt,
|
||||
updatedAt: building.updatedAt,
|
||||
name: building.spaceName,
|
||||
type: building.spaceType.type,
|
||||
};
|
||||
} catch (err) {
|
||||
throw new HttpException(
|
||||
err.message,
|
||||
err.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
async getBuildingChildByUuid(
|
||||
buildingUuid: string,
|
||||
getBuildingChildDto: GetBuildingChildDto,
|
||||
): Promise<BuildingChildInterface> {
|
||||
const { includeSubSpaces, page, pageSize } = getBuildingChildDto;
|
||||
|
||||
const space = await this.spaceRepository.findOneOrFail({
|
||||
where: { uuid: buildingUuid },
|
||||
relations: ['children', 'spaceType'],
|
||||
});
|
||||
|
||||
if (space.spaceType.type !== 'building') {
|
||||
throw new HttpException('Building not found', HttpStatus.NOT_FOUND);
|
||||
}
|
||||
|
||||
const totalCount = await this.spaceRepository.count({
|
||||
where: { parent: { uuid: space.uuid } },
|
||||
});
|
||||
|
||||
const children = await this.buildHierarchy(
|
||||
space,
|
||||
includeSubSpaces,
|
||||
page,
|
||||
pageSize,
|
||||
);
|
||||
|
||||
return {
|
||||
uuid: space.uuid,
|
||||
name: space.spaceName,
|
||||
type: space.spaceType.type,
|
||||
totalCount,
|
||||
children,
|
||||
};
|
||||
}
|
||||
|
||||
private async buildHierarchy(
|
||||
space: SpaceEntity,
|
||||
includeSubSpaces: boolean,
|
||||
page: number,
|
||||
pageSize: number,
|
||||
): Promise<BuildingChildInterface[]> {
|
||||
const children = await this.spaceRepository.find({
|
||||
where: { parent: { uuid: space.uuid } },
|
||||
relations: ['spaceType'],
|
||||
skip: (page - 1) * pageSize,
|
||||
take: pageSize,
|
||||
});
|
||||
|
||||
if (!children || children.length === 0 || !includeSubSpaces) {
|
||||
return children
|
||||
.filter(
|
||||
(child) =>
|
||||
child.spaceType.type !== 'building' &&
|
||||
child.spaceType.type !== 'community',
|
||||
) // Filter remaining building and community types
|
||||
.map((child) => ({
|
||||
uuid: child.uuid,
|
||||
name: child.spaceName,
|
||||
type: child.spaceType.type,
|
||||
}));
|
||||
}
|
||||
|
||||
const childHierarchies = await Promise.all(
|
||||
children
|
||||
.filter(
|
||||
(child) =>
|
||||
child.spaceType.type !== 'building' &&
|
||||
child.spaceType.type !== 'community',
|
||||
) // Filter remaining building and community types
|
||||
.map(async (child) => ({
|
||||
uuid: child.uuid,
|
||||
name: child.spaceName,
|
||||
type: child.spaceType.type,
|
||||
children: await this.buildHierarchy(child, true, 1, pageSize),
|
||||
})),
|
||||
);
|
||||
|
||||
return childHierarchies;
|
||||
}
|
||||
|
||||
async getBuildingParentByUuid(
|
||||
buildingUuid: string,
|
||||
): Promise<BuildingParentInterface> {
|
||||
try {
|
||||
const building = await this.spaceRepository.findOne({
|
||||
where: {
|
||||
uuid: buildingUuid,
|
||||
spaceType: {
|
||||
type: 'building',
|
||||
},
|
||||
},
|
||||
relations: ['spaceType', 'parent', 'parent.spaceType'],
|
||||
});
|
||||
if (!building) {
|
||||
throw new HttpException('Building not found', HttpStatus.NOT_FOUND);
|
||||
}
|
||||
|
||||
return {
|
||||
uuid: building.uuid,
|
||||
name: building.spaceName,
|
||||
type: building.spaceType.type,
|
||||
parent: {
|
||||
uuid: building.parent.uuid,
|
||||
name: building.parent.spaceName,
|
||||
type: building.parent.spaceType.type,
|
||||
},
|
||||
};
|
||||
} catch (err) {
|
||||
throw new HttpException(
|
||||
err.message,
|
||||
err.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
export * from './building.service';
|
||||
Reference in New Issue
Block a user