fix: commission device API

This commit is contained in:
Mhd Zayd Skaff
2025-07-09 16:05:31 +03:00
parent 09322c5b80
commit a8d33bbc52

View File

@ -323,7 +323,7 @@ export class DeviceService {
async addNewDevice(addDeviceDto: AddDeviceDto, projectUuid: string) { async addNewDevice(addDeviceDto: AddDeviceDto, projectUuid: string) {
try { try {
const device = await this.getDeviceDetailsByDeviceIdTuya( const device = await this.getNewDeviceDetailsFromTuya(
addDeviceDto.deviceTuyaUuid, addDeviceDto.deviceTuyaUuid,
); );
@ -349,6 +349,7 @@ export class DeviceService {
spaceDevice: { uuid: addDeviceDto.spaceUuid }, spaceDevice: { uuid: addDeviceDto.spaceUuid },
tag: { uuid: addDeviceDto.tagUuid }, tag: { uuid: addDeviceDto.tagUuid },
name: addDeviceDto.deviceName, name: addDeviceDto.deviceName,
deviceTuyaConstUuid: device.uuid,
}); });
if (deviceSaved.uuid) { if (deviceSaved.uuid) {
const deviceStatus: BaseResponseDto = const deviceStatus: BaseResponseDto =
@ -752,6 +753,45 @@ export class DeviceService {
); );
} }
} }
async getNewDeviceDetailsFromTuya(
deviceId: string,
): Promise<GetDeviceDetailsInterface> {
console.log('fetching device details from Tuya for deviceId:', deviceId);
try {
const result = await this.tuyaService.getDeviceDetails(deviceId);
if (!result) {
throw new NotFoundException('Device not found');
}
// Convert keys to camel case
const camelCaseResponse = convertKeysToCamelCase(result);
const product = await this.productRepository.findOne({
where: { prodId: camelCaseResponse.productId },
});
if (!product) {
throw new NotFoundException('Product Type is not supported');
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { productId, id, productName, ...rest } = camelCaseResponse;
return {
...rest,
productUuid: product.uuid,
productName: product.name,
} as GetDeviceDetailsInterface;
} catch (error) {
console.log('error', error);
throw new HttpException(
error.message || 'Error fetching device details from Tuya',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
async getDeviceInstructionByDeviceId( async getDeviceInstructionByDeviceId(
deviceUuid: string, deviceUuid: string,
projectUuid: string, projectUuid: string,