refactor: Implement test run helper to work with services
This commit is contained in:
43
packages/backend/src/helpers/compute-parameters.ts
Normal file
43
packages/backend/src/helpers/compute-parameters.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import Step from '../models/step';
|
||||
import ExecutionStep from '../models/execution-step';
|
||||
import get from 'lodash.get';
|
||||
|
||||
const variableRegExp = /({{step\.[\da-zA-Z-]+(?:\.[\da-zA-Z-]+)+}})/g;
|
||||
|
||||
export default function computeParameters(
|
||||
parameters: Step['parameters'],
|
||||
executionSteps: ExecutionStep[]
|
||||
): Step['parameters'] {
|
||||
const entries = Object.entries(parameters);
|
||||
return entries.reduce((result, [key, value]: [string, unknown]) => {
|
||||
if (typeof value === 'string') {
|
||||
const parts = value.split(variableRegExp);
|
||||
|
||||
const computedValue = parts
|
||||
.map((part: string) => {
|
||||
const isVariable = part.match(variableRegExp);
|
||||
if (isVariable) {
|
||||
const stepIdAndKeyPath = part.replace(/{{step.|}}/g, '') as string;
|
||||
const [stepId, ...keyPaths] = stepIdAndKeyPath.split('.');
|
||||
const keyPath = keyPaths.join('.');
|
||||
const executionStep = executionSteps.find((executionStep) => {
|
||||
return executionStep.stepId === stepId;
|
||||
});
|
||||
const data = executionStep?.dataOut;
|
||||
const dataValue = get(data, keyPath);
|
||||
return dataValue;
|
||||
}
|
||||
|
||||
return part;
|
||||
})
|
||||
.join('');
|
||||
|
||||
return {
|
||||
...result,
|
||||
[key]: computedValue,
|
||||
};
|
||||
}
|
||||
|
||||
return result;
|
||||
}, {});
|
||||
}
|
@@ -3,13 +3,7 @@ import Connection from '../models/connection';
|
||||
import Flow from '../models/flow';
|
||||
import Step from '../models/step';
|
||||
import Execution from '../models/execution';
|
||||
import {
|
||||
IJSONObject,
|
||||
IApp,
|
||||
IGlobalVariable,
|
||||
ITriggerDataItem,
|
||||
} from '@automatisch/types';
|
||||
import triggerQueue from '../queues/trigger';
|
||||
import { IJSONObject, IApp, IGlobalVariable } from '@automatisch/types';
|
||||
|
||||
type GlobalVariableOptions = {
|
||||
connection?: Connection;
|
||||
@@ -17,12 +11,13 @@ type GlobalVariableOptions = {
|
||||
flow?: Flow;
|
||||
step?: Step;
|
||||
execution?: Execution;
|
||||
testRun?: boolean;
|
||||
};
|
||||
|
||||
const globalVariable = async (
|
||||
options: GlobalVariableOptions
|
||||
): Promise<IGlobalVariable> => {
|
||||
const { connection, app, flow, step, execution } = options;
|
||||
const { connection, app, flow, step, execution, testRun = false } = options;
|
||||
|
||||
const lastInternalId = await flow?.lastInternalId();
|
||||
|
||||
@@ -66,19 +61,10 @@ const globalVariable = async (
|
||||
},
|
||||
execution: {
|
||||
id: execution?.id,
|
||||
testRun,
|
||||
},
|
||||
};
|
||||
|
||||
variable.process = async (triggerDataItem: ITriggerDataItem) => {
|
||||
const jobName = `${step.appKey}-${triggerDataItem.meta.internalId}`;
|
||||
const jobPayload = {
|
||||
$: variable,
|
||||
triggerDataItem,
|
||||
};
|
||||
|
||||
await triggerQueue.add(jobName, jobPayload);
|
||||
};
|
||||
|
||||
if (trigger && trigger.dedupeStrategy === 'unique') {
|
||||
const lastInternalIds = await flow?.lastInternalIds();
|
||||
|
||||
|
Reference in New Issue
Block a user