Refactor error handling in controllers and services

This commit is contained in:
faris Aljohari
2024-04-14 11:28:52 +03:00
parent 53628236a6
commit d92ae03eac
10 changed files with 287 additions and 308 deletions

View File

@ -35,7 +35,7 @@ export class BuildingController {
} catch (error) { } catch (error) {
throw new HttpException( throw new HttpException(
error.message || 'Internal server error', error.message || 'Internal server error',
HttpStatus.INTERNAL_SERVER_ERROR, error.status || HttpStatus.INTERNAL_SERVER_ERROR,
); );
} }
} }
@ -49,14 +49,10 @@ export class BuildingController {
await this.buildingService.getBuildingByUuid(buildingUuid); await this.buildingService.getBuildingByUuid(buildingUuid);
return building; return building;
} catch (error) { } catch (error) {
if (error.status === 404) { throw new HttpException(
throw new HttpException('Building not found', HttpStatus.NOT_FOUND); error.message || 'Internal server error',
} else { error.status || HttpStatus.INTERNAL_SERVER_ERROR,
throw new HttpException( );
error.message || 'Internal server error',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
} }
@ -74,14 +70,10 @@ export class BuildingController {
); );
return building; return building;
} catch (error) { } catch (error) {
if (error.status === 404) { throw new HttpException(
throw new HttpException('Building not found', HttpStatus.NOT_FOUND); error.message || 'Internal server error',
} else { error.status || HttpStatus.INTERNAL_SERVER_ERROR,
throw new HttpException( );
error.message || 'Internal server error',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
} }
@ApiBearerAuth() @ApiBearerAuth()
@ -93,14 +85,10 @@ export class BuildingController {
await this.buildingService.getBuildingParentByUuid(buildingUuid); await this.buildingService.getBuildingParentByUuid(buildingUuid);
return building; return building;
} catch (error) { } catch (error) {
if (error.status === 404) { throw new HttpException(
throw new HttpException('Building not found', HttpStatus.NOT_FOUND); error.message || 'Internal server error',
} else { error.status || HttpStatus.INTERNAL_SERVER_ERROR,
throw new HttpException( );
error.message || 'Internal server error',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
} }
@ApiBearerAuth() @ApiBearerAuth()
@ -117,14 +105,10 @@ export class BuildingController {
); );
return building; return building;
} catch (error) { } catch (error) {
if (error.status === 404) { throw new HttpException(
throw new HttpException('Building not found', HttpStatus.NOT_FOUND); error.message || 'Internal server error',
} else { error.status || HttpStatus.INTERNAL_SERVER_ERROR,
throw new HttpException( );
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
} }
} }

View File

@ -55,8 +55,12 @@ export class BuildingService {
}, },
relations: ['spaceType'], relations: ['spaceType'],
}); });
if (!building) { if (
throw new HttpException('Building not found', HttpStatus.NOT_FOUND); !building ||
!building.spaceType ||
building.spaceType.type !== 'building'
) {
throw new BadRequestException('Invalid building UUID');
} }
return { return {
uuid: building.uuid, uuid: building.uuid,
@ -66,45 +70,53 @@ export class BuildingService {
type: building.spaceType.type, type: building.spaceType.type,
}; };
} catch (err) { } catch (err) {
throw new HttpException( if (err instanceof BadRequestException) {
err.message, throw err; // Re-throw BadRequestException
err.status || HttpStatus.INTERNAL_SERVER_ERROR, } else {
); throw new HttpException('Building not found', HttpStatus.NOT_FOUND);
}
} }
} }
async getBuildingChildByUuid( async getBuildingChildByUuid(
buildingUuid: string, buildingUuid: string,
getBuildingChildDto: GetBuildingChildDto, getBuildingChildDto: GetBuildingChildDto,
): Promise<BuildingChildInterface> { ): Promise<BuildingChildInterface> {
const { includeSubSpaces, page, pageSize } = getBuildingChildDto; try {
const { includeSubSpaces, page, pageSize } = getBuildingChildDto;
const space = await this.spaceRepository.findOneOrFail({ const space = await this.spaceRepository.findOneOrFail({
where: { uuid: buildingUuid }, where: { uuid: buildingUuid },
relations: ['children', 'spaceType'], relations: ['children', 'spaceType'],
}); });
if (!space || !space.spaceType || space.spaceType.type !== 'building') {
throw new BadRequestException('Invalid building UUID');
}
if (space.spaceType.type !== 'building') { const totalCount = await this.spaceRepository.count({
throw new HttpException('Building not found', HttpStatus.NOT_FOUND); 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,
};
} catch (err) {
if (err instanceof BadRequestException) {
throw err; // Re-throw BadRequestException
} else {
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( private async buildHierarchy(
@ -165,10 +177,13 @@ export class BuildingService {
}, },
relations: ['spaceType', 'parent', 'parent.spaceType'], relations: ['spaceType', 'parent', 'parent.spaceType'],
}); });
if (!building) { if (
throw new HttpException('Building not found', HttpStatus.NOT_FOUND); !building ||
!building.spaceType ||
building.spaceType.type !== 'building'
) {
throw new BadRequestException('Invalid building UUID');
} }
return { return {
uuid: building.uuid, uuid: building.uuid,
name: building.spaceName, name: building.spaceName,
@ -180,10 +195,11 @@ export class BuildingService {
}, },
}; };
} catch (err) { } catch (err) {
throw new HttpException( if (err instanceof BadRequestException) {
err.message, throw err; // Re-throw BadRequestException
err.status || HttpStatus.INTERNAL_SERVER_ERROR, } else {
); throw new HttpException('Building not found', HttpStatus.NOT_FOUND);
}
} }
} }

View File

@ -35,7 +35,7 @@ export class CommunityController {
} catch (error) { } catch (error) {
throw new HttpException( throw new HttpException(
error.message || 'Internal server error', error.message || 'Internal server error',
HttpStatus.INTERNAL_SERVER_ERROR, error.status || HttpStatus.INTERNAL_SERVER_ERROR,
); );
} }
} }
@ -49,14 +49,10 @@ export class CommunityController {
await this.communityService.getCommunityByUuid(communityUuid); await this.communityService.getCommunityByUuid(communityUuid);
return community; return community;
} catch (error) { } catch (error) {
if (error.status === 404) { throw new HttpException(
throw new HttpException('Community not found', HttpStatus.NOT_FOUND); error.message || 'Internal server error',
} else { error.status || HttpStatus.INTERNAL_SERVER_ERROR,
throw new HttpException( );
error.message || 'Internal server error',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
} }
@ -74,14 +70,10 @@ export class CommunityController {
); );
return community; return community;
} catch (error) { } catch (error) {
if (error.status === 404) { throw new HttpException(
throw new HttpException('Community not found', HttpStatus.NOT_FOUND); error.message || 'Internal server error',
} else { error.status || HttpStatus.INTERNAL_SERVER_ERROR,
throw new HttpException( );
error.message || 'Internal server error',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
} }
@ -99,14 +91,10 @@ export class CommunityController {
); );
return community; return community;
} catch (error) { } catch (error) {
if (error.status === 404) { throw new HttpException(
throw new HttpException('Community not found', HttpStatus.NOT_FOUND); error.message || 'Internal server error',
} else { error.status || HttpStatus.INTERNAL_SERVER_ERROR,
throw new HttpException( );
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
} }
} }

View File

@ -53,8 +53,12 @@ export class CommunityService {
}, },
relations: ['spaceType'], relations: ['spaceType'],
}); });
if (!community) { if (
throw new HttpException('Community not found', HttpStatus.NOT_FOUND); !community ||
!community.spaceType ||
community.spaceType.type !== 'community'
) {
throw new BadRequestException('Invalid community UUID');
} }
return { return {
uuid: community.uuid, uuid: community.uuid,
@ -64,42 +68,51 @@ export class CommunityService {
type: community.spaceType.type, type: community.spaceType.type,
}; };
} catch (err) { } catch (err) {
throw new HttpException( if (err instanceof BadRequestException) {
err.message, throw err; // Re-throw BadRequestException
err.status || HttpStatus.INTERNAL_SERVER_ERROR, } else {
); throw new HttpException('Community not found', HttpStatus.NOT_FOUND);
}
} }
} }
async getCommunityChildByUuid( async getCommunityChildByUuid(
communityUuid: string, communityUuid: string,
getCommunityChildDto: GetCommunityChildDto, getCommunityChildDto: GetCommunityChildDto,
): Promise<CommunityChildInterface> { ): Promise<CommunityChildInterface> {
const { includeSubSpaces, page, pageSize } = getCommunityChildDto; try {
const { includeSubSpaces, page, pageSize } = getCommunityChildDto;
const space = await this.spaceRepository.findOneOrFail({ const space = await this.spaceRepository.findOneOrFail({
where: { uuid: communityUuid }, where: { uuid: communityUuid },
relations: ['children', 'spaceType'], relations: ['children', 'spaceType'],
}); });
if (space.spaceType.type !== 'community') { if (!space || !space.spaceType || space.spaceType.type !== 'community') {
throw new HttpException('Community not found', HttpStatus.NOT_FOUND); throw new BadRequestException('Invalid community UUID');
}
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,
};
} catch (err) {
if (err instanceof BadRequestException) {
throw err; // Re-throw BadRequestException
} else {
throw new HttpException('Community 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( private async buildHierarchy(

View File

@ -25,8 +25,8 @@ import { UpdateFloorNameDto } from '../dtos/update.floor.dto';
export class FloorController { export class FloorController {
constructor(private readonly floorService: FloorService) {} constructor(private readonly floorService: FloorService) {}
// @ApiBearerAuth() @ApiBearerAuth()
// @UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@Post() @Post()
async addFloor(@Body() addFloorDto: AddFloorDto) { async addFloor(@Body() addFloorDto: AddFloorDto) {
try { try {
@ -35,7 +35,7 @@ export class FloorController {
} catch (error) { } catch (error) {
throw new HttpException( throw new HttpException(
error.message || 'Internal server error', error.message || 'Internal server error',
HttpStatus.INTERNAL_SERVER_ERROR, error.status || HttpStatus.INTERNAL_SERVER_ERROR,
); );
} }
} }
@ -48,14 +48,10 @@ export class FloorController {
const floor = await this.floorService.getFloorByUuid(floorUuid); const floor = await this.floorService.getFloorByUuid(floorUuid);
return floor; return floor;
} catch (error) { } catch (error) {
if (error.status === 404) { throw new HttpException(
throw new HttpException('Floor not found', HttpStatus.NOT_FOUND); error.message || 'Internal server error',
} else { error.status || HttpStatus.INTERNAL_SERVER_ERROR,
throw new HttpException( );
error.message || 'Internal server error',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
} }
@ -73,14 +69,10 @@ export class FloorController {
); );
return floor; return floor;
} catch (error) { } catch (error) {
if (error.status === 404) { throw new HttpException(
throw new HttpException('Floor not found', HttpStatus.NOT_FOUND); error.message || 'Internal server error',
} else { error.status || HttpStatus.INTERNAL_SERVER_ERROR,
throw new HttpException( );
error.message || 'Internal server error',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
} }
@ApiBearerAuth() @ApiBearerAuth()
@ -91,14 +83,10 @@ export class FloorController {
const floor = await this.floorService.getFloorParentByUuid(floorUuid); const floor = await this.floorService.getFloorParentByUuid(floorUuid);
return floor; return floor;
} catch (error) { } catch (error) {
if (error.status === 404) { throw new HttpException(
throw new HttpException('Floor not found', HttpStatus.NOT_FOUND); error.message || 'Internal server error',
} else { error.status || HttpStatus.INTERNAL_SERVER_ERROR,
throw new HttpException( );
error.message || 'Internal server error',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
} }
@ -116,14 +104,10 @@ export class FloorController {
); );
return floor; return floor;
} catch (error) { } catch (error) {
if (error.status === 404) { throw new HttpException(
throw new HttpException('Floor not found', HttpStatus.NOT_FOUND); error.message || 'Internal server error',
} else { error.status || HttpStatus.INTERNAL_SERVER_ERROR,
throw new HttpException( );
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
} }
} }

View File

@ -53,9 +53,10 @@ export class FloorService {
}, },
relations: ['spaceType'], relations: ['spaceType'],
}); });
if (!floor) { if (!floor || !floor.spaceType || floor.spaceType.type !== 'floor') {
throw new HttpException('Floor not found', HttpStatus.NOT_FOUND); throw new BadRequestException('Invalid floor UUID');
} }
return { return {
uuid: floor.uuid, uuid: floor.uuid,
createdAt: floor.createdAt, createdAt: floor.createdAt,
@ -64,45 +65,53 @@ export class FloorService {
type: floor.spaceType.type, type: floor.spaceType.type,
}; };
} catch (err) { } catch (err) {
throw new HttpException( if (err instanceof BadRequestException) {
err.message, throw err; // Re-throw BadRequestException
err.status || HttpStatus.INTERNAL_SERVER_ERROR, } else {
); throw new HttpException('Floor not found', HttpStatus.NOT_FOUND);
}
} }
} }
async getFloorChildByUuid( async getFloorChildByUuid(
floorUuid: string, floorUuid: string,
getFloorChildDto: GetFloorChildDto, getFloorChildDto: GetFloorChildDto,
): Promise<FloorChildInterface> { ): Promise<FloorChildInterface> {
const { includeSubSpaces, page, pageSize } = getFloorChildDto; try {
const { includeSubSpaces, page, pageSize } = getFloorChildDto;
const space = await this.spaceRepository.findOneOrFail({ const space = await this.spaceRepository.findOneOrFail({
where: { uuid: floorUuid }, where: { uuid: floorUuid },
relations: ['children', 'spaceType'], relations: ['children', 'spaceType'],
}); });
if (space.spaceType.type !== 'floor') { if (!space || !space.spaceType || space.spaceType.type !== 'floor') {
throw new HttpException('Floor not found', HttpStatus.NOT_FOUND); throw new BadRequestException('Invalid floor UUID');
}
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,
};
} catch (err) {
if (err instanceof BadRequestException) {
throw err; // Re-throw BadRequestException
} else {
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( private async buildHierarchy(
@ -163,8 +172,8 @@ export class FloorService {
}, },
relations: ['spaceType', 'parent', 'parent.spaceType'], relations: ['spaceType', 'parent', 'parent.spaceType'],
}); });
if (!floor) { if (!floor || !floor.spaceType || floor.spaceType.type !== 'floor') {
throw new HttpException('Floor not found', HttpStatus.NOT_FOUND); throw new BadRequestException('Invalid floor UUID');
} }
return { return {
@ -178,10 +187,11 @@ export class FloorService {
}, },
}; };
} catch (err) { } catch (err) {
throw new HttpException( if (err instanceof BadRequestException) {
err.message, throw err; // Re-throw BadRequestException
err.status || HttpStatus.INTERNAL_SERVER_ERROR, } else {
); throw new HttpException('Floor not found', HttpStatus.NOT_FOUND);
}
} }
} }

View File

@ -33,7 +33,7 @@ export class RoomController {
} catch (error) { } catch (error) {
throw new HttpException( throw new HttpException(
error.message || 'Internal server error', error.message || 'Internal server error',
HttpStatus.INTERNAL_SERVER_ERROR, error.status || HttpStatus.INTERNAL_SERVER_ERROR,
); );
} }
} }
@ -46,14 +46,10 @@ export class RoomController {
const room = await this.roomService.getRoomByUuid(roomUuid); const room = await this.roomService.getRoomByUuid(roomUuid);
return room; return room;
} catch (error) { } catch (error) {
if (error.status === 404) { throw new HttpException(
throw new HttpException('Room not found', HttpStatus.NOT_FOUND); error.message || 'Internal server error',
} else { error.status || HttpStatus.INTERNAL_SERVER_ERROR,
throw new HttpException( );
error.message || 'Internal server error',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
} }
@ -65,14 +61,10 @@ export class RoomController {
const room = await this.roomService.getRoomParentByUuid(roomUuid); const room = await this.roomService.getRoomParentByUuid(roomUuid);
return room; return room;
} catch (error) { } catch (error) {
if (error.status === 404) { throw new HttpException(
throw new HttpException('Room not found', HttpStatus.NOT_FOUND); error.message || 'Internal server error',
} else { error.status || HttpStatus.INTERNAL_SERVER_ERROR,
throw new HttpException( );
error.message || 'Internal server error',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
} }
@ -90,14 +82,10 @@ export class RoomController {
); );
return room; return room;
} catch (error) { } catch (error) {
if (error.status === 404) { throw new HttpException(
throw new HttpException('Room not found', HttpStatus.NOT_FOUND); error.message || 'Internal server error',
} else { error.status || HttpStatus.INTERNAL_SERVER_ERROR,
throw new HttpException( );
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
} }
} }

View File

@ -50,9 +50,10 @@ export class RoomService {
}, },
relations: ['spaceType'], relations: ['spaceType'],
}); });
if (!room) { if (!room || !room.spaceType || room.spaceType.type !== 'room') {
throw new HttpException('Room not found', HttpStatus.NOT_FOUND); throw new BadRequestException('Invalid room UUID');
} }
return { return {
uuid: room.uuid, uuid: room.uuid,
createdAt: room.createdAt, createdAt: room.createdAt,
@ -61,10 +62,11 @@ export class RoomService {
type: room.spaceType.type, type: room.spaceType.type,
}; };
} catch (err) { } catch (err) {
throw new HttpException( if (err instanceof BadRequestException) {
err.message, throw err; // Re-throw BadRequestException
err.status || HttpStatus.INTERNAL_SERVER_ERROR, } else {
); throw new HttpException('Room not found', HttpStatus.NOT_FOUND);
}
} }
} }
@ -79,8 +81,8 @@ export class RoomService {
}, },
relations: ['spaceType', 'parent', 'parent.spaceType'], relations: ['spaceType', 'parent', 'parent.spaceType'],
}); });
if (!room) { if (!room || !room.spaceType || room.spaceType.type !== 'room') {
throw new HttpException('Room not found', HttpStatus.NOT_FOUND); throw new BadRequestException('Invalid room UUID');
} }
return { return {
@ -94,10 +96,11 @@ export class RoomService {
}, },
}; };
} catch (err) { } catch (err) {
throw new HttpException( if (err instanceof BadRequestException) {
err.message, throw err; // Re-throw BadRequestException
err.status || HttpStatus.INTERNAL_SERVER_ERROR, } else {
); throw new HttpException('Room not found', HttpStatus.NOT_FOUND);
}
} }
} }
async renameRoomByUuid( async renameRoomByUuid(

View File

@ -35,7 +35,7 @@ export class UnitController {
} catch (error) { } catch (error) {
throw new HttpException( throw new HttpException(
error.message || 'Internal server error', error.message || 'Internal server error',
HttpStatus.INTERNAL_SERVER_ERROR, error.status || HttpStatus.INTERNAL_SERVER_ERROR,
); );
} }
} }
@ -48,14 +48,10 @@ export class UnitController {
const unit = await this.unitService.getUnitByUuid(unitUuid); const unit = await this.unitService.getUnitByUuid(unitUuid);
return unit; return unit;
} catch (error) { } catch (error) {
if (error.status === 404) { throw new HttpException(
throw new HttpException('Unit not found', HttpStatus.NOT_FOUND); error.message || 'Internal server error',
} else { error.status || HttpStatus.INTERNAL_SERVER_ERROR,
throw new HttpException( );
error.message || 'Internal server error',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
} }
@ -70,14 +66,10 @@ export class UnitController {
const unit = await this.unitService.getUnitChildByUuid(unitUuid, query); const unit = await this.unitService.getUnitChildByUuid(unitUuid, query);
return unit; return unit;
} catch (error) { } catch (error) {
if (error.status === 404) { throw new HttpException(
throw new HttpException('Unit not found', HttpStatus.NOT_FOUND); error.message || 'Internal server error',
} else { error.status || HttpStatus.INTERNAL_SERVER_ERROR,
throw new HttpException( );
error.message || 'Internal server error',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
} }
@ApiBearerAuth() @ApiBearerAuth()
@ -88,14 +80,10 @@ export class UnitController {
const unit = await this.unitService.getUnitParentByUuid(unitUuid); const unit = await this.unitService.getUnitParentByUuid(unitUuid);
return unit; return unit;
} catch (error) { } catch (error) {
if (error.status === 404) { throw new HttpException(
throw new HttpException('Unit not found', HttpStatus.NOT_FOUND); error.message || 'Internal server error',
} else { error.status || HttpStatus.INTERNAL_SERVER_ERROR,
throw new HttpException( );
error.message || 'Internal server error',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
} }
@ -113,14 +101,10 @@ export class UnitController {
); );
return unit; return unit;
} catch (error) { } catch (error) {
if (error.status === 404) { throw new HttpException(
throw new HttpException('Unit not found', HttpStatus.NOT_FOUND); error.message || 'Internal server error',
} else { error.status || HttpStatus.INTERNAL_SERVER_ERROR,
throw new HttpException( );
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
} }
} }

View File

@ -53,8 +53,8 @@ export class UnitService {
}, },
relations: ['spaceType'], relations: ['spaceType'],
}); });
if (!unit) { if (!unit || !unit.spaceType || unit.spaceType.type !== 'unit') {
throw new HttpException('Unit not found', HttpStatus.NOT_FOUND); throw new BadRequestException('Invalid unit UUID');
} }
return { return {
uuid: unit.uuid, uuid: unit.uuid,
@ -64,45 +64,54 @@ export class UnitService {
type: unit.spaceType.type, type: unit.spaceType.type,
}; };
} catch (err) { } catch (err) {
throw new HttpException( if (err instanceof BadRequestException) {
err.message, throw err; // Re-throw BadRequestException
err.status || HttpStatus.INTERNAL_SERVER_ERROR, } else {
); throw new HttpException('Unit not found', HttpStatus.NOT_FOUND);
}
} }
} }
async getUnitChildByUuid( async getUnitChildByUuid(
unitUuid: string, unitUuid: string,
getUnitChildDto: GetUnitChildDto, getUnitChildDto: GetUnitChildDto,
): Promise<UnitChildInterface> { ): Promise<UnitChildInterface> {
const { includeSubSpaces, page, pageSize } = getUnitChildDto; try {
const { includeSubSpaces, page, pageSize } = getUnitChildDto;
const space = await this.spaceRepository.findOneOrFail({ const space = await this.spaceRepository.findOneOrFail({
where: { uuid: unitUuid }, where: { uuid: unitUuid },
relations: ['children', 'spaceType'], relations: ['children', 'spaceType'],
}); });
if (space.spaceType.type !== 'unit') { if (!space || !space.spaceType || space.spaceType.type !== 'unit') {
throw new HttpException('Unit not found', HttpStatus.NOT_FOUND); throw new BadRequestException('Invalid unit UUID');
}
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,
};
} catch (err) {
if (err instanceof BadRequestException) {
throw err; // Re-throw BadRequestException
} else {
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( private async buildHierarchy(
@ -165,10 +174,9 @@ export class UnitService {
}, },
relations: ['spaceType', 'parent', 'parent.spaceType'], relations: ['spaceType', 'parent', 'parent.spaceType'],
}); });
if (!unit) { if (!unit || !unit.spaceType || unit.spaceType.type !== 'unit') {
throw new HttpException('Unit not found', HttpStatus.NOT_FOUND); throw new BadRequestException('Invalid unit UUID');
} }
return { return {
uuid: unit.uuid, uuid: unit.uuid,
name: unit.spaceName, name: unit.spaceName,
@ -180,10 +188,11 @@ export class UnitService {
}, },
}; };
} catch (err) { } catch (err) {
throw new HttpException( if (err instanceof BadRequestException) {
err.message, throw err; // Re-throw BadRequestException
err.status || HttpStatus.INTERNAL_SERVER_ERROR, } else {
); throw new HttpException('Unit not found', HttpStatus.NOT_FOUND);
}
} }
} }