mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-16 02:36:19 +00:00
added exception filter and removed controller error handling
This commit is contained in:
@ -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,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user