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): workflow creation api flow didn't generate payload schema #7366

Merged
merged 6 commits into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
@@ -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) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is what caused the issue, as the workflow didn't existed here yet.

Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (workflow && !workflow?.steps.length) {
if (workflow && workflow.steps && 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
Loading