mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-15 02:15:21 +00:00
Refactor automation and scene fetching to handle errors and improve readability
This commit is contained in:
@ -198,8 +198,7 @@ export class AutomationService {
|
||||
|
||||
async getAutomationBySpace(spaceUuid: string) {
|
||||
try {
|
||||
const space = await this.getSpaceByUuid(spaceUuid);
|
||||
|
||||
// Fetch automation data from the repository
|
||||
const automationData = await this.automationRepository.find({
|
||||
where: {
|
||||
space: { uuid: spaceUuid },
|
||||
@ -208,47 +207,49 @@ export class AutomationService {
|
||||
relations: ['space'],
|
||||
});
|
||||
|
||||
if (!space.spaceTuyaUuid) {
|
||||
throw new BadRequestException(`Invalid space UUID ${spaceUuid}`);
|
||||
}
|
||||
const automations = await Promise.all(
|
||||
automationData.map(async (automation) => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
const automationDetails = await this.tuyaService.getSceneRule(
|
||||
// Safe fetch function to handle individual automation fetching
|
||||
const safeFetch = async (automation: any) => {
|
||||
try {
|
||||
const automationDetails = (await this.tuyaService.getSceneRule(
|
||||
automation.automationTuyaUuid,
|
||||
);
|
||||
)) as { result?: { name?: string; status?: string } }; // Explicit type assertion
|
||||
|
||||
if (
|
||||
!automationDetails?.result?.name ||
|
||||
automationDetails.result.name.startsWith(AUTO_PREFIX)
|
||||
) {
|
||||
return null; // Skip invalid or auto-generated automations
|
||||
}
|
||||
|
||||
return {
|
||||
uuid: automation.uuid,
|
||||
...automationDetails,
|
||||
};
|
||||
}),
|
||||
);
|
||||
|
||||
return automations
|
||||
.filter(
|
||||
(item: any) =>
|
||||
item.result.name && !item.result.name.startsWith(AUTO_PREFIX),
|
||||
)
|
||||
.map((item: any) => {
|
||||
return {
|
||||
uuid: item.uuid,
|
||||
name: item.result.name,
|
||||
status: item.result.status,
|
||||
name: automationDetails.result.name,
|
||||
status: automationDetails.result.status,
|
||||
type: AUTOMATION_TYPE,
|
||||
};
|
||||
});
|
||||
} catch (error) {
|
||||
console.warn(
|
||||
`Skipping automation with UUID: ${automation.uuid} due to error. ${error.message}`,
|
||||
);
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
// Process automations using safeFetch
|
||||
const automations = await Promise.all(automationData.map(safeFetch));
|
||||
|
||||
return automations.filter(Boolean); // Remove null values
|
||||
} catch (err) {
|
||||
console.error('Error retrieving automations:', err);
|
||||
if (err instanceof BadRequestException) {
|
||||
throw err; // Re-throw BadRequestException
|
||||
} else {
|
||||
throw err;
|
||||
}
|
||||
throw new HttpException(
|
||||
err.message || 'Automation not found',
|
||||
err.status || HttpStatus.NOT_FOUND,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
async findAutomationBySpace(spaceUuid: string) {
|
||||
try {
|
||||
await this.getSpaceByUuid(spaceUuid);
|
||||
|
@ -229,35 +229,39 @@ export class SceneService {
|
||||
relations: ['sceneIcon', 'space'],
|
||||
});
|
||||
|
||||
const scenes = await Promise.all(
|
||||
scenesData.map(async (scene) => {
|
||||
const safeFetch = async (scene: any) => {
|
||||
try {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
const { actions, ...sceneDetails } = await this.getScene(
|
||||
scene,
|
||||
spaceUuid,
|
||||
);
|
||||
|
||||
return sceneDetails;
|
||||
}),
|
||||
} catch (error) {
|
||||
console.warn(
|
||||
`Skipping scene UUID: ${scene.uuid} due to error: ${error.message}`,
|
||||
);
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
return scenes;
|
||||
} catch (err) {
|
||||
const scenes = await Promise.all(scenesData.map(safeFetch));
|
||||
|
||||
return scenes.filter(Boolean); // Remove null values
|
||||
} catch (error) {
|
||||
console.error(
|
||||
`Error fetching Tap-to-Run scenes for space UUID ${spaceUuid}:`,
|
||||
err.message,
|
||||
error.message,
|
||||
);
|
||||
|
||||
if (err instanceof HttpException) {
|
||||
throw err;
|
||||
} else {
|
||||
throw new HttpException(
|
||||
throw error instanceof HttpException
|
||||
? error
|
||||
: new HttpException(
|
||||
'An error occurred while retrieving scenes',
|
||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async triggerTapToRunScene(sceneUuid: string) {
|
||||
try {
|
||||
|
Reference in New Issue
Block a user