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): Use the MongoId for stepId when fetching step controls #6679

Merged
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
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",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not needed.

"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');
}
Comment on lines +179 to +181
Copy link
Contributor

Choose a reason for hiding this comment

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

Question: can we do something like this?
I started to use this pattern in this PR this way we have the clean message to search by with the additional metadata (step id)

Suggested change
if (!step || !step._id) {
throw new NotFoundException('Step not found');
}
if (!step || !step._id) {
throw new NotFoundException({
message: 'Step not found',
stepId: stepId,
});
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As discussed, we will address this holistically across the API using shared classes.


const result = await this.controlValuesRepository.findOne({
_environmentId: user.environmentId,
_organizationId: user.organizationId,
_workflowId: workflowExist._id,
stepId,
_stepId: step._id,
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 change fixes the fetching of persisted step controls.

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');
}
Comment on lines +21 to +27
Copy link
Contributor

Choose a reason for hiding this comment

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

same here.


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

return await this.upsertControlValuesUseCase.execute(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Defaults are generated on @novu/framework, there is no need to generate defaults on the API.

Copy link
Contributor

Choose a reason for hiding this comment

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

question just to clarify, for the dashboard, we need to make sure it gets the default values for the code-first solution, so it can display them. If we have a code-first preview, we first get the default values from the bridge app and then override them with control values if they exist (persisted in novu), correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The JSON schema from @novu/framework is passed to the frontend to render the default values, so we don't need to create defaults on the API.

UpsertControlValuesCommand.create({
organizationId: command.organizationId,
environmentId: command.environmentId,
notificationStepEntity: step,
workflowId: workflowExist._id,
newControlValues: command.variables,
controlSchemas: { schema: stepDefaultControls },
})
Copy link
Contributor

Choose a reason for hiding this comment

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

🧹 👏

);
}
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 {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The incorrect base command was used here, which resulted in environmentId being an optional type when it should have been required.

@IsObject()
notificationStepEntity: NotificationStepEntity;

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

@IsObject()
controlSchemas: { schema: JsonSchema };
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 was never used. Cleaning up.

}
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';
Copy link
Contributor

Choose a reason for hiding this comment

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

i am a bit confused should not it be a dot here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The new code shows a dot.

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;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These are no longer used.

}
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
Loading