diff --git a/src/space/controllers/subspace/subspace-device.controller.ts b/src/space/controllers/subspace/subspace-device.controller.ts index 46c4863..189d490 100644 --- a/src/space/controllers/subspace/subspace-device.controller.ts +++ b/src/space/controllers/subspace/subspace-device.controller.ts @@ -1,6 +1,13 @@ import { ControllerRoute } from '@app/common/constants/controller-route'; import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard'; -import { Controller, Get, Param, Post, UseGuards } from '@nestjs/common'; +import { + Controller, + Delete, + Get, + Param, + Post, + UseGuards, +} from '@nestjs/common'; import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger'; import { DeviceSubSpaceParam, GetSubSpaceParam } from '../../dtos'; import { SubspaceDeviceService } from 'src/space/services'; @@ -55,10 +62,12 @@ export class SubSpaceDeviceController { ControllerRoute.SUBSPACE_DEVICE.ACTIONS .DISASSOCIATE_SUBSPACE_DEVICE_DESCRIPTION, }) - @Post('/:deviceUuid') + @Delete('/:deviceUuid') async disassociateDeviceFromSubspace( @Param() params: DeviceSubSpaceParam, ): Promise { - return await this.subspaceDeviceService.associateDeviceToSubspace(params); + return await this.subspaceDeviceService.disassociateDeviceFromSubspace( + params, + ); } } diff --git a/src/space/services/subspace/subspace-device.service.ts b/src/space/services/subspace/subspace-device.service.ts index 06464b4..3f14a50 100644 --- a/src/space/services/subspace/subspace-device.service.ts +++ b/src/space/services/subspace/subspace-device.service.ts @@ -57,40 +57,84 @@ export class SubspaceDeviceService { message: 'Successfully retrieved list of devices', }); } - async associateDeviceToSubspace(params: DeviceSubSpaceParam) { + + async associateDeviceToSubspace( + params: DeviceSubSpaceParam, + ): Promise { const { subSpaceUuid, deviceUuid, spaceUuid, communityUuid } = params; + try { + await this.validateCommunityAndSpace(communityUuid, spaceUuid); - await this.validateCommunityAndSpace(communityUuid, spaceUuid); + const subspace = await this.findSubspace(subSpaceUuid); + const device = await this.findDevice(deviceUuid); - const subspace = await this.findSubspace(subSpaceUuid); - const device = await this.findDevice(deviceUuid); + device.subspace = subspace; - device.subspace = subspace; - await this.deviceRepository.save(device); + console.log( + 'Starting to save device to subspace:', + new Date(), + device.subspace, + ); + const newDevice = await this.deviceRepository.save(device); + console.log('Device saved to subspace:', new Date(), newDevice); - return new SuccessResponseDto({ - data: device, - message: 'Successfully associated device to subspace', - }); + return new SuccessResponseDto({ + data: device, + message: 'Successfully associated device to subspace', + }); + } catch (error) { + if (error instanceof HttpException) { + throw error; + } else { + throw new HttpException( + 'Failed to associate device to subspace', + HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + } } - async disassociateDeviceFromSubspace(params: DeviceSubSpaceParam) { + async disassociateDeviceFromSubspace( + params: DeviceSubSpaceParam, + ): Promise { const { subSpaceUuid, deviceUuid, spaceUuid, communityUuid } = params; + try { + await this.validateCommunityAndSpace(communityUuid, spaceUuid); - await this.validateCommunityAndSpace(communityUuid, spaceUuid); + const subspace = await this.findSubspace(subSpaceUuid); + const device = await this.findDevice(deviceUuid); - await this.findSubspace(subSpaceUuid); - const device = await this.findDevice(deviceUuid); + if (!device.subspace || device.subspace.uuid !== subspace.uuid) { + throw new HttpException( + 'Device is not associated with the specified subspace', + HttpStatus.BAD_REQUEST, + ); + } - device.subspace = null; - await this.deviceRepository.save(device); + device.subspace = null; - return new SuccessResponseDto({ - data: device, - message: 'Successfully dissociated device from subspace', - }); + console.log( + 'Starting to save device with null subspace:', + new Date(), + device.subspace, + ); + const updatedDevice = await this.deviceRepository.save(device); + + return new SuccessResponseDto({ + data: updatedDevice, + message: 'Successfully dissociated device from subspace', + }); + } catch (error) { + if (error instanceof HttpException) { + throw error; + } else { + throw new HttpException( + 'Failed to dissociate device from subspace', + HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + } } - // Helper method to validate community and space private async validateCommunityAndSpace( communityUuid: string,