Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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,5 +1,5 @@
import { EnvironmentWithUserObjectCommand } from '@novu/application-generic';
import { NotificationTemplateEntity } from '@novu/dal';
import { ControlValuesEntity, NotificationTemplateEntity } from '@novu/dal';
import { ResourceOriginEnum, StepTypeEnum } from '@novu/shared';
import { IsDefined, IsEnum, IsObject, IsOptional, IsString } from 'class-validator';
import { JSONSchemaDto } from '../../../shared/dtos/json-schema.dto';
Expand Down Expand Up @@ -39,4 +39,10 @@ export class BuildStepIssuesCommand extends EnvironmentWithUserObjectCommand {
*/
@IsOptional()
optimisticSteps?: IOptimisticStepInfo[];

/**
* Pre-loaded control values to avoid redundant database queries
*/
@IsOptional()
preloadedControlValues?: ControlValuesEntity[];
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export class BuildStepIssuesUsecase {
controlSchema,
controlsDto: controlValuesDto,
stepType,
preloadedControlValues,
} = command;

const variableSchema = await this.buildAvailableVariableSchemaUsecase.execute(
Expand All @@ -68,21 +69,26 @@ export class BuildStepIssuesUsecase {
workflow: persistedWorkflow,
...(controlValuesDto ? { optimisticControlValues: controlValuesDto } : {}),
...(command.optimisticSteps ? { optimisticSteps: command.optimisticSteps } : {}),
...(preloadedControlValues ? { preloadedControlValues } : {}),
})
);

let newControlValues = controlValuesDto;

if (!newControlValues) {
newControlValues = (
await this.controlValuesRepository.findOne({
_environmentId: user.environmentId,
_organizationId: user.organizationId,
_workflowId: persistedWorkflow?._id,
_stepId: stepInternalId,
level: ControlValuesLevelEnum.STEP_CONTROLS,
})
)?.controls;
if (preloadedControlValues && stepInternalId) {
newControlValues = preloadedControlValues.find((cv) => cv._stepId === stepInternalId)?.controls;
} else {
newControlValues = (
await this.controlValuesRepository.findOne({
_environmentId: user.environmentId,
_organizationId: user.organizationId,
_workflowId: persistedWorkflow?._id,
_stepId: stepInternalId,
level: ControlValuesLevelEnum.STEP_CONTROLS,
})
)?.controls;
}
}

const sanitizedControlValues = this.sanitizeControlValues(newControlValues, workflowOrigin, stepType);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EnvironmentWithUserCommand } from '@novu/application-generic';
import { NotificationTemplateEntity } from '@novu/dal';
import { ControlValuesEntity, NotificationTemplateEntity } from '@novu/dal';
import { StepTypeEnum } from '@novu/shared';
import { IsDefined, IsOptional, IsString } from 'class-validator';
import { PreviewPayloadDto } from '../../dtos';
Expand Down Expand Up @@ -33,4 +33,10 @@ export class BuildVariableSchemaCommand extends EnvironmentWithUserCommand {

@IsOptional()
previewData?: PreviewPayloadDto;

/**
* Pre-loaded control values to avoid redundant database queries
*/
@IsOptional()
preloadedControlValues?: ControlValuesEntity[];
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Injectable } from '@nestjs/common';
import { Instrument, InstrumentUsecase } from '@novu/application-generic';
import {
ControlValuesEntity,
ControlValuesRepository,
JsonSchemaTypeEnum,
NotificationStepEntity,
Expand Down Expand Up @@ -31,23 +32,28 @@ export class BuildVariableSchemaUsecase {

@InstrumentUsecase()
async execute(command: BuildVariableSchemaCommand): Promise<JSONSchemaDto> {
const { workflow, stepInternalId, optimisticSteps, previewData } = command;
const { workflow, stepInternalId, optimisticSteps, previewData, preloadedControlValues } = command;

let workflowControlValues: unknown[] = [];
if (workflow) {
const controls = await this.controlValuesRepository.find(
{
_environmentId: command.environmentId,
_organizationId: command.organizationId,
_workflowId: workflow._id,
level: ControlValuesLevelEnum.STEP_CONTROLS,
controls: { $ne: null },
},
{
controls: 1,
_id: 0,
}
);
let controls: ControlValuesEntity[];
if (preloadedControlValues) {
controls = preloadedControlValues;
} else {
controls = await this.controlValuesRepository.find(
{
_environmentId: command.environmentId,
_organizationId: command.organizationId,
_workflowId: workflow._id,
level: ControlValuesLevelEnum.STEP_CONTROLS,
controls: { $ne: null },
},
{
controls: 1,
_id: 0,
}
);
}

workflowControlValues = controls
.flatMap((item) => item.controls)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
import {
ClientSession,
ControlSchemas,
ControlValuesEntity,
ControlValuesRepository,
NotificationGroupRepository,
NotificationStepEntity,
Expand Down Expand Up @@ -210,64 +211,95 @@ export class UpsertWorkflowUseCase {
command: UpsertWorkflowCommand,
existingWorkflow?: NotificationTemplateEntity
): Promise<NotificationStep[]> {
const steps: NotificationStep[] = [];
const {
user,
workflowDto: { origin: workflowOrigin },
} = command;

// Build optimistic step information for sync scenarios
const optimisticSteps = command.workflowDto.steps.map((step, index) => ({
stepId: step.stepId || this.generateUniqueStepId(step, steps.slice(0, index)),
type: step.type,
}));
let preloadedControlValues: ControlValuesEntity[] | undefined;
if (existingWorkflow) {
preloadedControlValues = await this.controlValuesRepository.find(
{
_environmentId: user.environmentId,
_organizationId: user.organizationId,
_workflowId: existingWorkflow._id,
level: ControlValuesLevelEnum.STEP_CONTROLS,
controls: { $ne: null },
},
{
controls: 1,
_stepId: 1,
_id: 0,
}
);
}

const tempSteps: NotificationStep[] = [];
const stepIds: string[] = [];

for (const step of command.workflowDto.steps) {
const existingStep: NotificationStepEntity | null | undefined =
'_id' in step ? existingWorkflow?.steps.find((s) => !!step._id && s._templateId === step._id) : null;

const {
user,
workflowDto: { origin: workflowOrigin },
} = command;

const controlSchemas: ControlSchemas = existingStep?.template?.controls || stepTypeToControlSchema[step.type];
const issues: StepIssuesDto = await this.buildStepIssuesUsecase.execute({
workflowOrigin,
user,
stepInternalId: existingStep?._id,
workflow: existingWorkflow,
stepType: step.type,
controlSchema: controlSchemas.schema,
controlsDto: step.controlValues,
optimisticSteps, // Pass optimistic steps for variable schema building
});

const updateStepId = existingStep?.stepId;
const syncToEnvironmentCreateStepId = step.stepId;
const finalStep = {
template: {
type: step.type,
name: step.name,
controls: controlSchemas,
content: '',
},
stepId:
updateStepId ||
syncToEnvironmentCreateStepId ||
this.generateUniqueStepId(step, existingWorkflow ? existingWorkflow.steps : steps),
name: step.name,
issues,
};

if (existingStep) {
Object.assign(finalStep, {
_id: existingStep._templateId,
_templateId: existingStep._templateId,
template: { ...finalStep.template, _id: existingStep._templateId },
});
}
const generatedStepId =
updateStepId ||
syncToEnvironmentCreateStepId ||
this.generateUniqueStepId(step, existingWorkflow ? existingWorkflow.steps : tempSteps);

steps.push(finalStep);
stepIds.push(generatedStepId);
tempSteps.push({ stepId: generatedStepId } as NotificationStep);
}

return steps;
const optimisticSteps = command.workflowDto.steps.map((step, index) => ({
stepId: stepIds[index],
type: step.type,
}));

const stepsWithIssues = await Promise.all(
command.workflowDto.steps.map(async (step, index) => {
const existingStep: NotificationStepEntity | null | undefined =
'_id' in step ? existingWorkflow?.steps.find((s) => !!step._id && s._templateId === step._id) : null;

const controlSchemas: ControlSchemas = existingStep?.template?.controls || stepTypeToControlSchema[step.type];
const issues: StepIssuesDto = await this.buildStepIssuesUsecase.execute({
workflowOrigin,
user,
stepInternalId: existingStep?._id,
workflow: existingWorkflow,
stepType: step.type,
controlSchema: controlSchemas.schema,
controlsDto: step.controlValues,
optimisticSteps,
preloadedControlValues,
});

const finalStep = {
template: {
type: step.type,
name: step.name,
controls: controlSchemas,
content: '',
},
stepId: stepIds[index],
name: step.name,
issues,
};

if (existingStep) {
Object.assign(finalStep, {
_id: existingStep._templateId,
_templateId: existingStep._templateId,
template: { ...finalStep.template, _id: existingStep._templateId },
});
}

return finalStep;
})
);

return stepsWithIssues;
}

@Instrument()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Separator } from '@/components/primitives/separator';
import { TabsContent, TabsList, TabsTrigger } from '@/components/primitives/tabs';
import { Tooltip, TooltipContent, TooltipTrigger } from '@/components/primitives/tooltip';
import { AMOUNT_KEY, CRON_KEY, TYPE_KEY, UNIT_KEY } from '@/components/workflow-editor/steps/digest-delay-tabs/keys';
import { LookbackWindow } from '@/components/workflow-editor/steps/digest-delay-tabs/lookback-window';
import { RegularType } from '@/components/workflow-editor/steps/digest-delay-tabs/regular-type';
import { ScheduledType } from '@/components/workflow-editor/steps/digest-delay-tabs/scheduled-type';
import { EVERY_MINUTE_CRON } from '@/components/workflow-editor/steps/digest-delay-tabs/utils';
Expand Down Expand Up @@ -136,11 +137,17 @@ export const DigestDelayTabs = ({ isDigest = true }: { isDigest?: boolean }) =>
</TabsList>
</div>
<Separator className="before:bg-neutral-100" />
<div className="bg-background rounded-b-lg p-2">
<TabsContent value={REGULAR_TYPE}>
<div className="bg-background flex flex-col gap-2 rounded-b-lg p-2">
<TabsContent value={REGULAR_TYPE} className="m-0">
<RegularType isReadOnly={isReadOnly} isDigest={isDigest} />
{isDigest && (
<>
<Separator className="my-2 stroke-stroke-weak" />
<LookbackWindow isReadOnly={isReadOnly} />
</>
)}
</TabsContent>
<TabsContent value={SCHEDULED_TYPE}>
<TabsContent value={SCHEDULED_TYPE} className="m-0">
<FormField
control={control}
name={CRON_KEY}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ export const AMOUNT_KEY = 'controlValues.amount';
export const UNIT_KEY = 'controlValues.unit';
export const CRON_KEY = 'controlValues.cron';
export const TYPE_KEY = 'controlValues.type';
export const LOOKBACK_AMOUNT_KEY = 'controlValues.lookBackWindow.amount';
export const LOOKBACK_UNIT_KEY = 'controlValues.lookBackWindow.unit';
Loading
Loading