finished factory reset endpoint

This commit is contained in:
faris Aljohari
2024-09-19 12:58:46 +03:00
parent 90127bd510
commit c49aad3e43
3 changed files with 98 additions and 0 deletions

View File

@ -24,6 +24,7 @@ import {
} from '../dtos/get.device.dto';
import {
BatchControlDevicesDto,
BatchFactoryResetDevicesDto,
BatchStatusDevicesDto,
ControlDeviceDto,
} from '../dtos/control.device.dto';
@ -288,6 +289,24 @@ export class DeviceService {
);
}
}
async factoryResetDeviceTuya(
deviceUuid: string,
): Promise<controlDeviceInterface> {
try {
const path = `/v2.0/cloud/thing/${deviceUuid}/reset`;
const response = await this.tuya.request({
method: 'POST',
path,
});
return response as controlDeviceInterface;
} catch (error) {
throw new HttpException(
'Error factory resetting device from Tuya',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
async controlDeviceTuya(
deviceUuid: string,
controlDeviceDto: ControlDeviceDto,
@ -413,6 +432,58 @@ export class DeviceService {
}
}
}
async batchFactoryResetDevices(
batchFactoryResetDevicesDto: BatchFactoryResetDevicesDto,
) {
const { devicesUuid } = batchFactoryResetDevicesDto;
try {
// Check if all devices have the same product UUID
await this.checkAllDevicesHaveSameProductUuid(devicesUuid);
// Perform all operations concurrently
const results = await Promise.allSettled(
devicesUuid.map(async (deviceUuid) => {
const deviceDetails = await this.getDeviceByDeviceUuid(deviceUuid);
const result = await this.factoryResetDeviceTuya(
deviceDetails.deviceTuyaUuid,
);
return { deviceUuid, result };
}),
);
// Separate successful and failed operations
const successResults = [];
const failedResults = [];
for (const result of results) {
if (result.status === 'fulfilled') {
const { deviceUuid, result: operationResult } = result.value;
if (operationResult.success) {
// Add to success results if operationResult.success is true
successResults.push({ deviceUuid, result: operationResult });
} else {
// Add to failed results if operationResult.success is false
failedResults.push({ deviceUuid, error: operationResult.msg });
}
} else {
// Add to failed results if promise is rejected
failedResults.push({
deviceUuid: devicesUuid[results.indexOf(result)],
error: result.reason.message,
});
}
}
return { successResults, failedResults };
} catch (error) {
throw new HttpException(
error.message || 'Device Not Found',
error.status || HttpStatus.NOT_FOUND,
);
}
}
async getDeviceDetailsByDeviceId(deviceUuid: string, userUuid: string) {
try {
const userDevicePermission = await this.getUserDevicePermission(