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): invalidate stale workflows #6887

Merged
merged 13 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
5 changes: 5 additions & 0 deletions apps/api/src/app/bridge/bridge.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {
CreateMessageTemplate,
CreateWorkflow,
DeleteMessageTemplate,
DeleteWorkflowUseCase,
GetWorkflowByIdsUseCase,
UpdateChange,
UpdateMessageTemplate,
UpdateWorkflow,
Expand All @@ -18,6 +20,9 @@ import { USECASES } from './usecases';
const PROVIDERS = [
CreateWorkflow,
UpdateWorkflow,
GetWorkflowByIdsUseCase,
DeleteWorkflowUseCase,
UpsertControlValuesUseCase,
CreateMessageTemplate,
UpdateMessageTemplate,
DeleteMessageTemplate,
Expand Down

This file was deleted.

This file was deleted.

2 changes: 0 additions & 2 deletions apps/api/src/app/bridge/usecases/delete-workflow/index.ts

This file was deleted.

11 changes: 1 addition & 10 deletions apps/api/src/app/bridge/usecases/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
import { UpsertControlValuesUseCase } from '@novu/application-generic';
import { DeleteWorkflow } from './delete-workflow';
import { GetBridgeStatus } from './get-bridge-status';
import { PreviewStep } from './preview-step';
import { StoreControlValuesUseCase } from './store-control-values';
import { Sync } from './sync';

export const USECASES = [
DeleteWorkflow,
GetBridgeStatus,
PreviewStep,
StoreControlValuesUseCase,
Sync,
UpsertControlValuesUseCase,
];
export const USECASES = [GetBridgeStatus, PreviewStep, StoreControlValuesUseCase, Sync];
31 changes: 16 additions & 15 deletions apps/api/src/app/bridge/usecases/sync/sync.usecase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import {
AnalyticsService,
CreateWorkflow,
CreateWorkflowCommand,
DeleteWorkflowCommand,
DeleteWorkflowUseCase,
ExecuteBridgeRequest,
InvalidateCacheService,
NotificationStep,
UpdateWorkflow,
UpdateWorkflowCommand,
Expand All @@ -26,20 +29,20 @@ import {
import { DiscoverOutput, DiscoverStepOutput, DiscoverWorkflowOutput, GetActionEnum } from '@novu/framework/internal';

import { SyncCommand } from './sync.command';
import { DeleteWorkflow, DeleteWorkflowCommand } from '../delete-workflow';
import { CreateBridgeResponseDto } from '../../dtos/create-bridge-response.dto';

@Injectable()
export class Sync {
constructor(
private createWorkflowUsecase: CreateWorkflow,
private updateWorkflowUsecase: UpdateWorkflow,
private deleteWorkflow: DeleteWorkflow,
private deleteWorkflowUseCase: DeleteWorkflowUseCase,
private notificationTemplateRepository: NotificationTemplateRepository,
private notificationGroupRepository: NotificationGroupRepository,
private environmentRepository: EnvironmentRepository,
private executeBridgeRequest: ExecuteBridgeRequest,
private analyticsService: AnalyticsService,
private invalidateCacheService: InvalidateCacheService,
private upsertPreferences: UpsertPreferences
) {}
async execute(command: SyncCommand): Promise<CreateBridgeResponseDto> {
Expand Down Expand Up @@ -111,21 +114,19 @@ export class Sync {
createdWorkflows: NotificationTemplateEntity[]
): Promise<void> {
const persistedWorkflowIdsInBridge = createdWorkflows.map((i) => i._id);

const workflowsToDelete = await this.findAllWorkflowsWithOtherIds(command, persistedWorkflowIdsInBridge);

await Promise.all(
workflowsToDelete?.map((workflow) => {
return this.deleteWorkflow.execute(
DeleteWorkflowCommand.create({
environmentId: command.environmentId,
organizationId: command.organizationId,
userId: command.userId,
workflowId: workflow._id,
})
);
})
const deleteWorkflowFromStoragePromises = workflowsToDelete.map((workflow) =>
this.deleteWorkflowUseCase.execute(
DeleteWorkflowCommand.create({
environmentId: command.environmentId,
organizationId: command.organizationId,
userId: command.userId,
identifierOrInternalId: workflow._id,
})
)
);

await Promise.all([...deleteWorkflowFromStoragePromises]);
}

private async findAllWorkflowsWithOtherIds(
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import {
} from '@novu/shared';
import { merge } from 'lodash/fp';
import _ = require('lodash');
import { GetWorkflowByIdsUseCase } from '@novu/application-generic';
import { GeneratePreviewCommand } from './generate-preview-command';
import { PreviewStep, PreviewStepCommand } from '../../../bridge/usecases/preview-step';
import { StepMissingControlsException, StepNotFoundException } from '../../exceptions/step-not-found-exception';
import { GetWorkflowByIdsUseCase } from '../get-workflow-by-ids/get-workflow-by-ids.usecase';
import { OriginMissingException, StepIdMissingException } from './step-id-missing.exception';
import { BuildDefaultPayloadUseCase } from '../build-payload-from-placeholder';
import { FrameworkPreviousStepsOutputState } from '../../../bridge/usecases/preview-step/preview-step.command';
Expand Down Expand Up @@ -129,7 +129,9 @@ export class GeneratePreviewUsecase {
private async getWorkflowUserIdentifierFromWorkflowObject(command: GeneratePreviewCommand) {
const persistedWorkflow = await this.getWorkflowByIdsUseCase.execute({
identifierOrInternalId: command.workflowId,
user: command.user,
environmentId: command.user.environmentId,
organizationId: command.user.organizationId,
userId: command.user._id,
});
const { steps } = persistedWorkflow;
const step = steps.find((stepDto) => stepDto._id === command.stepDatabaseId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import { BadRequestException, Injectable } from '@nestjs/common';
import { ControlValuesLevelEnum, StepDataDto } from '@novu/shared';
import { JSONSchema } from 'json-schema-to-ts';
import { ControlValuesRepository, NotificationStepEntity, NotificationTemplateEntity } from '@novu/dal';
import { GetWorkflowByIdsUseCase } from '@novu/application-generic';

import { GetStepDataCommand } from './get-step-data.command';
import { mapStepTypeToResult } from '../../shared';
import { GetWorkflowByIdsUseCase } from '../get-workflow-by-ids/get-workflow-by-ids.usecase';
import { InvalidStepException } from '../../exceptions/invalid-step.exception';
import { BuildDefaultPayloadUseCase } from '../build-payload-from-placeholder';
import { buildJSONSchema } from '../../shared/build-string-schema';
Expand Down Expand Up @@ -51,7 +52,9 @@ export class GetStepDataUsecase {
private async fetchWorkflow(command: GetStepDataCommand) {
const workflow = await this.getWorkflowByIdsUseCase.execute({
identifierOrInternalId: command.identifierOrInternalId,
user: command.user,
environmentId: command.user.environmentId,
organizationId: command.user.organizationId,
userId: command.user._id,
});

if (!workflow) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ import { Injectable } from '@nestjs/common';

import { NotificationTemplateEntity } from '@novu/dal';
import { WorkflowResponseDto } from '@novu/shared';
import { GetPreferences, GetPreferencesCommand } from '@novu/application-generic';
import {
GetPreferences,
GetPreferencesCommand,
GetWorkflowByIdsCommand,
GetWorkflowByIdsUseCase,
} from '@novu/application-generic';

import { GetWorkflowCommand } from './get-workflow.command';
import { toResponseWorkflowDto } from '../../mappers/notification-template-mapper';
import { GetWorkflowByIdsUseCase } from '../get-workflow-by-ids/get-workflow-by-ids.usecase';
import { GetWorkflowByIdsCommand } from '../get-workflow-by-ids/get-workflow-by-ids.command';

@Injectable()
export class GetWorkflowUseCase {
Expand All @@ -15,9 +19,11 @@ export class GetWorkflowUseCase {
private getPreferencesUseCase: GetPreferences
) {}
async execute(command: GetWorkflowCommand): Promise<WorkflowResponseDto> {
const workflowEntity: NotificationTemplateEntity | null = await this.getWorkflowByIdsUseCase.execute(
const workflowEntity: NotificationTemplateEntity = await this.getWorkflowByIdsUseCase.execute(
GetWorkflowByIdsCommand.create({
...command,
environmentId: command.user.environmentId,
organizationId: command.user.organizationId,
userId: command.user._id,
identifierOrInternalId: command.identifierOrInternalId,
})
);
Expand Down
Loading
Loading