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

feat(api): allow pass empty step control value #6992

Open
wants to merge 5 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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 @@ -4,5 +4,4 @@ import { ControlValuesEntity, NotificationTemplateEntity } from '@novu/dal';
export class ProcessWorkflowIssuesCommand extends EnvironmentWithUserObjectCommand {
workflow: NotificationTemplateEntity;
preferences?: GetPreferencesResponseDto;
stepIdToControlValuesMap: { [p: string]: ControlValuesEntity };
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
ContentIssue,
ControlValuesLevelEnum,
RuntimeIssue,
StepIssueEnum,
StepIssues,
Expand All @@ -13,6 +14,7 @@ import {
NotificationStepEntity,
NotificationTemplateEntity,
NotificationTemplateRepository,
ControlValuesRepository,
} from '@novu/dal';
import { Injectable } from '@nestjs/common';
import { ProcessWorkflowIssuesCommand } from './process-workflow-issues.command';
Expand All @@ -23,18 +25,35 @@ import { WorkflowNotFoundException } from '../../exceptions/workflow-not-found-e
export class ProcessWorkflowIssuesUsecase {
constructor(
private notificationTemplateRepository: NotificationTemplateRepository,
private buildDefaultControlValuesUsecase: ValidateControlValuesAndConstructPassableStructureUsecase
private buildDefaultControlValuesUsecase: ValidateControlValuesAndConstructPassableStructureUsecase,
private controlValuesRepository: ControlValuesRepository
) {}

async execute(command: ProcessWorkflowIssuesCommand): Promise<NotificationTemplateEntity> {
const workflowIssues = await this.validateWorkflow(command);
const stepIssues = this.validateSteps(command.workflow.steps, command.stepIdToControlValuesMap);
const stepIdToControlValuesMap: { [p: string]: ControlValuesEntity } = await this.fetchStepsControlValue(command);
const stepIssues = this.validateSteps(command.workflow.steps, stepIdToControlValuesMap);
const workflowWithIssues = this.updateIssuesOnWorkflow(command.workflow, workflowIssues, stepIssues);
await this.persistWorkflow(command, workflowWithIssues);

return await this.getWorkflow(command);
}

private async fetchStepsControlValue(command: ProcessWorkflowIssuesCommand) {
const stepsControlValues = await this.controlValuesRepository.find({
_environmentId: command.user.environmentId,
_organizationId: command.user.organizationId,
_workflowId: command.workflow._id,
level: ControlValuesLevelEnum.STEP_CONTROLS,
});
const stepIdToControlValuesMap: { [p: string]: ControlValuesEntity } = {};
for (const stepsControlValue of stepsControlValues) {
stepIdToControlValuesMap[stepsControlValue._stepId] = stepsControlValue;
}

return stepIdToControlValuesMap;
}

private async persistWorkflow(command: ProcessWorkflowIssuesCommand, workflowWithIssues: NotificationTemplateEntity) {
const isWorkflowCompleteAndValid = this.isWorkflowCompleteAndValid(workflowWithIssues);
const status = this.calculateStatus(isWorkflowCompleteAndValid, workflowWithIssues);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,13 @@ export class UpsertWorkflowUseCase {
const workflowForUpdate = await this.queryWorkflow(command);

const workflow = await this.createOrUpdateWorkflow(workflowForUpdate, command);
const stepIdToControlValuesMap = await this.upsertControlValues(workflow, command);
await this.upsertControlValues(workflow, command);
const preferences = await this.upsertPreference(command, workflow);
const workflowIssues = await this.processWorkflowIssuesUsecase.execute(
ProcessWorkflowIssuesCommand.create({
user: command.user,
workflow,
preferences,
stepIdToControlValuesMap,
})
);

Expand All @@ -108,41 +107,25 @@ export class UpsertWorkflowUseCase {
}

private async upsertControlValues(workflow: NotificationTemplateEntity, command: UpsertWorkflowCommand) {
const stepIdToControlValuesMap: { [p: string]: ControlValuesEntity } = {};
for (const persistedStep of workflow.steps) {
const controlValuesEntity = await this.upsertControlValuesForSingleStep(persistedStep, command, workflow);
if (controlValuesEntity) {
stepIdToControlValuesMap[persistedStep._templateId] = controlValuesEntity;
const stepDatabaseId = persistedStep._templateId;
const stepExternalId = persistedStep.name;
if (!stepDatabaseId && !stepExternalId) {
throw new StepUpsertMechanismFailedMissingIdException(stepDatabaseId, stepExternalId, persistedStep);
}
}
const stepInDto = command.workflowDto?.steps.find(
(commandStepItem) => commandStepItem.name === persistedStep.name
);

return stepIdToControlValuesMap;
}
if (!stepInDto || Object.keys(stepInDto.controlValues || {}).length === 0) {
// TODO: should delete the values from the database? or just ignore?
continue;
}

private async upsertControlValuesForSingleStep(
persistedStep: NotificationStepEntity,
command: UpsertWorkflowCommand,
persistedWorkflow: NotificationTemplateEntity
): Promise<ControlValuesEntity | undefined> {
const stepDatabaseId = persistedStep._templateId;
const stepExternalId = persistedStep.name;
if (!stepDatabaseId && !stepExternalId) {
throw new StepUpsertMechanismFailedMissingIdException(stepDatabaseId, stepExternalId, persistedStep);
}
const stepInDto = command.workflowDto?.steps.find((commandStepItem) => commandStepItem.name === persistedStep.name);
if (!stepInDto) {
// TODO: should delete the values from the database? or just ignore?
return;
await this.upsertControlValuesUseCase.execute(
buildUpsertControlValuesCommand(command, persistedStep, workflow, stepInDto)
);
}

const upsertControlValuesCommand = buildUpsertControlValuesCommand(
command,
persistedStep,
persistedWorkflow,
stepInDto
);

return await this.upsertControlValuesUseCase.execute(upsertControlValuesCommand);
}

private async upsertPreference(
Expand Down
Loading