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

Conversation

rifont
Copy link
Contributor

@rifont rifont commented Oct 11, 2024

What changed? Why was the change needed?

  • Use the MongoId for stepId when fetching step controls
    • Previously the user-provided stepId was used when fetching step controls. A recent PR removed persistence of stepId, this caused null to always be returned when fetching step controls from the Dashboard, meaning that the default controls were always shown
  • Remove redundant controlSchema property from UpsertControlValues use-case
    • Correspondingly, remove the json-schema-defaults package from API which is redundant - @novu/framework already generates the defaults
  • Remove redundant workflowId and stepId properties from ControlValuesEntity

Screenshots

persisted-controls-fetching.mov
Expand for optional sections

Related enterprise PR

Special notes for your reviewer

Copy link

linear bot commented Oct 11, 2024

Copy link

netlify bot commented Oct 11, 2024

Deploy Preview for novu-stg-vite-dashboard-poc ready!

Name Link
🔨 Latest commit db86d8c
🔍 Latest deploy log https://app.netlify.com/sites/novu-stg-vite-dashboard-poc/deploys/67090c355ba5150008303cbb
😎 Deploy Preview https://deploy-preview-6679--novu-stg-vite-dashboard-poc.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@@ -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.

@@ -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.


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.

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

Choose a reason for hiding this comment

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

Fixing to follow naming conventions.

(step.template as any)?.controls?.schema || (step.template as any)?.inputs?.schema,
{}
) as 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.

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.


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.

@@ -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.

@rifont rifont requested a review from tatarco October 11, 2024 11:33
Copy link
Contributor

@djabarovgeorge djabarovgeorge left a comment

Choose a reason for hiding this comment

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

awesome work, left couple of comments for your consideration

Comment on lines +179 to +181
if (!step || !step._id) {
throw new NotFoundException('Step not found');
}
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.

Comment on lines +21 to +27
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');
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.

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

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?

return await this.upsertControlValuesUseCase.execute(
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.

🧹 👏

@@ -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.

@rifont rifont merged commit 4a3da93 into next Oct 11, 2024
52 checks passed
@rifont rifont deleted the nv-4484-persisted-control-values-dont-load-in-dashboard branch October 11, 2024 12:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants