mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-10 15:17:41 +00:00
Add unit module with controller, service, dtos, and middleware
This commit is contained in:
@ -11,6 +11,7 @@ import { DeviceModule } from './device/device.module';
|
||||
import { CommunityModule } from './community/community.module';
|
||||
import { BuildingModule } from './building/building.module';
|
||||
import { FloorModule } from './floor/floor.module';
|
||||
import { UnitModule } from './unit/unit.module';
|
||||
@Module({
|
||||
imports: [
|
||||
ConfigModule.forRoot({
|
||||
@ -21,6 +22,7 @@ import { FloorModule } from './floor/floor.module';
|
||||
CommunityModule,
|
||||
BuildingModule,
|
||||
FloorModule,
|
||||
UnitModule,
|
||||
HomeModule,
|
||||
RoomModule,
|
||||
GroupModule,
|
||||
|
74
src/middleware/CheckFloorTypeMiddleware.ts
Normal file
74
src/middleware/CheckFloorTypeMiddleware.ts
Normal 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 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
src/unit/controllers/index.ts
Normal file
1
src/unit/controllers/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './unit.controller';
|
99
src/unit/controllers/unit.controller.ts
Normal file
99
src/unit/controllers/unit.controller.ts
Normal file
@ -0,0 +1,99 @@
|
||||
import { UnitService } from '../services/unit.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 { AddUnitDto } from '../dtos/add.unit.dto';
|
||||
import { GetUnitChildDto } from '../dtos/get.unit.dto';
|
||||
|
||||
@ApiTags('Unit Module')
|
||||
@Controller({
|
||||
version: '1',
|
||||
path: 'unit',
|
||||
})
|
||||
export class UnitController {
|
||||
constructor(private readonly unitService: UnitService) {}
|
||||
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Post()
|
||||
async addUnit(@Body() addUnitDto: AddUnitDto) {
|
||||
try {
|
||||
await this.unitService.addUnit(addUnitDto);
|
||||
return { message: 'Unit added successfully' };
|
||||
} catch (error) {
|
||||
throw new HttpException(
|
||||
error.message || 'Internal server error',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Get(':unitUuid')
|
||||
async getUnitByUuid(@Param('unitUuid') unitUuid: string) {
|
||||
try {
|
||||
const unit = await this.unitService.getUnitByUuid(unitUuid);
|
||||
return unit;
|
||||
} catch (error) {
|
||||
if (error.status === 404) {
|
||||
throw new HttpException('Unit not found', HttpStatus.NOT_FOUND);
|
||||
} else {
|
||||
throw new HttpException(
|
||||
error.message || 'Internal server error',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Get('child/:unitUuid')
|
||||
async getUnitChildByUuid(
|
||||
@Param('unitUuid') unitUuid: string,
|
||||
@Query() query: GetUnitChildDto,
|
||||
) {
|
||||
try {
|
||||
const unit = await this.unitService.getUnitChildByUuid(unitUuid, query);
|
||||
return unit;
|
||||
} catch (error) {
|
||||
if (error.status === 404) {
|
||||
throw new HttpException('Unit not found', HttpStatus.NOT_FOUND);
|
||||
} else {
|
||||
throw new HttpException(
|
||||
error.message || 'Internal server error',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ApiBearerAuth()
|
||||
@UseGuards(JwtAuthGuard)
|
||||
@Get('parent/:unitUuid')
|
||||
async getUnitParentByUuid(@Param('unitUuid') unitUuid: string) {
|
||||
try {
|
||||
const unit = await this.unitService.getUnitParentByUuid(unitUuid);
|
||||
return unit;
|
||||
} catch (error) {
|
||||
if (error.status === 404) {
|
||||
throw new HttpException('Unit not found', HttpStatus.NOT_FOUND);
|
||||
} else {
|
||||
throw new HttpException(
|
||||
error.message || 'Internal server error',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
23
src/unit/dtos/add.unit.dto.ts
Normal file
23
src/unit/dtos/add.unit.dto.ts
Normal file
@ -0,0 +1,23 @@
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { IsNotEmpty, IsString } from 'class-validator';
|
||||
|
||||
export class AddUnitDto {
|
||||
@ApiProperty({
|
||||
description: 'unitName',
|
||||
required: true,
|
||||
})
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public unitName: string;
|
||||
|
||||
@ApiProperty({
|
||||
description: 'floorUuid',
|
||||
required: true,
|
||||
})
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public floorUuid: string;
|
||||
constructor(dto: Partial<AddUnitDto>) {
|
||||
Object.assign(this, dto);
|
||||
}
|
||||
}
|
51
src/unit/dtos/get.unit.dto.ts
Normal file
51
src/unit/dtos/get.unit.dto.ts
Normal 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 GetUnitDto {
|
||||
@ApiProperty({
|
||||
description: 'unitUuid',
|
||||
required: true,
|
||||
})
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
public unitUuid: string;
|
||||
}
|
||||
|
||||
export class GetUnitChildDto {
|
||||
@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/unit/dtos/index.ts
Normal file
1
src/unit/dtos/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './add.unit.dto';
|
21
src/unit/interface/unit.interface.ts
Normal file
21
src/unit/interface/unit.interface.ts
Normal file
@ -0,0 +1,21 @@
|
||||
export interface GetUnitByUuidInterface {
|
||||
uuid: string;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
name: string;
|
||||
type: string;
|
||||
}
|
||||
|
||||
export interface UnitChildInterface {
|
||||
uuid: string;
|
||||
name: string;
|
||||
type: string;
|
||||
totalCount?: number;
|
||||
children?: UnitChildInterface[];
|
||||
}
|
||||
export interface UnitParentInterface {
|
||||
uuid: string;
|
||||
name: string;
|
||||
type: string;
|
||||
parent?: UnitParentInterface;
|
||||
}
|
1
src/unit/services/index.ts
Normal file
1
src/unit/services/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './unit.service';
|
180
src/unit/services/unit.service.ts
Normal file
180
src/unit/services/unit.service.ts
Normal file
@ -0,0 +1,180 @@
|
||||
import { GetUnitChildDto } from '../dtos/get.unit.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 { AddUnitDto } from '../dtos';
|
||||
import {
|
||||
UnitChildInterface,
|
||||
UnitParentInterface,
|
||||
GetUnitByUuidInterface,
|
||||
} from '../interface/unit.interface';
|
||||
import { SpaceEntity } from '@app/common/modules/space/entities';
|
||||
|
||||
@Injectable()
|
||||
export class UnitService {
|
||||
constructor(
|
||||
private readonly spaceRepository: SpaceRepository,
|
||||
private readonly spaceTypeRepository: SpaceTypeRepository,
|
||||
) {}
|
||||
|
||||
async addUnit(addUnitDto: AddUnitDto) {
|
||||
try {
|
||||
const spaceType = await this.spaceTypeRepository.findOne({
|
||||
where: {
|
||||
type: 'unit',
|
||||
},
|
||||
});
|
||||
|
||||
await this.spaceRepository.save({
|
||||
spaceName: addUnitDto.unitName,
|
||||
parent: { uuid: addUnitDto.floorUuid },
|
||||
spaceType: { uuid: spaceType.uuid },
|
||||
});
|
||||
} catch (err) {
|
||||
throw new HttpException(err.message, HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
async getUnitByUuid(unitUuid: string): Promise<GetUnitByUuidInterface> {
|
||||
try {
|
||||
const unit = await this.spaceRepository.findOne({
|
||||
where: {
|
||||
uuid: unitUuid,
|
||||
spaceType: {
|
||||
type: 'unit',
|
||||
},
|
||||
},
|
||||
relations: ['spaceType'],
|
||||
});
|
||||
if (!unit) {
|
||||
throw new HttpException('Unit not found', HttpStatus.NOT_FOUND);
|
||||
}
|
||||
return {
|
||||
uuid: unit.uuid,
|
||||
createdAt: unit.createdAt,
|
||||
updatedAt: unit.updatedAt,
|
||||
name: unit.spaceName,
|
||||
type: unit.spaceType.type,
|
||||
};
|
||||
} catch (err) {
|
||||
throw new HttpException(
|
||||
err.message,
|
||||
err.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
async getUnitChildByUuid(
|
||||
unitUuid: string,
|
||||
getUnitChildDto: GetUnitChildDto,
|
||||
): Promise<UnitChildInterface> {
|
||||
const { includeSubSpaces, page, pageSize } = getUnitChildDto;
|
||||
|
||||
const space = await this.spaceRepository.findOneOrFail({
|
||||
where: { uuid: unitUuid },
|
||||
relations: ['children', 'spaceType'],
|
||||
});
|
||||
|
||||
if (space.spaceType.type !== 'unit') {
|
||||
throw new HttpException('Unit 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<UnitChildInterface[]> {
|
||||
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 !== 'unit' &&
|
||||
child.spaceType.type !== 'floor' &&
|
||||
child.spaceType.type !== 'community',
|
||||
) // Filter remaining unit and floor 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 !== 'unit' &&
|
||||
child.spaceType.type !== 'floor' &&
|
||||
child.spaceType.type !== 'community',
|
||||
) // Filter remaining unit and floor 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 getUnitParentByUuid(unitUuid: string): Promise<UnitParentInterface> {
|
||||
try {
|
||||
const unit = await this.spaceRepository.findOne({
|
||||
where: {
|
||||
uuid: unitUuid,
|
||||
spaceType: {
|
||||
type: 'unit',
|
||||
},
|
||||
},
|
||||
relations: ['spaceType', 'parent', 'parent.spaceType'],
|
||||
});
|
||||
if (!unit) {
|
||||
throw new HttpException('Unit not found', HttpStatus.NOT_FOUND);
|
||||
}
|
||||
|
||||
return {
|
||||
uuid: unit.uuid,
|
||||
name: unit.spaceName,
|
||||
type: unit.spaceType.type,
|
||||
parent: {
|
||||
uuid: unit.parent.uuid,
|
||||
name: unit.parent.spaceName,
|
||||
type: unit.parent.spaceType.type,
|
||||
},
|
||||
};
|
||||
} catch (err) {
|
||||
throw new HttpException(
|
||||
err.message,
|
||||
err.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
29
src/unit/unit.module.ts
Normal file
29
src/unit/unit.module.ts
Normal file
@ -0,0 +1,29 @@
|
||||
import {
|
||||
MiddlewareConsumer,
|
||||
Module,
|
||||
NestModule,
|
||||
RequestMethod,
|
||||
} from '@nestjs/common';
|
||||
import { UnitService } from './services/unit.service';
|
||||
import { UnitController } from './controllers/unit.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 { CheckFloorTypeMiddleware } from 'src/middleware/CheckFloorTypeMiddleware';
|
||||
|
||||
@Module({
|
||||
imports: [ConfigModule, SpaceRepositoryModule, SpaceTypeRepositoryModule],
|
||||
controllers: [UnitController],
|
||||
providers: [UnitService, SpaceRepository, SpaceTypeRepository],
|
||||
exports: [UnitService],
|
||||
})
|
||||
export class UnitModule implements NestModule {
|
||||
configure(consumer: MiddlewareConsumer) {
|
||||
consumer.apply(CheckFloorTypeMiddleware).forRoutes({
|
||||
path: '/unit',
|
||||
method: RequestMethod.POST,
|
||||
});
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user