Refactor automation and scene fetching to handle errors and improve readability

This commit is contained in:
faris Aljohari
2025-01-28 19:12:31 -06:00
parent 8161e8469b
commit 2477f4825e
2 changed files with 53 additions and 48 deletions

View File

@ -198,8 +198,7 @@ export class AutomationService {
async getAutomationBySpace(spaceUuid: string) { async getAutomationBySpace(spaceUuid: string) {
try { try {
const space = await this.getSpaceByUuid(spaceUuid); // Fetch automation data from the repository
const automationData = await this.automationRepository.find({ const automationData = await this.automationRepository.find({
where: { where: {
space: { uuid: spaceUuid }, space: { uuid: spaceUuid },
@ -208,45 +207,47 @@ export class AutomationService {
relations: ['space'], relations: ['space'],
}); });
if (!space.spaceTuyaUuid) { // Safe fetch function to handle individual automation fetching
throw new BadRequestException(`Invalid space UUID ${spaceUuid}`); const safeFetch = async (automation: any) => {
} try {
const automations = await Promise.all( const automationDetails = (await this.tuyaService.getSceneRule(
automationData.map(async (automation) => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const automationDetails = await this.tuyaService.getSceneRule(
automation.automationTuyaUuid, 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 { return {
uuid: automation.uuid, uuid: automation.uuid,
...automationDetails, name: automationDetails.result.name,
}; status: automationDetails.result.status,
}),
);
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,
type: AUTOMATION_TYPE, 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) { } catch (err) {
console.error('Error retrieving automations:', err);
if (err instanceof BadRequestException) { if (err instanceof BadRequestException) {
throw err; // Re-throw BadRequestException throw err;
} else {
throw new HttpException(
err.message || 'Automation not found',
err.status || HttpStatus.NOT_FOUND,
);
} }
throw new HttpException(
err.message || 'Automation not found',
err.status || HttpStatus.NOT_FOUND,
);
} }
} }
async findAutomationBySpace(spaceUuid: string) { async findAutomationBySpace(spaceUuid: string) {

View File

@ -229,33 +229,37 @@ export class SceneService {
relations: ['sceneIcon', 'space'], relations: ['sceneIcon', 'space'],
}); });
const scenes = await Promise.all( const safeFetch = async (scene: any) => {
scenesData.map(async (scene) => { try {
// eslint-disable-next-line @typescript-eslint/no-unused-vars // eslint-disable-next-line @typescript-eslint/no-unused-vars
const { actions, ...sceneDetails } = await this.getScene( const { actions, ...sceneDetails } = await this.getScene(
scene, scene,
spaceUuid, spaceUuid,
); );
return sceneDetails; return sceneDetails;
}), } catch (error) {
); console.warn(
`Skipping scene UUID: ${scene.uuid} due to error: ${error.message}`,
);
return null;
}
};
return scenes; const scenes = await Promise.all(scenesData.map(safeFetch));
} catch (err) {
return scenes.filter(Boolean); // Remove null values
} catch (error) {
console.error( console.error(
`Error fetching Tap-to-Run scenes for space UUID ${spaceUuid}:`, `Error fetching Tap-to-Run scenes for space UUID ${spaceUuid}:`,
err.message, error.message,
); );
if (err instanceof HttpException) { throw error instanceof HttpException
throw err; ? error
} else { : new HttpException(
throw new HttpException( 'An error occurred while retrieving scenes',
'An error occurred while retrieving scenes', HttpStatus.INTERNAL_SERVER_ERROR,
HttpStatus.INTERNAL_SERVER_ERROR, );
);
}
} }
} }