Skip to content

Commit

Permalink
fix(api-service): workflow creation api flow didn't generate payload …
Browse files Browse the repository at this point in the history
…schema (#7366)
  • Loading branch information
scopsy authored Dec 24, 2024
1 parent e8e8fab commit 55f7de6
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { EnvironmentWithUserCommand } from '@novu/application-generic';
import { IsString, IsObject, IsNotEmpty, IsOptional } from 'class-validator';
import { IsString, IsObject, IsOptional } from 'class-validator';

export class BuildPayloadSchemaCommand extends EnvironmentWithUserCommand {
@IsString()
@IsNotEmpty()
workflowId: string;
@IsOptional()
workflowId?: string;

/**
* Control values used for preview purposes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class BuildPayloadSchema {
private async getControlValues(command: BuildPayloadSchemaCommand) {
let controlValues = command.controlValues ? [command.controlValues] : [];

if (!controlValues.length) {
if (!controlValues.length && command.workflowId) {
controlValues = (
await this.controlValuesRepository.find(
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ export class BuildAvailableVariableSchemaUsecase {
workflow: NotificationTemplateEntity | undefined,
command: BuildAvailableVariableSchemaCommand
): Promise<JSONSchemaDto> {
if (!workflow) {
if (workflow && workflow.steps.length === 0) {
return {
type: 'object',
properties: {},
additionalProperties: true,
};
}

if (workflow.payloadSchema) {
if (workflow?.payloadSchema) {
return parsePayloadSchema(workflow.payloadSchema, { safe: true }) || emptyJsonSchema();
}

Expand All @@ -79,7 +79,7 @@ export class BuildAvailableVariableSchemaUsecase {
environmentId: command.environmentId,
organizationId: command.organizationId,
userId: command.userId,
workflowId: workflow._id,
workflowId: workflow?._id,
...(command.optimisticControlValues ? { controlValues: command.optimisticControlValues } : {}),
})
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import {
WorkflowStatusEnum,
StepIssues,
ControlSchemas,
DigestUnitEnum,
} from '@novu/shared';
import {
CreateWorkflow as CreateWorkflowGeneric,
Expand Down
33 changes: 33 additions & 0 deletions apps/api/src/app/workflows-v2/workflow.controller.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,39 @@ describe('Workflow Controller E2E API Testing', () => {
await assertValuesInSteps(workflowCreated);
}
});

it('should generate a payload schema if only control values are provided during workflow creation', async () => {
const steps = [
{
...buildEmailStep(),
controlValues: {
body: 'Welcome {{payload.name}}',
subject: 'Hello {{payload.name}}',
},
},
];

const nameSuffix = `Test Workflow${new Date().toISOString()}`;

const createWorkflowDto: CreateWorkflowDto = buildCreateWorkflowDto(`${nameSuffix}`, { steps });
const res = await workflowsClient.createWorkflow(createWorkflowDto);
expect(res.isSuccessResult()).to.be.true;

const workflow = res.value as WorkflowResponseDto;
expect(workflow).to.be.ok;

expect(workflow.steps[0].variables).to.be.ok;

const stepData = await getStepData(workflow._id, workflow.steps[0]._id);
expect(stepData.variables).to.be.ok;

const { properties } = stepData.variables as JSONSchemaDto;
expect(properties).to.be.ok;

const payloadProperties = properties?.payload as JSONSchemaDto;
expect(payloadProperties).to.be.ok;
expect(payloadProperties.properties?.name).to.be.ok;
});
});

describe('Update Workflow Permutations', () => {
Expand Down

0 comments on commit 55f7de6

Please sign in to comment.