fixed endpoint for disassociate

This commit is contained in:
hannathkadher
2024-11-04 12:20:08 +04:00
parent 015b139428
commit a133f78bb3
2 changed files with 77 additions and 24 deletions
@@ -57,40 +57,84 @@ export class SubspaceDeviceService {
message: 'Successfully retrieved list of devices',
});
}
async associateDeviceToSubspace(params: DeviceSubSpaceParam) {
async associateDeviceToSubspace(
params: DeviceSubSpaceParam,
): Promise<BaseResponseDto> {
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<BaseResponseDto> {
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,