From 45b82515fca57c70cc9c77d1c1dc86f878679e1b Mon Sep 17 00:00:00 2001 From: Gabriel Amorim Date: Fri, 24 Feb 2023 14:31:50 -0300 Subject: [PATCH] FIX: do not return config_controls that the pipeline don't have --- src/modules/pipelinesV2/pipelines.service.ts | 34 ++++++++++++-------- src/utils/ObjValueFromPath.ts | 15 +++++++++ 2 files changed, 35 insertions(+), 14 deletions(-) diff --git a/src/modules/pipelinesV2/pipelines.service.ts b/src/modules/pipelinesV2/pipelines.service.ts index dcf740b..88f7a42 100644 --- a/src/modules/pipelinesV2/pipelines.service.ts +++ b/src/modules/pipelinesV2/pipelines.service.ts @@ -23,7 +23,7 @@ import { ConnectorClientService } from '../connector/client.service'; import { InputsService } from '../inputs/inputs.service'; import { RequestUser } from 'src/decorators/user.decorator'; import { TransformationsService } from '../transformations/transformations.service'; -import { getObjValueFromPath } from 'src/utils/ObjValueFromPath'; +import { getObjValueFromPath, objHasPath } from 'src/utils/ObjValueFromPath'; import ErrorCodes from 'src/utils/errorCodes'; import ErrorBuilder from 'src/utils/ErrorBuilder'; @@ -220,7 +220,7 @@ export class PipelinesService implements OnModuleInit { this.logger.info('PipelinesClientService - findOneProperties'); const { pipeline } = await this.findOne({ id }, metadata); - const pipelineProperties = JSON.parse(pipeline.properties); + const properties = JSON.parse(pipeline.properties); const connectorId = `${pipeline.connector_plugin}-${pipeline.connector_version}`; @@ -228,24 +228,30 @@ export class PipelinesService implements OnModuleInit { connectorId, metadata.get('language')[0].toString(), ); - const connector = JSON.parse(res.connector); - const config_controls = connector.config_controls || []; + const connector: { [key: string]: any; config_controls: any[] } = + JSON.parse(res.connector); + const config_controls = (connector.config_controls || []).filter( + (config_control) => { + const path: string = config_control.name; - for (const config_control of config_controls) { - const path: string = config_control.name; - const default_value = getObjValueFromPath({ - object: { properties: pipelineProperties }, - path, - }); - if (default_value != undefined) - config_control.default_value = default_value; - } + if (!objHasPath({ object: { properties }, path })) return false; + + const default_value = getObjValueFromPath({ + object: { properties }, + path, + }); + if (default_value != undefined) + config_control.default_value = default_value; + + return true; + }, + ); return { pipeline: { ...pipeline, config_controls, - properties: pipelineProperties, + properties: properties, }, }; } diff --git a/src/utils/ObjValueFromPath.ts b/src/utils/ObjValueFromPath.ts index 43674bd..0bdb8e9 100644 --- a/src/utils/ObjValueFromPath.ts +++ b/src/utils/ObjValueFromPath.ts @@ -8,3 +8,18 @@ export function getObjValueFromPath(data: { object: T; path: string }): T { } return value; } + +export function objHasPath(data: { + object: T; + path: string; +}): boolean { + const { object, path } = data; + const keys = path.split('.'); + + let value = JSON.parse(JSON.stringify(object)); + for (const key of keys) { + if (!(key in value)) return false; + value = value[key]; + } + return true; +}