Skip to content

Commit

Permalink
feat(api): add origin backward compatibility mapping (#6973)
Browse files Browse the repository at this point in the history
  • Loading branch information
djabarovgeorge authored Nov 12, 2024
1 parent 0fb9eee commit e17502f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import {
StepContentIssueEnum,
StepTypeEnum,
WorkflowOriginEnum,
WorkflowTypeEnum,
} from '@novu/shared';
import { merge } from 'lodash/fp';
import _ = require('lodash');
import { GetWorkflowByIdsUseCase } from '@novu/application-generic';
import { NotificationTemplateEntity } from '@novu/dal';
import { GeneratePreviewCommand } from './generate-preview-command';
import { PreviewStep, PreviewStepCommand } from '../../../bridge/usecases/preview-step';
import { StepMissingControlsException, StepNotFoundException } from '../../exceptions/step-not-found-exception';
Expand Down Expand Up @@ -117,15 +119,37 @@ export class GeneratePreviewUsecase {
if (!step.template || !step.template.controls) {
throw new StepMissingControlsException(command.stepDatabaseId, step);
}
const origin = this.buildOrigin(persistedWorkflow);

return {
workflowId: persistedWorkflow.triggers[0].identifier,
stepId: step.stepId,
stepType: step.template.type,
stepControlSchema: step.template.controls,
origin: persistedWorkflow.origin,
origin,
};
}

/**
* Builds the origin of the workflow based on the workflow type.
* If the origin is not set, it will be built based on the workflow type.
* We need to do so for backward compatibility reasons.
*/
private buildOrigin(persistedWorkflow: NotificationTemplateEntity): WorkflowOriginEnum {
if (persistedWorkflow.origin) {
return persistedWorkflow.origin;
}

if (persistedWorkflow.type === WorkflowTypeEnum.ECHO || persistedWorkflow.type === WorkflowTypeEnum.BRIDGE) {
return WorkflowOriginEnum.EXTERNAL;
}

if (persistedWorkflow.type === WorkflowTypeEnum.REGULAR) {
return WorkflowOriginEnum.NOVU_CLOUD_V1;
}

return WorkflowOriginEnum.NOVU_CLOUD;
}
}

function buildResponse(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ export class StepIdMissingException extends InternalServerErrorException {
}

export class OriginMissingException extends InternalServerErrorException {
constructor(stepId: string) {
super({ message: `Origin is missing for the stepId: ${stepId}`, stepId });
constructor(workflowId: string) {
super({
message: 'Origin is missing on the workflow',
workflowId,
});
}
}

0 comments on commit e17502f

Please sign in to comment.