Merge pull request #110 from SyncrowIOT/exception-handling-in-controllers

Exception handling in controllers
This commit is contained in:
yousef-khriasat-uba
2024-10-08 10:57:26 +03:00
committed by GitHub
28 changed files with 672 additions and 1326 deletions

View File

@ -6,10 +6,16 @@ import { AuthModule } from './auth/auth.module';
import { ConfigModule } from '@nestjs/config'; import { ConfigModule } from '@nestjs/config';
import config from './config'; import config from './config';
import { EmailService } from './util/email.service'; import { EmailService } from './util/email.service';
import { ErrorMessageService } from 'src/error-message/error-message.service';
@Module({ @Module({
providers: [CommonService, EmailService], providers: [CommonService, EmailService, ErrorMessageService],
exports: [CommonService, HelperModule, AuthModule, EmailService], exports: [
CommonService,
HelperModule,
AuthModule,
EmailService,
ErrorMessageService,
],
imports: [ imports: [
ConfigModule.forRoot({ ConfigModule.forRoot({
load: config, load: config,

View File

@ -18,6 +18,7 @@ import * as argon2 from 'argon2';
import { differenceInSeconds } from '@app/common/helper/differenceInSeconds'; import { differenceInSeconds } from '@app/common/helper/differenceInSeconds';
import { LessThan, MoreThan } from 'typeorm'; import { LessThan, MoreThan } from 'typeorm';
import { ConfigService } from '@nestjs/config'; import { ConfigService } from '@nestjs/config';
import { UUID } from 'typeorm/driver/mongodb/bson.typings';
@Injectable() @Injectable()
export class UserAuthService { export class UserAuthService {

View File

@ -4,7 +4,6 @@ import {
Controller, Controller,
Delete, Delete,
Get, Get,
HttpException,
HttpStatus, HttpStatus,
Param, Param,
Post, Post,
@ -31,52 +30,30 @@ export class AutomationController {
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@Post() @Post()
async addAutomation(@Body() addAutomationDto: AddAutomationDto) { async addAutomation(@Body() addAutomationDto: AddAutomationDto) {
try { const automation =
const automation = await this.automationService.addAutomation(addAutomationDto);
await this.automationService.addAutomation(addAutomationDto); return {
return { statusCode: HttpStatus.CREATED,
statusCode: HttpStatus.CREATED, success: true,
success: true, message: 'Automation added successfully',
message: 'Automation added successfully', data: automation,
data: automation, };
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@Get(':unitUuid') @Get(':unitUuid')
async getAutomationByUnit(@Param('unitUuid') unitUuid: string) { async getAutomationByUnit(@Param('unitUuid') unitUuid: string) {
try { const automation =
const automation = await this.automationService.getAutomationByUnit(unitUuid);
await this.automationService.getAutomationByUnit(unitUuid); return automation;
return automation;
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@Get('details/:automationId') @Get('details/:automationId')
async getAutomationDetails(@Param('automationId') automationId: string) { async getAutomationDetails(@Param('automationId') automationId: string) {
try { const automation =
const automation = await this.automationService.getAutomationDetails(automationId);
await this.automationService.getAutomationDetails(automationId); return automation;
return automation;
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
``;
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@ -85,18 +62,11 @@ export class AutomationController {
@Param('unitUuid') unitUuid: string, @Param('unitUuid') unitUuid: string,
@Param('automationId') automationId: string, @Param('automationId') automationId: string,
) { ) {
try { await this.automationService.deleteAutomation(unitUuid, automationId);
await this.automationService.deleteAutomation(unitUuid, automationId); return {
return { statusCode: HttpStatus.OK,
statusCode: HttpStatus.OK, message: 'Automation Deleted Successfully',
message: 'Automation Deleted Successfully', };
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@ -105,23 +75,16 @@ export class AutomationController {
@Body() updateAutomationDto: UpdateAutomationDto, @Body() updateAutomationDto: UpdateAutomationDto,
@Param('automationId') automationId: string, @Param('automationId') automationId: string,
) { ) {
try { const automation = await this.automationService.updateAutomation(
const automation = await this.automationService.updateAutomation( updateAutomationDto,
updateAutomationDto, automationId,
automationId, );
); return {
return { statusCode: HttpStatus.CREATED,
statusCode: HttpStatus.CREATED, success: true,
success: true, message: 'Automation updated successfully',
message: 'Automation updated successfully', data: automation,
data: automation, };
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@ -130,21 +93,14 @@ export class AutomationController {
@Body() updateAutomationStatusDto: UpdateAutomationStatusDto, @Body() updateAutomationStatusDto: UpdateAutomationStatusDto,
@Param('automationId') automationId: string, @Param('automationId') automationId: string,
) { ) {
try { await this.automationService.updateAutomationStatus(
await this.automationService.updateAutomationStatus( updateAutomationStatusDto,
updateAutomationStatusDto, automationId,
automationId, );
); return {
return { statusCode: HttpStatus.CREATED,
statusCode: HttpStatus.CREATED, success: true,
success: true, message: 'Automation status updated successfully',
message: 'Automation status updated successfully', };
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
} }

View File

@ -3,7 +3,6 @@ import {
Body, Body,
Controller, Controller,
Get, Get,
HttpException,
HttpStatus, HttpStatus,
Param, Param,
Post, Post,
@ -33,36 +32,21 @@ export class BuildingController {
@UseGuards(JwtAuthGuard, CheckCommunityTypeGuard) @UseGuards(JwtAuthGuard, CheckCommunityTypeGuard)
@Post() @Post()
async addBuilding(@Body() addBuildingDto: AddBuildingDto) { async addBuilding(@Body() addBuildingDto: AddBuildingDto) {
try { const building = await this.buildingService.addBuilding(addBuildingDto);
const building = await this.buildingService.addBuilding(addBuildingDto); return {
return { statusCode: HttpStatus.CREATED,
statusCode: HttpStatus.CREATED, success: true,
success: true, message: 'Building added successfully',
message: 'Building added successfully', data: building,
data: building, };
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard, BuildingPermissionGuard) @UseGuards(JwtAuthGuard, BuildingPermissionGuard)
@Get(':buildingUuid') @Get(':buildingUuid')
async getBuildingByUuid(@Param('buildingUuid') buildingUuid: string) { async getBuildingByUuid(@Param('buildingUuid') buildingUuid: string) {
try { const building = await this.buildingService.getBuildingByUuid(buildingUuid);
const building = return building;
await this.buildingService.getBuildingByUuid(buildingUuid);
return building;
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@ -72,64 +56,36 @@ export class BuildingController {
@Param('buildingUuid') buildingUuid: string, @Param('buildingUuid') buildingUuid: string,
@Query() query: GetBuildingChildDto, @Query() query: GetBuildingChildDto,
) { ) {
try { const building = await this.buildingService.getBuildingChildByUuid(
const building = await this.buildingService.getBuildingChildByUuid( buildingUuid,
buildingUuid, query,
query, );
); return building;
return building;
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard, BuildingPermissionGuard) @UseGuards(JwtAuthGuard, BuildingPermissionGuard)
@Get('parent/:buildingUuid') @Get('parent/:buildingUuid')
async getBuildingParentByUuid(@Param('buildingUuid') buildingUuid: string) { async getBuildingParentByUuid(@Param('buildingUuid') buildingUuid: string) {
try { const building =
const building = await this.buildingService.getBuildingParentByUuid(buildingUuid);
await this.buildingService.getBuildingParentByUuid(buildingUuid); return building;
return building;
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(AdminRoleGuard, CheckUserBuildingGuard) @UseGuards(AdminRoleGuard, CheckUserBuildingGuard)
@Post('user') @Post('user')
async addUserBuilding(@Body() addUserBuildingDto: AddUserBuildingDto) { async addUserBuilding(@Body() addUserBuildingDto: AddUserBuildingDto) {
try { await this.buildingService.addUserBuilding(addUserBuildingDto);
await this.buildingService.addUserBuilding(addUserBuildingDto); return {
return { statusCode: HttpStatus.CREATED,
statusCode: HttpStatus.CREATED, success: true,
success: true, message: 'user building added successfully',
message: 'user building added successfully', };
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@Get('user/:userUuid') @Get('user/:userUuid')
async getBuildingsByUserId(@Param('userUuid') userUuid: string) { async getBuildingsByUserId(@Param('userUuid') userUuid: string) {
try { return await this.buildingService.getBuildingsByUserId(userUuid);
return await this.buildingService.getBuildingsByUserId(userUuid);
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@ -139,17 +95,10 @@ export class BuildingController {
@Param('buildingUuid') buildingUuid: string, @Param('buildingUuid') buildingUuid: string,
@Body() updateBuildingDto: UpdateBuildingNameDto, @Body() updateBuildingDto: UpdateBuildingNameDto,
) { ) {
try { const building = await this.buildingService.renameBuildingByUuid(
const building = await this.buildingService.renameBuildingByUuid( buildingUuid,
buildingUuid, updateBuildingDto,
updateBuildingDto, );
); return building;
return building;
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
} }

View File

@ -0,0 +1,7 @@
import { HttpExceptionFilter } from './http-exception.filter';
describe('HttpExceptionFilter', () => {
it('should be defined', () => {
expect(new HttpExceptionFilter()).toBeDefined();
});
});

View File

@ -0,0 +1,38 @@
import {
ExceptionFilter,
Catch,
ArgumentsHost,
HttpException,
HttpStatus,
} from '@nestjs/common';
import { Response } from 'express';
@Catch()
export class HttpExceptionFilter implements ExceptionFilter {
catch(exception: unknown, host: ArgumentsHost) {
const ctx = host.switchToHttp();
const response = ctx.getResponse<Response>();
const request = ctx.getRequest<Request>();
const status =
exception instanceof HttpException
? exception.getStatus()
: HttpStatus.INTERNAL_SERVER_ERROR;
const message =
exception instanceof HttpException
? exception.getResponse()
: 'Internal server error';
const errorResponse = {
statusCode: status,
timestamp: new Date().toISOString(),
path: request.url,
error: message,
};
// Optionally log the exception
console.error(`Error occurred:`, exception);
response.status(status).json(errorResponse);
}
}

View File

@ -3,7 +3,6 @@ import {
Body, Body,
Controller, Controller,
Get, Get,
HttpException,
HttpStatus, HttpStatus,
Param, Param,
Post, Post,
@ -33,51 +32,29 @@ export class CommunityController {
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@Post() @Post()
async addCommunity(@Body() addCommunityDto: AddCommunityDto) { async addCommunity(@Body() addCommunityDto: AddCommunityDto) {
try { const community = await this.communityService.addCommunity(addCommunityDto);
const community = return {
await this.communityService.addCommunity(addCommunityDto); statusCode: HttpStatus.CREATED,
return { success: true,
statusCode: HttpStatus.CREATED, message: 'Community added successfully',
success: true, data: community,
message: 'Community added successfully', };
data: community,
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@Get(':communityUuid') @Get(':communityUuid')
async getCommunityByUuid(@Param('communityUuid') communityUuid: string) { async getCommunityByUuid(@Param('communityUuid') communityUuid: string) {
try { const community =
const community = await this.communityService.getCommunityByUuid(communityUuid);
await this.communityService.getCommunityByUuid(communityUuid); return community;
return community;
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@Get() @Get()
async getCommunities() { async getCommunities() {
try { const communities = await this.communityService.getCommunities();
const communities = await this.communityService.getCommunities(); return communities;
return communities;
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@ -86,50 +63,29 @@ export class CommunityController {
@Param('communityUuid') communityUuid: string, @Param('communityUuid') communityUuid: string,
@Query() query: GetCommunityChildDto, @Query() query: GetCommunityChildDto,
) { ) {
try { const community = await this.communityService.getCommunityChildByUuid(
const community = await this.communityService.getCommunityChildByUuid( communityUuid,
communityUuid, query,
query, );
); return community;
return community;
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@Get('user/:userUuid') @Get('user/:userUuid')
async getCommunitiesByUserId(@Param('userUuid') userUuid: string) { async getCommunitiesByUserId(@Param('userUuid') userUuid: string) {
try { return await this.communityService.getCommunitiesByUserId(userUuid);
return await this.communityService.getCommunitiesByUserId(userUuid);
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(AdminRoleGuard) @UseGuards(AdminRoleGuard)
@Post('user') @Post('user')
async addUserCommunity(@Body() addUserCommunityDto: AddUserCommunityDto) { async addUserCommunity(@Body() addUserCommunityDto: AddUserCommunityDto) {
try { await this.communityService.addUserCommunity(addUserCommunityDto);
await this.communityService.addUserCommunity(addUserCommunityDto); return {
return { statusCode: HttpStatus.CREATED,
statusCode: HttpStatus.CREATED, success: true,
success: true, message: 'user community added successfully',
message: 'user community added successfully', };
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@ -138,17 +94,10 @@ export class CommunityController {
@Param('communityUuid') communityUuid: string, @Param('communityUuid') communityUuid: string,
@Body() updateCommunityDto: UpdateCommunityNameDto, @Body() updateCommunityDto: UpdateCommunityNameDto,
) { ) {
try { const community = await this.communityService.renameCommunityByUuid(
const community = await this.communityService.renameCommunityByUuid( communityUuid,
communityUuid, updateCommunityDto,
updateCommunityDto, );
); return community;
return community;
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
} }

View File

@ -3,7 +3,6 @@ import {
Controller, Controller,
Delete, Delete,
Get, Get,
HttpException,
HttpStatus, HttpStatus,
Param, Param,
Post, Post,
@ -31,22 +30,15 @@ export class DeviceMessagesSubscriptionController {
async addDeviceMessagesSubscription( async addDeviceMessagesSubscription(
@Body() deviceMessagesAddDto: DeviceMessagesAddDto, @Body() deviceMessagesAddDto: DeviceMessagesAddDto,
) { ) {
try { const addDetails =
const addDetails = await this.deviceMessagesSubscriptionService.addDeviceMessagesSubscription(
await this.deviceMessagesSubscriptionService.addDeviceMessagesSubscription( deviceMessagesAddDto,
deviceMessagesAddDto,
);
return {
statusCode: HttpStatus.CREATED,
message: 'Device Messages Subscription Added Successfully',
data: addDetails,
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
); );
} return {
statusCode: HttpStatus.CREATED,
message: 'Device Messages Subscription Added Successfully',
data: addDetails,
};
} }
@ApiBearerAuth() @ApiBearerAuth()
@ -56,23 +48,16 @@ export class DeviceMessagesSubscriptionController {
@Param('deviceUuid') deviceUuid: string, @Param('deviceUuid') deviceUuid: string,
@Param('userUuid') userUuid: string, @Param('userUuid') userUuid: string,
) { ) {
try { const deviceDetails =
const deviceDetails = await this.deviceMessagesSubscriptionService.getDeviceMessagesSubscription(
await this.deviceMessagesSubscriptionService.getDeviceMessagesSubscription( userUuid,
userUuid, deviceUuid,
deviceUuid,
);
return {
statusCode: HttpStatus.OK,
message: 'User Device Subscription fetched Successfully',
data: deviceDetails,
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
); );
} return {
statusCode: HttpStatus.OK,
message: 'User Device Subscription fetched Successfully',
data: deviceDetails,
};
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@ -80,19 +65,12 @@ export class DeviceMessagesSubscriptionController {
async deleteDeviceMessagesSubscription( async deleteDeviceMessagesSubscription(
@Body() deviceMessagesAddDto: DeviceMessagesAddDto, @Body() deviceMessagesAddDto: DeviceMessagesAddDto,
) { ) {
try { await this.deviceMessagesSubscriptionService.deleteDeviceMessagesSubscription(
await this.deviceMessagesSubscriptionService.deleteDeviceMessagesSubscription( deviceMessagesAddDto,
deviceMessagesAddDto, );
); return {
return { statusCode: HttpStatus.OK,
statusCode: HttpStatus.OK, message: 'User subscription deleted Successfully',
message: 'User subscription deleted Successfully', };
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
} }

View File

@ -6,7 +6,6 @@ import {
Post, Post,
Query, Query,
Param, Param,
HttpException,
HttpStatus, HttpStatus,
UseGuards, UseGuards,
Req, Req,
@ -42,34 +41,20 @@ export class DeviceController {
@UseGuards(SuperAdminRoleGuard, CheckDeviceGuard) @UseGuards(SuperAdminRoleGuard, CheckDeviceGuard)
@Post() @Post()
async addDeviceUser(@Body() addDeviceDto: AddDeviceDto) { async addDeviceUser(@Body() addDeviceDto: AddDeviceDto) {
try { const device = await this.deviceService.addDeviceUser(addDeviceDto);
const device = await this.deviceService.addDeviceUser(addDeviceDto);
return { return {
statusCode: HttpStatus.CREATED, statusCode: HttpStatus.CREATED,
success: true, success: true,
message: 'device added successfully', message: 'device added successfully',
data: device, data: device,
}; };
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@Get('user/:userUuid') @Get('user/:userUuid')
async getDevicesByUser(@Param('userUuid') userUuid: string) { async getDevicesByUser(@Param('userUuid') userUuid: string) {
try { return await this.deviceService.getDevicesByUser(userUuid);
return await this.deviceService.getDevicesByUser(userUuid);
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard, CheckRoomGuard) @UseGuards(JwtAuthGuard, CheckRoomGuard)
@ -78,31 +63,17 @@ export class DeviceController {
@Query() getDeviceByRoomUuidDto: GetDeviceByRoomUuidDto, @Query() getDeviceByRoomUuidDto: GetDeviceByRoomUuidDto,
@Req() req: any, @Req() req: any,
) { ) {
try { const userUuid = req.user.uuid;
const userUuid = req.user.uuid; return await this.deviceService.getDevicesByRoomId(
return await this.deviceService.getDevicesByRoomId( getDeviceByRoomUuidDto,
getDeviceByRoomUuidDto, userUuid,
userUuid, );
);
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@Get('unit/:unitUuid') @Get('unit/:unitUuid')
async getDevicesByUnitId(@Param('unitUuid') unitUuid: string) { async getDevicesByUnitId(@Param('unitUuid') unitUuid: string) {
try { return await this.deviceService.getDevicesByUnitId(unitUuid);
return await this.deviceService.getDevicesByUnitId(unitUuid);
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard, CheckRoomGuard) @UseGuards(JwtAuthGuard, CheckRoomGuard)
@ -110,23 +81,16 @@ export class DeviceController {
async updateDeviceInRoom( async updateDeviceInRoom(
@Body() updateDeviceInRoomDto: UpdateDeviceInRoomDto, @Body() updateDeviceInRoomDto: UpdateDeviceInRoomDto,
) { ) {
try { const device = await this.deviceService.updateDeviceInRoom(
const device = await this.deviceService.updateDeviceInRoom( updateDeviceInRoomDto,
updateDeviceInRoomDto, );
);
return { return {
statusCode: HttpStatus.CREATED, statusCode: HttpStatus.CREATED,
success: true, success: true,
message: 'device updated in room successfully', message: 'device updated in room successfully',
data: device, data: device,
}; };
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@ -136,18 +100,11 @@ export class DeviceController {
@Param('deviceUuid') deviceUuid: string, @Param('deviceUuid') deviceUuid: string,
@Req() req: any, @Req() req: any,
) { ) {
try { const userUuid = req.user.uuid;
const userUuid = req.user.uuid; return await this.deviceService.getDeviceDetailsByDeviceId(
return await this.deviceService.getDeviceDetailsByDeviceId( deviceUuid,
deviceUuid, userUuid,
userUuid, );
);
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard, CheckUserHavePermission) @UseGuards(JwtAuthGuard, CheckUserHavePermission)
@ -155,29 +112,13 @@ export class DeviceController {
async getDeviceInstructionByDeviceId( async getDeviceInstructionByDeviceId(
@Param('deviceUuid') deviceUuid: string, @Param('deviceUuid') deviceUuid: string,
) { ) {
try { return await this.deviceService.getDeviceInstructionByDeviceId(deviceUuid);
return await this.deviceService.getDeviceInstructionByDeviceId(
deviceUuid,
);
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard, CheckUserHavePermission) @UseGuards(JwtAuthGuard, CheckUserHavePermission)
@Get(':deviceUuid/functions/status') @Get(':deviceUuid/functions/status')
async getDevicesInstructionStatus(@Param('deviceUuid') deviceUuid: string) { async getDevicesInstructionStatus(@Param('deviceUuid') deviceUuid: string) {
try { return await this.deviceService.getDevicesInstructionStatus(deviceUuid);
return await this.deviceService.getDevicesInstructionStatus(deviceUuid);
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@ -187,17 +128,7 @@ export class DeviceController {
@Body() controlDeviceDto: ControlDeviceDto, @Body() controlDeviceDto: ControlDeviceDto,
@Param('deviceUuid') deviceUuid: string, @Param('deviceUuid') deviceUuid: string,
) { ) {
try { return await this.deviceService.controlDevice(controlDeviceDto, deviceUuid);
return await this.deviceService.controlDevice(
controlDeviceDto,
deviceUuid,
);
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@ -206,43 +137,22 @@ export class DeviceController {
@Param('deviceUuid') deviceUuid: string, @Param('deviceUuid') deviceUuid: string,
@Param('firmwareVersion') firmwareVersion: number, @Param('firmwareVersion') firmwareVersion: number,
) { ) {
try { return await this.deviceService.updateDeviceFirmware(
return await this.deviceService.updateDeviceFirmware( deviceUuid,
deviceUuid, firmwareVersion,
firmwareVersion, );
);
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@Get('gateway/:gatewayUuid/devices') @Get('gateway/:gatewayUuid/devices')
async getDevicesInGateway(@Param('gatewayUuid') gatewayUuid: string) { async getDevicesInGateway(@Param('gatewayUuid') gatewayUuid: string) {
try { return await this.deviceService.getDevicesInGateway(gatewayUuid);
return await this.deviceService.getDevicesInGateway(gatewayUuid);
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@Get() @Get()
async getAllDevices() { async getAllDevices() {
try { return await this.deviceService.getAllDevices();
return await this.deviceService.getAllDevices();
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@ -251,14 +161,7 @@ export class DeviceController {
@Param('deviceUuid') deviceUuid: string, @Param('deviceUuid') deviceUuid: string,
@Query() query: GetDeviceLogsDto, @Query() query: GetDeviceLogsDto,
) { ) {
try { return await this.deviceService.getDeviceLogs(deviceUuid, query);
return await this.deviceService.getDeviceLogs(deviceUuid, query);
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@ -266,16 +169,7 @@ export class DeviceController {
async batchControlDevices( async batchControlDevices(
@Body() batchControlDevicesDto: BatchControlDevicesDto, @Body() batchControlDevicesDto: BatchControlDevicesDto,
) { ) {
try { return await this.deviceService.batchControlDevices(batchControlDevicesDto);
return await this.deviceService.batchControlDevices(
batchControlDevicesDto,
);
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@ -283,14 +177,7 @@ export class DeviceController {
async batchStatusDevices( async batchStatusDevices(
@Query() batchStatusDevicesDto: BatchStatusDevicesDto, @Query() batchStatusDevicesDto: BatchStatusDevicesDto,
) { ) {
try { return await this.deviceService.batchStatusDevices(batchStatusDevicesDto);
return await this.deviceService.batchStatusDevices(batchStatusDevicesDto);
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@ -298,15 +185,8 @@ export class DeviceController {
async batchFactoryResetDevices( async batchFactoryResetDevices(
@Body() batchFactoryResetDevicesDto: BatchFactoryResetDevicesDto, @Body() batchFactoryResetDevicesDto: BatchFactoryResetDevicesDto,
) { ) {
try { return await this.deviceService.batchFactoryResetDevices(
return await this.deviceService.batchFactoryResetDevices( batchFactoryResetDevicesDto,
batchFactoryResetDevicesDto, );
);
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
} }

View File

@ -4,7 +4,6 @@ import {
Controller, Controller,
Post, Post,
Param, Param,
HttpException,
HttpStatus, HttpStatus,
Get, Get,
Delete, Delete,
@ -31,27 +30,20 @@ export class DoorLockController {
@Body() addDoorLockDto: AddDoorLockOnlineDto, @Body() addDoorLockDto: AddDoorLockOnlineDto,
@Param('doorLockUuid') doorLockUuid: string, @Param('doorLockUuid') doorLockUuid: string,
) { ) {
try { const temporaryPassword =
const temporaryPassword = await this.doorLockService.addOnlineTemporaryPassword(
await this.doorLockService.addOnlineTemporaryPassword( addDoorLockDto,
addDoorLockDto, doorLockUuid,
doorLockUuid,
);
return {
statusCode: HttpStatus.CREATED,
success: true,
message: 'online temporary password added successfully',
data: {
id: temporaryPassword.id,
},
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
); );
}
return {
statusCode: HttpStatus.CREATED,
success: true,
message: 'online temporary password added successfully',
data: {
id: temporaryPassword.id,
},
};
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@ -59,24 +51,17 @@ export class DoorLockController {
async addOfflineOneTimeTemporaryPassword( async addOfflineOneTimeTemporaryPassword(
@Param('doorLockUuid') doorLockUuid: string, @Param('doorLockUuid') doorLockUuid: string,
) { ) {
try { const temporaryPassword =
const temporaryPassword = await this.doorLockService.addOfflineOneTimeTemporaryPassword(
await this.doorLockService.addOfflineOneTimeTemporaryPassword( doorLockUuid,
doorLockUuid,
);
return {
statusCode: HttpStatus.CREATED,
success: true,
message: 'offline temporary password added successfully',
data: temporaryPassword,
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
); );
}
return {
statusCode: HttpStatus.CREATED,
success: true,
message: 'offline temporary password added successfully',
data: temporaryPassword,
};
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@ -86,25 +71,18 @@ export class DoorLockController {
addDoorLockOfflineTempMultipleTimeDto: AddDoorLockOfflineTempMultipleTimeDto, addDoorLockOfflineTempMultipleTimeDto: AddDoorLockOfflineTempMultipleTimeDto,
@Param('doorLockUuid') doorLockUuid: string, @Param('doorLockUuid') doorLockUuid: string,
) { ) {
try { const temporaryPassword =
const temporaryPassword = await this.doorLockService.addOfflineMultipleTimeTemporaryPassword(
await this.doorLockService.addOfflineMultipleTimeTemporaryPassword( addDoorLockOfflineTempMultipleTimeDto,
addDoorLockOfflineTempMultipleTimeDto, doorLockUuid,
doorLockUuid,
);
return {
statusCode: HttpStatus.CREATED,
success: true,
message: 'offline temporary password added successfully',
data: temporaryPassword,
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
); );
}
return {
statusCode: HttpStatus.CREATED,
success: true,
message: 'offline temporary password added successfully',
data: temporaryPassword,
};
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@ -112,16 +90,9 @@ export class DoorLockController {
async getOnlineTemporaryPasswords( async getOnlineTemporaryPasswords(
@Param('doorLockUuid') doorLockUuid: string, @Param('doorLockUuid') doorLockUuid: string,
) { ) {
try { return await this.doorLockService.getOnlineTemporaryPasswordsMultiple(
return await this.doorLockService.getOnlineTemporaryPasswordsMultiple( doorLockUuid,
doorLockUuid, );
);
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@ -130,21 +101,11 @@ export class DoorLockController {
@Param('doorLockUuid') doorLockUuid: string, @Param('doorLockUuid') doorLockUuid: string,
@Param('passwordId') passwordId: string, @Param('passwordId') passwordId: string,
) { ) {
try { await this.doorLockService.deleteDoorLockPassword(doorLockUuid, passwordId);
await this.doorLockService.deleteDoorLockPassword( return {
doorLockUuid, statusCode: HttpStatus.OK,
passwordId, message: 'Temporary Password deleted Successfully',
); };
return {
statusCode: HttpStatus.OK,
message: 'Temporary Password deleted Successfully',
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@ -152,16 +113,9 @@ export class DoorLockController {
async getOfflineOneTimeTemporaryPasswords( async getOfflineOneTimeTemporaryPasswords(
@Param('doorLockUuid') doorLockUuid: string, @Param('doorLockUuid') doorLockUuid: string,
) { ) {
try { return await this.doorLockService.getOfflineOneTimeTemporaryPasswords(
return await this.doorLockService.getOfflineOneTimeTemporaryPasswords( doorLockUuid,
doorLockUuid, );
);
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@ -169,16 +123,9 @@ export class DoorLockController {
async getOfflineMultipleTimeTemporaryPasswords( async getOfflineMultipleTimeTemporaryPasswords(
@Param('doorLockUuid') doorLockUuid: string, @Param('doorLockUuid') doorLockUuid: string,
) { ) {
try { return await this.doorLockService.getOfflineMultipleTimeTemporaryPasswords(
return await this.doorLockService.getOfflineMultipleTimeTemporaryPasswords( doorLockUuid,
doorLockUuid, );
);
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@ -190,44 +137,30 @@ export class DoorLockController {
@Param('doorLockUuid') doorLockUuid: string, @Param('doorLockUuid') doorLockUuid: string,
@Param('passwordId') passwordId: string, @Param('passwordId') passwordId: string,
) { ) {
try { const temporaryPassword =
const temporaryPassword = await this.doorLockService.updateOfflineTemporaryPassword(
await this.doorLockService.updateOfflineTemporaryPassword( updateDoorLockOfflineTempDto,
updateDoorLockOfflineTempDto, doorLockUuid,
doorLockUuid, passwordId,
passwordId,
);
return {
statusCode: HttpStatus.CREATED,
success: true,
message: 'offline temporary password updated successfully',
data: temporaryPassword,
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
); );
}
return {
statusCode: HttpStatus.CREATED,
success: true,
message: 'offline temporary password updated successfully',
data: temporaryPassword,
};
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@Post('open/:doorLockUuid') @Post('open/:doorLockUuid')
async openDoorLock(@Param('doorLockUuid') doorLockUuid: string) { async openDoorLock(@Param('doorLockUuid') doorLockUuid: string) {
try { await this.doorLockService.openDoorLock(doorLockUuid);
await this.doorLockService.openDoorLock(doorLockUuid);
return { return {
statusCode: HttpStatus.CREATED, statusCode: HttpStatus.CREATED,
success: true, success: true,
message: 'door lock opened successfully', message: 'door lock opened successfully',
}; };
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
} }

View File

@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { ErrorMessageService } from './error-message.service';
describe('ErrorMessageService', () => {
let service: ErrorMessageService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [ErrorMessageService],
}).compile();
service = module.get<ErrorMessageService>(ErrorMessageService);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
});

View File

@ -0,0 +1,40 @@
// src/common/services/error-message.service.ts
import { Injectable } from '@nestjs/common';
type ErrorMessageKey = keyof typeof ErrorMessageService.prototype.messages;
@Injectable()
export class ErrorMessageService {
public readonly messages = {
NOT_FOUND: '{entity} not found', // Single key for "not found" errors
INVALID_MINUTES: 'Invalid minutes value',
INVALID_TIME_FORMAT: 'Invalid time format',
USER_NOT_FOUND: '{entity} not found', // Can reuse NOT_FOUND if desired
INTERNAL_SERVER_ERROR: 'Internal server error',
ERROR_ADDING_TEMP_PASSWORD:
'Error adding {type} temporary password from Tuya',
INVALID_UUID: 'Invalid {entity} UUID',
USER_ALREADY_BELONGS: 'This user already belongs to this {entity}',
USER_HAS_NO_ENTITIES: 'This user has no {entity}',
DEVICE_OPERATION_FAILED: 'All device operations failed',
REQUEST_FAILED: 'Error processing {operation} request',
COOLDOWN_ERROR:
'Please wait {time} more seconds before requesting a new OTP.',
};
getMessage(
key: ErrorMessageKey,
params?: Record<string, string | number>,
): string {
let message = this.messages[key] || 'Unknown error';
// Replace placeholders with provided params
if (params) {
Object.keys(params).forEach((param) => {
const regex = new RegExp(`{${param}}`, 'g');
message = message.replace(regex, params[param].toString());
});
}
return message;
}
}

View File

@ -3,7 +3,6 @@ import {
Body, Body,
Controller, Controller,
Get, Get,
HttpException,
HttpStatus, HttpStatus,
Param, Param,
Post, Post,
@ -33,35 +32,21 @@ export class FloorController {
@UseGuards(JwtAuthGuard, CheckBuildingTypeGuard) @UseGuards(JwtAuthGuard, CheckBuildingTypeGuard)
@Post() @Post()
async addFloor(@Body() addFloorDto: AddFloorDto) { async addFloor(@Body() addFloorDto: AddFloorDto) {
try { const floor = await this.floorService.addFloor(addFloorDto);
const floor = await this.floorService.addFloor(addFloorDto); return {
return { statusCode: HttpStatus.CREATED,
statusCode: HttpStatus.CREATED, success: true,
success: true, message: 'Floor added successfully',
message: 'Floor added successfully', data: floor,
data: floor, };
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard, FloorPermissionGuard) @UseGuards(JwtAuthGuard, FloorPermissionGuard)
@Get(':floorUuid') @Get(':floorUuid')
async getFloorByUuid(@Param('floorUuid') floorUuid: string) { async getFloorByUuid(@Param('floorUuid') floorUuid: string) {
try { const floor = await this.floorService.getFloorByUuid(floorUuid);
const floor = await this.floorService.getFloorByUuid(floorUuid); return floor;
return floor;
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@ -71,65 +56,34 @@ export class FloorController {
@Param('floorUuid') floorUuid: string, @Param('floorUuid') floorUuid: string,
@Query() query: GetFloorChildDto, @Query() query: GetFloorChildDto,
) { ) {
try { const floor = await this.floorService.getFloorChildByUuid(floorUuid, query);
const floor = await this.floorService.getFloorChildByUuid( return floor;
floorUuid,
query,
);
return floor;
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard, FloorPermissionGuard) @UseGuards(JwtAuthGuard, FloorPermissionGuard)
@Get('parent/:floorUuid') @Get('parent/:floorUuid')
async getFloorParentByUuid(@Param('floorUuid') floorUuid: string) { async getFloorParentByUuid(@Param('floorUuid') floorUuid: string) {
try { const floor = await this.floorService.getFloorParentByUuid(floorUuid);
const floor = await this.floorService.getFloorParentByUuid(floorUuid); return floor;
return floor;
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(AdminRoleGuard, CheckUserFloorGuard) @UseGuards(AdminRoleGuard, CheckUserFloorGuard)
@Post('user') @Post('user')
async addUserFloor(@Body() addUserFloorDto: AddUserFloorDto) { async addUserFloor(@Body() addUserFloorDto: AddUserFloorDto) {
try { await this.floorService.addUserFloor(addUserFloorDto);
await this.floorService.addUserFloor(addUserFloorDto); return {
return { statusCode: HttpStatus.CREATED,
statusCode: HttpStatus.CREATED, success: true,
success: true, message: 'user floor added successfully',
message: 'user floor added successfully', };
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@Get('user/:userUuid') @Get('user/:userUuid')
async getFloorsByUserId(@Param('userUuid') userUuid: string) { async getFloorsByUserId(@Param('userUuid') userUuid: string) {
try { return await this.floorService.getFloorsByUserId(userUuid);
return await this.floorService.getFloorsByUserId(userUuid);
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@ -139,17 +93,10 @@ export class FloorController {
@Param('floorUuid') floorUuid: string, @Param('floorUuid') floorUuid: string,
@Body() updateFloorNameDto: UpdateFloorNameDto, @Body() updateFloorNameDto: UpdateFloorNameDto,
) { ) {
try { const floor = await this.floorService.renameFloorByUuid(
const floor = await this.floorService.renameFloorByUuid( floorUuid,
floorUuid, updateFloorNameDto,
updateFloorNameDto, );
); return floor;
return floor;
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
} }

View File

@ -1,13 +1,5 @@
import { GroupService } from '../services/group.service'; import { GroupService } from '../services/group.service';
import { import { Controller, Get, UseGuards, Param, Req } from '@nestjs/common';
Controller,
Get,
UseGuards,
Param,
HttpException,
HttpStatus,
Req,
} from '@nestjs/common';
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger'; import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard'; import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
import { UnitPermissionGuard } from 'src/guards/unit.permission.guard'; import { UnitPermissionGuard } from 'src/guards/unit.permission.guard';
@ -24,14 +16,7 @@ export class GroupController {
@UseGuards(JwtAuthGuard, UnitPermissionGuard) @UseGuards(JwtAuthGuard, UnitPermissionGuard)
@Get(':unitUuid') @Get(':unitUuid')
async getGroupsBySpaceUuid(@Param('unitUuid') unitUuid: string) { async getGroupsBySpaceUuid(@Param('unitUuid') unitUuid: string) {
try { return await this.groupService.getGroupsByUnitUuid(unitUuid);
return await this.groupService.getGroupsByUnitUuid(unitUuid);
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard, UnitPermissionGuard) @UseGuards(JwtAuthGuard, UnitPermissionGuard)
@ -41,19 +26,12 @@ export class GroupController {
@Param('groupName') groupName: string, @Param('groupName') groupName: string,
@Req() req: any, @Req() req: any,
) { ) {
try { const userUuid = req.user.uuid;
const userUuid = req.user.uuid;
return await this.groupService.getUnitDevicesByGroupName( return await this.groupService.getUnitDevicesByGroupName(
unitUuid, unitUuid,
groupName, groupName,
userUuid, userUuid,
); );
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
} }

View File

@ -6,6 +6,7 @@ import { setupSwaggerAuthentication } from '../libs/common/src/util/user-auth.sw
import { ValidationPipe } from '@nestjs/common'; import { ValidationPipe } from '@nestjs/common';
import { json, urlencoded } from 'body-parser'; import { json, urlencoded } from 'body-parser';
import { SeederService } from '@app/common/seed/services/seeder.service'; import { SeederService } from '@app/common/seed/services/seeder.service';
import { HttpExceptionFilter } from './common/filters/http-exception/http-exception.filter';
async function bootstrap() { async function bootstrap() {
const app = await NestFactory.create(AppModule); const app = await NestFactory.create(AppModule);
@ -15,6 +16,7 @@ async function bootstrap() {
// Set the body parser limit to 1 MB // Set the body parser limit to 1 MB
app.use(json({ limit: '1mb' })); app.use(json({ limit: '1mb' }));
app.use(urlencoded({ limit: '1mb', extended: true })); app.use(urlencoded({ limit: '1mb', extended: true }));
app.useGlobalFilters(new HttpExceptionFilter());
app.use( app.use(
rateLimit({ rateLimit({

View File

@ -1,4 +1,4 @@
import { Controller, Get, HttpException, HttpStatus } from '@nestjs/common'; import { Controller, Get } from '@nestjs/common';
import { RegionService } from '../services/region.service'; import { RegionService } from '../services/region.service';
import { ApiTags, ApiOperation } from '@nestjs/swagger'; import { ApiTags, ApiOperation } from '@nestjs/swagger';
import { ControllerRoute } from '@app/common/constants/controller-route'; import { ControllerRoute } from '@app/common/constants/controller-route';
@ -17,13 +17,6 @@ export class RegionController {
description: ControllerRoute.REGION.ACTIONS.GET_REGIONS_DESCRIPTION, description: ControllerRoute.REGION.ACTIONS.GET_REGIONS_DESCRIPTION,
}) })
async getAllRegions() { async getAllRegions() {
try { return await this.regionService.getAllRegions();
return await this.regionService.getAllRegions();
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
} }

View File

@ -3,9 +3,10 @@ import { RegionService } from './services/region.service';
import { RegionController } from './controllers/region.controller'; import { RegionController } from './controllers/region.controller';
import { ConfigModule } from '@nestjs/config'; import { ConfigModule } from '@nestjs/config';
import { RegionRepository } from '@app/common/modules/region/repositories'; import { RegionRepository } from '@app/common/modules/region/repositories';
import { CommonModule } from '@app/common';
@Module({ @Module({
imports: [ConfigModule], imports: [ConfigModule, CommonModule],
controllers: [RegionController], controllers: [RegionController],
providers: [RegionService, RegionRepository], providers: [RegionService, RegionRepository],
exports: [RegionService], exports: [RegionService],

View File

@ -2,23 +2,33 @@ import {
BadRequestException, BadRequestException,
HttpException, HttpException,
HttpStatus, HttpStatus,
Inject,
Injectable, Injectable,
} from '@nestjs/common'; } from '@nestjs/common';
import { RegionRepository } from '@app/common/modules/region/repositories'; import { RegionRepository } from '@app/common/modules/region/repositories';
import { ErrorMessageService } from 'src/error-message/error-message.service';
@Injectable() @Injectable()
export class RegionService { export class RegionService {
constructor(private readonly regionRepository: RegionRepository) {} constructor(
private readonly regionRepository: RegionRepository,
@Inject(ErrorMessageService)
private readonly errorMessageService: ErrorMessageService,
) {}
async getAllRegions() { async getAllRegions() {
try { try {
const regions = await this.regionRepository.find(); const regions = await this.regionRepository.find();
return regions; return regions;
} catch (err) { } catch (err) {
if (err instanceof BadRequestException) { if (err instanceof BadRequestException) {
throw err; // Re-throw BadRequestException throw err; // Re-throw BadRequestException
} else { } else {
throw new HttpException('Regions found', HttpStatus.NOT_FOUND); throw new HttpException(
this.errorMessageService.getMessage('NOT_FOUND', {
entity: 'Regions',
}),
HttpStatus.NOT_FOUND,
);
} }
} }
} }

View File

@ -2,7 +2,6 @@ import {
Body, Body,
Controller, Controller,
Get, Get,
HttpException,
HttpStatus, HttpStatus,
Post, Post,
UseGuards, UseGuards,
@ -23,32 +22,21 @@ export class RoleController {
@UseGuards(SuperAdminRoleGuard) @UseGuards(SuperAdminRoleGuard)
@Get('types') @Get('types')
async fetchRoleTypes() { async fetchRoleTypes() {
try { const roleTypes = await this.roleService.fetchRoleTypes();
const roleTypes = await this.roleService.fetchRoleTypes(); return {
return { statusCode: HttpStatus.OK,
statusCode: HttpStatus.OK, message: 'Role Types fetched Successfully',
message: 'Role Types fetched Successfully', data: roleTypes,
data: roleTypes, };
};
} catch (err) {
throw new Error(err);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(SuperAdminRoleGuard) @UseGuards(SuperAdminRoleGuard)
@Post() @Post()
async addUserRoleType(@Body() addUserRoleDto: AddUserRoleDto) { async addUserRoleType(@Body() addUserRoleDto: AddUserRoleDto) {
try { await this.roleService.addUserRoleType(addUserRoleDto);
await this.roleService.addUserRoleType(addUserRoleDto); return {
return { statusCode: HttpStatus.OK,
statusCode: HttpStatus.OK, message: 'User Role Added Successfully',
message: 'User Role Added Successfully', };
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
} }

View File

@ -3,7 +3,6 @@ import {
Body, Body,
Controller, Controller,
Get, Get,
HttpException,
HttpStatus, HttpStatus,
Param, Param,
Post, Post,
@ -31,81 +30,46 @@ export class RoomController {
@UseGuards(JwtAuthGuard, CheckUnitTypeGuard) @UseGuards(JwtAuthGuard, CheckUnitTypeGuard)
@Post() @Post()
async addRoom(@Body() addRoomDto: AddRoomDto) { async addRoom(@Body() addRoomDto: AddRoomDto) {
try { const room = await this.roomService.addRoom(addRoomDto);
const room = await this.roomService.addRoom(addRoomDto); return {
return { statusCode: HttpStatus.CREATED,
statusCode: HttpStatus.CREATED, success: true,
success: true, message: 'Room added successfully',
message: 'Room added successfully', data: room,
data: room, };
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard, RoomPermissionGuard) @UseGuards(JwtAuthGuard, RoomPermissionGuard)
@Get(':roomUuid') @Get(':roomUuid')
async getRoomByUuid(@Param('roomUuid') roomUuid: string) { async getRoomByUuid(@Param('roomUuid') roomUuid: string) {
try { const room = await this.roomService.getRoomByUuid(roomUuid);
const room = await this.roomService.getRoomByUuid(roomUuid); return room;
return room;
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard, RoomPermissionGuard) @UseGuards(JwtAuthGuard, RoomPermissionGuard)
@Get('parent/:roomUuid') @Get('parent/:roomUuid')
async getRoomParentByUuid(@Param('roomUuid') roomUuid: string) { async getRoomParentByUuid(@Param('roomUuid') roomUuid: string) {
try { const room = await this.roomService.getRoomParentByUuid(roomUuid);
const room = await this.roomService.getRoomParentByUuid(roomUuid); return room;
return room;
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(AdminRoleGuard, CheckUserRoomGuard) @UseGuards(AdminRoleGuard, CheckUserRoomGuard)
@Post('user') @Post('user')
async addUserRoom(@Body() addUserRoomDto: AddUserRoomDto) { async addUserRoom(@Body() addUserRoomDto: AddUserRoomDto) {
try { await this.roomService.addUserRoom(addUserRoomDto);
await this.roomService.addUserRoom(addUserRoomDto); return {
return { statusCode: HttpStatus.CREATED,
statusCode: HttpStatus.CREATED, success: true,
success: true, message: 'user room added successfully',
message: 'user room added successfully', };
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@Get('user/:userUuid') @Get('user/:userUuid')
async getRoomsByUserId(@Param('userUuid') userUuid: string) { async getRoomsByUserId(@Param('userUuid') userUuid: string) {
try { return await this.roomService.getRoomsByUserId(userUuid);
return await this.roomService.getRoomsByUserId(userUuid);
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@ -115,17 +79,10 @@ export class RoomController {
@Param('roomUuid') roomUuid: string, @Param('roomUuid') roomUuid: string,
@Body() updateRoomNameDto: UpdateRoomNameDto, @Body() updateRoomNameDto: UpdateRoomNameDto,
) { ) {
try { const room = await this.roomService.renameRoomByUuid(
const room = await this.roomService.renameRoomByUuid( roomUuid,
roomUuid, updateRoomNameDto,
updateRoomNameDto, );
); return room;
return room;
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
} }

View File

@ -4,7 +4,6 @@ import {
Controller, Controller,
Delete, Delete,
Get, Get,
HttpException,
HttpStatus, HttpStatus,
Param, Param,
Post, Post,
@ -27,36 +26,22 @@ export class SceneController {
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@Post('tap-to-run') @Post('tap-to-run')
async addTapToRunScene(@Body() addSceneTapToRunDto: AddSceneTapToRunDto) { async addTapToRunScene(@Body() addSceneTapToRunDto: AddSceneTapToRunDto) {
try { const tapToRunScene =
const tapToRunScene = await this.sceneService.addTapToRunScene(addSceneTapToRunDto);
await this.sceneService.addTapToRunScene(addSceneTapToRunDto); return {
return { statusCode: HttpStatus.CREATED,
statusCode: HttpStatus.CREATED, success: true,
success: true, message: 'Scene added successfully',
message: 'Scene added successfully', data: tapToRunScene,
data: tapToRunScene, };
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@Get('tap-to-run/:unitUuid') @Get('tap-to-run/:unitUuid')
async getTapToRunSceneByUnit(@Param('unitUuid') unitUuid: string) { async getTapToRunSceneByUnit(@Param('unitUuid') unitUuid: string) {
try { const tapToRunScenes =
const tapToRunScenes = await this.sceneService.getTapToRunSceneByUnit(unitUuid);
await this.sceneService.getTapToRunSceneByUnit(unitUuid); return tapToRunScenes;
return tapToRunScenes;
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@ -65,53 +50,31 @@ export class SceneController {
@Param('unitUuid') unitUuid: string, @Param('unitUuid') unitUuid: string,
@Param('sceneId') sceneId: string, @Param('sceneId') sceneId: string,
) { ) {
try { await this.sceneService.deleteTapToRunScene(unitUuid, sceneId);
await this.sceneService.deleteTapToRunScene(unitUuid, sceneId); return {
return { statusCode: HttpStatus.OK,
statusCode: HttpStatus.OK, message: 'Scene Deleted Successfully',
message: 'Scene Deleted Successfully', };
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@Post('tap-to-run/trigger/:sceneId') @Post('tap-to-run/trigger/:sceneId')
async triggerTapToRunScene(@Param('sceneId') sceneId: string) { async triggerTapToRunScene(@Param('sceneId') sceneId: string) {
try { await this.sceneService.triggerTapToRunScene(sceneId);
await this.sceneService.triggerTapToRunScene(sceneId); return {
return { statusCode: HttpStatus.CREATED,
statusCode: HttpStatus.CREATED, success: true,
success: true, message: 'Scene trigger successfully',
message: 'Scene trigger successfully', };
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@Get('tap-to-run/details/:sceneId') @Get('tap-to-run/details/:sceneId')
async getTapToRunSceneDetails(@Param('sceneId') sceneId: string) { async getTapToRunSceneDetails(@Param('sceneId') sceneId: string) {
try { const tapToRunScenes =
const tapToRunScenes = await this.sceneService.getTapToRunSceneDetails(sceneId);
await this.sceneService.getTapToRunSceneDetails(sceneId); return tapToRunScenes;
return tapToRunScenes;
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
``;
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@ -120,22 +83,15 @@ export class SceneController {
@Body() updateSceneTapToRunDto: UpdateSceneTapToRunDto, @Body() updateSceneTapToRunDto: UpdateSceneTapToRunDto,
@Param('sceneId') sceneId: string, @Param('sceneId') sceneId: string,
) { ) {
try { const tapToRunScene = await this.sceneService.updateTapToRunScene(
const tapToRunScene = await this.sceneService.updateTapToRunScene( updateSceneTapToRunDto,
updateSceneTapToRunDto, sceneId,
sceneId, );
); return {
return { statusCode: HttpStatus.CREATED,
statusCode: HttpStatus.CREATED, success: true,
success: true, message: 'Scene updated successfully',
message: 'Scene updated successfully', data: tapToRunScene,
data: tapToRunScene, };
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
} }

View File

@ -5,7 +5,6 @@ import {
Get, Get,
Post, Post,
Param, Param,
HttpException,
HttpStatus, HttpStatus,
UseGuards, UseGuards,
Put, Put,
@ -36,24 +35,17 @@ export class ScheduleController {
@Param('deviceUuid') deviceUuid: string, @Param('deviceUuid') deviceUuid: string,
@Body() addScheduleDto: AddScheduleDto, @Body() addScheduleDto: AddScheduleDto,
) { ) {
try { const schedule = await this.scheduleService.addDeviceSchedule(
const schedule = await this.scheduleService.addDeviceSchedule( deviceUuid,
deviceUuid, addScheduleDto,
addScheduleDto, );
);
return { return {
statusCode: HttpStatus.CREATED, statusCode: HttpStatus.CREATED,
success: true, success: true,
message: 'schedule added successfully', message: 'schedule added successfully',
data: schedule, data: schedule,
}; };
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@ -62,17 +54,10 @@ export class ScheduleController {
@Param('deviceUuid') deviceUuid: string, @Param('deviceUuid') deviceUuid: string,
@Query() query: GetScheduleDeviceDto, @Query() query: GetScheduleDeviceDto,
) { ) {
try { return await this.scheduleService.getDeviceScheduleByCategory(
return await this.scheduleService.getDeviceScheduleByCategory( deviceUuid,
deviceUuid, query.category,
query.category, );
);
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@ -81,19 +66,12 @@ export class ScheduleController {
@Param('deviceUuid') deviceUuid: string, @Param('deviceUuid') deviceUuid: string,
@Param('scheduleId') scheduleId: string, @Param('scheduleId') scheduleId: string,
) { ) {
try { await this.scheduleService.deleteDeviceSchedule(deviceUuid, scheduleId);
await this.scheduleService.deleteDeviceSchedule(deviceUuid, scheduleId); return {
return { statusCode: HttpStatus.CREATED,
statusCode: HttpStatus.CREATED, success: true,
success: true, message: 'schedule deleted successfully',
message: 'schedule deleted successfully', };
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@ -102,22 +80,15 @@ export class ScheduleController {
@Param('deviceUuid') deviceUuid: string, @Param('deviceUuid') deviceUuid: string,
@Body() enableScheduleDto: EnableScheduleDto, @Body() enableScheduleDto: EnableScheduleDto,
) { ) {
try { await this.scheduleService.enableDeviceSchedule(
await this.scheduleService.enableDeviceSchedule( deviceUuid,
deviceUuid, enableScheduleDto,
enableScheduleDto, );
); return {
return { statusCode: HttpStatus.CREATED,
statusCode: HttpStatus.CREATED, success: true,
success: true, message: 'schedule updated successfully',
message: 'schedule updated successfully', };
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@ -126,23 +97,16 @@ export class ScheduleController {
@Param('deviceUuid') deviceUuid: string, @Param('deviceUuid') deviceUuid: string,
@Body() updateScheduleDto: UpdateScheduleDto, @Body() updateScheduleDto: UpdateScheduleDto,
) { ) {
try { const schedule = await this.scheduleService.updateDeviceSchedule(
const schedule = await this.scheduleService.updateDeviceSchedule( deviceUuid,
deviceUuid, updateScheduleDto,
updateScheduleDto, );
);
return { return {
statusCode: HttpStatus.CREATED, statusCode: HttpStatus.CREATED,
success: true, success: true,
message: 'schedule updated successfully', message: 'schedule updated successfully',
data: schedule, data: schedule,
}; };
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
} }

View File

@ -1,10 +1,4 @@
import { import { Controller, Get, UseGuards } from '@nestjs/common';
Controller,
Get,
HttpException,
HttpStatus,
UseGuards,
} from '@nestjs/common';
import { TimeZoneService } from '../services/timezone.service'; import { TimeZoneService } from '../services/timezone.service';
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger'; 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';
@ -21,13 +15,6 @@ export class TimeZoneController {
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@Get() @Get()
async getAllTimeZones() { async getAllTimeZones() {
try { return await this.timeZoneService.getAllTimeZones();
return await this.timeZoneService.getAllTimeZones();
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
} }

View File

@ -3,7 +3,6 @@ import {
Body, Body,
Controller, Controller,
Get, Get,
HttpException,
HttpStatus, HttpStatus,
Param, Param,
Post, Post,
@ -36,35 +35,21 @@ export class UnitController {
@UseGuards(JwtAuthGuard, CheckFloorTypeGuard) @UseGuards(JwtAuthGuard, CheckFloorTypeGuard)
@Post() @Post()
async addUnit(@Body() addUnitDto: AddUnitDto) { async addUnit(@Body() addUnitDto: AddUnitDto) {
try { const unit = await this.unitService.addUnit(addUnitDto);
const unit = await this.unitService.addUnit(addUnitDto); return {
return { statusCode: HttpStatus.CREATED,
statusCode: HttpStatus.CREATED, success: true,
success: true, message: 'Unit added successfully',
message: 'Unit added successfully', data: unit,
data: unit, };
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard, UnitPermissionGuard) @UseGuards(JwtAuthGuard, UnitPermissionGuard)
@Get(':unitUuid') @Get(':unitUuid')
async getUnitByUuid(@Param('unitUuid') unitUuid: string) { async getUnitByUuid(@Param('unitUuid') unitUuid: string) {
try { const unit = await this.unitService.getUnitByUuid(unitUuid);
const unit = await this.unitService.getUnitByUuid(unitUuid); return unit;
return unit;
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@ -74,60 +59,32 @@ export class UnitController {
@Param('unitUuid') unitUuid: string, @Param('unitUuid') unitUuid: string,
@Query() query: GetUnitChildDto, @Query() query: GetUnitChildDto,
) { ) {
try { const unit = await this.unitService.getUnitChildByUuid(unitUuid, query);
const unit = await this.unitService.getUnitChildByUuid(unitUuid, query); return unit;
return unit;
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard, UnitPermissionGuard) @UseGuards(JwtAuthGuard, UnitPermissionGuard)
@Get('parent/:unitUuid') @Get('parent/:unitUuid')
async getUnitParentByUuid(@Param('unitUuid') unitUuid: string) { async getUnitParentByUuid(@Param('unitUuid') unitUuid: string) {
try { const unit = await this.unitService.getUnitParentByUuid(unitUuid);
const unit = await this.unitService.getUnitParentByUuid(unitUuid); return unit;
return unit;
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard, CheckUserUnitGuard) @UseGuards(JwtAuthGuard, CheckUserUnitGuard)
@Post('user') @Post('user')
async addUserUnit(@Body() addUserUnitDto: AddUserUnitDto) { async addUserUnit(@Body() addUserUnitDto: AddUserUnitDto) {
try { await this.unitService.addUserUnit(addUserUnitDto);
await this.unitService.addUserUnit(addUserUnitDto); return {
return { statusCode: HttpStatus.CREATED,
statusCode: HttpStatus.CREATED, success: true,
success: true, message: 'user unit added successfully',
message: 'user unit added successfully', };
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@Get('user/:userUuid') @Get('user/:userUuid')
async getUnitsByUserId(@Param('userUuid') userUuid: string) { async getUnitsByUserId(@Param('userUuid') userUuid: string) {
try { return await this.unitService.getUnitsByUserId(userUuid);
return await this.unitService.getUnitsByUserId(userUuid);
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@ -137,32 +94,18 @@ export class UnitController {
@Param('unitUuid') unitUuid: string, @Param('unitUuid') unitUuid: string,
@Body() updateUnitNameDto: UpdateUnitNameDto, @Body() updateUnitNameDto: UpdateUnitNameDto,
) { ) {
try { const unit = await this.unitService.renameUnitByUuid(
const unit = await this.unitService.renameUnitByUuid( unitUuid,
unitUuid, updateUnitNameDto,
updateUnitNameDto, );
); return unit;
return unit;
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard, UnitPermissionGuard) @UseGuards(JwtAuthGuard, UnitPermissionGuard)
@Get(':unitUuid/invitation-code') @Get(':unitUuid/invitation-code')
async getUnitInvitationCode(@Param('unitUuid') unitUuid: string) { async getUnitInvitationCode(@Param('unitUuid') unitUuid: string) {
try { const unit = await this.unitService.getUnitInvitationCode(unitUuid);
const unit = await this.unitService.getUnitInvitationCode(unitUuid); return unit;
return unit;
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@ -171,18 +114,11 @@ export class UnitController {
async verifyCodeAndAddUserUnit( async verifyCodeAndAddUserUnit(
@Body() addUserUnitUsingCodeDto: AddUserUnitUsingCodeDto, @Body() addUserUnitUsingCodeDto: AddUserUnitUsingCodeDto,
) { ) {
try { await this.unitService.verifyCodeAndAddUserUnit(addUserUnitUsingCodeDto);
await this.unitService.verifyCodeAndAddUserUnit(addUserUnitUsingCodeDto); return {
return { statusCode: HttpStatus.CREATED,
statusCode: HttpStatus.CREATED, success: true,
success: true, message: 'user unit added successfully',
message: 'user unit added successfully', };
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
} }

View File

@ -3,7 +3,6 @@ import {
Controller, Controller,
Delete, Delete,
Get, Get,
HttpException,
HttpStatus, HttpStatus,
Param, Param,
Post, Post,
@ -32,22 +31,14 @@ export class UserDevicePermissionController {
async addDevicePermission( async addDevicePermission(
@Body() userDevicePermissionDto: UserDevicePermissionAddDto, @Body() userDevicePermissionDto: UserDevicePermissionAddDto,
) { ) {
try { const addDetails = await this.userDevicePermissionService.addUserPermission(
const addDetails = userDevicePermissionDto,
await this.userDevicePermissionService.addUserPermission( );
userDevicePermissionDto, return {
); statusCode: HttpStatus.CREATED,
return { message: 'User Permission for Devices Added Successfully',
statusCode: HttpStatus.CREATED, data: addDetails,
message: 'User Permission for Devices Added Successfully', };
data: addDetails,
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@ -57,38 +48,27 @@ export class UserDevicePermissionController {
@Param('devicePermissionUuid') devicePermissionUuid: string, @Param('devicePermissionUuid') devicePermissionUuid: string,
@Body() userDevicePermissionEditDto: UserDevicePermissionEditDto, @Body() userDevicePermissionEditDto: UserDevicePermissionEditDto,
) { ) {
try { await this.userDevicePermissionService.editUserPermission(
await this.userDevicePermissionService.editUserPermission( devicePermissionUuid,
devicePermissionUuid, userDevicePermissionEditDto,
userDevicePermissionEditDto, );
); return {
return { statusCode: HttpStatus.OK,
statusCode: HttpStatus.OK, message: 'User Permission for Devices Updated Successfully',
message: 'User Permission for Devices Updated Successfully', };
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(AdminRoleGuard) @UseGuards(AdminRoleGuard)
@Get(':deviceUuid') @Get(':deviceUuid')
async fetchDevicePermission(@Param('deviceUuid') deviceUuid: string) { async fetchDevicePermission(@Param('deviceUuid') deviceUuid: string) {
try { const deviceDetails =
const deviceDetails = await this.userDevicePermissionService.fetchUserPermission(deviceUuid);
await this.userDevicePermissionService.fetchUserPermission(deviceUuid); return {
return { statusCode: HttpStatus.OK,
statusCode: HttpStatus.OK, message: 'Device Details fetched Successfully',
message: 'Device Details fetched Successfully', data: deviceDetails,
data: deviceDetails, };
};
} catch (err) {
throw new Error(err);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(AdminRoleGuard) @UseGuards(AdminRoleGuard)
@ -96,19 +76,12 @@ export class UserDevicePermissionController {
async deleteDevicePermission( async deleteDevicePermission(
@Param('devicePermissionUuid') devicePermissionUuid: string, @Param('devicePermissionUuid') devicePermissionUuid: string,
) { ) {
try { await this.userDevicePermissionService.deleteDevicePermission(
await this.userDevicePermissionService.deleteDevicePermission( devicePermissionUuid,
devicePermissionUuid, );
); return {
return { statusCode: HttpStatus.OK,
statusCode: HttpStatus.OK, message: 'User Permission for Devices Deleted Successfully',
message: 'User Permission for Devices Deleted Successfully', };
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
} }

View File

@ -2,7 +2,6 @@ import {
Body, Body,
Controller, Controller,
Get, Get,
HttpException,
HttpStatus, HttpStatus,
Param, Param,
Post, Post,
@ -34,41 +33,27 @@ export class UserNotificationController {
async addUserSubscription( async addUserSubscription(
@Body() userNotificationAddDto: UserNotificationAddDto, @Body() userNotificationAddDto: UserNotificationAddDto,
) { ) {
try { const addDetails = await this.userNotificationService.addUserSubscription(
const addDetails = await this.userNotificationService.addUserSubscription( userNotificationAddDto,
userNotificationAddDto, );
); return {
return { statusCode: HttpStatus.CREATED,
statusCode: HttpStatus.CREATED, message: 'User Notification Added Successfully',
message: 'User Notification Added Successfully', data: addDetails,
data: addDetails, };
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@Get(':userUuid') @Get(':userUuid')
async fetchUserSubscriptions(@Param('userUuid') userUuid: string) { async fetchUserSubscriptions(@Param('userUuid') userUuid: string) {
try { const userDetails =
const userDetails = await this.userNotificationService.fetchUserSubscriptions(userUuid);
await this.userNotificationService.fetchUserSubscriptions(userUuid); return {
return { statusCode: HttpStatus.OK,
statusCode: HttpStatus.OK, message: 'User Notification fetched Successfully',
message: 'User Notification fetched Successfully', data: { ...userDetails },
data: { ...userDetails }, };
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@ -76,19 +61,12 @@ export class UserNotificationController {
async updateUserSubscription( async updateUserSubscription(
@Body() userNotificationUpdateDto: UserNotificationUpdateDto, @Body() userNotificationUpdateDto: UserNotificationUpdateDto,
) { ) {
try { await this.userNotificationService.updateUserSubscription(
await this.userNotificationService.updateUserSubscription( userNotificationUpdateDto,
userNotificationUpdateDto, );
); return {
return { statusCode: HttpStatus.OK,
statusCode: HttpStatus.OK, message: 'User subscription updated Successfully',
message: 'User subscription updated Successfully', };
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
} }

View File

@ -3,7 +3,6 @@ import {
Controller, Controller,
Delete, Delete,
Get, Get,
HttpException,
HttpStatus, HttpStatus,
Param, Param,
Put, Put,
@ -33,14 +32,7 @@ export class UserController {
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@Get(':userUuid') @Get(':userUuid')
async getUserDetailsByUserUuid(@Param('userUuid') userUuid: string) { async getUserDetailsByUserUuid(@Param('userUuid') userUuid: string) {
try { return await this.userService.getUserDetailsByUserUuid(userUuid);
return await this.userService.getUserDetailsByUserUuid(userUuid);
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard, CheckProfilePictureGuard) @UseGuards(JwtAuthGuard, CheckProfilePictureGuard)
@ -49,23 +41,16 @@ export class UserController {
@Param('userUuid') userUuid: string, @Param('userUuid') userUuid: string,
@Body() updateProfilePictureDataDto: UpdateProfilePictureDataDto, @Body() updateProfilePictureDataDto: UpdateProfilePictureDataDto,
) { ) {
try { const userData = await this.userService.updateProfilePictureByUserUuid(
const userData = await this.userService.updateProfilePictureByUserUuid( userUuid,
userUuid, updateProfilePictureDataDto,
updateProfilePictureDataDto, );
); return {
return { statusCode: HttpStatus.CREATED,
statusCode: HttpStatus.CREATED, success: true,
success: true, message: 'profile picture updated successfully',
message: 'profile picture updated successfully', data: userData,
data: userData, };
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@ -74,23 +59,16 @@ export class UserController {
@Param('userUuid') userUuid: string, @Param('userUuid') userUuid: string,
@Body() updateRegionDataDto: UpdateRegionDataDto, @Body() updateRegionDataDto: UpdateRegionDataDto,
) { ) {
try { const userData = await this.userService.updateRegionByUserUuid(
const userData = await this.userService.updateRegionByUserUuid( userUuid,
userUuid, updateRegionDataDto,
updateRegionDataDto, );
); return {
return { statusCode: HttpStatus.CREATED,
statusCode: HttpStatus.CREATED, success: true,
success: true, message: 'region updated successfully',
message: 'region updated successfully', data: userData,
data: userData, };
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@ -99,23 +77,16 @@ export class UserController {
@Param('userUuid') userUuid: string, @Param('userUuid') userUuid: string,
@Body() updateTimezoneDataDto: UpdateTimezoneDataDto, @Body() updateTimezoneDataDto: UpdateTimezoneDataDto,
) { ) {
try { const userData = await this.userService.updateTimezoneByUserUuid(
const userData = await this.userService.updateTimezoneByUserUuid( userUuid,
userUuid, updateTimezoneDataDto,
updateTimezoneDataDto, );
); return {
return { statusCode: HttpStatus.CREATED,
statusCode: HttpStatus.CREATED, success: true,
success: true, message: 'timezone updated successfully',
message: 'timezone updated successfully', data: userData,
data: userData, };
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@ -124,23 +95,16 @@ export class UserController {
@Param('userUuid') userUuid: string, @Param('userUuid') userUuid: string,
@Body() updateNameDto: UpdateNameDto, @Body() updateNameDto: UpdateNameDto,
) { ) {
try { const userData = await this.userService.updateNameByUserUuid(
const userData = await this.userService.updateNameByUserUuid( userUuid,
userUuid, updateNameDto,
updateNameDto, );
); return {
return { statusCode: HttpStatus.CREATED,
statusCode: HttpStatus.CREATED, success: true,
success: true, message: 'name updated successfully',
message: 'name updated successfully', data: userData,
data: userData, };
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(SuperAdminRoleGuard) @UseGuards(SuperAdminRoleGuard)

View File

@ -3,7 +3,6 @@ import {
Body, Body,
Controller, Controller,
Post, Post,
HttpException,
HttpStatus, HttpStatus,
UseGuards, UseGuards,
Get, Get,
@ -34,24 +33,17 @@ export class VisitorPasswordController {
@Body() addDoorLockOnlineMultipleDto: AddDoorLockOnlineMultipleDto, @Body() addDoorLockOnlineMultipleDto: AddDoorLockOnlineMultipleDto,
@Req() req: any, @Req() req: any,
) { ) {
try { const userUuid = req.user.uuid;
const userUuid = req.user.uuid; const temporaryPasswords =
const temporaryPasswords = await this.visitorPasswordService.addOnlineTemporaryPasswordMultipleTime(
await this.visitorPasswordService.addOnlineTemporaryPasswordMultipleTime( addDoorLockOnlineMultipleDto,
addDoorLockOnlineMultipleDto, userUuid,
userUuid,
);
return {
statusCode: HttpStatus.CREATED,
data: temporaryPasswords,
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
); );
}
return {
statusCode: HttpStatus.CREATED,
data: temporaryPasswords,
};
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@ -60,24 +52,17 @@ export class VisitorPasswordController {
@Body() addDoorLockOnlineOneTimeDto: AddDoorLockOnlineOneTimeDto, @Body() addDoorLockOnlineOneTimeDto: AddDoorLockOnlineOneTimeDto,
@Req() req: any, @Req() req: any,
) { ) {
try { const userUuid = req.user.uuid;
const userUuid = req.user.uuid; const temporaryPasswords =
const temporaryPasswords = await this.visitorPasswordService.addOnlineTemporaryPasswordOneTime(
await this.visitorPasswordService.addOnlineTemporaryPasswordOneTime( addDoorLockOnlineOneTimeDto,
addDoorLockOnlineOneTimeDto, userUuid,
userUuid,
);
return {
statusCode: HttpStatus.CREATED,
data: temporaryPasswords,
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
); );
}
return {
statusCode: HttpStatus.CREATED,
data: temporaryPasswords,
};
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@ -86,24 +71,17 @@ export class VisitorPasswordController {
@Body() addDoorLockOfflineOneTimeDto: AddDoorLockOfflineOneTimeDto, @Body() addDoorLockOfflineOneTimeDto: AddDoorLockOfflineOneTimeDto,
@Req() req: any, @Req() req: any,
) { ) {
try { const userUuid = req.user.uuid;
const userUuid = req.user.uuid; const temporaryPassword =
const temporaryPassword = await this.visitorPasswordService.addOfflineOneTimeTemporaryPassword(
await this.visitorPasswordService.addOfflineOneTimeTemporaryPassword( addDoorLockOfflineOneTimeDto,
addDoorLockOfflineOneTimeDto, userUuid,
userUuid,
);
return {
statusCode: HttpStatus.CREATED,
data: temporaryPassword,
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
); );
}
return {
statusCode: HttpStatus.CREATED,
data: temporaryPassword,
};
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@ -113,49 +91,28 @@ export class VisitorPasswordController {
addDoorLockOfflineMultipleDto: AddDoorLockOfflineMultipleDto, addDoorLockOfflineMultipleDto: AddDoorLockOfflineMultipleDto,
@Req() req: any, @Req() req: any,
) { ) {
try { const userUuid = req.user.uuid;
const userUuid = req.user.uuid; const temporaryPassword =
const temporaryPassword = await this.visitorPasswordService.addOfflineMultipleTimeTemporaryPassword(
await this.visitorPasswordService.addOfflineMultipleTimeTemporaryPassword( addDoorLockOfflineMultipleDto,
addDoorLockOfflineMultipleDto, userUuid,
userUuid,
);
return {
statusCode: HttpStatus.CREATED,
data: temporaryPassword,
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
); );
}
return {
statusCode: HttpStatus.CREATED,
data: temporaryPassword,
};
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@Get() @Get()
async GetVisitorPassword() { async GetVisitorPassword() {
try { return await this.visitorPasswordService.getPasswords();
return await this.visitorPasswordService.getPasswords();
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
@ApiBearerAuth() @ApiBearerAuth()
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@Get('/devices') @Get('/devices')
async GetVisitorDevices() { async GetVisitorDevices() {
try { return await this.visitorPasswordService.getAllPassDevices();
return await this.visitorPasswordService.getAllPassDevices();
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
} }