finished devices batch control endpoint

This commit is contained in:
faris Aljohari
2024-09-09 15:32:51 +03:00
parent 336d8baf60
commit 1fc04b5c78
3 changed files with 126 additions and 3 deletions

View File

@ -22,7 +22,10 @@ import {
GetDeviceByRoomUuidDto,
GetDeviceLogsDto,
} from '../dtos/get.device.dto';
import { ControlDeviceDto } from '../dtos/control.device.dto';
import {
BatchControlDevicesDto,
ControlDeviceDto,
} from '../dtos/control.device.dto';
import { convertKeysToCamelCase } from '@app/common/helper/camelCaseConverter';
import { DeviceRepository } from '@app/common/modules/device/repositories';
import { PermissionType } from '@app/common/constants/permission-type.enum';
@ -308,7 +311,85 @@ export class DeviceService {
);
}
}
async batchControlDevices(batchControlDevicesDto: BatchControlDevicesDto) {
const { devicesUuid } = batchControlDevicesDto;
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.controlDeviceTuya(
deviceDetails.deviceTuyaUuid,
batchControlDevicesDto,
);
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 checkAllDevicesHaveSameProductUuid(deviceUuids: string[]) {
const firstDevice = await this.deviceRepository.findOne({
where: { uuid: deviceUuids[0] },
relations: ['productDevice'],
});
if (!firstDevice) {
throw new BadRequestException('First device not found');
}
const firstProductType = firstDevice.productDevice.prodType;
for (let i = 1; i < deviceUuids.length; i++) {
const device = await this.deviceRepository.findOne({
where: { uuid: deviceUuids[i] },
relations: ['productDevice'],
});
if (!device) {
throw new BadRequestException(`Device ${deviceUuids[i]} not found`);
}
if (device.productDevice.prodType !== firstProductType) {
throw new BadRequestException(`Devices have different product types`);
}
}
}
async getDeviceDetailsByDeviceId(deviceUuid: string, userUuid: string) {
try {
const userDevicePermission = await this.getUserDevicePermission(