Skip to content

Commit

Permalink
feat(api): invalidate stale workflows (#6887)
Browse files Browse the repository at this point in the history
  • Loading branch information
djabarovgeorge authored Nov 11, 2024
1 parent 344c044 commit f5af1d4
Show file tree
Hide file tree
Showing 24 changed files with 268 additions and 237 deletions.
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
@@ -1,9 +1,10 @@
import { BadRequestException, Injectable } from '@nestjs/common';
import { ControlValuesLevelEnum, JSONSchemaDto, StepDataDto } from '@novu/shared';
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 @@ -50,7 +51,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

0 comments on commit f5af1d4

Please sign in to comment.