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 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,

View File

@ -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 {

View File

@ -4,7 +4,6 @@ import {
Controller,
Delete,
Get,
HttpException,
HttpStatus,
Param,
Post,
@ -31,7 +30,6 @@ export class AutomationController {
@UseGuards(JwtAuthGuard)
@Post()
async addAutomation(@Body() addAutomationDto: AddAutomationDto) {
try {
const automation =
await this.automationService.addAutomation(addAutomationDto);
return {
@ -40,43 +38,22 @@ export class AutomationController {
message: 'Automation added successfully',
data: automation,
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@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,
);
}
}
@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,
);
``;
}
}
@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,
);
}
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@ -105,7 +75,6 @@ export class AutomationController {
@Body() updateAutomationDto: UpdateAutomationDto,
@Param('automationId') automationId: string,
) {
try {
const automation = await this.automationService.updateAutomation(
updateAutomationDto,
automationId,
@ -116,12 +85,6 @@ export class AutomationController {
message: 'Automation updated successfully',
data: automation,
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@ -130,7 +93,6 @@ export class AutomationController {
@Body() updateAutomationStatusDto: UpdateAutomationStatusDto,
@Param('automationId') automationId: string,
) {
try {
await this.automationService.updateAutomationStatus(
updateAutomationStatusDto,
automationId,
@ -140,11 +102,5 @@ export class AutomationController {
success: true,
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,
Controller,
Get,
HttpException,
HttpStatus,
Param,
Post,
@ -33,7 +32,6 @@ export class BuildingController {
@UseGuards(JwtAuthGuard, CheckCommunityTypeGuard)
@Post()
async addBuilding(@Body() addBuildingDto: AddBuildingDto) {
try {
const building = await this.buildingService.addBuilding(addBuildingDto);
return {
statusCode: HttpStatus.CREATED,
@ -41,28 +39,14 @@ export class BuildingController {
message: 'Building added successfully',
data: building,
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard, BuildingPermissionGuard)
@Get(':buildingUuid')
async getBuildingByUuid(@Param('buildingUuid') buildingUuid: string) {
try {
const building =
await this.buildingService.getBuildingByUuid(buildingUuid);
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,
);
}
}
@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,
);
}
}
@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,
);
}
}
@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,
);
}
}
@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,
);
}
}
@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,
);
}
}
}

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,
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);
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,
);
}
}
@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,
);
}
}
@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,
);
}
}
@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,
);
}
}
@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,
);
}
}
@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,
);
}
}
@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,
);
}
}
}

View File

@ -3,7 +3,6 @@ import {
Controller,
Delete,
Get,
HttpException,
HttpStatus,
Param,
Post,
@ -31,7 +30,6 @@ export class DeviceMessagesSubscriptionController {
async addDeviceMessagesSubscription(
@Body() deviceMessagesAddDto: DeviceMessagesAddDto,
) {
try {
const addDetails =
await this.deviceMessagesSubscriptionService.addDeviceMessagesSubscription(
deviceMessagesAddDto,
@ -41,12 +39,6 @@ export class DeviceMessagesSubscriptionController {
message: 'Device Messages Subscription Added Successfully',
data: addDetails,
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@ApiBearerAuth()
@ -56,7 +48,6 @@ export class DeviceMessagesSubscriptionController {
@Param('deviceUuid') deviceUuid: string,
@Param('userUuid') userUuid: string,
) {
try {
const deviceDetails =
await this.deviceMessagesSubscriptionService.getDeviceMessagesSubscription(
userUuid,
@ -67,12 +58,6 @@ export class DeviceMessagesSubscriptionController {
message: 'User Device Subscription fetched Successfully',
data: deviceDetails,
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@ -80,7 +65,6 @@ export class DeviceMessagesSubscriptionController {
async deleteDeviceMessagesSubscription(
@Body() deviceMessagesAddDto: DeviceMessagesAddDto,
) {
try {
await this.deviceMessagesSubscriptionService.deleteDeviceMessagesSubscription(
deviceMessagesAddDto,
);
@ -88,11 +72,5 @@ export class DeviceMessagesSubscriptionController {
statusCode: HttpStatus.OK,
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,
Query,
Param,
HttpException,
HttpStatus,
UseGuards,
Req,
@ -42,7 +41,6 @@ export class DeviceController {
@UseGuards(SuperAdminRoleGuard, CheckDeviceGuard)
@Post()
async addDeviceUser(@Body() addDeviceDto: AddDeviceDto) {
try {
const device = await this.deviceService.addDeviceUser(addDeviceDto);
return {
@ -51,25 +49,12 @@ export class DeviceController {
message: 'device added successfully',
data: device,
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@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,
);
}
}
@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,
);
}
}
@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,
);
}
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard, CheckRoomGuard)
@ -110,7 +81,6 @@ export class DeviceController {
async updateDeviceInRoom(
@Body() updateDeviceInRoomDto: UpdateDeviceInRoomDto,
) {
try {
const device = await this.deviceService.updateDeviceInRoom(
updateDeviceInRoomDto,
);
@ -121,12 +91,6 @@ export class DeviceController {
message: 'device updated in room successfully',
data: device,
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@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,
);
}
}
@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,
);
}
}
@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,
);
}
}
@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,
);
}
}
@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,
);
}
}
@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,
);
}
}
@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,
);
}
}
@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,
);
}
}
}

View File

@ -4,7 +4,6 @@ import {
Controller,
Post,
Param,
HttpException,
HttpStatus,
Get,
Delete,
@ -31,7 +30,6 @@ export class DoorLockController {
@Body() addDoorLockDto: AddDoorLockOnlineDto,
@Param('doorLockUuid') doorLockUuid: string,
) {
try {
const temporaryPassword =
await this.doorLockService.addOnlineTemporaryPassword(
addDoorLockDto,
@ -46,12 +44,6 @@ export class DoorLockController {
id: temporaryPassword.id,
},
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@ -59,7 +51,6 @@ export class DoorLockController {
async addOfflineOneTimeTemporaryPassword(
@Param('doorLockUuid') doorLockUuid: string,
) {
try {
const temporaryPassword =
await this.doorLockService.addOfflineOneTimeTemporaryPassword(
doorLockUuid,
@ -71,12 +62,6 @@ export class DoorLockController {
message: 'offline temporary password added successfully',
data: temporaryPassword,
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@ -86,7 +71,6 @@ export class DoorLockController {
addDoorLockOfflineTempMultipleTimeDto: AddDoorLockOfflineTempMultipleTimeDto,
@Param('doorLockUuid') doorLockUuid: string,
) {
try {
const temporaryPassword =
await this.doorLockService.addOfflineMultipleTimeTemporaryPassword(
addDoorLockOfflineTempMultipleTimeDto,
@ -99,12 +83,6 @@ export class DoorLockController {
message: 'offline temporary password added successfully',
data: temporaryPassword,
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@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,
);
}
}
@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,
);
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,
);
}
}
@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,
);
}
}
@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,
);
}
}
@ApiBearerAuth()
@ -190,7 +137,6 @@ export class DoorLockController {
@Param('doorLockUuid') doorLockUuid: string,
@Param('passwordId') passwordId: string,
) {
try {
const temporaryPassword =
await this.doorLockService.updateOfflineTemporaryPassword(
updateDoorLockOfflineTempDto,
@ -204,18 +150,11 @@ export class DoorLockController {
message: 'offline temporary password updated successfully',
data: temporaryPassword,
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Post('open/:doorLockUuid')
async openDoorLock(@Param('doorLockUuid') doorLockUuid: string) {
try {
await this.doorLockService.openDoorLock(doorLockUuid);
return {
@ -223,11 +162,5 @@ export class DoorLockController {
success: true,
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,
Controller,
Get,
HttpException,
HttpStatus,
Param,
Post,
@ -33,7 +32,6 @@ export class FloorController {
@UseGuards(JwtAuthGuard, CheckBuildingTypeGuard)
@Post()
async addFloor(@Body() addFloorDto: AddFloorDto) {
try {
const floor = await this.floorService.addFloor(addFloorDto);
return {
statusCode: HttpStatus.CREATED,
@ -41,27 +39,14 @@ export class FloorController {
message: 'Floor added successfully',
data: floor,
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@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,
);
}
}
@ApiBearerAuth()
@ -71,65 +56,34 @@ export class FloorController {
@Param('floorUuid') floorUuid: string,
@Query() query: GetFloorChildDto,
) {
try {
const floor = await this.floorService.getFloorChildByUuid(
floorUuid,
query,
);
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,
);
}
}
@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,
);
}
}
@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,
);
}
}
@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,
);
}
}
@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,
);
}
}
}

View File

@ -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,
);
}
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard, UnitPermissionGuard)
@ -41,7 +26,6 @@ export class GroupController {
@Param('groupName') groupName: string,
@Req() req: any,
) {
try {
const userUuid = req.user.uuid;
return await this.groupService.getUnitDevicesByGroupName(
@ -49,11 +33,5 @@ export class GroupController {
groupName,
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 { 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({

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 { 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,
);
}
}
}

View File

@ -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],

View File

@ -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,
);
}
}
}

View File

@ -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);
}
}
@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,
);
}
}
}

View File

@ -3,7 +3,6 @@ import {
Body,
Controller,
Get,
HttpException,
HttpStatus,
Param,
Post,
@ -31,7 +30,6 @@ export class RoomController {
@UseGuards(JwtAuthGuard, CheckUnitTypeGuard)
@Post()
async addRoom(@Body() addRoomDto: AddRoomDto) {
try {
const room = await this.roomService.addRoom(addRoomDto);
return {
statusCode: HttpStatus.CREATED,
@ -39,73 +37,39 @@ export class RoomController {
message: 'Room added successfully',
data: room,
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@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,
);
}
}
@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,
);
}
}
@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,
);
}
}
@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,
);
}
}
@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,
);
}
}
}

View File

@ -4,7 +4,6 @@ import {
Controller,
Delete,
Get,
HttpException,
HttpStatus,
Param,
Post,
@ -27,7 +26,6 @@ export class SceneController {
@UseGuards(JwtAuthGuard)
@Post('tap-to-run')
async addTapToRunScene(@Body() addSceneTapToRunDto: AddSceneTapToRunDto) {
try {
const tapToRunScene =
await this.sceneService.addTapToRunScene(addSceneTapToRunDto);
return {
@ -36,27 +34,14 @@ export class SceneController {
message: 'Scene added successfully',
data: tapToRunScene,
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@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,
);
}
}
@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,
);
}
}
@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,
);
}
}
@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,
);
``;
}
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@ -120,7 +83,6 @@ export class SceneController {
@Body() updateSceneTapToRunDto: UpdateSceneTapToRunDto,
@Param('sceneId') sceneId: string,
) {
try {
const tapToRunScene = await this.sceneService.updateTapToRunScene(
updateSceneTapToRunDto,
sceneId,
@ -131,11 +93,5 @@ export class SceneController {
message: 'Scene updated successfully',
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,
Post,
Param,
HttpException,
HttpStatus,
UseGuards,
Put,
@ -36,7 +35,6 @@ export class ScheduleController {
@Param('deviceUuid') deviceUuid: string,
@Body() addScheduleDto: AddScheduleDto,
) {
try {
const schedule = await this.scheduleService.addDeviceSchedule(
deviceUuid,
addScheduleDto,
@ -48,12 +46,6 @@ export class ScheduleController {
message: 'schedule added successfully',
data: schedule,
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@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,
);
}
}
@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,
);
}
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@ -102,7 +80,6 @@ export class ScheduleController {
@Param('deviceUuid') deviceUuid: string,
@Body() enableScheduleDto: EnableScheduleDto,
) {
try {
await this.scheduleService.enableDeviceSchedule(
deviceUuid,
enableScheduleDto,
@ -112,12 +89,6 @@ export class ScheduleController {
success: true,
message: 'schedule updated successfully',
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@ -126,7 +97,6 @@ export class ScheduleController {
@Param('deviceUuid') deviceUuid: string,
@Body() updateScheduleDto: UpdateScheduleDto,
) {
try {
const schedule = await this.scheduleService.updateDeviceSchedule(
deviceUuid,
updateScheduleDto,
@ -138,11 +108,5 @@ export class ScheduleController {
message: 'schedule updated successfully',
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 {
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,
);
}
}
}

View File

@ -3,7 +3,6 @@ import {
Body,
Controller,
Get,
HttpException,
HttpStatus,
Param,
Post,
@ -36,7 +35,6 @@ export class UnitController {
@UseGuards(JwtAuthGuard, CheckFloorTypeGuard)
@Post()
async addUnit(@Body() addUnitDto: AddUnitDto) {
try {
const unit = await this.unitService.addUnit(addUnitDto);
return {
statusCode: HttpStatus.CREATED,
@ -44,27 +42,14 @@ export class UnitController {
message: 'Unit added successfully',
data: unit,
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@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,
);
}
}
@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,
);
}
}
@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,
);
}
}
@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,
);
}
}
@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,
);
}
}
@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,
);
}
}
@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,
);
}
}
@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,
);
}
}
}

View File

@ -3,7 +3,6 @@ import {
Controller,
Delete,
Get,
HttpException,
HttpStatus,
Param,
Post,
@ -32,9 +31,7 @@ export class UserDevicePermissionController {
async addDevicePermission(
@Body() userDevicePermissionDto: UserDevicePermissionAddDto,
) {
try {
const addDetails =
await this.userDevicePermissionService.addUserPermission(
const addDetails = await this.userDevicePermissionService.addUserPermission(
userDevicePermissionDto,
);
return {
@ -42,12 +39,6 @@ export class UserDevicePermissionController {
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()
@ -57,7 +48,6 @@ export class UserDevicePermissionController {
@Param('devicePermissionUuid') devicePermissionUuid: string,
@Body() userDevicePermissionEditDto: UserDevicePermissionEditDto,
) {
try {
await this.userDevicePermissionService.editUserPermission(
devicePermissionUuid,
userDevicePermissionEditDto,
@ -66,19 +56,12 @@ export class UserDevicePermissionController {
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,
);
}
}
@ApiBearerAuth()
@UseGuards(AdminRoleGuard)
@Get(':deviceUuid')
async fetchDevicePermission(@Param('deviceUuid') deviceUuid: string) {
try {
const deviceDetails =
await this.userDevicePermissionService.fetchUserPermission(deviceUuid);
return {
@ -86,9 +69,6 @@ export class UserDevicePermissionController {
message: 'Device Details fetched Successfully',
data: deviceDetails,
};
} catch (err) {
throw new Error(err);
}
}
@ApiBearerAuth()
@UseGuards(AdminRoleGuard)
@ -96,7 +76,6 @@ export class UserDevicePermissionController {
async deleteDevicePermission(
@Param('devicePermissionUuid') devicePermissionUuid: string,
) {
try {
await this.userDevicePermissionService.deleteDevicePermission(
devicePermissionUuid,
);
@ -104,11 +83,5 @@ export class UserDevicePermissionController {
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,
);
}
}
}

View File

@ -2,7 +2,6 @@ import {
Body,
Controller,
Get,
HttpException,
HttpStatus,
Param,
Post,
@ -34,7 +33,6 @@ export class UserNotificationController {
async addUserSubscription(
@Body() userNotificationAddDto: UserNotificationAddDto,
) {
try {
const addDetails = await this.userNotificationService.addUserSubscription(
userNotificationAddDto,
);
@ -43,19 +41,12 @@ export class UserNotificationController {
message: 'User Notification Added Successfully',
data: addDetails,
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Get(':userUuid')
async fetchUserSubscriptions(@Param('userUuid') userUuid: string) {
try {
const userDetails =
await this.userNotificationService.fetchUserSubscriptions(userUuid);
return {
@ -63,12 +54,6 @@ export class UserNotificationController {
message: 'User Notification fetched Successfully',
data: { ...userDetails },
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@ -76,7 +61,6 @@ export class UserNotificationController {
async updateUserSubscription(
@Body() userNotificationUpdateDto: UserNotificationUpdateDto,
) {
try {
await this.userNotificationService.updateUserSubscription(
userNotificationUpdateDto,
);
@ -84,11 +68,5 @@ export class UserNotificationController {
statusCode: HttpStatus.OK,
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,
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,
);
}
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard, CheckProfilePictureGuard)
@ -49,7 +41,6 @@ export class UserController {
@Param('userUuid') userUuid: string,
@Body() updateProfilePictureDataDto: UpdateProfilePictureDataDto,
) {
try {
const userData = await this.userService.updateProfilePictureByUserUuid(
userUuid,
updateProfilePictureDataDto,
@ -60,12 +51,6 @@ export class UserController {
message: 'profile picture updated successfully',
data: userData,
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@ -74,7 +59,6 @@ export class UserController {
@Param('userUuid') userUuid: string,
@Body() updateRegionDataDto: UpdateRegionDataDto,
) {
try {
const userData = await this.userService.updateRegionByUserUuid(
userUuid,
updateRegionDataDto,
@ -85,12 +69,6 @@ export class UserController {
message: 'region updated successfully',
data: userData,
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@ -99,7 +77,6 @@ export class UserController {
@Param('userUuid') userUuid: string,
@Body() updateTimezoneDataDto: UpdateTimezoneDataDto,
) {
try {
const userData = await this.userService.updateTimezoneByUserUuid(
userUuid,
updateTimezoneDataDto,
@ -110,12 +87,6 @@ export class UserController {
message: 'timezone updated successfully',
data: userData,
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@ -124,7 +95,6 @@ export class UserController {
@Param('userUuid') userUuid: string,
@Body() updateNameDto: UpdateNameDto,
) {
try {
const userData = await this.userService.updateNameByUserUuid(
userUuid,
updateNameDto,
@ -135,12 +105,6 @@ export class UserController {
message: 'name updated successfully',
data: userData,
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@ApiBearerAuth()
@UseGuards(SuperAdminRoleGuard)

View File

@ -3,7 +3,6 @@ import {
Body,
Controller,
Post,
HttpException,
HttpStatus,
UseGuards,
Get,
@ -34,7 +33,6 @@ export class VisitorPasswordController {
@Body() addDoorLockOnlineMultipleDto: AddDoorLockOnlineMultipleDto,
@Req() req: any,
) {
try {
const userUuid = req.user.uuid;
const temporaryPasswords =
await this.visitorPasswordService.addOnlineTemporaryPasswordMultipleTime(
@ -46,12 +44,6 @@ export class VisitorPasswordController {
statusCode: HttpStatus.CREATED,
data: temporaryPasswords,
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@ -60,7 +52,6 @@ export class VisitorPasswordController {
@Body() addDoorLockOnlineOneTimeDto: AddDoorLockOnlineOneTimeDto,
@Req() req: any,
) {
try {
const userUuid = req.user.uuid;
const temporaryPasswords =
await this.visitorPasswordService.addOnlineTemporaryPasswordOneTime(
@ -72,12 +63,6 @@ export class VisitorPasswordController {
statusCode: HttpStatus.CREATED,
data: temporaryPasswords,
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@ -86,7 +71,6 @@ export class VisitorPasswordController {
@Body() addDoorLockOfflineOneTimeDto: AddDoorLockOfflineOneTimeDto,
@Req() req: any,
) {
try {
const userUuid = req.user.uuid;
const temporaryPassword =
await this.visitorPasswordService.addOfflineOneTimeTemporaryPassword(
@ -98,12 +82,6 @@ export class VisitorPasswordController {
statusCode: HttpStatus.CREATED,
data: temporaryPassword,
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@ -113,7 +91,6 @@ export class VisitorPasswordController {
addDoorLockOfflineMultipleDto: AddDoorLockOfflineMultipleDto,
@Req() req: any,
) {
try {
const userUuid = req.user.uuid;
const temporaryPassword =
await this.visitorPasswordService.addOfflineMultipleTimeTemporaryPassword(
@ -125,37 +102,17 @@ export class VisitorPasswordController {
statusCode: HttpStatus.CREATED,
data: temporaryPassword,
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
@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,
);
}
}
@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,
);
}
}
}