Skip to content

Commit

Permalink
fix(api): Use the MongoId for stepId when fetching step controls (#6679)
Browse files Browse the repository at this point in the history
  • Loading branch information
rifont authored Oct 11, 2024
1 parent 3671e95 commit 4a3da93
Show file tree
Hide file tree
Showing 10 changed files with 212 additions and 154 deletions.
1 change: 0 additions & 1 deletion apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@
"helmet": "^6.0.1",
"i18next": "^23.7.6",
"ioredis": "5.3.2",
"json-schema-defaults": "^0.4.0",
"jsonwebtoken": "9.0.0",
"lodash": "^4.17.15",
"nanoid": "^3.1.20",
Expand Down
7 changes: 6 additions & 1 deletion apps/api/src/app/bridge/bridge.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,17 @@ export class BridgeController {
if (!workflowExist) {
throw new NotFoundException('Workflow not found');
}
const step = workflowExist?.steps.find((item) => item.stepId === stepId);

if (!step || !step._id) {
throw new NotFoundException('Step not found');
}

const result = await this.controlValuesRepository.findOne({
_environmentId: user.environmentId,
_organizationId: user.organizationId,
_workflowId: workflowExist._id,
stepId,
_stepId: step._id,
level: ControlVariablesLevelEnum.STEP_CONTROLS,
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { Injectable } from '@nestjs/common';
import defaults from 'json-schema-defaults';

import { Injectable, NotFoundException } from '@nestjs/common';
import { NotificationTemplateRepository } from '@novu/dal';
import { JsonSchema } from '@novu/framework';

import { ApiException, UpsertControlValuesCommand, UpsertControlValuesUseCase } from '@novu/application-generic';
import { UpsertControlValuesCommand, UpsertControlValuesUseCase } from '@novu/application-generic';
import { StoreControlVariablesCommand } from './store-control-variables.command';

@Injectable()
Expand All @@ -21,28 +18,22 @@ export class StoreControlVariablesUseCase {
);

if (!workflowExist) {
throw new ApiException('Workflow not found');
throw new NotFoundException('Workflow not found');
}

const step = workflowExist?.steps.find((item) => item.stepId === command.stepId);

if (!step || !step._id) {
throw new ApiException('Step not found');
throw new NotFoundException('Step not found');
}

const stepDefaultControls = defaults(
(step.template as any)?.controls?.schema || (step.template as any)?.inputs?.schema,
{}
) as JsonSchema;

return await this.upsertControlValuesUseCase.execute(
UpsertControlValuesCommand.create({
organizationId: command.organizationId,
environmentId: command.environmentId,
notificationStepEntity: step,
workflowId: workflowExist._id,
newControlValues: command.variables,
controlSchemas: { schema: stepDefaultControls },
})
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,18 @@ import { WorkflowAlreadyExistException } from '../../exceptions/workflow-already
import { StepUpsertMechanismFailedMissingIdException } from '../../exceptions/step-upsert-mechanism-failed-missing-id.exception';
import { toResponseWorkflowDto } from '../../mappers/notification-template-mapper';

function buildUpsertControlValuesCommand(command: UpsertWorkflowCommand, persistedStep, persistedWorkflow, stepInDto) {
function buildUpsertControlValuesCommand(
command: UpsertWorkflowCommand,
persistedStep: NotificationStepEntity,
persistedWorkflow: NotificationTemplateEntity,
stepInDto: StepDto
): UpsertControlValuesCommand {
return UpsertControlValuesCommand.create({
organizationId: command.user.organizationId,
environmentId: command.user.environmentId,
notificationStepEntity: persistedStep,
workflowId: persistedWorkflow._id,
newControlValues: stepInDto.controlValues || {},
controlSchemas: stepInDto?.controls || { schema: {} },
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './upsert-control-values-command';
export * from './upsert-control-values-use-case';
export * from './upsert-control-values.command';
export * from './upsert-control-values.usecase';
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { IsNotEmpty, IsObject, IsOptional, IsString } from 'class-validator';
import { JsonSchema } from '@novu/framework';
import { NotificationStepEntity } from '@novu/dal';
import { OrganizationLevelCommand } from '../../commands';
import { EnvironmentCommand } from '../../commands';

export class UpsertControlValuesCommand extends OrganizationLevelCommand {
export class UpsertControlValuesCommand extends EnvironmentCommand {
@IsObject()
notificationStepEntity: NotificationStepEntity;

Expand All @@ -14,7 +13,4 @@ export class UpsertControlValuesCommand extends OrganizationLevelCommand {
@IsObject()
@IsOptional()
newControlValues?: Record<string, unknown>;

@IsObject()
controlSchemas: { schema: JsonSchema };
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import { Injectable } from '@nestjs/common';

import { ControlValuesEntity, ControlValuesRepository } from '@novu/dal';
import { ControlVariablesLevelEnum } from '@novu/shared';
import { UpsertControlValuesCommand } from './upsert-control-values-command';
import { UpsertControlValuesCommand } from './upsert-control-values.command';

@Injectable()
export class UpsertControlValuesUseCase {
constructor(private controlValuesRepository: ControlValuesRepository) {}

async execute(command: UpsertControlValuesCommand) {
const existingControlValues = await this.controlValuesRepository.findFirst({
_environmentId: command.environmentId || '',
_environmentId: command.environmentId,
_organizationId: command.organizationId,
_workflowId: command.workflowId,
_stepId: command.notificationStepEntity._templateId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,4 @@ export class ControlValuesEntity {
controls: Record<string, unknown>;
_workflowId: string;
_stepId: string;
workflowId: string;
stepId: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ const controlVariablesSchema = new Schema<ControlValuesModel>(
index: true,
type: Schema.Types.ObjectId,
} as any,
workflowId: Schema.Types.String,
stepId: Schema.Types.String,
level: Schema.Types.String,
priority: Schema.Types.Number,
inputs: Schema.Types.Mixed,
Expand Down
Loading

0 comments on commit 4a3da93

Please sign in to comment.