diff --git a/libs/common/src/common.module.ts b/libs/common/src/common.module.ts index 8c3e78c..d156de3 100644 --- a/libs/common/src/common.module.ts +++ b/libs/common/src/common.module.ts @@ -6,10 +6,16 @@ import { AuthModule } from './auth/auth.module'; import { ConfigModule } from '@nestjs/config'; import config from './config'; import { EmailService } from './util/email.service'; - +import { ErrorMessageService } from 'src/error-message/error-message.service'; @Module({ - providers: [CommonService, EmailService], - exports: [CommonService, HelperModule, AuthModule, EmailService], + providers: [CommonService, EmailService, ErrorMessageService], + exports: [ + CommonService, + HelperModule, + AuthModule, + EmailService, + ErrorMessageService, + ], imports: [ ConfigModule.forRoot({ load: config, diff --git a/src/auth/services/user-auth.service.ts b/src/auth/services/user-auth.service.ts index c924658..9fd9fe3 100644 --- a/src/auth/services/user-auth.service.ts +++ b/src/auth/services/user-auth.service.ts @@ -18,6 +18,7 @@ import * as argon2 from 'argon2'; import { differenceInSeconds } from '@app/common/helper/differenceInSeconds'; import { LessThan, MoreThan } from 'typeorm'; import { ConfigService } from '@nestjs/config'; +import { UUID } from 'typeorm/driver/mongodb/bson.typings'; @Injectable() export class UserAuthService { diff --git a/src/automation/controllers/automation.controller.ts b/src/automation/controllers/automation.controller.ts index 4328602..280ba82 100644 --- a/src/automation/controllers/automation.controller.ts +++ b/src/automation/controllers/automation.controller.ts @@ -4,7 +4,6 @@ import { Controller, Delete, Get, - HttpException, HttpStatus, Param, Post, @@ -31,52 +30,30 @@ export class AutomationController { @UseGuards(JwtAuthGuard) @Post() async addAutomation(@Body() addAutomationDto: AddAutomationDto) { - try { - const automation = - await this.automationService.addAutomation(addAutomationDto); - return { - statusCode: HttpStatus.CREATED, - success: true, - message: 'Automation added successfully', - data: automation, - }; - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + const automation = + await this.automationService.addAutomation(addAutomationDto); + return { + statusCode: HttpStatus.CREATED, + success: true, + message: 'Automation added successfully', + data: automation, + }; } @ApiBearerAuth() @UseGuards(JwtAuthGuard) @Get(':unitUuid') async getAutomationByUnit(@Param('unitUuid') unitUuid: string) { - try { - const automation = - await this.automationService.getAutomationByUnit(unitUuid); - return automation; - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + const automation = + await this.automationService.getAutomationByUnit(unitUuid); + return automation; } @ApiBearerAuth() @UseGuards(JwtAuthGuard) @Get('details/:automationId') async getAutomationDetails(@Param('automationId') automationId: string) { - try { - const automation = - await this.automationService.getAutomationDetails(automationId); - return automation; - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - ``; - } + const automation = + await this.automationService.getAutomationDetails(automationId); + return automation; } @ApiBearerAuth() @UseGuards(JwtAuthGuard) @@ -85,18 +62,11 @@ export class AutomationController { @Param('unitUuid') unitUuid: string, @Param('automationId') automationId: string, ) { - try { - await this.automationService.deleteAutomation(unitUuid, automationId); - return { - statusCode: HttpStatus.OK, - message: 'Automation Deleted Successfully', - }; - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + await this.automationService.deleteAutomation(unitUuid, automationId); + return { + statusCode: HttpStatus.OK, + message: 'Automation Deleted Successfully', + }; } @ApiBearerAuth() @UseGuards(JwtAuthGuard) @@ -105,23 +75,16 @@ export class AutomationController { @Body() updateAutomationDto: UpdateAutomationDto, @Param('automationId') automationId: string, ) { - try { - const automation = await this.automationService.updateAutomation( - updateAutomationDto, - automationId, - ); - return { - statusCode: HttpStatus.CREATED, - success: true, - message: 'Automation updated successfully', - data: automation, - }; - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + const automation = await this.automationService.updateAutomation( + updateAutomationDto, + automationId, + ); + return { + statusCode: HttpStatus.CREATED, + success: true, + message: 'Automation updated successfully', + data: automation, + }; } @ApiBearerAuth() @UseGuards(JwtAuthGuard) @@ -130,21 +93,14 @@ export class AutomationController { @Body() updateAutomationStatusDto: UpdateAutomationStatusDto, @Param('automationId') automationId: string, ) { - try { - await this.automationService.updateAutomationStatus( - updateAutomationStatusDto, - automationId, - ); - return { - statusCode: HttpStatus.CREATED, - success: true, - message: 'Automation status updated successfully', - }; - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + await this.automationService.updateAutomationStatus( + updateAutomationStatusDto, + automationId, + ); + return { + statusCode: HttpStatus.CREATED, + success: true, + message: 'Automation status updated successfully', + }; } } diff --git a/src/building/controllers/building.controller.ts b/src/building/controllers/building.controller.ts index ad61cb3..e40a2a4 100644 --- a/src/building/controllers/building.controller.ts +++ b/src/building/controllers/building.controller.ts @@ -3,7 +3,6 @@ import { Body, Controller, Get, - HttpException, HttpStatus, Param, Post, @@ -33,36 +32,21 @@ export class BuildingController { @UseGuards(JwtAuthGuard, CheckCommunityTypeGuard) @Post() async addBuilding(@Body() addBuildingDto: AddBuildingDto) { - try { - const building = await this.buildingService.addBuilding(addBuildingDto); - return { - statusCode: HttpStatus.CREATED, - success: true, - message: 'Building added successfully', - data: building, - }; - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + const building = await this.buildingService.addBuilding(addBuildingDto); + return { + statusCode: HttpStatus.CREATED, + success: true, + message: 'Building added successfully', + data: building, + }; } @ApiBearerAuth() @UseGuards(JwtAuthGuard, BuildingPermissionGuard) @Get(':buildingUuid') async getBuildingByUuid(@Param('buildingUuid') buildingUuid: string) { - try { - const building = - await this.buildingService.getBuildingByUuid(buildingUuid); - return building; - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + const building = await this.buildingService.getBuildingByUuid(buildingUuid); + return building; } @ApiBearerAuth() @@ -72,64 +56,36 @@ export class BuildingController { @Param('buildingUuid') buildingUuid: string, @Query() query: GetBuildingChildDto, ) { - try { - const building = await this.buildingService.getBuildingChildByUuid( - buildingUuid, - query, - ); - return building; - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + const building = await this.buildingService.getBuildingChildByUuid( + buildingUuid, + query, + ); + return building; } @ApiBearerAuth() @UseGuards(JwtAuthGuard, BuildingPermissionGuard) @Get('parent/:buildingUuid') async getBuildingParentByUuid(@Param('buildingUuid') buildingUuid: string) { - try { - const building = - await this.buildingService.getBuildingParentByUuid(buildingUuid); - return building; - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + const building = + await this.buildingService.getBuildingParentByUuid(buildingUuid); + return building; } @ApiBearerAuth() @UseGuards(AdminRoleGuard, CheckUserBuildingGuard) @Post('user') async addUserBuilding(@Body() addUserBuildingDto: AddUserBuildingDto) { - try { - await this.buildingService.addUserBuilding(addUserBuildingDto); - return { - statusCode: HttpStatus.CREATED, - success: true, - message: 'user building added successfully', - }; - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + await this.buildingService.addUserBuilding(addUserBuildingDto); + return { + statusCode: HttpStatus.CREATED, + success: true, + message: 'user building added successfully', + }; } @ApiBearerAuth() @UseGuards(JwtAuthGuard) @Get('user/:userUuid') async getBuildingsByUserId(@Param('userUuid') userUuid: string) { - try { - return await this.buildingService.getBuildingsByUserId(userUuid); - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + return await this.buildingService.getBuildingsByUserId(userUuid); } @ApiBearerAuth() @@ -139,17 +95,10 @@ export class BuildingController { @Param('buildingUuid') buildingUuid: string, @Body() updateBuildingDto: UpdateBuildingNameDto, ) { - try { - const building = await this.buildingService.renameBuildingByUuid( - buildingUuid, - updateBuildingDto, - ); - return building; - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + const building = await this.buildingService.renameBuildingByUuid( + buildingUuid, + updateBuildingDto, + ); + return building; } } diff --git a/src/common/filters/http-exception/http-exception.filter.spec.ts b/src/common/filters/http-exception/http-exception.filter.spec.ts new file mode 100644 index 0000000..8f016dd --- /dev/null +++ b/src/common/filters/http-exception/http-exception.filter.spec.ts @@ -0,0 +1,7 @@ +import { HttpExceptionFilter } from './http-exception.filter'; + +describe('HttpExceptionFilter', () => { + it('should be defined', () => { + expect(new HttpExceptionFilter()).toBeDefined(); + }); +}); diff --git a/src/common/filters/http-exception/http-exception.filter.ts b/src/common/filters/http-exception/http-exception.filter.ts new file mode 100644 index 0000000..c587769 --- /dev/null +++ b/src/common/filters/http-exception/http-exception.filter.ts @@ -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(); + const request = ctx.getRequest(); + 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); + } +} diff --git a/src/community/controllers/community.controller.ts b/src/community/controllers/community.controller.ts index adc38b7..a651c77 100644 --- a/src/community/controllers/community.controller.ts +++ b/src/community/controllers/community.controller.ts @@ -3,7 +3,6 @@ import { Body, Controller, Get, - HttpException, HttpStatus, Param, Post, @@ -33,51 +32,29 @@ export class CommunityController { @UseGuards(JwtAuthGuard) @Post() async addCommunity(@Body() addCommunityDto: AddCommunityDto) { - try { - const community = - await this.communityService.addCommunity(addCommunityDto); - return { - statusCode: HttpStatus.CREATED, - success: true, - message: 'Community added successfully', - data: community, - }; - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + const community = await this.communityService.addCommunity(addCommunityDto); + return { + statusCode: HttpStatus.CREATED, + success: true, + message: 'Community added successfully', + data: community, + }; } @ApiBearerAuth() @UseGuards(JwtAuthGuard) @Get(':communityUuid') async getCommunityByUuid(@Param('communityUuid') communityUuid: string) { - try { - const community = - await this.communityService.getCommunityByUuid(communityUuid); - return community; - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + const community = + await this.communityService.getCommunityByUuid(communityUuid); + return community; } @ApiBearerAuth() @UseGuards(JwtAuthGuard) @Get() async getCommunities() { - try { - const communities = await this.communityService.getCommunities(); - return communities; - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + const communities = await this.communityService.getCommunities(); + return communities; } @ApiBearerAuth() @UseGuards(JwtAuthGuard) @@ -86,50 +63,29 @@ export class CommunityController { @Param('communityUuid') communityUuid: string, @Query() query: GetCommunityChildDto, ) { - try { - const community = await this.communityService.getCommunityChildByUuid( - communityUuid, - query, - ); - return community; - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + const community = await this.communityService.getCommunityChildByUuid( + communityUuid, + query, + ); + return community; } @ApiBearerAuth() @UseGuards(JwtAuthGuard) @Get('user/:userUuid') async getCommunitiesByUserId(@Param('userUuid') userUuid: string) { - try { - return await this.communityService.getCommunitiesByUserId(userUuid); - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + return await this.communityService.getCommunitiesByUserId(userUuid); } @ApiBearerAuth() @UseGuards(AdminRoleGuard) @Post('user') async addUserCommunity(@Body() addUserCommunityDto: AddUserCommunityDto) { - try { - await this.communityService.addUserCommunity(addUserCommunityDto); - return { - statusCode: HttpStatus.CREATED, - success: true, - message: 'user community added successfully', - }; - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + await this.communityService.addUserCommunity(addUserCommunityDto); + return { + statusCode: HttpStatus.CREATED, + success: true, + message: 'user community added successfully', + }; } @ApiBearerAuth() @UseGuards(JwtAuthGuard) @@ -138,17 +94,10 @@ export class CommunityController { @Param('communityUuid') communityUuid: string, @Body() updateCommunityDto: UpdateCommunityNameDto, ) { - try { - const community = await this.communityService.renameCommunityByUuid( - communityUuid, - updateCommunityDto, - ); - return community; - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + const community = await this.communityService.renameCommunityByUuid( + communityUuid, + updateCommunityDto, + ); + return community; } } diff --git a/src/device-messages/controllers/device-messages.controller.ts b/src/device-messages/controllers/device-messages.controller.ts index c8f7834..f4ae797 100644 --- a/src/device-messages/controllers/device-messages.controller.ts +++ b/src/device-messages/controllers/device-messages.controller.ts @@ -3,7 +3,6 @@ import { Controller, Delete, Get, - HttpException, HttpStatus, Param, Post, @@ -31,22 +30,15 @@ export class DeviceMessagesSubscriptionController { async addDeviceMessagesSubscription( @Body() deviceMessagesAddDto: DeviceMessagesAddDto, ) { - try { - const addDetails = - await this.deviceMessagesSubscriptionService.addDeviceMessagesSubscription( - 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, + const addDetails = + await this.deviceMessagesSubscriptionService.addDeviceMessagesSubscription( + deviceMessagesAddDto, ); - } + return { + statusCode: HttpStatus.CREATED, + message: 'Device Messages Subscription Added Successfully', + data: addDetails, + }; } @ApiBearerAuth() @@ -56,23 +48,16 @@ export class DeviceMessagesSubscriptionController { @Param('deviceUuid') deviceUuid: string, @Param('userUuid') userUuid: string, ) { - try { - const deviceDetails = - await this.deviceMessagesSubscriptionService.getDeviceMessagesSubscription( - userUuid, - 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, + const deviceDetails = + await this.deviceMessagesSubscriptionService.getDeviceMessagesSubscription( + userUuid, + deviceUuid, ); - } + return { + statusCode: HttpStatus.OK, + message: 'User Device Subscription fetched Successfully', + data: deviceDetails, + }; } @ApiBearerAuth() @UseGuards(JwtAuthGuard) @@ -80,19 +65,12 @@ export class DeviceMessagesSubscriptionController { async deleteDeviceMessagesSubscription( @Body() deviceMessagesAddDto: DeviceMessagesAddDto, ) { - try { - await this.deviceMessagesSubscriptionService.deleteDeviceMessagesSubscription( - deviceMessagesAddDto, - ); - return { - statusCode: HttpStatus.OK, - message: 'User subscription deleted Successfully', - }; - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + await this.deviceMessagesSubscriptionService.deleteDeviceMessagesSubscription( + deviceMessagesAddDto, + ); + return { + statusCode: HttpStatus.OK, + message: 'User subscription deleted Successfully', + }; } } diff --git a/src/device/controllers/device.controller.ts b/src/device/controllers/device.controller.ts index 6a844d9..85c338f 100644 --- a/src/device/controllers/device.controller.ts +++ b/src/device/controllers/device.controller.ts @@ -6,7 +6,6 @@ import { Post, Query, Param, - HttpException, HttpStatus, UseGuards, Req, @@ -42,34 +41,20 @@ export class DeviceController { @UseGuards(SuperAdminRoleGuard, CheckDeviceGuard) @Post() async addDeviceUser(@Body() addDeviceDto: AddDeviceDto) { - try { - const device = await this.deviceService.addDeviceUser(addDeviceDto); + const device = await this.deviceService.addDeviceUser(addDeviceDto); - return { - statusCode: HttpStatus.CREATED, - success: true, - message: 'device added successfully', - data: device, - }; - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + return { + statusCode: HttpStatus.CREATED, + success: true, + message: 'device added successfully', + data: device, + }; } @ApiBearerAuth() @UseGuards(JwtAuthGuard) @Get('user/:userUuid') async getDevicesByUser(@Param('userUuid') userUuid: string) { - try { - return await this.deviceService.getDevicesByUser(userUuid); - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + return await this.deviceService.getDevicesByUser(userUuid); } @ApiBearerAuth() @UseGuards(JwtAuthGuard, CheckRoomGuard) @@ -78,31 +63,17 @@ export class DeviceController { @Query() getDeviceByRoomUuidDto: GetDeviceByRoomUuidDto, @Req() req: any, ) { - try { - const userUuid = req.user.uuid; - return await this.deviceService.getDevicesByRoomId( - getDeviceByRoomUuidDto, - userUuid, - ); - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + const userUuid = req.user.uuid; + return await this.deviceService.getDevicesByRoomId( + getDeviceByRoomUuidDto, + userUuid, + ); } @ApiBearerAuth() @UseGuards(JwtAuthGuard) @Get('unit/:unitUuid') async getDevicesByUnitId(@Param('unitUuid') unitUuid: string) { - try { - return await this.deviceService.getDevicesByUnitId(unitUuid); - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + return await this.deviceService.getDevicesByUnitId(unitUuid); } @ApiBearerAuth() @UseGuards(JwtAuthGuard, CheckRoomGuard) @@ -110,23 +81,16 @@ export class DeviceController { async updateDeviceInRoom( @Body() updateDeviceInRoomDto: UpdateDeviceInRoomDto, ) { - try { - const device = await this.deviceService.updateDeviceInRoom( - updateDeviceInRoomDto, - ); + const device = await this.deviceService.updateDeviceInRoom( + updateDeviceInRoomDto, + ); - return { - statusCode: HttpStatus.CREATED, - success: true, - message: 'device updated in room successfully', - data: device, - }; - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + return { + statusCode: HttpStatus.CREATED, + success: true, + message: 'device updated in room successfully', + data: device, + }; } @ApiBearerAuth() @@ -136,18 +100,11 @@ export class DeviceController { @Param('deviceUuid') deviceUuid: string, @Req() req: any, ) { - try { - const userUuid = req.user.uuid; - return await this.deviceService.getDeviceDetailsByDeviceId( - deviceUuid, - userUuid, - ); - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + const userUuid = req.user.uuid; + return await this.deviceService.getDeviceDetailsByDeviceId( + deviceUuid, + userUuid, + ); } @ApiBearerAuth() @UseGuards(JwtAuthGuard, CheckUserHavePermission) @@ -155,29 +112,13 @@ export class DeviceController { async getDeviceInstructionByDeviceId( @Param('deviceUuid') deviceUuid: string, ) { - try { - return await this.deviceService.getDeviceInstructionByDeviceId( - deviceUuid, - ); - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + return await this.deviceService.getDeviceInstructionByDeviceId(deviceUuid); } @ApiBearerAuth() @UseGuards(JwtAuthGuard, CheckUserHavePermission) @Get(':deviceUuid/functions/status') async getDevicesInstructionStatus(@Param('deviceUuid') deviceUuid: string) { - try { - return await this.deviceService.getDevicesInstructionStatus(deviceUuid); - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + return await this.deviceService.getDevicesInstructionStatus(deviceUuid); } @ApiBearerAuth() @@ -187,17 +128,7 @@ export class DeviceController { @Body() controlDeviceDto: ControlDeviceDto, @Param('deviceUuid') deviceUuid: string, ) { - try { - return await this.deviceService.controlDevice( - controlDeviceDto, - deviceUuid, - ); - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + return await this.deviceService.controlDevice(controlDeviceDto, deviceUuid); } @ApiBearerAuth() @UseGuards(JwtAuthGuard) @@ -206,43 +137,22 @@ export class DeviceController { @Param('deviceUuid') deviceUuid: string, @Param('firmwareVersion') firmwareVersion: number, ) { - try { - return await this.deviceService.updateDeviceFirmware( - deviceUuid, - firmwareVersion, - ); - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + return await this.deviceService.updateDeviceFirmware( + deviceUuid, + firmwareVersion, + ); } @ApiBearerAuth() @UseGuards(JwtAuthGuard) @Get('gateway/:gatewayUuid/devices') async getDevicesInGateway(@Param('gatewayUuid') gatewayUuid: string) { - try { - return await this.deviceService.getDevicesInGateway(gatewayUuid); - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + return await this.deviceService.getDevicesInGateway(gatewayUuid); } @ApiBearerAuth() @UseGuards(JwtAuthGuard) @Get() async getAllDevices() { - try { - return await this.deviceService.getAllDevices(); - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + return await this.deviceService.getAllDevices(); } @ApiBearerAuth() @UseGuards(JwtAuthGuard) @@ -251,14 +161,7 @@ export class DeviceController { @Param('deviceUuid') deviceUuid: string, @Query() query: GetDeviceLogsDto, ) { - try { - return await this.deviceService.getDeviceLogs(deviceUuid, query); - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + return await this.deviceService.getDeviceLogs(deviceUuid, query); } @ApiBearerAuth() @UseGuards(JwtAuthGuard) @@ -266,16 +169,7 @@ export class DeviceController { async batchControlDevices( @Body() batchControlDevicesDto: BatchControlDevicesDto, ) { - try { - return await this.deviceService.batchControlDevices( - batchControlDevicesDto, - ); - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + return await this.deviceService.batchControlDevices(batchControlDevicesDto); } @ApiBearerAuth() @UseGuards(JwtAuthGuard) @@ -283,14 +177,7 @@ export class DeviceController { async batchStatusDevices( @Query() batchStatusDevicesDto: BatchStatusDevicesDto, ) { - try { - return await this.deviceService.batchStatusDevices(batchStatusDevicesDto); - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + return await this.deviceService.batchStatusDevices(batchStatusDevicesDto); } @ApiBearerAuth() @UseGuards(JwtAuthGuard) @@ -298,15 +185,8 @@ export class DeviceController { async batchFactoryResetDevices( @Body() batchFactoryResetDevicesDto: BatchFactoryResetDevicesDto, ) { - try { - return await this.deviceService.batchFactoryResetDevices( - batchFactoryResetDevicesDto, - ); - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + return await this.deviceService.batchFactoryResetDevices( + batchFactoryResetDevicesDto, + ); } } diff --git a/src/door-lock/controllers/door.lock.controller.ts b/src/door-lock/controllers/door.lock.controller.ts index 56ba2b8..a56db36 100644 --- a/src/door-lock/controllers/door.lock.controller.ts +++ b/src/door-lock/controllers/door.lock.controller.ts @@ -4,7 +4,6 @@ import { Controller, Post, Param, - HttpException, HttpStatus, Get, Delete, @@ -31,27 +30,20 @@ export class DoorLockController { @Body() addDoorLockDto: AddDoorLockOnlineDto, @Param('doorLockUuid') doorLockUuid: string, ) { - try { - const temporaryPassword = - await this.doorLockService.addOnlineTemporaryPassword( - addDoorLockDto, - 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, + const temporaryPassword = + await this.doorLockService.addOnlineTemporaryPassword( + addDoorLockDto, + doorLockUuid, ); - } + + return { + statusCode: HttpStatus.CREATED, + success: true, + message: 'online temporary password added successfully', + data: { + id: temporaryPassword.id, + }, + }; } @ApiBearerAuth() @UseGuards(JwtAuthGuard) @@ -59,24 +51,17 @@ export class DoorLockController { async addOfflineOneTimeTemporaryPassword( @Param('doorLockUuid') doorLockUuid: string, ) { - try { - const temporaryPassword = - await this.doorLockService.addOfflineOneTimeTemporaryPassword( - 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, + const temporaryPassword = + await this.doorLockService.addOfflineOneTimeTemporaryPassword( + doorLockUuid, ); - } + + return { + statusCode: HttpStatus.CREATED, + success: true, + message: 'offline temporary password added successfully', + data: temporaryPassword, + }; } @ApiBearerAuth() @UseGuards(JwtAuthGuard) @@ -86,25 +71,18 @@ export class DoorLockController { addDoorLockOfflineTempMultipleTimeDto: AddDoorLockOfflineTempMultipleTimeDto, @Param('doorLockUuid') doorLockUuid: string, ) { - try { - const temporaryPassword = - await this.doorLockService.addOfflineMultipleTimeTemporaryPassword( - addDoorLockOfflineTempMultipleTimeDto, - 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, + const temporaryPassword = + await this.doorLockService.addOfflineMultipleTimeTemporaryPassword( + addDoorLockOfflineTempMultipleTimeDto, + doorLockUuid, ); - } + + return { + statusCode: HttpStatus.CREATED, + success: true, + message: 'offline temporary password added successfully', + data: temporaryPassword, + }; } @ApiBearerAuth() @UseGuards(JwtAuthGuard) @@ -112,16 +90,9 @@ export class DoorLockController { async getOnlineTemporaryPasswords( @Param('doorLockUuid') doorLockUuid: string, ) { - try { - return await this.doorLockService.getOnlineTemporaryPasswordsMultiple( - doorLockUuid, - ); - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + return await this.doorLockService.getOnlineTemporaryPasswordsMultiple( + doorLockUuid, + ); } @ApiBearerAuth() @UseGuards(JwtAuthGuard) @@ -130,21 +101,11 @@ export class DoorLockController { @Param('doorLockUuid') doorLockUuid: string, @Param('passwordId') passwordId: string, ) { - try { - await this.doorLockService.deleteDoorLockPassword( - doorLockUuid, - passwordId, - ); - 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, - ); - } + await this.doorLockService.deleteDoorLockPassword(doorLockUuid, passwordId); + return { + statusCode: HttpStatus.OK, + message: 'Temporary Password deleted Successfully', + }; } @ApiBearerAuth() @UseGuards(JwtAuthGuard) @@ -152,16 +113,9 @@ export class DoorLockController { async getOfflineOneTimeTemporaryPasswords( @Param('doorLockUuid') doorLockUuid: string, ) { - try { - return await this.doorLockService.getOfflineOneTimeTemporaryPasswords( - doorLockUuid, - ); - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + return await this.doorLockService.getOfflineOneTimeTemporaryPasswords( + doorLockUuid, + ); } @ApiBearerAuth() @UseGuards(JwtAuthGuard) @@ -169,16 +123,9 @@ export class DoorLockController { async getOfflineMultipleTimeTemporaryPasswords( @Param('doorLockUuid') doorLockUuid: string, ) { - try { - return await this.doorLockService.getOfflineMultipleTimeTemporaryPasswords( - doorLockUuid, - ); - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + return await this.doorLockService.getOfflineMultipleTimeTemporaryPasswords( + doorLockUuid, + ); } @ApiBearerAuth() @@ -190,44 +137,30 @@ export class DoorLockController { @Param('doorLockUuid') doorLockUuid: string, @Param('passwordId') passwordId: string, ) { - try { - const temporaryPassword = - await this.doorLockService.updateOfflineTemporaryPassword( - updateDoorLockOfflineTempDto, - doorLockUuid, - 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, + const temporaryPassword = + await this.doorLockService.updateOfflineTemporaryPassword( + updateDoorLockOfflineTempDto, + doorLockUuid, + passwordId, ); - } + + return { + statusCode: HttpStatus.CREATED, + success: true, + message: 'offline temporary password updated successfully', + data: temporaryPassword, + }; } @ApiBearerAuth() @UseGuards(JwtAuthGuard) @Post('open/:doorLockUuid') async openDoorLock(@Param('doorLockUuid') doorLockUuid: string) { - try { - await this.doorLockService.openDoorLock(doorLockUuid); + await this.doorLockService.openDoorLock(doorLockUuid); - return { - statusCode: HttpStatus.CREATED, - success: true, - message: 'door lock opened successfully', - }; - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + return { + statusCode: HttpStatus.CREATED, + success: true, + message: 'door lock opened successfully', + }; } } diff --git a/src/error-message/error-message.service.spec.ts b/src/error-message/error-message.service.spec.ts new file mode 100644 index 0000000..ae9c353 --- /dev/null +++ b/src/error-message/error-message.service.spec.ts @@ -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); + }); + + it('should be defined', () => { + expect(service).toBeDefined(); + }); +}); diff --git a/src/error-message/error-message.service.ts b/src/error-message/error-message.service.ts new file mode 100644 index 0000000..83f7497 --- /dev/null +++ b/src/error-message/error-message.service.ts @@ -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 { + 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; + } +} diff --git a/src/floor/controllers/floor.controller.ts b/src/floor/controllers/floor.controller.ts index be7d921..a38e117 100644 --- a/src/floor/controllers/floor.controller.ts +++ b/src/floor/controllers/floor.controller.ts @@ -3,7 +3,6 @@ import { Body, Controller, Get, - HttpException, HttpStatus, Param, Post, @@ -33,35 +32,21 @@ export class FloorController { @UseGuards(JwtAuthGuard, CheckBuildingTypeGuard) @Post() async addFloor(@Body() addFloorDto: AddFloorDto) { - try { - const floor = await this.floorService.addFloor(addFloorDto); - return { - statusCode: HttpStatus.CREATED, - success: true, - message: 'Floor added successfully', - data: floor, - }; - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + const floor = await this.floorService.addFloor(addFloorDto); + return { + statusCode: HttpStatus.CREATED, + success: true, + message: 'Floor added successfully', + data: floor, + }; } @ApiBearerAuth() @UseGuards(JwtAuthGuard, FloorPermissionGuard) @Get(':floorUuid') async getFloorByUuid(@Param('floorUuid') floorUuid: string) { - try { - const floor = await this.floorService.getFloorByUuid(floorUuid); - return floor; - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + const floor = await this.floorService.getFloorByUuid(floorUuid); + return floor; } @ApiBearerAuth() @@ -71,65 +56,34 @@ export class FloorController { @Param('floorUuid') floorUuid: string, @Query() query: GetFloorChildDto, ) { - try { - const floor = await this.floorService.getFloorChildByUuid( - floorUuid, - query, - ); - return floor; - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + const floor = await this.floorService.getFloorChildByUuid(floorUuid, query); + return floor; } @ApiBearerAuth() @UseGuards(JwtAuthGuard, FloorPermissionGuard) @Get('parent/:floorUuid') async getFloorParentByUuid(@Param('floorUuid') floorUuid: string) { - try { - const floor = await this.floorService.getFloorParentByUuid(floorUuid); - return floor; - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + const floor = await this.floorService.getFloorParentByUuid(floorUuid); + return floor; } @ApiBearerAuth() @UseGuards(AdminRoleGuard, CheckUserFloorGuard) @Post('user') async addUserFloor(@Body() addUserFloorDto: AddUserFloorDto) { - try { - await this.floorService.addUserFloor(addUserFloorDto); - return { - statusCode: HttpStatus.CREATED, - success: true, - message: 'user floor added successfully', - }; - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + await this.floorService.addUserFloor(addUserFloorDto); + return { + statusCode: HttpStatus.CREATED, + success: true, + message: 'user floor added successfully', + }; } @ApiBearerAuth() @UseGuards(JwtAuthGuard) @Get('user/:userUuid') async getFloorsByUserId(@Param('userUuid') userUuid: string) { - try { - return await this.floorService.getFloorsByUserId(userUuid); - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + return await this.floorService.getFloorsByUserId(userUuid); } @ApiBearerAuth() @@ -139,17 +93,10 @@ export class FloorController { @Param('floorUuid') floorUuid: string, @Body() updateFloorNameDto: UpdateFloorNameDto, ) { - try { - const floor = await this.floorService.renameFloorByUuid( - floorUuid, - updateFloorNameDto, - ); - return floor; - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + const floor = await this.floorService.renameFloorByUuid( + floorUuid, + updateFloorNameDto, + ); + return floor; } } diff --git a/src/group/controllers/group.controller.ts b/src/group/controllers/group.controller.ts index ce6fae8..ca7e1c5 100644 --- a/src/group/controllers/group.controller.ts +++ b/src/group/controllers/group.controller.ts @@ -1,13 +1,5 @@ import { GroupService } from '../services/group.service'; -import { - Controller, - Get, - UseGuards, - Param, - HttpException, - HttpStatus, - Req, -} from '@nestjs/common'; +import { Controller, Get, UseGuards, Param, Req } from '@nestjs/common'; import { ApiTags, ApiBearerAuth } from '@nestjs/swagger'; import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard'; import { UnitPermissionGuard } from 'src/guards/unit.permission.guard'; @@ -24,14 +16,7 @@ export class GroupController { @UseGuards(JwtAuthGuard, UnitPermissionGuard) @Get(':unitUuid') async getGroupsBySpaceUuid(@Param('unitUuid') unitUuid: string) { - try { - return await this.groupService.getGroupsByUnitUuid(unitUuid); - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + return await this.groupService.getGroupsByUnitUuid(unitUuid); } @ApiBearerAuth() @UseGuards(JwtAuthGuard, UnitPermissionGuard) @@ -41,19 +26,12 @@ export class GroupController { @Param('groupName') groupName: string, @Req() req: any, ) { - try { - const userUuid = req.user.uuid; + const userUuid = req.user.uuid; - return await this.groupService.getUnitDevicesByGroupName( - unitUuid, - groupName, - userUuid, - ); - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + return await this.groupService.getUnitDevicesByGroupName( + unitUuid, + groupName, + userUuid, + ); } } diff --git a/src/main.ts b/src/main.ts index 894ecd1..6dbfc45 100644 --- a/src/main.ts +++ b/src/main.ts @@ -6,6 +6,7 @@ import { setupSwaggerAuthentication } from '../libs/common/src/util/user-auth.sw import { ValidationPipe } from '@nestjs/common'; import { json, urlencoded } from 'body-parser'; import { SeederService } from '@app/common/seed/services/seeder.service'; +import { HttpExceptionFilter } from './common/filters/http-exception/http-exception.filter'; async function bootstrap() { const app = await NestFactory.create(AppModule); @@ -15,6 +16,7 @@ async function bootstrap() { // Set the body parser limit to 1 MB app.use(json({ limit: '1mb' })); app.use(urlencoded({ limit: '1mb', extended: true })); + app.useGlobalFilters(new HttpExceptionFilter()); app.use( rateLimit({ diff --git a/src/region/controllers/region.controller.ts b/src/region/controllers/region.controller.ts index 8df4a11..0affb5a 100644 --- a/src/region/controllers/region.controller.ts +++ b/src/region/controllers/region.controller.ts @@ -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 { ApiTags, ApiOperation } from '@nestjs/swagger'; import { ControllerRoute } from '@app/common/constants/controller-route'; @@ -17,13 +17,6 @@ export class RegionController { description: ControllerRoute.REGION.ACTIONS.GET_REGIONS_DESCRIPTION, }) async getAllRegions() { - try { - return await this.regionService.getAllRegions(); - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + return await this.regionService.getAllRegions(); } } diff --git a/src/region/region.module.ts b/src/region/region.module.ts index c0da5ab..22eaa2d 100644 --- a/src/region/region.module.ts +++ b/src/region/region.module.ts @@ -3,9 +3,10 @@ import { RegionService } from './services/region.service'; import { RegionController } from './controllers/region.controller'; import { ConfigModule } from '@nestjs/config'; import { RegionRepository } from '@app/common/modules/region/repositories'; +import { CommonModule } from '@app/common'; @Module({ - imports: [ConfigModule], + imports: [ConfigModule, CommonModule], controllers: [RegionController], providers: [RegionService, RegionRepository], exports: [RegionService], diff --git a/src/region/services/region.service.ts b/src/region/services/region.service.ts index 580f285..9061e0a 100644 --- a/src/region/services/region.service.ts +++ b/src/region/services/region.service.ts @@ -2,23 +2,33 @@ import { BadRequestException, HttpException, HttpStatus, + Inject, Injectable, } from '@nestjs/common'; import { RegionRepository } from '@app/common/modules/region/repositories'; +import { ErrorMessageService } from 'src/error-message/error-message.service'; @Injectable() export class RegionService { - constructor(private readonly regionRepository: RegionRepository) {} + constructor( + private readonly regionRepository: RegionRepository, + @Inject(ErrorMessageService) + private readonly errorMessageService: ErrorMessageService, + ) {} async getAllRegions() { try { const regions = await this.regionRepository.find(); - return regions; } catch (err) { if (err instanceof BadRequestException) { throw err; // Re-throw BadRequestException } else { - throw new HttpException('Regions found', HttpStatus.NOT_FOUND); + throw new HttpException( + this.errorMessageService.getMessage('NOT_FOUND', { + entity: 'Regions', + }), + HttpStatus.NOT_FOUND, + ); } } } diff --git a/src/role/controllers/role.controller.ts b/src/role/controllers/role.controller.ts index cb2038d..4c409ec 100644 --- a/src/role/controllers/role.controller.ts +++ b/src/role/controllers/role.controller.ts @@ -2,7 +2,6 @@ import { Body, Controller, Get, - HttpException, HttpStatus, Post, UseGuards, @@ -23,32 +22,21 @@ export class RoleController { @UseGuards(SuperAdminRoleGuard) @Get('types') async fetchRoleTypes() { - try { - const roleTypes = await this.roleService.fetchRoleTypes(); - return { - statusCode: HttpStatus.OK, - message: 'Role Types fetched Successfully', - data: roleTypes, - }; - } catch (err) { - throw new Error(err); - } + const roleTypes = await this.roleService.fetchRoleTypes(); + return { + statusCode: HttpStatus.OK, + message: 'Role Types fetched Successfully', + data: roleTypes, + }; } @ApiBearerAuth() @UseGuards(SuperAdminRoleGuard) @Post() async addUserRoleType(@Body() addUserRoleDto: AddUserRoleDto) { - try { - await this.roleService.addUserRoleType(addUserRoleDto); - return { - statusCode: HttpStatus.OK, - message: 'User Role Added Successfully', - }; - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + await this.roleService.addUserRoleType(addUserRoleDto); + return { + statusCode: HttpStatus.OK, + message: 'User Role Added Successfully', + }; } } diff --git a/src/room/controllers/room.controller.ts b/src/room/controllers/room.controller.ts index 8564b0e..b16f323 100644 --- a/src/room/controllers/room.controller.ts +++ b/src/room/controllers/room.controller.ts @@ -3,7 +3,6 @@ import { Body, Controller, Get, - HttpException, HttpStatus, Param, Post, @@ -31,81 +30,46 @@ export class RoomController { @UseGuards(JwtAuthGuard, CheckUnitTypeGuard) @Post() async addRoom(@Body() addRoomDto: AddRoomDto) { - try { - const room = await this.roomService.addRoom(addRoomDto); - return { - statusCode: HttpStatus.CREATED, - success: true, - message: 'Room added successfully', - data: room, - }; - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + const room = await this.roomService.addRoom(addRoomDto); + return { + statusCode: HttpStatus.CREATED, + success: true, + message: 'Room added successfully', + data: room, + }; } @ApiBearerAuth() @UseGuards(JwtAuthGuard, RoomPermissionGuard) @Get(':roomUuid') async getRoomByUuid(@Param('roomUuid') roomUuid: string) { - try { - const room = await this.roomService.getRoomByUuid(roomUuid); - return room; - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + const room = await this.roomService.getRoomByUuid(roomUuid); + return room; } @ApiBearerAuth() @UseGuards(JwtAuthGuard, RoomPermissionGuard) @Get('parent/:roomUuid') async getRoomParentByUuid(@Param('roomUuid') roomUuid: string) { - try { - const room = await this.roomService.getRoomParentByUuid(roomUuid); - return room; - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + const room = await this.roomService.getRoomParentByUuid(roomUuid); + return room; } @ApiBearerAuth() @UseGuards(AdminRoleGuard, CheckUserRoomGuard) @Post('user') async addUserRoom(@Body() addUserRoomDto: AddUserRoomDto) { - try { - await this.roomService.addUserRoom(addUserRoomDto); - return { - statusCode: HttpStatus.CREATED, - success: true, - message: 'user room added successfully', - }; - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + await this.roomService.addUserRoom(addUserRoomDto); + return { + statusCode: HttpStatus.CREATED, + success: true, + message: 'user room added successfully', + }; } @ApiBearerAuth() @UseGuards(JwtAuthGuard) @Get('user/:userUuid') async getRoomsByUserId(@Param('userUuid') userUuid: string) { - try { - return await this.roomService.getRoomsByUserId(userUuid); - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + return await this.roomService.getRoomsByUserId(userUuid); } @ApiBearerAuth() @@ -115,17 +79,10 @@ export class RoomController { @Param('roomUuid') roomUuid: string, @Body() updateRoomNameDto: UpdateRoomNameDto, ) { - try { - const room = await this.roomService.renameRoomByUuid( - roomUuid, - updateRoomNameDto, - ); - return room; - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + const room = await this.roomService.renameRoomByUuid( + roomUuid, + updateRoomNameDto, + ); + return room; } } diff --git a/src/scene/controllers/scene.controller.ts b/src/scene/controllers/scene.controller.ts index 40af2a8..5f3d73f 100644 --- a/src/scene/controllers/scene.controller.ts +++ b/src/scene/controllers/scene.controller.ts @@ -4,7 +4,6 @@ import { Controller, Delete, Get, - HttpException, HttpStatus, Param, Post, @@ -27,36 +26,22 @@ export class SceneController { @UseGuards(JwtAuthGuard) @Post('tap-to-run') async addTapToRunScene(@Body() addSceneTapToRunDto: AddSceneTapToRunDto) { - try { - const tapToRunScene = - await this.sceneService.addTapToRunScene(addSceneTapToRunDto); - return { - statusCode: HttpStatus.CREATED, - success: true, - message: 'Scene added successfully', - data: tapToRunScene, - }; - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + const tapToRunScene = + await this.sceneService.addTapToRunScene(addSceneTapToRunDto); + return { + statusCode: HttpStatus.CREATED, + success: true, + message: 'Scene added successfully', + data: tapToRunScene, + }; } @ApiBearerAuth() @UseGuards(JwtAuthGuard) @Get('tap-to-run/:unitUuid') async getTapToRunSceneByUnit(@Param('unitUuid') unitUuid: string) { - try { - const tapToRunScenes = - await this.sceneService.getTapToRunSceneByUnit(unitUuid); - return tapToRunScenes; - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + const tapToRunScenes = + await this.sceneService.getTapToRunSceneByUnit(unitUuid); + return tapToRunScenes; } @ApiBearerAuth() @UseGuards(JwtAuthGuard) @@ -65,53 +50,31 @@ export class SceneController { @Param('unitUuid') unitUuid: string, @Param('sceneId') sceneId: string, ) { - try { - await this.sceneService.deleteTapToRunScene(unitUuid, sceneId); - return { - statusCode: HttpStatus.OK, - message: 'Scene Deleted Successfully', - }; - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + await this.sceneService.deleteTapToRunScene(unitUuid, sceneId); + return { + statusCode: HttpStatus.OK, + message: 'Scene Deleted Successfully', + }; } @ApiBearerAuth() @UseGuards(JwtAuthGuard) @Post('tap-to-run/trigger/:sceneId') async triggerTapToRunScene(@Param('sceneId') sceneId: string) { - try { - await this.sceneService.triggerTapToRunScene(sceneId); - return { - statusCode: HttpStatus.CREATED, - success: true, - message: 'Scene trigger successfully', - }; - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + await this.sceneService.triggerTapToRunScene(sceneId); + return { + statusCode: HttpStatus.CREATED, + success: true, + message: 'Scene trigger successfully', + }; } @ApiBearerAuth() @UseGuards(JwtAuthGuard) @Get('tap-to-run/details/:sceneId') async getTapToRunSceneDetails(@Param('sceneId') sceneId: string) { - try { - const tapToRunScenes = - await this.sceneService.getTapToRunSceneDetails(sceneId); - return tapToRunScenes; - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - ``; - } + const tapToRunScenes = + await this.sceneService.getTapToRunSceneDetails(sceneId); + return tapToRunScenes; } @ApiBearerAuth() @UseGuards(JwtAuthGuard) @@ -120,22 +83,15 @@ export class SceneController { @Body() updateSceneTapToRunDto: UpdateSceneTapToRunDto, @Param('sceneId') sceneId: string, ) { - try { - const tapToRunScene = await this.sceneService.updateTapToRunScene( - updateSceneTapToRunDto, - sceneId, - ); - return { - statusCode: HttpStatus.CREATED, - success: true, - message: 'Scene updated successfully', - data: tapToRunScene, - }; - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + const tapToRunScene = await this.sceneService.updateTapToRunScene( + updateSceneTapToRunDto, + sceneId, + ); + return { + statusCode: HttpStatus.CREATED, + success: true, + message: 'Scene updated successfully', + data: tapToRunScene, + }; } } diff --git a/src/schedule/controllers/schedule.controller.ts b/src/schedule/controllers/schedule.controller.ts index 63bf182..13e78fd 100644 --- a/src/schedule/controllers/schedule.controller.ts +++ b/src/schedule/controllers/schedule.controller.ts @@ -5,7 +5,6 @@ import { Get, Post, Param, - HttpException, HttpStatus, UseGuards, Put, @@ -36,24 +35,17 @@ export class ScheduleController { @Param('deviceUuid') deviceUuid: string, @Body() addScheduleDto: AddScheduleDto, ) { - try { - const schedule = await this.scheduleService.addDeviceSchedule( - deviceUuid, - addScheduleDto, - ); + const schedule = await this.scheduleService.addDeviceSchedule( + deviceUuid, + addScheduleDto, + ); - return { - statusCode: HttpStatus.CREATED, - success: true, - message: 'schedule added successfully', - data: schedule, - }; - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + return { + statusCode: HttpStatus.CREATED, + success: true, + message: 'schedule added successfully', + data: schedule, + }; } @ApiBearerAuth() @UseGuards(JwtAuthGuard) @@ -62,17 +54,10 @@ export class ScheduleController { @Param('deviceUuid') deviceUuid: string, @Query() query: GetScheduleDeviceDto, ) { - try { - return await this.scheduleService.getDeviceScheduleByCategory( - deviceUuid, - query.category, - ); - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + return await this.scheduleService.getDeviceScheduleByCategory( + deviceUuid, + query.category, + ); } @ApiBearerAuth() @UseGuards(JwtAuthGuard) @@ -81,19 +66,12 @@ export class ScheduleController { @Param('deviceUuid') deviceUuid: string, @Param('scheduleId') scheduleId: string, ) { - try { - await this.scheduleService.deleteDeviceSchedule(deviceUuid, scheduleId); - return { - statusCode: HttpStatus.CREATED, - success: true, - message: 'schedule deleted successfully', - }; - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + await this.scheduleService.deleteDeviceSchedule(deviceUuid, scheduleId); + return { + statusCode: HttpStatus.CREATED, + success: true, + message: 'schedule deleted successfully', + }; } @ApiBearerAuth() @UseGuards(JwtAuthGuard) @@ -102,22 +80,15 @@ export class ScheduleController { @Param('deviceUuid') deviceUuid: string, @Body() enableScheduleDto: EnableScheduleDto, ) { - try { - await this.scheduleService.enableDeviceSchedule( - deviceUuid, - enableScheduleDto, - ); - return { - statusCode: HttpStatus.CREATED, - success: true, - message: 'schedule updated successfully', - }; - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + await this.scheduleService.enableDeviceSchedule( + deviceUuid, + enableScheduleDto, + ); + return { + statusCode: HttpStatus.CREATED, + success: true, + message: 'schedule updated successfully', + }; } @ApiBearerAuth() @UseGuards(JwtAuthGuard) @@ -126,23 +97,16 @@ export class ScheduleController { @Param('deviceUuid') deviceUuid: string, @Body() updateScheduleDto: UpdateScheduleDto, ) { - try { - const schedule = await this.scheduleService.updateDeviceSchedule( - deviceUuid, - updateScheduleDto, - ); + const schedule = await this.scheduleService.updateDeviceSchedule( + deviceUuid, + updateScheduleDto, + ); - return { - statusCode: HttpStatus.CREATED, - success: true, - message: 'schedule updated successfully', - data: schedule, - }; - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + return { + statusCode: HttpStatus.CREATED, + success: true, + message: 'schedule updated successfully', + data: schedule, + }; } } diff --git a/src/timezone/controllers/timezone.controller.ts b/src/timezone/controllers/timezone.controller.ts index fc4dd8a..b31e473 100644 --- a/src/timezone/controllers/timezone.controller.ts +++ b/src/timezone/controllers/timezone.controller.ts @@ -1,10 +1,4 @@ -import { - Controller, - Get, - HttpException, - HttpStatus, - UseGuards, -} from '@nestjs/common'; +import { Controller, Get, UseGuards } from '@nestjs/common'; import { TimeZoneService } from '../services/timezone.service'; import { ApiTags, ApiBearerAuth } from '@nestjs/swagger'; import { JwtAuthGuard } from '../../../libs/common/src/guards/jwt.auth.guard'; @@ -21,13 +15,6 @@ export class TimeZoneController { @UseGuards(JwtAuthGuard) @Get() async getAllTimeZones() { - try { - return await this.timeZoneService.getAllTimeZones(); - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + return await this.timeZoneService.getAllTimeZones(); } } diff --git a/src/unit/controllers/unit.controller.ts b/src/unit/controllers/unit.controller.ts index 9f3e50b..e98f2f7 100644 --- a/src/unit/controllers/unit.controller.ts +++ b/src/unit/controllers/unit.controller.ts @@ -3,7 +3,6 @@ import { Body, Controller, Get, - HttpException, HttpStatus, Param, Post, @@ -36,35 +35,21 @@ export class UnitController { @UseGuards(JwtAuthGuard, CheckFloorTypeGuard) @Post() async addUnit(@Body() addUnitDto: AddUnitDto) { - try { - const unit = await this.unitService.addUnit(addUnitDto); - return { - statusCode: HttpStatus.CREATED, - success: true, - message: 'Unit added successfully', - data: unit, - }; - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + const unit = await this.unitService.addUnit(addUnitDto); + return { + statusCode: HttpStatus.CREATED, + success: true, + message: 'Unit added successfully', + data: unit, + }; } @ApiBearerAuth() @UseGuards(JwtAuthGuard, UnitPermissionGuard) @Get(':unitUuid') async getUnitByUuid(@Param('unitUuid') unitUuid: string) { - try { - const unit = await this.unitService.getUnitByUuid(unitUuid); - return unit; - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + const unit = await this.unitService.getUnitByUuid(unitUuid); + return unit; } @ApiBearerAuth() @@ -74,60 +59,32 @@ export class UnitController { @Param('unitUuid') unitUuid: string, @Query() query: GetUnitChildDto, ) { - try { - const unit = await this.unitService.getUnitChildByUuid(unitUuid, query); - return unit; - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + const unit = await this.unitService.getUnitChildByUuid(unitUuid, query); + return unit; } @ApiBearerAuth() @UseGuards(JwtAuthGuard, UnitPermissionGuard) @Get('parent/:unitUuid') async getUnitParentByUuid(@Param('unitUuid') unitUuid: string) { - try { - const unit = await this.unitService.getUnitParentByUuid(unitUuid); - return unit; - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + const unit = await this.unitService.getUnitParentByUuid(unitUuid); + return unit; } @ApiBearerAuth() @UseGuards(JwtAuthGuard, CheckUserUnitGuard) @Post('user') async addUserUnit(@Body() addUserUnitDto: AddUserUnitDto) { - try { - await this.unitService.addUserUnit(addUserUnitDto); - return { - statusCode: HttpStatus.CREATED, - success: true, - message: 'user unit added successfully', - }; - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + await this.unitService.addUserUnit(addUserUnitDto); + return { + statusCode: HttpStatus.CREATED, + success: true, + message: 'user unit added successfully', + }; } @ApiBearerAuth() @UseGuards(JwtAuthGuard) @Get('user/:userUuid') async getUnitsByUserId(@Param('userUuid') userUuid: string) { - try { - return await this.unitService.getUnitsByUserId(userUuid); - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + return await this.unitService.getUnitsByUserId(userUuid); } @ApiBearerAuth() @@ -137,32 +94,18 @@ export class UnitController { @Param('unitUuid') unitUuid: string, @Body() updateUnitNameDto: UpdateUnitNameDto, ) { - try { - const unit = await this.unitService.renameUnitByUuid( - unitUuid, - updateUnitNameDto, - ); - return unit; - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + const unit = await this.unitService.renameUnitByUuid( + unitUuid, + updateUnitNameDto, + ); + return unit; } @ApiBearerAuth() @UseGuards(JwtAuthGuard, UnitPermissionGuard) @Get(':unitUuid/invitation-code') async getUnitInvitationCode(@Param('unitUuid') unitUuid: string) { - try { - const unit = await this.unitService.getUnitInvitationCode(unitUuid); - return unit; - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + const unit = await this.unitService.getUnitInvitationCode(unitUuid); + return unit; } @ApiBearerAuth() @@ -171,18 +114,11 @@ export class UnitController { async verifyCodeAndAddUserUnit( @Body() addUserUnitUsingCodeDto: AddUserUnitUsingCodeDto, ) { - try { - await this.unitService.verifyCodeAndAddUserUnit(addUserUnitUsingCodeDto); - return { - statusCode: HttpStatus.CREATED, - success: true, - message: 'user unit added successfully', - }; - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + await this.unitService.verifyCodeAndAddUserUnit(addUserUnitUsingCodeDto); + return { + statusCode: HttpStatus.CREATED, + success: true, + message: 'user unit added successfully', + }; } } diff --git a/src/user-device-permission/controllers/user-device-permission.controller.ts b/src/user-device-permission/controllers/user-device-permission.controller.ts index d54637e..b8e5ccd 100644 --- a/src/user-device-permission/controllers/user-device-permission.controller.ts +++ b/src/user-device-permission/controllers/user-device-permission.controller.ts @@ -3,7 +3,6 @@ import { Controller, Delete, Get, - HttpException, HttpStatus, Param, Post, @@ -32,22 +31,14 @@ export class UserDevicePermissionController { async addDevicePermission( @Body() userDevicePermissionDto: UserDevicePermissionAddDto, ) { - try { - const addDetails = - await this.userDevicePermissionService.addUserPermission( - userDevicePermissionDto, - ); - return { - statusCode: HttpStatus.CREATED, - 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, - ); - } + const addDetails = await this.userDevicePermissionService.addUserPermission( + userDevicePermissionDto, + ); + return { + statusCode: HttpStatus.CREATED, + message: 'User Permission for Devices Added Successfully', + data: addDetails, + }; } @ApiBearerAuth() @@ -57,38 +48,27 @@ export class UserDevicePermissionController { @Param('devicePermissionUuid') devicePermissionUuid: string, @Body() userDevicePermissionEditDto: UserDevicePermissionEditDto, ) { - try { - await this.userDevicePermissionService.editUserPermission( - devicePermissionUuid, - userDevicePermissionEditDto, - ); - return { - statusCode: HttpStatus.OK, - message: 'User Permission for Devices Updated Successfully', - }; - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + await this.userDevicePermissionService.editUserPermission( + devicePermissionUuid, + userDevicePermissionEditDto, + ); + return { + statusCode: HttpStatus.OK, + message: 'User Permission for Devices Updated Successfully', + }; } @ApiBearerAuth() @UseGuards(AdminRoleGuard) @Get(':deviceUuid') async fetchDevicePermission(@Param('deviceUuid') deviceUuid: string) { - try { - const deviceDetails = - await this.userDevicePermissionService.fetchUserPermission(deviceUuid); - return { - statusCode: HttpStatus.OK, - message: 'Device Details fetched Successfully', - data: deviceDetails, - }; - } catch (err) { - throw new Error(err); - } + const deviceDetails = + await this.userDevicePermissionService.fetchUserPermission(deviceUuid); + return { + statusCode: HttpStatus.OK, + message: 'Device Details fetched Successfully', + data: deviceDetails, + }; } @ApiBearerAuth() @UseGuards(AdminRoleGuard) @@ -96,19 +76,12 @@ export class UserDevicePermissionController { async deleteDevicePermission( @Param('devicePermissionUuid') devicePermissionUuid: string, ) { - try { - await this.userDevicePermissionService.deleteDevicePermission( - devicePermissionUuid, - ); - return { - statusCode: HttpStatus.OK, - message: 'User Permission for Devices Deleted Successfully', - }; - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + await this.userDevicePermissionService.deleteDevicePermission( + devicePermissionUuid, + ); + return { + statusCode: HttpStatus.OK, + message: 'User Permission for Devices Deleted Successfully', + }; } } diff --git a/src/user-notification/controllers/user-notification.controller.ts b/src/user-notification/controllers/user-notification.controller.ts index 83fd215..ce52662 100644 --- a/src/user-notification/controllers/user-notification.controller.ts +++ b/src/user-notification/controllers/user-notification.controller.ts @@ -2,7 +2,6 @@ import { Body, Controller, Get, - HttpException, HttpStatus, Param, Post, @@ -34,41 +33,27 @@ export class UserNotificationController { async addUserSubscription( @Body() userNotificationAddDto: UserNotificationAddDto, ) { - try { - const addDetails = await this.userNotificationService.addUserSubscription( - userNotificationAddDto, - ); - return { - statusCode: HttpStatus.CREATED, - message: 'User Notification Added Successfully', - data: addDetails, - }; - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + const addDetails = await this.userNotificationService.addUserSubscription( + userNotificationAddDto, + ); + return { + statusCode: HttpStatus.CREATED, + message: 'User Notification Added Successfully', + data: addDetails, + }; } @ApiBearerAuth() @UseGuards(JwtAuthGuard) @Get(':userUuid') async fetchUserSubscriptions(@Param('userUuid') userUuid: string) { - try { - const userDetails = - await this.userNotificationService.fetchUserSubscriptions(userUuid); - return { - statusCode: HttpStatus.OK, - message: 'User Notification fetched Successfully', - data: { ...userDetails }, - }; - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + const userDetails = + await this.userNotificationService.fetchUserSubscriptions(userUuid); + return { + statusCode: HttpStatus.OK, + message: 'User Notification fetched Successfully', + data: { ...userDetails }, + }; } @ApiBearerAuth() @UseGuards(JwtAuthGuard) @@ -76,19 +61,12 @@ export class UserNotificationController { async updateUserSubscription( @Body() userNotificationUpdateDto: UserNotificationUpdateDto, ) { - try { - await this.userNotificationService.updateUserSubscription( - userNotificationUpdateDto, - ); - return { - statusCode: HttpStatus.OK, - message: 'User subscription updated Successfully', - }; - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + await this.userNotificationService.updateUserSubscription( + userNotificationUpdateDto, + ); + return { + statusCode: HttpStatus.OK, + message: 'User subscription updated Successfully', + }; } } diff --git a/src/users/controllers/user.controller.ts b/src/users/controllers/user.controller.ts index c669f08..0940587 100644 --- a/src/users/controllers/user.controller.ts +++ b/src/users/controllers/user.controller.ts @@ -3,7 +3,6 @@ import { Controller, Delete, Get, - HttpException, HttpStatus, Param, Put, @@ -33,14 +32,7 @@ export class UserController { @UseGuards(JwtAuthGuard) @Get(':userUuid') async getUserDetailsByUserUuid(@Param('userUuid') userUuid: string) { - try { - return await this.userService.getUserDetailsByUserUuid(userUuid); - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + return await this.userService.getUserDetailsByUserUuid(userUuid); } @ApiBearerAuth() @UseGuards(JwtAuthGuard, CheckProfilePictureGuard) @@ -49,23 +41,16 @@ export class UserController { @Param('userUuid') userUuid: string, @Body() updateProfilePictureDataDto: UpdateProfilePictureDataDto, ) { - try { - const userData = await this.userService.updateProfilePictureByUserUuid( - userUuid, - updateProfilePictureDataDto, - ); - return { - statusCode: HttpStatus.CREATED, - success: true, - message: 'profile picture updated successfully', - data: userData, - }; - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + const userData = await this.userService.updateProfilePictureByUserUuid( + userUuid, + updateProfilePictureDataDto, + ); + return { + statusCode: HttpStatus.CREATED, + success: true, + message: 'profile picture updated successfully', + data: userData, + }; } @ApiBearerAuth() @UseGuards(JwtAuthGuard) @@ -74,23 +59,16 @@ export class UserController { @Param('userUuid') userUuid: string, @Body() updateRegionDataDto: UpdateRegionDataDto, ) { - try { - const userData = await this.userService.updateRegionByUserUuid( - userUuid, - updateRegionDataDto, - ); - return { - statusCode: HttpStatus.CREATED, - success: true, - message: 'region updated successfully', - data: userData, - }; - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + const userData = await this.userService.updateRegionByUserUuid( + userUuid, + updateRegionDataDto, + ); + return { + statusCode: HttpStatus.CREATED, + success: true, + message: 'region updated successfully', + data: userData, + }; } @ApiBearerAuth() @UseGuards(JwtAuthGuard) @@ -99,23 +77,16 @@ export class UserController { @Param('userUuid') userUuid: string, @Body() updateTimezoneDataDto: UpdateTimezoneDataDto, ) { - try { - const userData = await this.userService.updateTimezoneByUserUuid( - userUuid, - updateTimezoneDataDto, - ); - return { - statusCode: HttpStatus.CREATED, - success: true, - message: 'timezone updated successfully', - data: userData, - }; - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + const userData = await this.userService.updateTimezoneByUserUuid( + userUuid, + updateTimezoneDataDto, + ); + return { + statusCode: HttpStatus.CREATED, + success: true, + message: 'timezone updated successfully', + data: userData, + }; } @ApiBearerAuth() @UseGuards(JwtAuthGuard) @@ -124,23 +95,16 @@ export class UserController { @Param('userUuid') userUuid: string, @Body() updateNameDto: UpdateNameDto, ) { - try { - const userData = await this.userService.updateNameByUserUuid( - userUuid, - updateNameDto, - ); - return { - statusCode: HttpStatus.CREATED, - success: true, - message: 'name updated successfully', - data: userData, - }; - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + const userData = await this.userService.updateNameByUserUuid( + userUuid, + updateNameDto, + ); + return { + statusCode: HttpStatus.CREATED, + success: true, + message: 'name updated successfully', + data: userData, + }; } @ApiBearerAuth() @UseGuards(SuperAdminRoleGuard) diff --git a/src/vistor-password/controllers/visitor-password.controller.ts b/src/vistor-password/controllers/visitor-password.controller.ts index bdd4324..24515ca 100644 --- a/src/vistor-password/controllers/visitor-password.controller.ts +++ b/src/vistor-password/controllers/visitor-password.controller.ts @@ -3,7 +3,6 @@ import { Body, Controller, Post, - HttpException, HttpStatus, UseGuards, Get, @@ -34,24 +33,17 @@ export class VisitorPasswordController { @Body() addDoorLockOnlineMultipleDto: AddDoorLockOnlineMultipleDto, @Req() req: any, ) { - try { - const userUuid = req.user.uuid; - const temporaryPasswords = - await this.visitorPasswordService.addOnlineTemporaryPasswordMultipleTime( - addDoorLockOnlineMultipleDto, - userUuid, - ); - - return { - statusCode: HttpStatus.CREATED, - data: temporaryPasswords, - }; - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, + const userUuid = req.user.uuid; + const temporaryPasswords = + await this.visitorPasswordService.addOnlineTemporaryPasswordMultipleTime( + addDoorLockOnlineMultipleDto, + userUuid, ); - } + + return { + statusCode: HttpStatus.CREATED, + data: temporaryPasswords, + }; } @ApiBearerAuth() @UseGuards(JwtAuthGuard) @@ -60,24 +52,17 @@ export class VisitorPasswordController { @Body() addDoorLockOnlineOneTimeDto: AddDoorLockOnlineOneTimeDto, @Req() req: any, ) { - try { - const userUuid = req.user.uuid; - const temporaryPasswords = - await this.visitorPasswordService.addOnlineTemporaryPasswordOneTime( - addDoorLockOnlineOneTimeDto, - userUuid, - ); - - return { - statusCode: HttpStatus.CREATED, - data: temporaryPasswords, - }; - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, + const userUuid = req.user.uuid; + const temporaryPasswords = + await this.visitorPasswordService.addOnlineTemporaryPasswordOneTime( + addDoorLockOnlineOneTimeDto, + userUuid, ); - } + + return { + statusCode: HttpStatus.CREATED, + data: temporaryPasswords, + }; } @ApiBearerAuth() @UseGuards(JwtAuthGuard) @@ -86,24 +71,17 @@ export class VisitorPasswordController { @Body() addDoorLockOfflineOneTimeDto: AddDoorLockOfflineOneTimeDto, @Req() req: any, ) { - try { - const userUuid = req.user.uuid; - const temporaryPassword = - await this.visitorPasswordService.addOfflineOneTimeTemporaryPassword( - addDoorLockOfflineOneTimeDto, - userUuid, - ); - - return { - statusCode: HttpStatus.CREATED, - data: temporaryPassword, - }; - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, + const userUuid = req.user.uuid; + const temporaryPassword = + await this.visitorPasswordService.addOfflineOneTimeTemporaryPassword( + addDoorLockOfflineOneTimeDto, + userUuid, ); - } + + return { + statusCode: HttpStatus.CREATED, + data: temporaryPassword, + }; } @ApiBearerAuth() @UseGuards(JwtAuthGuard) @@ -113,49 +91,28 @@ export class VisitorPasswordController { addDoorLockOfflineMultipleDto: AddDoorLockOfflineMultipleDto, @Req() req: any, ) { - try { - const userUuid = req.user.uuid; - const temporaryPassword = - await this.visitorPasswordService.addOfflineMultipleTimeTemporaryPassword( - addDoorLockOfflineMultipleDto, - userUuid, - ); - - return { - statusCode: HttpStatus.CREATED, - data: temporaryPassword, - }; - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, + const userUuid = req.user.uuid; + const temporaryPassword = + await this.visitorPasswordService.addOfflineMultipleTimeTemporaryPassword( + addDoorLockOfflineMultipleDto, + userUuid, ); - } + + return { + statusCode: HttpStatus.CREATED, + data: temporaryPassword, + }; } @ApiBearerAuth() @UseGuards(JwtAuthGuard) @Get() async GetVisitorPassword() { - try { - return await this.visitorPasswordService.getPasswords(); - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + return await this.visitorPasswordService.getPasswords(); } @ApiBearerAuth() @UseGuards(JwtAuthGuard) @Get('/devices') async GetVisitorDevices() { - try { - return await this.visitorPasswordService.getAllPassDevices(); - } catch (error) { - throw new HttpException( - error.message || 'Internal server error', - error.status || HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + return await this.visitorPasswordService.getAllPassDevices(); } }