Merge pull request #16 from SyncrowIOT/fix-get-device-group-api

Refactor device service to fetch devices by group ID
This commit is contained in:
Ammar Qaffaf
2024-03-26 12:35:00 +03:00
committed by GitHub

View File

@ -74,15 +74,18 @@ export class DeviceService {
} }
async getDevicesByGroupId(getDeviceByGroupIdDto: GetDeviceByGroupIdDto) { async getDevicesByGroupId(getDeviceByGroupIdDto: GetDeviceByGroupIdDto) {
try { try {
const response = await this.getDevicesByGroupIdTuya( const devicesIds: GetDevicesByGroupIdInterface =
getDeviceByGroupIdDto, await this.getDevicesByGroupIdTuya(getDeviceByGroupIdDto);
const devicesDetails = await Promise.all(
devicesIds.result.data_list.map(async (device: any) => {
const deviceData = await this.getDevicesByDeviceId(device.dev_id);
return deviceData.result;
}),
); );
return { return {
success: response.success, success: devicesIds.success,
devices: response.result.data_list, devices: devicesDetails,
count: response.result.count, msg: devicesIds.msg,
msg: response.msg,
}; };
} catch (error) { } catch (error) {
throw new HttpException( throw new HttpException(
@ -96,12 +99,11 @@ export class DeviceService {
getDeviceByGroupIdDto: GetDeviceByGroupIdDto, getDeviceByGroupIdDto: GetDeviceByGroupIdDto,
): Promise<GetDevicesByGroupIdInterface> { ): Promise<GetDevicesByGroupIdInterface> {
try { try {
const path = `/v2.0/cloud/thing/group`; const path = `/v2.0/cloud/thing/group/${getDeviceByGroupIdDto.groupId}/devices`;
const response = await this.tuya.request({ const response = await this.tuya.request({
method: 'GET', method: 'GET',
path, path,
query: { query: {
space_id: getDeviceByGroupIdDto.groupId,
page_size: getDeviceByGroupIdDto.pageSize, page_size: getDeviceByGroupIdDto.pageSize,
page_no: getDeviceByGroupIdDto.pageNo, page_no: getDeviceByGroupIdDto.pageNo,
}, },