Refactor device service to fetch devices by group ID

This commit is contained in:
faris Aljohari
2024-03-20 13:17:53 +03:00
parent 4d6c4ed787
commit 57fb43f47b

View File

@ -74,15 +74,19 @@ export class DeviceService {
} }
async getDevicesByGroupId(getDeviceByGroupIdDto: GetDeviceByGroupIdDto) { async getDevicesByGroupId(getDeviceByGroupIdDto: GetDeviceByGroupIdDto) {
try { try {
const response = await this.getDevicesByGroupIdTuya( const devicesIds = await this.getDevicesByGroupIdTuya(
getDeviceByGroupIdDto, 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 +100,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,
}, },