mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-15 10:25:23 +00:00
Refactor middleware to guards for type checking
This commit is contained in:
@ -1,9 +1,4 @@
|
|||||||
import {
|
import { Module } from '@nestjs/common';
|
||||||
MiddlewareConsumer,
|
|
||||||
Module,
|
|
||||||
NestModule,
|
|
||||||
RequestMethod,
|
|
||||||
} from '@nestjs/common';
|
|
||||||
import { BuildingService } from './services/building.service';
|
import { BuildingService } from './services/building.service';
|
||||||
import { BuildingController } from './controllers/building.controller';
|
import { BuildingController } from './controllers/building.controller';
|
||||||
import { ConfigModule } from '@nestjs/config';
|
import { ConfigModule } from '@nestjs/config';
|
||||||
@ -11,7 +6,6 @@ import { SpaceRepositoryModule } from '@app/common/modules/space/space.repositor
|
|||||||
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||||
import { SpaceTypeRepositoryModule } from '@app/common/modules/space-type/space.type.repository.module';
|
import { SpaceTypeRepositoryModule } from '@app/common/modules/space-type/space.type.repository.module';
|
||||||
import { SpaceTypeRepository } from '@app/common/modules/space-type/repositories';
|
import { SpaceTypeRepository } from '@app/common/modules/space-type/repositories';
|
||||||
import { CheckCommunityTypeMiddleware } from 'src/middleware/CheckCommunityTypeMiddleware';
|
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [ConfigModule, SpaceRepositoryModule, SpaceTypeRepositoryModule],
|
imports: [ConfigModule, SpaceRepositoryModule, SpaceTypeRepositoryModule],
|
||||||
@ -19,11 +13,4 @@ import { CheckCommunityTypeMiddleware } from 'src/middleware/CheckCommunityTypeM
|
|||||||
providers: [BuildingService, SpaceRepository, SpaceTypeRepository],
|
providers: [BuildingService, SpaceRepository, SpaceTypeRepository],
|
||||||
exports: [BuildingService],
|
exports: [BuildingService],
|
||||||
})
|
})
|
||||||
export class BuildingModule implements NestModule {
|
export class BuildingModule {}
|
||||||
configure(consumer: MiddlewareConsumer) {
|
|
||||||
consumer.apply(CheckCommunityTypeMiddleware).forRoutes({
|
|
||||||
path: '/building',
|
|
||||||
method: RequestMethod.POST,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -16,6 +16,7 @@ import { JwtAuthGuard } from '../../../libs/common/src/guards/jwt.auth.guard';
|
|||||||
import { AddBuildingDto } from '../dtos/add.building.dto';
|
import { AddBuildingDto } from '../dtos/add.building.dto';
|
||||||
import { GetBuildingChildDto } from '../dtos/get.building.dto';
|
import { GetBuildingChildDto } from '../dtos/get.building.dto';
|
||||||
import { UpdateBuildingNameDto } from '../dtos/update.building.dto';
|
import { UpdateBuildingNameDto } from '../dtos/update.building.dto';
|
||||||
|
import { CheckCommunityTypeGuard } from 'src/guards/community.type.guard';
|
||||||
|
|
||||||
@ApiTags('Building Module')
|
@ApiTags('Building Module')
|
||||||
@Controller({
|
@Controller({
|
||||||
@ -26,7 +27,7 @@ export class BuildingController {
|
|||||||
constructor(private readonly buildingService: BuildingService) {}
|
constructor(private readonly buildingService: BuildingService) {}
|
||||||
|
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(JwtAuthGuard)
|
@UseGuards(JwtAuthGuard, CheckCommunityTypeGuard)
|
||||||
@Post()
|
@Post()
|
||||||
async addBuilding(@Body() addBuildingDto: AddBuildingDto) {
|
async addBuilding(@Body() addBuildingDto: AddBuildingDto) {
|
||||||
try {
|
try {
|
||||||
|
@ -32,13 +32,20 @@ export class BuildingService {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (!spaceType) {
|
||||||
|
throw new BadRequestException('Invalid building UUID');
|
||||||
|
}
|
||||||
await this.spaceRepository.save({
|
await this.spaceRepository.save({
|
||||||
spaceName: addBuildingDto.buildingName,
|
spaceName: addBuildingDto.buildingName,
|
||||||
parent: { uuid: addBuildingDto.communityUuid },
|
parent: { uuid: addBuildingDto.communityUuid },
|
||||||
spaceType: { uuid: spaceType.uuid },
|
spaceType: { uuid: spaceType.uuid },
|
||||||
});
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw new HttpException(err.message, HttpStatus.INTERNAL_SERVER_ERROR);
|
if (err instanceof BadRequestException) {
|
||||||
|
throw err; // Re-throw BadRequestException
|
||||||
|
} else {
|
||||||
|
throw new HttpException('Building not found', HttpStatus.NOT_FOUND);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,6 +16,7 @@ import { JwtAuthGuard } from '../../../libs/common/src/guards/jwt.auth.guard';
|
|||||||
import { AddFloorDto } from '../dtos/add.floor.dto';
|
import { AddFloorDto } from '../dtos/add.floor.dto';
|
||||||
import { GetFloorChildDto } from '../dtos/get.floor.dto';
|
import { GetFloorChildDto } from '../dtos/get.floor.dto';
|
||||||
import { UpdateFloorNameDto } from '../dtos/update.floor.dto';
|
import { UpdateFloorNameDto } from '../dtos/update.floor.dto';
|
||||||
|
import { CheckBuildingTypeGuard } from 'src/guards/building.type.guard';
|
||||||
|
|
||||||
@ApiTags('Floor Module')
|
@ApiTags('Floor Module')
|
||||||
@Controller({
|
@Controller({
|
||||||
@ -26,7 +27,7 @@ export class FloorController {
|
|||||||
constructor(private readonly floorService: FloorService) {}
|
constructor(private readonly floorService: FloorService) {}
|
||||||
|
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(JwtAuthGuard)
|
@UseGuards(JwtAuthGuard, CheckBuildingTypeGuard)
|
||||||
@Post()
|
@Post()
|
||||||
async addFloor(@Body() addFloorDto: AddFloorDto) {
|
async addFloor(@Body() addFloorDto: AddFloorDto) {
|
||||||
try {
|
try {
|
||||||
|
@ -1,9 +1,4 @@
|
|||||||
import {
|
import { Module } from '@nestjs/common';
|
||||||
MiddlewareConsumer,
|
|
||||||
Module,
|
|
||||||
NestModule,
|
|
||||||
RequestMethod,
|
|
||||||
} from '@nestjs/common';
|
|
||||||
import { FloorService } from './services/floor.service';
|
import { FloorService } from './services/floor.service';
|
||||||
import { FloorController } from './controllers/floor.controller';
|
import { FloorController } from './controllers/floor.controller';
|
||||||
import { ConfigModule } from '@nestjs/config';
|
import { ConfigModule } from '@nestjs/config';
|
||||||
@ -11,7 +6,6 @@ import { SpaceRepositoryModule } from '@app/common/modules/space/space.repositor
|
|||||||
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||||
import { SpaceTypeRepositoryModule } from '@app/common/modules/space-type/space.type.repository.module';
|
import { SpaceTypeRepositoryModule } from '@app/common/modules/space-type/space.type.repository.module';
|
||||||
import { SpaceTypeRepository } from '@app/common/modules/space-type/repositories';
|
import { SpaceTypeRepository } from '@app/common/modules/space-type/repositories';
|
||||||
import { CheckBuildingTypeMiddleware } from 'src/middleware/CheckBuildingTypeMiddleware';
|
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [ConfigModule, SpaceRepositoryModule, SpaceTypeRepositoryModule],
|
imports: [ConfigModule, SpaceRepositoryModule, SpaceTypeRepositoryModule],
|
||||||
@ -19,11 +13,4 @@ import { CheckBuildingTypeMiddleware } from 'src/middleware/CheckBuildingTypeMid
|
|||||||
providers: [FloorService, SpaceRepository, SpaceTypeRepository],
|
providers: [FloorService, SpaceRepository, SpaceTypeRepository],
|
||||||
exports: [FloorService],
|
exports: [FloorService],
|
||||||
})
|
})
|
||||||
export class FloorModule implements NestModule {
|
export class FloorModule {}
|
||||||
configure(consumer: MiddlewareConsumer) {
|
|
||||||
consumer.apply(CheckBuildingTypeMiddleware).forRoutes({
|
|
||||||
path: '/floor',
|
|
||||||
method: RequestMethod.POST,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
66
src/guards/building.type.guard.ts
Normal file
66
src/guards/building.type.guard.ts
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||||
|
import {
|
||||||
|
Injectable,
|
||||||
|
CanActivate,
|
||||||
|
HttpStatus,
|
||||||
|
BadRequestException,
|
||||||
|
ExecutionContext,
|
||||||
|
} from '@nestjs/common';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class CheckBuildingTypeGuard implements CanActivate {
|
||||||
|
constructor(private readonly spaceRepository: SpaceRepository) {}
|
||||||
|
|
||||||
|
async canActivate(context: ExecutionContext): Promise<boolean> {
|
||||||
|
const req = context.switchToHttp().getRequest();
|
||||||
|
|
||||||
|
try {
|
||||||
|
const { floorName, buildingUuid } = req.body;
|
||||||
|
|
||||||
|
if (!floorName) {
|
||||||
|
throw new BadRequestException('floorName is required');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!buildingUuid) {
|
||||||
|
throw new BadRequestException('buildingUuid is required');
|
||||||
|
}
|
||||||
|
|
||||||
|
await this.checkBuildingIsBuildingType(buildingUuid);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
} catch (error) {
|
||||||
|
this.handleGuardError(error, context);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async checkBuildingIsBuildingType(buildingUuid: string) {
|
||||||
|
const buildingData = await this.spaceRepository.findOne({
|
||||||
|
where: { uuid: buildingUuid },
|
||||||
|
relations: ['spaceType'],
|
||||||
|
});
|
||||||
|
if (
|
||||||
|
!buildingData ||
|
||||||
|
!buildingData.spaceType ||
|
||||||
|
buildingData.spaceType.type !== 'building'
|
||||||
|
) {
|
||||||
|
throw new BadRequestException('Invalid building UUID');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private handleGuardError(error: Error, context: ExecutionContext) {
|
||||||
|
const response = context.switchToHttp().getResponse();
|
||||||
|
console.error(error);
|
||||||
|
|
||||||
|
if (error instanceof BadRequestException) {
|
||||||
|
response
|
||||||
|
.status(HttpStatus.BAD_REQUEST)
|
||||||
|
.json({ statusCode: HttpStatus.BAD_REQUEST, message: error.message });
|
||||||
|
} else {
|
||||||
|
response.status(HttpStatus.NOT_FOUND).json({
|
||||||
|
statusCode: HttpStatus.NOT_FOUND,
|
||||||
|
message: 'Building not found',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
67
src/guards/community.type.guard.ts
Normal file
67
src/guards/community.type.guard.ts
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
import {
|
||||||
|
CanActivate,
|
||||||
|
ExecutionContext,
|
||||||
|
Injectable,
|
||||||
|
HttpStatus,
|
||||||
|
} from '@nestjs/common';
|
||||||
|
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||||
|
import { BadRequestException } from '@nestjs/common';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class CheckCommunityTypeGuard implements CanActivate {
|
||||||
|
constructor(private readonly spaceRepository: SpaceRepository) {}
|
||||||
|
|
||||||
|
async canActivate(context: ExecutionContext): Promise<boolean> {
|
||||||
|
const req = context.switchToHttp().getRequest();
|
||||||
|
|
||||||
|
try {
|
||||||
|
const { buildingName, communityUuid } = req.body;
|
||||||
|
|
||||||
|
if (!buildingName) {
|
||||||
|
throw new BadRequestException('buildingName is required');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!communityUuid) {
|
||||||
|
throw new BadRequestException('communityUuid is required');
|
||||||
|
}
|
||||||
|
|
||||||
|
await this.checkCommunityIsCommunityType(communityUuid);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
} catch (error) {
|
||||||
|
this.handleGuardError(error, context);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async checkCommunityIsCommunityType(communityUuid: string) {
|
||||||
|
const communityData = await this.spaceRepository.findOne({
|
||||||
|
where: { uuid: communityUuid },
|
||||||
|
relations: ['spaceType'],
|
||||||
|
});
|
||||||
|
|
||||||
|
if (
|
||||||
|
!communityData ||
|
||||||
|
!communityData.spaceType ||
|
||||||
|
communityData.spaceType.type !== 'community'
|
||||||
|
) {
|
||||||
|
throw new BadRequestException('Invalid community UUID');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private handleGuardError(error: Error, context: ExecutionContext) {
|
||||||
|
const response = context.switchToHttp().getResponse();
|
||||||
|
console.error(error);
|
||||||
|
|
||||||
|
if (error instanceof BadRequestException) {
|
||||||
|
response
|
||||||
|
.status(HttpStatus.BAD_REQUEST)
|
||||||
|
.json({ statusCode: HttpStatus.BAD_REQUEST, message: error.message });
|
||||||
|
} else {
|
||||||
|
response.status(HttpStatus.NOT_FOUND).json({
|
||||||
|
statusCode: HttpStatus.NOT_FOUND,
|
||||||
|
message: 'Community not found',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
66
src/guards/floor.type.guard.ts
Normal file
66
src/guards/floor.type.guard.ts
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||||
|
import {
|
||||||
|
Injectable,
|
||||||
|
CanActivate,
|
||||||
|
HttpStatus,
|
||||||
|
ExecutionContext,
|
||||||
|
BadRequestException,
|
||||||
|
} from '@nestjs/common';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class CheckFloorTypeGuard implements CanActivate {
|
||||||
|
constructor(private readonly spaceRepository: SpaceRepository) {}
|
||||||
|
|
||||||
|
async canActivate(context: ExecutionContext): Promise<boolean> {
|
||||||
|
const req = context.switchToHttp().getRequest();
|
||||||
|
try {
|
||||||
|
const { unitName, floorUuid } = req.body;
|
||||||
|
|
||||||
|
if (!unitName) {
|
||||||
|
throw new BadRequestException('unitName is required');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!floorUuid) {
|
||||||
|
throw new BadRequestException('floorUuid is required');
|
||||||
|
}
|
||||||
|
|
||||||
|
await this.checkFloorIsFloorType(floorUuid);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
} catch (error) {
|
||||||
|
this.handleGuardError(error, context);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async checkFloorIsFloorType(floorUuid: string) {
|
||||||
|
const floorData = await this.spaceRepository.findOne({
|
||||||
|
where: { uuid: floorUuid },
|
||||||
|
relations: ['spaceType'],
|
||||||
|
});
|
||||||
|
|
||||||
|
if (
|
||||||
|
!floorData ||
|
||||||
|
!floorData.spaceType ||
|
||||||
|
floorData.spaceType.type !== 'floor'
|
||||||
|
) {
|
||||||
|
throw new BadRequestException('Invalid floor UUID');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private handleGuardError(error: Error, context: ExecutionContext) {
|
||||||
|
const response = context.switchToHttp().getResponse();
|
||||||
|
console.error(error);
|
||||||
|
|
||||||
|
if (error instanceof BadRequestException) {
|
||||||
|
response
|
||||||
|
.status(HttpStatus.BAD_REQUEST)
|
||||||
|
.json({ statusCode: HttpStatus.BAD_REQUEST, message: error.message });
|
||||||
|
} else {
|
||||||
|
response.status(HttpStatus.NOT_FOUND).json({
|
||||||
|
statusCode: HttpStatus.NOT_FOUND,
|
||||||
|
message: 'Floor not found',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
66
src/guards/unit.type.guard.ts
Normal file
66
src/guards/unit.type.guard.ts
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||||
|
import {
|
||||||
|
Injectable,
|
||||||
|
CanActivate,
|
||||||
|
HttpStatus,
|
||||||
|
BadRequestException,
|
||||||
|
ExecutionContext,
|
||||||
|
} from '@nestjs/common';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class CheckUnitTypeGuard implements CanActivate {
|
||||||
|
constructor(private readonly spaceRepository: SpaceRepository) {}
|
||||||
|
|
||||||
|
async canActivate(context: ExecutionContext): Promise<boolean> {
|
||||||
|
const req = context.switchToHttp().getRequest();
|
||||||
|
try {
|
||||||
|
const { roomName, unitUuid } = req.body;
|
||||||
|
|
||||||
|
if (!roomName) {
|
||||||
|
throw new BadRequestException('roomName is required');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!unitUuid) {
|
||||||
|
throw new BadRequestException('unitUuid is required');
|
||||||
|
}
|
||||||
|
|
||||||
|
await this.checkFloorIsFloorType(unitUuid);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
} catch (error) {
|
||||||
|
this.handleGuardError(error, context);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async checkFloorIsFloorType(unitUuid: string) {
|
||||||
|
const unitData = await this.spaceRepository.findOne({
|
||||||
|
where: { uuid: unitUuid },
|
||||||
|
relations: ['spaceType'],
|
||||||
|
});
|
||||||
|
|
||||||
|
if (
|
||||||
|
!unitData ||
|
||||||
|
!unitData.spaceType ||
|
||||||
|
unitData.spaceType.type !== 'unit'
|
||||||
|
) {
|
||||||
|
throw new BadRequestException('Invalid unit UUID');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private handleGuardError(error: Error, context: ExecutionContext) {
|
||||||
|
const response = context.switchToHttp().getResponse();
|
||||||
|
console.error(error);
|
||||||
|
|
||||||
|
if (error instanceof BadRequestException) {
|
||||||
|
response
|
||||||
|
.status(HttpStatus.BAD_REQUEST)
|
||||||
|
.json({ statusCode: HttpStatus.BAD_REQUEST, message: error.message });
|
||||||
|
} else {
|
||||||
|
response.status(HttpStatus.NOT_FOUND).json({
|
||||||
|
statusCode: HttpStatus.NOT_FOUND,
|
||||||
|
message: 'Unit not found',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,74 +0,0 @@
|
|||||||
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.checkBuildingIsBuildingType(buildingUuid);
|
|
||||||
|
|
||||||
// Call next middleware
|
|
||||||
next();
|
|
||||||
} catch (error) {
|
|
||||||
// Handle errors
|
|
||||||
this.handleMiddlewareError(error, res);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async checkBuildingIsBuildingType(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 });
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,74 +0,0 @@
|
|||||||
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 CheckCommunityTypeMiddleware implements NestMiddleware {
|
|
||||||
constructor(private readonly spaceRepository: SpaceRepository) {}
|
|
||||||
|
|
||||||
async use(req: Request, res: Response, next: NextFunction) {
|
|
||||||
try {
|
|
||||||
// Destructure request body for cleaner code
|
|
||||||
const { buildingName, communityUuid } = req.body;
|
|
||||||
|
|
||||||
// Guard clauses for early return
|
|
||||||
if (!buildingName) {
|
|
||||||
return res.status(HttpStatus.BAD_REQUEST).json({
|
|
||||||
statusCode: HttpStatus.BAD_REQUEST,
|
|
||||||
message: 'buildingName is required',
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!communityUuid) {
|
|
||||||
return res.status(HttpStatus.BAD_REQUEST).json({
|
|
||||||
statusCode: HttpStatus.BAD_REQUEST,
|
|
||||||
message: 'communityUuid is required',
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Call function to check if community is a building
|
|
||||||
await this.checkCommunityIsCommunityType(communityUuid);
|
|
||||||
|
|
||||||
// Call next middleware
|
|
||||||
next();
|
|
||||||
} catch (error) {
|
|
||||||
// Handle errors
|
|
||||||
this.handleMiddlewareError(error, res);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async checkCommunityIsCommunityType(communityUuid: string) {
|
|
||||||
const communityData = await this.spaceRepository.findOne({
|
|
||||||
where: { uuid: communityUuid },
|
|
||||||
relations: ['spaceType'],
|
|
||||||
});
|
|
||||||
|
|
||||||
// Throw error if community not found
|
|
||||||
if (!communityData) {
|
|
||||||
throw new HttpException('Community not found', HttpStatus.NOT_FOUND);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Throw error if community is not of type 'community'
|
|
||||||
if (communityData.spaceType.type !== 'community') {
|
|
||||||
throw new HttpException(
|
|
||||||
"communityUuid is not of type 'community'",
|
|
||||||
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 });
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,74 +0,0 @@
|
|||||||
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 CheckFloorTypeMiddleware implements NestMiddleware {
|
|
||||||
constructor(private readonly spaceRepository: SpaceRepository) {}
|
|
||||||
|
|
||||||
async use(req: Request, res: Response, next: NextFunction) {
|
|
||||||
try {
|
|
||||||
// Destructure request body for cleaner code
|
|
||||||
const { unitName, floorUuid } = req.body;
|
|
||||||
|
|
||||||
// Guard clauses for early return
|
|
||||||
if (!unitName) {
|
|
||||||
return res.status(HttpStatus.BAD_REQUEST).json({
|
|
||||||
statusCode: HttpStatus.BAD_REQUEST,
|
|
||||||
message: 'unitName is required',
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!floorUuid) {
|
|
||||||
return res.status(HttpStatus.BAD_REQUEST).json({
|
|
||||||
statusCode: HttpStatus.BAD_REQUEST,
|
|
||||||
message: 'floorUuid is required',
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Call function to check if floor is a floor
|
|
||||||
await this.checkFloorIsFloorType(floorUuid);
|
|
||||||
|
|
||||||
// Call next middleware
|
|
||||||
next();
|
|
||||||
} catch (error) {
|
|
||||||
// Handle errors
|
|
||||||
this.handleMiddlewareError(error, res);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async checkFloorIsFloorType(floorUuid: string) {
|
|
||||||
const floorData = await this.spaceRepository.findOne({
|
|
||||||
where: { uuid: floorUuid },
|
|
||||||
relations: ['spaceType'],
|
|
||||||
});
|
|
||||||
|
|
||||||
// Throw error if floor not found
|
|
||||||
if (!floorData) {
|
|
||||||
throw new HttpException('Floor not found', HttpStatus.NOT_FOUND);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Throw error if floor is not of type 'floor'
|
|
||||||
if (floorData.spaceType.type !== 'floor') {
|
|
||||||
throw new HttpException(
|
|
||||||
"floorUuid is not of type 'floor'",
|
|
||||||
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 });
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,74 +0,0 @@
|
|||||||
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 CheckUnitTypeMiddleware implements NestMiddleware {
|
|
||||||
constructor(private readonly spaceRepository: SpaceRepository) {}
|
|
||||||
|
|
||||||
async use(req: Request, res: Response, next: NextFunction) {
|
|
||||||
try {
|
|
||||||
// Destructure request body for cleaner code
|
|
||||||
const { roomName, unitUuid } = req.body;
|
|
||||||
|
|
||||||
// Guard clauses for early return
|
|
||||||
if (!roomName) {
|
|
||||||
return res.status(HttpStatus.BAD_REQUEST).json({
|
|
||||||
statusCode: HttpStatus.BAD_REQUEST,
|
|
||||||
message: 'roomName is required',
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!unitUuid) {
|
|
||||||
return res.status(HttpStatus.BAD_REQUEST).json({
|
|
||||||
statusCode: HttpStatus.BAD_REQUEST,
|
|
||||||
message: 'unitUuid is required',
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Call function to check if unit is a unit
|
|
||||||
await this.checkFloorIsFloorType(unitUuid);
|
|
||||||
|
|
||||||
// Call next middleware
|
|
||||||
next();
|
|
||||||
} catch (error) {
|
|
||||||
// Handle errors
|
|
||||||
this.handleMiddlewareError(error, res);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async checkFloorIsFloorType(unitUuid: string) {
|
|
||||||
const unitData = await this.spaceRepository.findOne({
|
|
||||||
where: { uuid: unitUuid },
|
|
||||||
relations: ['spaceType'],
|
|
||||||
});
|
|
||||||
|
|
||||||
// Throw error if unit not found
|
|
||||||
if (!unitData) {
|
|
||||||
throw new HttpException('Floor not found', HttpStatus.NOT_FOUND);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Throw error if unit is not of type 'unit'
|
|
||||||
if (unitData.spaceType.type !== 'unit') {
|
|
||||||
throw new HttpException(
|
|
||||||
"unitUuid is not of type 'unit'",
|
|
||||||
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 });
|
|
||||||
}
|
|
||||||
}
|
|
@ -14,6 +14,7 @@ import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
|
|||||||
import { JwtAuthGuard } from '../../../libs/common/src/guards/jwt.auth.guard';
|
import { JwtAuthGuard } from '../../../libs/common/src/guards/jwt.auth.guard';
|
||||||
import { AddRoomDto } from '../dtos/add.room.dto';
|
import { AddRoomDto } from '../dtos/add.room.dto';
|
||||||
import { UpdateRoomNameDto } from '../dtos/update.room.dto';
|
import { UpdateRoomNameDto } from '../dtos/update.room.dto';
|
||||||
|
import { CheckUnitTypeGuard } from 'src/guards/unit.type.guard';
|
||||||
|
|
||||||
@ApiTags('Room Module')
|
@ApiTags('Room Module')
|
||||||
@Controller({
|
@Controller({
|
||||||
@ -24,7 +25,7 @@ export class RoomController {
|
|||||||
constructor(private readonly roomService: RoomService) {}
|
constructor(private readonly roomService: RoomService) {}
|
||||||
|
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(JwtAuthGuard)
|
@UseGuards(JwtAuthGuard, CheckUnitTypeGuard)
|
||||||
@Post()
|
@Post()
|
||||||
async addRoom(@Body() addRoomDto: AddRoomDto) {
|
async addRoom(@Body() addRoomDto: AddRoomDto) {
|
||||||
try {
|
try {
|
||||||
|
@ -1,9 +1,4 @@
|
|||||||
import {
|
import { Module } from '@nestjs/common';
|
||||||
MiddlewareConsumer,
|
|
||||||
Module,
|
|
||||||
NestModule,
|
|
||||||
RequestMethod,
|
|
||||||
} from '@nestjs/common';
|
|
||||||
import { RoomService } from './services/room.service';
|
import { RoomService } from './services/room.service';
|
||||||
import { RoomController } from './controllers/room.controller';
|
import { RoomController } from './controllers/room.controller';
|
||||||
import { ConfigModule } from '@nestjs/config';
|
import { ConfigModule } from '@nestjs/config';
|
||||||
@ -11,7 +6,6 @@ import { SpaceRepositoryModule } from '@app/common/modules/space/space.repositor
|
|||||||
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||||
import { SpaceTypeRepositoryModule } from '@app/common/modules/space-type/space.type.repository.module';
|
import { SpaceTypeRepositoryModule } from '@app/common/modules/space-type/space.type.repository.module';
|
||||||
import { SpaceTypeRepository } from '@app/common/modules/space-type/repositories';
|
import { SpaceTypeRepository } from '@app/common/modules/space-type/repositories';
|
||||||
import { CheckUnitTypeMiddleware } from 'src/middleware/CheckUnitTypeMiddleware';
|
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [ConfigModule, SpaceRepositoryModule, SpaceTypeRepositoryModule],
|
imports: [ConfigModule, SpaceRepositoryModule, SpaceTypeRepositoryModule],
|
||||||
@ -19,11 +13,4 @@ import { CheckUnitTypeMiddleware } from 'src/middleware/CheckUnitTypeMiddleware'
|
|||||||
providers: [RoomService, SpaceRepository, SpaceTypeRepository],
|
providers: [RoomService, SpaceRepository, SpaceTypeRepository],
|
||||||
exports: [RoomService],
|
exports: [RoomService],
|
||||||
})
|
})
|
||||||
export class RoomModule implements NestModule {
|
export class RoomModule {}
|
||||||
configure(consumer: MiddlewareConsumer) {
|
|
||||||
consumer.apply(CheckUnitTypeMiddleware).forRoutes({
|
|
||||||
path: '/room',
|
|
||||||
method: RequestMethod.POST,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -16,6 +16,7 @@ import { JwtAuthGuard } from '../../../libs/common/src/guards/jwt.auth.guard';
|
|||||||
import { AddUnitDto } from '../dtos/add.unit.dto';
|
import { AddUnitDto } from '../dtos/add.unit.dto';
|
||||||
import { GetUnitChildDto } from '../dtos/get.unit.dto';
|
import { GetUnitChildDto } from '../dtos/get.unit.dto';
|
||||||
import { UpdateUnitNameDto } from '../dtos/update.unit.dto';
|
import { UpdateUnitNameDto } from '../dtos/update.unit.dto';
|
||||||
|
import { CheckFloorTypeGuard } from 'src/guards/floor.type.guard';
|
||||||
|
|
||||||
@ApiTags('Unit Module')
|
@ApiTags('Unit Module')
|
||||||
@Controller({
|
@Controller({
|
||||||
@ -26,7 +27,7 @@ export class UnitController {
|
|||||||
constructor(private readonly unitService: UnitService) {}
|
constructor(private readonly unitService: UnitService) {}
|
||||||
|
|
||||||
@ApiBearerAuth()
|
@ApiBearerAuth()
|
||||||
@UseGuards(JwtAuthGuard)
|
@UseGuards(JwtAuthGuard, CheckFloorTypeGuard)
|
||||||
@Post()
|
@Post()
|
||||||
async addUnit(@Body() addUnitDto: AddUnitDto) {
|
async addUnit(@Body() addUnitDto: AddUnitDto) {
|
||||||
try {
|
try {
|
||||||
|
@ -1,9 +1,4 @@
|
|||||||
import {
|
import { Module } from '@nestjs/common';
|
||||||
MiddlewareConsumer,
|
|
||||||
Module,
|
|
||||||
NestModule,
|
|
||||||
RequestMethod,
|
|
||||||
} from '@nestjs/common';
|
|
||||||
import { UnitService } from './services/unit.service';
|
import { UnitService } from './services/unit.service';
|
||||||
import { UnitController } from './controllers/unit.controller';
|
import { UnitController } from './controllers/unit.controller';
|
||||||
import { ConfigModule } from '@nestjs/config';
|
import { ConfigModule } from '@nestjs/config';
|
||||||
@ -11,7 +6,6 @@ import { SpaceRepositoryModule } from '@app/common/modules/space/space.repositor
|
|||||||
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
import { SpaceRepository } from '@app/common/modules/space/repositories';
|
||||||
import { SpaceTypeRepositoryModule } from '@app/common/modules/space-type/space.type.repository.module';
|
import { SpaceTypeRepositoryModule } from '@app/common/modules/space-type/space.type.repository.module';
|
||||||
import { SpaceTypeRepository } from '@app/common/modules/space-type/repositories';
|
import { SpaceTypeRepository } from '@app/common/modules/space-type/repositories';
|
||||||
import { CheckFloorTypeMiddleware } from 'src/middleware/CheckFloorTypeMiddleware';
|
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [ConfigModule, SpaceRepositoryModule, SpaceTypeRepositoryModule],
|
imports: [ConfigModule, SpaceRepositoryModule, SpaceTypeRepositoryModule],
|
||||||
@ -19,11 +13,4 @@ import { CheckFloorTypeMiddleware } from 'src/middleware/CheckFloorTypeMiddlewar
|
|||||||
providers: [UnitService, SpaceRepository, SpaceTypeRepository],
|
providers: [UnitService, SpaceRepository, SpaceTypeRepository],
|
||||||
exports: [UnitService],
|
exports: [UnitService],
|
||||||
})
|
})
|
||||||
export class UnitModule implements NestModule {
|
export class UnitModule {}
|
||||||
configure(consumer: MiddlewareConsumer) {
|
|
||||||
consumer.apply(CheckFloorTypeMiddleware).forRoutes({
|
|
||||||
path: '/unit',
|
|
||||||
method: RequestMethod.POST,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Reference in New Issue
Block a user