Merge pull request #34 from SyncrowIOT/fixed-device-room-service

Fixed device room service
This commit is contained in:
Ammar Qaffaf
2024-05-19 10:21:31 -04:00
committed by GitHub
7 changed files with 68 additions and 14 deletions

View File

@ -34,7 +34,12 @@ export class BuildingController {
async addBuilding(@Body() addBuildingDto: AddBuildingDto) { async addBuilding(@Body() addBuildingDto: AddBuildingDto) {
try { try {
const building = await this.buildingService.addBuilding(addBuildingDto); const building = await this.buildingService.addBuilding(addBuildingDto);
return { message: 'Building added successfully', uuid: building.uuid }; return {
statusCode: HttpStatus.CREATED,
success: true,
message: 'Building added successfully',
data: building,
};
} catch (error) { } catch (error) {
throw new HttpException( throw new HttpException(
error.message || 'Internal server error', error.message || 'Internal server error',
@ -100,7 +105,11 @@ export class BuildingController {
async addUserBuilding(@Body() addUserBuildingDto: AddUserBuildingDto) { async addUserBuilding(@Body() addUserBuildingDto: AddUserBuildingDto) {
try { try {
await this.buildingService.addUserBuilding(addUserBuildingDto); await this.buildingService.addUserBuilding(addUserBuildingDto);
return { message: 'user building added successfully' }; return {
statusCode: HttpStatus.CREATED,
success: true,
message: 'user building added successfully',
};
} catch (error) { } catch (error) {
throw new HttpException( throw new HttpException(
error.message || 'Internal server error', error.message || 'Internal server error',

View File

@ -37,7 +37,12 @@ export class CommunityController {
try { try {
const community = const community =
await this.communityService.addCommunity(addCommunityDto); await this.communityService.addCommunity(addCommunityDto);
return { message: 'Community added successfully', uuid: community.uuid }; return {
statusCode: HttpStatus.CREATED,
success: true,
message: 'Community added successfully',
data: community,
};
} catch (error) { } catch (error) {
throw new HttpException( throw new HttpException(
error.message || 'Internal server error', error.message || 'Internal server error',
@ -102,7 +107,11 @@ export class CommunityController {
async addUserCommunity(@Body() addUserCommunityDto: AddUserCommunityDto) { async addUserCommunity(@Body() addUserCommunityDto: AddUserCommunityDto) {
try { try {
await this.communityService.addUserCommunity(addUserCommunityDto); await this.communityService.addUserCommunity(addUserCommunityDto);
return { message: 'user community added successfully' }; return {
statusCode: HttpStatus.CREATED,
success: true,
message: 'user community added successfully',
};
} catch (error) { } catch (error) {
throw new HttpException( throw new HttpException(
error.message || 'Internal server error', error.message || 'Internal server error',

View File

@ -61,7 +61,15 @@ export class DeviceController {
@Post('room') @Post('room')
async addDeviceInRoom(@Body() addDeviceInRoomDto: AddDeviceInRoomDto) { async addDeviceInRoom(@Body() addDeviceInRoomDto: AddDeviceInRoomDto) {
try { try {
return await this.deviceService.addDeviceInRoom(addDeviceInRoomDto); const device =
await this.deviceService.addDeviceInRoom(addDeviceInRoomDto);
return {
statusCode: HttpStatus.CREATED,
success: true,
message: 'device added in room successfully',
data: device,
};
} catch (error) { } catch (error) {
throw new HttpException( throw new HttpException(
error.message || 'Internal server error', error.message || 'Internal server error',

View File

@ -27,6 +27,7 @@ import { convertKeysToCamelCase } from '@app/common/helper/camelCaseConverter';
import { DeviceRepository } from '@app/common/modules/device/repositories'; import { DeviceRepository } from '@app/common/modules/device/repositories';
import { GroupDeviceRepository } from '@app/common/modules/group-device/repositories'; import { GroupDeviceRepository } from '@app/common/modules/group-device/repositories';
import { PermissionType } from '@app/common/constants/permission-type.enum'; import { PermissionType } from '@app/common/constants/permission-type.enum';
import { In } from 'typeorm';
@Injectable() @Injectable()
export class DeviceService { export class DeviceService {
@ -57,7 +58,7 @@ export class DeviceService {
permission: { permission: {
userUuid, userUuid,
permissionType: { permissionType: {
type: PermissionType.READ || PermissionType.CONTROLLABLE, type: In([PermissionType.READ, PermissionType.CONTROLLABLE]),
}, },
}, },
}, },
@ -81,6 +82,7 @@ export class DeviceService {
} as GetDeviceDetailsInterface; } as GetDeviceDetailsInterface;
}), }),
); );
return devicesData; return devicesData;
} catch (error) { } catch (error) {
// Handle the error here // Handle the error here
@ -147,12 +149,11 @@ export class DeviceService {
throw new Error('Product UUID is missing for the device.'); throw new Error('Product UUID is missing for the device.');
} }
await this.deviceRepository.save({ return await this.deviceRepository.save({
deviceTuyaUuid: addDeviceInRoomDto.deviceTuyaUuid, deviceTuyaUuid: addDeviceInRoomDto.deviceTuyaUuid,
spaceDevice: { uuid: addDeviceInRoomDto.roomUuid }, spaceDevice: { uuid: addDeviceInRoomDto.roomUuid },
productDevice: { uuid: device.productUuid }, productDevice: { uuid: device.productUuid },
}); });
return { message: 'device added in room successfully' };
} catch (error) { } catch (error) {
if (error.code === '23505') { if (error.code === '23505') {
throw new HttpException( throw new HttpException(

View File

@ -34,7 +34,12 @@ export class FloorController {
async addFloor(@Body() addFloorDto: AddFloorDto) { async addFloor(@Body() addFloorDto: AddFloorDto) {
try { try {
const floor = await this.floorService.addFloor(addFloorDto); const floor = await this.floorService.addFloor(addFloorDto);
return { message: 'Floor added successfully', uuid: floor.uuid }; return {
statusCode: HttpStatus.CREATED,
success: true,
message: 'Floor added successfully',
data: floor,
};
} catch (error) { } catch (error) {
throw new HttpException( throw new HttpException(
error.message || 'Internal server error', error.message || 'Internal server error',
@ -99,7 +104,11 @@ export class FloorController {
async addUserFloor(@Body() addUserFloorDto: AddUserFloorDto) { async addUserFloor(@Body() addUserFloorDto: AddUserFloorDto) {
try { try {
await this.floorService.addUserFloor(addUserFloorDto); await this.floorService.addUserFloor(addUserFloorDto);
return { message: 'user floor added successfully' }; return {
statusCode: HttpStatus.CREATED,
success: true,
message: 'user floor added successfully',
};
} catch (error) { } catch (error) {
throw new HttpException( throw new HttpException(
error.message || 'Internal server error', error.message || 'Internal server error',

View File

@ -32,7 +32,12 @@ export class RoomController {
async addRoom(@Body() addRoomDto: AddRoomDto) { async addRoom(@Body() addRoomDto: AddRoomDto) {
try { try {
const room = await this.roomService.addRoom(addRoomDto); const room = await this.roomService.addRoom(addRoomDto);
return { message: 'Room added successfully', uuid: room.uuid }; return {
statusCode: HttpStatus.CREATED,
success: true,
message: 'Room added successfully',
data: room,
};
} catch (error) { } catch (error) {
throw new HttpException( throw new HttpException(
error.message || 'Internal server error', error.message || 'Internal server error',
@ -76,7 +81,11 @@ export class RoomController {
async addUserRoom(@Body() addUserRoomDto: AddUserRoomDto) { async addUserRoom(@Body() addUserRoomDto: AddUserRoomDto) {
try { try {
await this.roomService.addUserRoom(addUserRoomDto); await this.roomService.addUserRoom(addUserRoomDto);
return { message: 'user room added successfully' }; return {
statusCode: HttpStatus.CREATED,
success: true,
message: 'user room added successfully',
};
} catch (error) { } catch (error) {
throw new HttpException( throw new HttpException(
error.message || 'Internal server error', error.message || 'Internal server error',

View File

@ -34,7 +34,12 @@ export class UnitController {
async addUnit(@Body() addUnitDto: AddUnitDto) { async addUnit(@Body() addUnitDto: AddUnitDto) {
try { try {
const unit = await this.unitService.addUnit(addUnitDto); const unit = await this.unitService.addUnit(addUnitDto);
return { message: 'Unit added successfully', uuid: unit.uuid }; return {
statusCode: HttpStatus.CREATED,
success: true,
message: 'Unit added successfully',
data: unit,
};
} catch (error) { } catch (error) {
throw new HttpException( throw new HttpException(
error.message || 'Internal server error', error.message || 'Internal server error',
@ -95,7 +100,11 @@ export class UnitController {
async addUserUnit(@Body() addUserUnitDto: AddUserUnitDto) { async addUserUnit(@Body() addUserUnitDto: AddUserUnitDto) {
try { try {
await this.unitService.addUserUnit(addUserUnitDto); await this.unitService.addUserUnit(addUserUnitDto);
return { message: 'user unit added successfully' }; return {
statusCode: HttpStatus.CREATED,
success: true,
message: 'user unit added successfully',
};
} catch (error) { } catch (error) {
throw new HttpException( throw new HttpException(
error.message || 'Internal server error', error.message || 'Internal server error',