Add floor module with controller, service, DTOs, and interfaces

This commit is contained in:
faris Aljohari
2024-04-13 17:20:20 +03:00
parent 9b837bd15a
commit d5f396e4f5
11 changed files with 483 additions and 0 deletions

View File

@ -10,6 +10,7 @@ import { GroupModule } from './group/group.module';
import { DeviceModule } from './device/device.module';
import { CommunityModule } from './community/community.module';
import { BuildingModule } from './building/building.module';
import { FloorModule } from './floor/floor.module';
@Module({
imports: [
ConfigModule.forRoot({
@ -19,6 +20,7 @@ import { BuildingModule } from './building/building.module';
UserModule,
CommunityModule,
BuildingModule,
FloorModule,
HomeModule,
RoomModule,
GroupModule,

View File

@ -0,0 +1,102 @@
import { FloorService } from '../services/floor.service';
import {
Body,
Controller,
Get,
HttpException,
HttpStatus,
Param,
Post,
Query,
UseGuards,
} from '@nestjs/common';
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
import { JwtAuthGuard } from '../../../libs/common/src/guards/jwt.auth.guard';
import { AddFloorDto } from '../dtos/add.floor.dto';
import { GetFloorChildDto } from '../dtos/get.floor.dto';
@ApiTags('Floor Module')
@Controller({
version: '1',
path: 'floor',
})
export class FloorController {
constructor(private readonly floorService: FloorService) {}
// @ApiBearerAuth()
// @UseGuards(JwtAuthGuard)
@Post()
async addFloor(@Body() addFloorDto: AddFloorDto) {
try {
await this.floorService.addFloor(addFloorDto);
return { message: 'Floor added successfully' };
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Get(':floorUuid')
async getFloorByUuid(@Param('floorUuid') floorUuid: string) {
try {
const floor = await this.floorService.getFloorByUuid(floorUuid);
return floor;
} catch (error) {
if (error.status === 404) {
throw new HttpException('Floor not found', HttpStatus.NOT_FOUND);
} else {
throw new HttpException(
error.message || 'Internal server error',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Get('child/:floorUuid')
async getFloorChildByUuid(
@Param('floorUuid') floorUuid: string,
@Query() query: GetFloorChildDto,
) {
try {
const floor = await this.floorService.getFloorChildByUuid(
floorUuid,
query,
);
return floor;
} catch (error) {
if (error.status === 404) {
throw new HttpException('Floor not found', HttpStatus.NOT_FOUND);
} else {
throw new HttpException(
error.message || 'Internal server error',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Get('parent/:floorUuid')
async getFloorParentByUuid(@Param('floorUuid') floorUuid: string) {
try {
const floor = await this.floorService.getFloorParentByUuid(floorUuid);
return floor;
} catch (error) {
if (error.status === 404) {
throw new HttpException('Floor not found', HttpStatus.NOT_FOUND);
} else {
throw new HttpException(
error.message || 'Internal server error',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
}
}

View File

@ -0,0 +1 @@
export * from './floor.controller';

View File

@ -0,0 +1,23 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty, IsString } from 'class-validator';
export class AddFloorDto {
@ApiProperty({
description: 'floorName',
required: true,
})
@IsString()
@IsNotEmpty()
public floorName: string;
@ApiProperty({
description: 'buildingUuid',
required: true,
})
@IsString()
@IsNotEmpty()
public buildingUuid: string;
constructor(dto: Partial<AddFloorDto>) {
Object.assign(this, dto);
}
}

View File

@ -0,0 +1,51 @@
import { ApiProperty } from '@nestjs/swagger';
import { Transform } from 'class-transformer';
import {
IsBoolean,
IsInt,
IsNotEmpty,
IsOptional,
IsString,
Min,
} from 'class-validator';
export class GetFloorDto {
@ApiProperty({
description: 'floorUuid',
required: true,
})
@IsString()
@IsNotEmpty()
public floorUuid: string;
}
export class GetFloorChildDto {
@ApiProperty({ example: 1, description: 'Page number', required: true })
@IsInt({ message: 'Page must be a number' })
@Min(1, { message: 'Page must not be less than 1' })
@IsNotEmpty()
public page: number;
@ApiProperty({
example: 10,
description: 'Number of items per page',
required: true,
})
@IsInt({ message: 'Page size must be a number' })
@Min(1, { message: 'Page size must not be less than 1' })
@IsNotEmpty()
public pageSize: number;
@ApiProperty({
example: true,
description: 'Flag to determine whether to fetch full hierarchy',
required: false,
default: false,
})
@IsOptional()
@IsBoolean()
@Transform((value) => {
return value.obj.includeSubSpaces === 'true';
})
public includeSubSpaces: boolean = false;
}

1
src/floor/dtos/index.ts Normal file
View File

@ -0,0 +1 @@
export * from './add.floor.dto';

29
src/floor/floor.module.ts Normal file
View File

@ -0,0 +1,29 @@
import {
MiddlewareConsumer,
Module,
NestModule,
RequestMethod,
} from '@nestjs/common';
import { FloorService } from './services/floor.service';
import { FloorController } from './controllers/floor.controller';
import { ConfigModule } from '@nestjs/config';
import { SpaceRepositoryModule } from '@app/common/modules/space/space.repository.module';
import { SpaceRepository } from '@app/common/modules/space/repositories';
import { SpaceTypeRepositoryModule } from '@app/common/modules/space-type/space.type.repository.module';
import { SpaceTypeRepository } from '@app/common/modules/space-type/repositories';
import { CheckBuildingTypeMiddleware } from 'src/middleware/CheckBuildingTypeMiddleware';
@Module({
imports: [ConfigModule, SpaceRepositoryModule, SpaceTypeRepositoryModule],
controllers: [FloorController],
providers: [FloorService, SpaceRepository, SpaceTypeRepository],
exports: [FloorService],
})
export class FloorModule implements NestModule {
configure(consumer: MiddlewareConsumer) {
consumer.apply(CheckBuildingTypeMiddleware).forRoutes({
path: '/floor',
method: RequestMethod.POST,
});
}
}

View File

@ -0,0 +1,21 @@
export interface GetFloorByUuidInterface {
uuid: string;
createdAt: Date;
updatedAt: Date;
name: string;
type: string;
}
export interface FloorChildInterface {
uuid: string;
name: string;
type: string;
totalCount?: number;
children?: FloorChildInterface[];
}
export interface FloorParentInterface {
uuid: string;
name: string;
type: string;
parent?: FloorParentInterface;
}

View File

@ -0,0 +1,178 @@
import { GetFloorChildDto } from '../dtos/get.floor.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 { AddFloorDto } from '../dtos';
import {
FloorChildInterface,
FloorParentInterface,
GetFloorByUuidInterface,
} from '../interface/floor.interface';
import { SpaceEntity } from '@app/common/modules/space/entities';
@Injectable()
export class FloorService {
constructor(
private readonly spaceRepository: SpaceRepository,
private readonly spaceTypeRepository: SpaceTypeRepository,
) {}
async addFloor(addFloorDto: AddFloorDto) {
try {
const spaceType = await this.spaceTypeRepository.findOne({
where: {
type: 'floor',
},
});
await this.spaceRepository.save({
spaceName: addFloorDto.floorName,
parent: { uuid: addFloorDto.buildingUuid },
spaceType: { uuid: spaceType.uuid },
});
} catch (err) {
throw new HttpException(err.message, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
async getFloorByUuid(floorUuid: string): Promise<GetFloorByUuidInterface> {
try {
const floor = await this.spaceRepository.findOne({
where: {
uuid: floorUuid,
spaceType: {
type: 'floor',
},
},
relations: ['spaceType'],
});
if (!floor) {
throw new HttpException('Floor not found', HttpStatus.NOT_FOUND);
}
return {
uuid: floor.uuid,
createdAt: floor.createdAt,
updatedAt: floor.updatedAt,
name: floor.spaceName,
type: floor.spaceType.type,
};
} catch (err) {
throw new HttpException(
err.message,
err.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
async getFloorChildByUuid(
floorUuid: string,
getFloorChildDto: GetFloorChildDto,
): Promise<FloorChildInterface> {
const { includeSubSpaces, page, pageSize } = getFloorChildDto;
const space = await this.spaceRepository.findOneOrFail({
where: { uuid: floorUuid },
relations: ['children', 'spaceType'],
});
if (space.spaceType.type !== 'floor') {
throw new HttpException('Floor 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<FloorChildInterface[]> {
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 !== 'floor' &&
child.spaceType.type !== 'building',
) // Filter remaining floor and building types
.map((child) => ({
uuid: child.uuid,
name: child.spaceName,
type: child.spaceType.type,
}));
}
const childHierarchies = await Promise.all(
children
.filter(
(child) =>
child.spaceType.type !== 'floor' &&
child.spaceType.type !== 'building',
) // Filter remaining floor and building 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 getFloorParentByUuid(floorUuid: string): Promise<FloorParentInterface> {
try {
const floor = await this.spaceRepository.findOne({
where: {
uuid: floorUuid,
spaceType: {
type: 'floor',
},
},
relations: ['spaceType', 'parent', 'parent.spaceType'],
});
if (!floor) {
throw new HttpException('Floor not found', HttpStatus.NOT_FOUND);
}
return {
uuid: floor.uuid,
name: floor.spaceName,
type: floor.spaceType.type,
parent: {
uuid: floor.parent.uuid,
name: floor.parent.spaceName,
type: floor.parent.spaceType.type,
},
};
} catch (err) {
throw new HttpException(
err.message,
err.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
}

View File

@ -0,0 +1 @@
export * from './floor.service';

View File

@ -0,0 +1,74 @@
import { SpaceRepository } from '@app/common/modules/space/repositories';
import {
Injectable,
NestMiddleware,
HttpStatus,
HttpException,
} from '@nestjs/common';
import { Request, Response, NextFunction } from 'express';
@Injectable()
export class CheckBuildingTypeMiddleware implements NestMiddleware {
constructor(private readonly spaceRepository: SpaceRepository) {}
async use(req: Request, res: Response, next: NextFunction) {
try {
// Destructure request body for cleaner code
const { floorName, buildingUuid } = req.body;
// Guard clauses for early return
if (!floorName) {
return res.status(HttpStatus.BAD_REQUEST).json({
statusCode: HttpStatus.BAD_REQUEST,
message: 'floorName is required',
});
}
if (!buildingUuid) {
return res.status(HttpStatus.BAD_REQUEST).json({
statusCode: HttpStatus.BAD_REQUEST,
message: 'buildingUuid is required',
});
}
// Call function to check if building is a building
await this.checkBuildingIsBuilding(buildingUuid);
// Call next middleware
next();
} catch (error) {
// Handle errors
this.handleMiddlewareError(error, res);
}
}
async checkBuildingIsBuilding(buildingUuid: string) {
const buildingData = await this.spaceRepository.findOne({
where: { uuid: buildingUuid },
relations: ['spaceType'],
});
// Throw error if building not found
if (!buildingData) {
throw new HttpException('Building not found', HttpStatus.NOT_FOUND);
}
// Throw error if building is not of type 'building'
if (buildingData.spaceType.type !== 'building') {
throw new HttpException(
"buildingUuid is not of type 'building'",
HttpStatus.BAD_REQUEST,
);
}
}
// Function to handle middleware errors
private handleMiddlewareError(error: Error, res: Response) {
const status =
error instanceof HttpException
? error.getStatus()
: HttpStatus.INTERNAL_SERVER_ERROR;
const message = error.message || 'Internal server error';
res.status(status).json({ statusCode: status, message });
}
}