feat: add device name field to DeviceEntity and update related DTOs and services

This commit is contained in:
faris Aljohari
2025-04-22 01:53:03 +03:00
parent 1e6503c072
commit 2f12189eef
7 changed files with 69 additions and 126 deletions
+8
View File
@@ -17,6 +17,14 @@ export class AddDeviceDto {
@IsString()
@IsNotEmpty()
public spaceUuid: string;
@ApiProperty({
description: 'deviceName',
required: true,
})
@IsString()
@IsNotEmpty()
public deviceName: string;
}
export class AssignDeviceToSpaceDto {
@ApiProperty({
+18 -20
View File
@@ -159,6 +159,7 @@ export class DeviceService {
deviceTuyaUuid: addDeviceDto.deviceTuyaUuid,
productDevice: { uuid: device.productUuid },
spaceDevice: { uuid: addDeviceDto.spaceUuid },
name: addDeviceDto.deviceName,
});
if (deviceSaved.uuid) {
const deviceStatus: BaseResponseDto =
@@ -299,22 +300,15 @@ export class DeviceService {
);
}
}
async updateDeviceNameTuya(
deviceId: string,
deviceName: string,
): Promise<controlDeviceInterface> {
async updateDeviceName(deviceUuid: string, deviceName: string) {
try {
const path = `/v2.0/cloud/thing/${deviceId}/attribute`;
const response = await this.tuya.request({
method: 'POST',
path,
body: { type: 1, data: deviceName },
});
return response as controlDeviceInterface;
await this.deviceRepository.update(
{ uuid: deviceUuid },
{ name: deviceName },
);
} catch (error) {
throw new HttpException(
'Error updating device name from Tuya',
'Error updating device name',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
@@ -643,11 +637,8 @@ export class DeviceService {
true,
projectUuid,
);
if (device.deviceTuyaUuid) {
await this.updateDeviceNameTuya(
device.deviceTuyaUuid,
updateDeviceDto.deviceName,
);
if (device.uuid) {
await this.updateDeviceName(deviceUuid, updateDeviceDto.deviceName);
}
return new SuccessResponseDto({
@@ -682,15 +673,22 @@ export class DeviceService {
prodId: camelCaseResponse.result.productId,
},
});
const deviceDetails = await this.deviceRepository.findOne({
where: {
deviceTuyaUuid: deviceId,
},
});
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { productId, id, ...rest } = camelCaseResponse.result;
const { productId, id, name, ...rest } = camelCaseResponse.result;
return {
...rest,
productUuid: product.uuid,
name: deviceDetails.name,
} as GetDeviceDetailsInterface;
} catch (error) {
console.log('error', error);
throw new HttpException(
'Error fetching device details from Tuya',
HttpStatus.INTERNAL_SERVER_ERROR,