From 2477f4825e4ca13a125541ba9e3fe2068c9906d8 Mon Sep 17 00:00:00 2001 From: faris Aljohari <83524184+farisaljohari@users.noreply.github.com> Date: Tue, 28 Jan 2025 19:12:31 -0600 Subject: [PATCH] Refactor automation and scene fetching to handle errors and improve readability --- src/automation/services/automation.service.ts | 65 ++++++++++--------- src/scene/services/scene.service.ts | 36 +++++----- 2 files changed, 53 insertions(+), 48 deletions(-) diff --git a/src/automation/services/automation.service.ts b/src/automation/services/automation.service.ts index 166e2d3..8999533 100644 --- a/src/automation/services/automation.service.ts +++ b/src/automation/services/automation.service.ts @@ -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,45 +207,47 @@ 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 new HttpException( - err.message || 'Automation not found', - err.status || HttpStatus.NOT_FOUND, - ); + throw err; } + throw new HttpException( + err.message || 'Automation not found', + err.status || HttpStatus.NOT_FOUND, + ); } } async findAutomationBySpace(spaceUuid: string) { diff --git a/src/scene/services/scene.service.ts b/src/scene/services/scene.service.ts index 6ff986d..1a9196e 100644 --- a/src/scene/services/scene.service.ts +++ b/src/scene/services/scene.service.ts @@ -229,33 +229,37 @@ 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( - 'An error occurred while retrieving scenes', - HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + throw error instanceof HttpException + ? error + : new HttpException( + 'An error occurred while retrieving scenes', + HttpStatus.INTERNAL_SERVER_ERROR, + ); } }