Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(api-service): variable not removed from preview #7422

Draft
wants to merge 4 commits into
base: next
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import { GeneratePreviewCommand } from './generate-preview.command';
import { BuildPayloadSchemaCommand } from '../build-payload-schema/build-payload-schema.command';
import { BuildPayloadSchema } from '../build-payload-schema/build-payload-schema.usecase';
import { Variable } from '../../util/template-parser/liquid-parser';
import { keysToObject } from '../../util/utils';
import { keysToObject, mergeCommonObjectKeys } from '../../util/utils';
import { isObjectTipTapNode } from '../../util/tip-tap.util';
import { buildVariables } from '../../util/build-variables';

Expand Down Expand Up @@ -159,7 +159,13 @@ export class GeneratePreviewUsecase {
finalVariablesExample = previewTemplateData.variablesExample;
}

finalVariablesExample = _.merge(finalVariablesExample, commandVariablesExample || {});
if (commandVariablesExample && Object.keys(commandVariablesExample).length > 0) {
// merge only values of common keys between finalVariablesExample and commandVariablesExample
finalVariablesExample = mergeCommonObjectKeys(
commandVariablesExample as Record<string, unknown>,
finalVariablesExample as Record<string, unknown>
);
}

return finalVariablesExample;
}
Expand Down
35 changes: 34 additions & 1 deletion apps/api/src/app/workflows-v2/util/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import set from 'lodash/set';
import values from 'lodash/values';
import isObject from 'lodash/isObject';
import isArray from 'lodash/isArray';

import { BadRequestException } from '@nestjs/common';

import { JSONSchemaDto } from '@novu/shared';
Expand Down Expand Up @@ -165,3 +164,37 @@ export function keysToObject(keys: string[], { fn } = { fn: (key: string) => key

return result;
}

/**
* Merges common/overlapping object keys from source into target.
*
* @example
* Target: { subscriber: { phone: '{{subscriber.phone}}', name: '{{subscriber.name}}' } }
* Source: { subscriber: { phone: '123' }, payload: { someone: '{{payload.someone}}' }}
* Result: { subscriber: { phone: '123', name: '{{subscriber.name}}' } }
*/
export function mergeCommonObjectKeys(
source: Record<string, unknown>,
target: Record<string, unknown>
Comment on lines +177 to +178
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤓 nitpick (non-blocking): ‏ Maybe we can change the order here, as far as i remember the first value is the target usually.

): Record<string, unknown> {
/* eslint-disable no-param-reassign */
return Object.entries(target).reduce(
(merged, [key, targetValue]) => {
const sourceValue = source[key];

// If both source and target values are objects, recursively merge them
if (isObject(targetValue) && isObject(sourceValue)) {
merged[key] = mergeCommonObjectKeys(
sourceValue as Record<string, unknown>,
targetValue as Record<string, unknown>
);
} else {
// Otherwise, use the target value if it exists, otherwise use the source value
merged[key] = sourceValue ?? targetValue;
}

return merged;
},
{} as Record<string, unknown>
);
}
Loading