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

chore(api): Remove usused workflow-v2 usecases #7293

Draft
wants to merge 2 commits into
base: next
Choose a base branch
from
Draft
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

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EnvironmentWithUserObjectCommand } from '@novu/application-generic';
import { IsArray, IsBoolean, IsNotEmpty, IsOptional, IsString } from 'class-validator';
import { IsBoolean, IsNotEmpty, IsOptional, IsString } from 'class-validator';

export class PatchWorkflowCommand extends EnvironmentWithUserObjectCommand {
@IsString()
Expand All @@ -9,16 +9,4 @@ export class PatchWorkflowCommand extends EnvironmentWithUserObjectCommand {
@IsBoolean()
@IsOptional()
active?: boolean;

@IsString()
@IsOptional()
name?: string;

@IsString()
@IsOptional()
description?: string;

@IsArray()
@IsOptional()
tags?: string[];
}
Original file line number Diff line number Diff line change
@@ -1,78 +1,51 @@
import { Injectable } from '@nestjs/common';
import { UserSessionData, WorkflowResponseDto } from '@novu/shared';
import { NotificationTemplateEntity, NotificationTemplateRepository } from '@novu/dal';
import { GetWorkflowByIdsUseCase, WorkflowInternalResponseDto } from '@novu/application-generic';
import { PostProcessWorkflowUpdate } from '../post-process-workflow-update';
import { Injectable, NotFoundException } from '@nestjs/common';
import { WorkflowResponseDto } from '@novu/shared';
import { NotificationTemplateRepository } from '@novu/dal';
import { PatchWorkflowCommand } from './patch-workflow.command';
import { GetWorkflowUseCase } from '../get-workflow';

@Injectable()
export class PatchWorkflowUsecase {
constructor(
private getWorkflowByIdsUseCase: GetWorkflowByIdsUseCase,
private notificationTemplateRepository: NotificationTemplateRepository,
private postProcessWorkflowUpdate: PostProcessWorkflowUpdate,
private getWorkflowUseCase: GetWorkflowUseCase
) {}

// NOTICE: The TODOS in this usecase explore a different way to fetch and update a resource, leveraging model classes
async execute(command: PatchWorkflowCommand): Promise<WorkflowResponseDto> {
const persistedWorkflow = await this.fetchWorkflow(command);
let transientWorkflow = this.patchWorkflowFields(persistedWorkflow, command);

transientWorkflow = await this.postProcessWorkflowUpdate.execute({
workflow: transientWorkflow,
user: command.user,
// TODO: Return a Mongoose model
const workflow = await this.notificationTemplateRepository.findOne({
_id: command.workflowIdOrInternalId,
_environmentId: command.user.environmentId,
});
await this.persistWorkflow(transientWorkflow, command.user);

return await this.getWorkflowUseCase.execute({
workflowIdOrInternalId: command.workflowIdOrInternalId,
user: command.user,
});
}

private patchWorkflowFields(
persistedWorkflow: WorkflowInternalResponseDto,
command: PatchWorkflowCommand
): WorkflowInternalResponseDto {
const transientWorkflow = { ...persistedWorkflow };
if (command.active !== undefined && command.active !== null) {
transientWorkflow.active = command.active;
}

if (command.name !== undefined && command.name !== null) {
transientWorkflow.name = command.name;
}

if (command.description !== undefined && command.description !== null) {
transientWorkflow.description = command.description;
if (!workflow) {
throw new NotFoundException({
message: 'Workflow cannot be found',
workflowId: command.workflowIdOrInternalId,
});
}

if (command.tags !== undefined && command.tags !== null) {
transientWorkflow.tags = command.tags;
if (typeof command.active === 'boolean') {
workflow.active = command.active;
}

return transientWorkflow;
}

private async persistWorkflow(workflowWithIssues: NotificationTemplateEntity, userSessionData: UserSessionData) {
// TODO: Update the workflow using the Mongoose model.save()
await this.notificationTemplateRepository.update(
{
_id: workflowWithIssues._id,
_environmentId: userSessionData.environmentId,
_id: workflow._id,
_environmentId: command.user.environmentId,
},
{
...workflowWithIssues,
}
workflow
);
}

private async fetchWorkflow(command: PatchWorkflowCommand): Promise<WorkflowInternalResponseDto> {
return await this.getWorkflowByIdsUseCase.execute({
/*
* TODO: Convert to a serializer that also enriches the workflow with necessary data from other collections
* such as preferences, message templates, etc...
*/
return await this.getWorkflowUseCase.execute({
workflowIdOrInternalId: command.workflowIdOrInternalId,
environmentId: command.user.environmentId,
organizationId: command.user.organizationId,
userId: command.user._id,
user: command.user,
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import {
import { BadRequestException, Injectable } from '@nestjs/common';
import { UpsertWorkflowCommand } from './upsert-workflow.command';
import { stepTypeToControlSchema } from '../../shared';
import { PatchStepUsecase } from '../patch-step-data';
import { PostProcessWorkflowUpdate } from '../post-process-workflow-update';
import { GetWorkflowCommand, GetWorkflowUseCase } from '../get-workflow';
import { UpsertWorkflowDataCommand } from './upsert-workflow-data.command';
Expand All @@ -42,29 +41,25 @@ export class UpsertWorkflowUseCase {
private notificationGroupRepository: NotificationGroupRepository,
private workflowUpdatePostProcess: PostProcessWorkflowUpdate,
private getWorkflowByIdsUseCase: GetWorkflowByIdsUseCase,
private getWorkflowUseCase: GetWorkflowUseCase,
private patchStepDataUsecase: PatchStepUsecase
private getWorkflowUseCase: GetWorkflowUseCase
) {}

@InstrumentUsecase()
async execute(command: UpsertWorkflowCommand): Promise<WorkflowResponseDto> {
const workflowForUpdate = await this.queryWorkflow(command);
let persistedWorkflow = await this.createOrUpdateWorkflow(workflowForUpdate, command);
persistedWorkflow = await this.upsertControlValues(persistedWorkflow, command);
const persistedWorkflow = await this.createOrUpdateWorkflow(workflowForUpdate, command);
const validatedWorkflowWithIssues = await this.workflowUpdatePostProcess.execute({
user: command.user,
workflow: persistedWorkflow,
});
await this.persistWorkflow(validatedWorkflowWithIssues);

const workflow = await this.getWorkflowUseCase.execute(
return await this.getWorkflowUseCase.execute(
GetWorkflowCommand.create({
workflowIdOrInternalId: validatedWorkflowWithIssues._id,
user: command.user,
})
);

return workflow;
}

@Instrument()
Expand Down Expand Up @@ -287,44 +282,6 @@ export class UpsertWorkflowUseCase {
)
)?._id;
}

/**
* @deprecated This method will be removed in future versions.
* Please use `the patch step data instead, do not add here anything` instead.
*/
@Instrument()
private async upsertControlValues(
workflow: NotificationTemplateEntity,
command: UpsertWorkflowCommand
): Promise<WorkflowInternalResponseDto> {
for (const step of workflow.steps) {
const controlValues = this.findControlValueInRequest(step, command.workflowDto.steps);
if (!controlValues) {
continue;
}
await this.patchStepDataUsecase.execute({
controlValues,
workflowIdOrInternalId: workflow._id,
stepIdOrInternalId: step._templateId,
user: command.user,
});
}

return await this.getWorkflow(workflow._id, command);
}

private findControlValueInRequest(
step: NotificationStepEntity,
steps: (StepCreateDto | StepUpdateDto)[] | StepCreateDto[]
): Record<string, unknown> | undefined {
return steps.find((stepRequest) => {
if (this.isStepUpdateDto(stepRequest)) {
return stepRequest._id === step._templateId;
}

return stepRequest.name === step.name;
})?.controlValues;
}
}

function isWorkflowUpdateDto(workflowDto: UpsertWorkflowDataCommand, id?: string): workflowDto is UpdateWorkflowDto {
Expand Down
Loading
Loading