Skip to content

Commit

Permalink
Merge branch 'next' into nv-4931-in-app-preview-improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
LetItRock authored Dec 2, 2024
2 parents 629ebff + cf3b1cd commit 7df7c4d
Show file tree
Hide file tree
Showing 22 changed files with 262 additions and 135 deletions.
7 changes: 4 additions & 3 deletions apps/api/src/app/workflows-v2/e2e/generate-preview.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe('Workflow Step Preview - POST /:workflowId/step/:stepId/preview', () =>
preview: {
subject: 'Welcome {{subscriber.firstName}}',
// cspell:disable-next-line
body: 'Hello {{subscriber.firstName}} {{subscriber.lastName}}, Welcome to {{PAYLOAD.ORGANIZATIONNAME}}!',
body: 'Hello {{subscriber.firstName}} {{subscriber.lastName}}, Welcome to {{PAYLOAD.ORGANIZATIONNAME | UPCASE}}!',
},
type: 'in_app',
},
Expand All @@ -69,7 +69,7 @@ describe('Workflow Step Preview - POST /:workflowId/step/:stepId/preview', () =>
lastName: '{{subscriber.lastName}}',
},
payload: {
organizationName: '{{payload.organizationName}}',
organizationName: '{{payload.organizationName | upcase}}',
},
},
},
Expand Down Expand Up @@ -409,14 +409,15 @@ describe('Workflow Step Preview - POST /:workflowId/step/:stepId/preview', () =>
result: {
preview: {
subject: 'Welcome John',
body: 'Hello John, your order #undefined is ready!', // orderId is not defined in the payload schema
body: 'Hello John, your order #{{payload.orderId}} is ready!', // orderId is not defined in the payload schema
},
type: 'in_app',
},
previewPayloadExample: {
payload: {
lastName: '{{payload.lastName}}',
organizationName: '{{payload.organizationName}}',
orderId: '{{payload.orderId}}',
firstName: 'John',
},
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BadRequestException, Injectable, InternalServerErrorException } from '@nestjs/common';
import { Injectable, InternalServerErrorException } from '@nestjs/common';
import _ from 'lodash';
import {
ChannelTypeEnum,
Expand All @@ -22,13 +22,15 @@ import { GeneratePreviewCommand } from './generate-preview.command';
import { extractTemplateVars } from '../../util/template-variables/extract-template-variables';
import { pathsToObject } from '../../util/path-to-object';
import { createMockPayloadFromSchema, flattenObjectValues } from '../../util/utils';
import { PrepareAndValidateContentUsecase } from '../validate-content/prepare-and-validate-content/prepare-and-validate-content.usecase';

@Injectable()
export class GeneratePreviewUsecase {
constructor(
private legacyPreviewStepUseCase: PreviewStep,
private buildStepDataUsecase: BuildStepDataUsecase,
private getWorkflowByIdsUseCase: GetWorkflowByIdsUseCase
private getWorkflowByIdsUseCase: GetWorkflowByIdsUseCase,
private prepareAndValidateContentUsecase: PrepareAndValidateContentUsecase
) {}

@InstrumentUsecase()
Expand All @@ -37,18 +39,24 @@ export class GeneratePreviewUsecase {
command.generatePreviewRequestDto;
const stepData = await this.getStepData(command);
const workflow = await this.findWorkflow(command);

const preparedAndValidatedContent = await this.prepareAndValidateContentUsecase.execute({
user: command.user,
previewPayloadFromDto: commandVariablesExample,
controlValues: commandControlValues || stepData.controls.values || {},
controlDataSchema: stepData.controls.dataSchema || {},
variableSchema: stepData.variables,
});
const variablesExample = this.buildVariablesExample(
workflow,
stepData,
commandVariablesExample,
commandControlValues
preparedAndValidatedContent.finalPayload,
commandVariablesExample
);

const executeOutput = await this.executePreviewUsecase(
command,
stepData,
variablesExample,
commandControlValues || {}
preparedAndValidatedContent.finalControlValues
);

return {
Expand All @@ -63,17 +71,24 @@ export class GeneratePreviewUsecase {
@Instrument()
private buildVariablesExample(
workflow: WorkflowInternalResponseDto,
stepData: StepDataDto,
commandVariablesExample?: PreviewPayload,
commandControlValues?: Record<string, unknown>
finalPayload?: PreviewPayload,
commandVariablesExample?: PreviewPayload | undefined
) {
const variablesExample = this.generateVariablesExample(stepData, commandControlValues);
if (workflow.origin !== WorkflowOriginEnum.EXTERNAL) {
return finalPayload;
}

if (workflow.origin === WorkflowOriginEnum.EXTERNAL) {
variablesExample.payload = createMockPayloadFromSchema(workflow.payloadSchema);
const examplePayloadSchema = createMockPayloadFromSchema(workflow.payloadSchema);

if (!examplePayloadSchema || Object.keys(examplePayloadSchema).length === 0) {
return finalPayload;
}

return _.merge(variablesExample, commandVariablesExample as Record<string, unknown>);
return _.merge(
finalPayload as Record<string, unknown>,
{ payload: examplePayloadSchema },
commandVariablesExample || {}
);
}

@Instrument()
Expand Down
11 changes: 9 additions & 2 deletions apps/api/src/app/workflows-v2/util/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,24 @@ export function flattenObjectValues(obj: Record<string, unknown>): string[] {
export function createMockPayloadFromSchema(
schema: JSONSchemaDto,
path = 'payload',
depth = 0
depth = 0,
safe = true
): Record<string, unknown> {
const MAX_DEPTH = 10;
if (depth >= MAX_DEPTH) {
if (safe) {
return {};
}
throw new BadRequestException({
message: 'Schema has surpassed the maximum allowed depth. Please specify a more shallow payload schema.',
maxDepth: MAX_DEPTH,
});
}

if (schema.type !== 'object' || !schema.properties) {
if (schema?.type !== 'object' || !schema?.properties) {
if (safe) {
return {};
}
throw new BadRequestException({
message: 'Schema must define an object with properties.',
});
Expand Down
15 changes: 7 additions & 8 deletions apps/dashboard/public/images/avatar.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 3 additions & 4 deletions apps/dashboard/public/images/bell.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
59 changes: 29 additions & 30 deletions apps/dashboard/public/images/building.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 7df7c4d

Please sign in to comment.