Transfer device to parent space in Tuya when updating device in room

This commit is contained in:
faris Aljohari
2024-07-20 18:19:05 +03:00
parent 9ad51fcffc
commit ae02b0b288

View File

@ -197,8 +197,15 @@ export class DeviceService {
where: {
uuid: updateDeviceInRoomDto.deviceUuid,
},
relations: ['spaceDevice'],
relations: ['spaceDevice', 'spaceDevice.parent'],
});
if (device.spaceDevice.parent.spaceTuyaUuid) {
await this.transferDeviceInSpacesTuya(
device.deviceTuyaUuid,
device.spaceDevice.parent.spaceTuyaUuid,
);
}
return {
uuid: device.uuid,
roomUuid: device.spaceDevice.uuid,
@ -210,7 +217,26 @@ export class DeviceService {
);
}
}
async transferDeviceInSpacesTuya(
deviceId: string,
spaceId: string,
): Promise<controlDeviceInterface> {
try {
const path = `/v2.0/cloud/thing/${deviceId}/transfer`;
const response = await this.tuya.request({
method: 'POST',
path,
body: { space_id: spaceId },
});
return response as controlDeviceInterface;
} catch (error) {
throw new HttpException(
'Error transferring device in spaces from Tuya',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
async controlDevice(controlDeviceDto: ControlDeviceDto, deviceUuid: string) {
try {
const deviceDetails = await this.getDeviceByDeviceUuid(deviceUuid, false);