-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(api): Add a Patch Workflow endpoint (#7019)
- Loading branch information
Showing
13 changed files
with
195 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
apps/api/src/app/workflows-v2/usecases/patch-step-data/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export * from './patch-step-data.command'; | ||
export * from './patch-step-data.usecase'; | ||
export * from './patch-step.command'; | ||
export * from './patch-step.usecase'; |
8 changes: 3 additions & 5 deletions
8
...atch-step-data/patch-step-data.command.ts → ...ses/patch-step-data/patch-step.command.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
apps/api/src/app/workflows-v2/usecases/patch-workflow/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './patch-workflow.command'; | ||
export * from './patch-workflow.usecase'; |
12 changes: 12 additions & 0 deletions
12
apps/api/src/app/workflows-v2/usecases/patch-workflow/patch-workflow.command.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { EnvironmentWithUserObjectCommand } from '@novu/application-generic'; | ||
import { IsBoolean, IsNotEmpty, IsOptional, IsString } from 'class-validator'; | ||
import { IdentifierOrInternalId } from '@novu/shared'; | ||
|
||
export class PatchWorkflowCommand extends EnvironmentWithUserObjectCommand { | ||
@IsString() | ||
@IsNotEmpty() | ||
identifierOrInternalId: IdentifierOrInternalId; | ||
@IsBoolean() | ||
@IsOptional() | ||
active?: boolean; | ||
} |
67 changes: 67 additions & 0 deletions
67
apps/api/src/app/workflows-v2/usecases/patch-workflow/patch-workflow.usecase.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { UserSessionData, WorkflowResponseDto } from '@novu/shared'; | ||
import { NotificationTemplateEntity, NotificationTemplateRepository } from '@novu/dal'; | ||
import { GetWorkflowByIdsUseCase } from '@novu/application-generic'; | ||
import { PostProcessWorkflowUpdate } from '../post-process-workflow-update'; | ||
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 | ||
) {} | ||
|
||
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, | ||
}); | ||
await this.persistWorkflow(transientWorkflow, command.user); | ||
|
||
return await this.getWorkflowUseCase.execute({ | ||
identifierOrInternalId: command.identifierOrInternalId, | ||
user: command.user, | ||
}); | ||
} | ||
|
||
private patchWorkflowFields(persistedWorkflow, command: PatchWorkflowCommand) { | ||
const transientWorkflow = { ...persistedWorkflow }; | ||
if (command.active !== undefined) { | ||
// @ts-ignore | ||
transientWorkflow.active = command.active; | ||
} | ||
|
||
return transientWorkflow; | ||
} | ||
|
||
private async persistWorkflow( | ||
workflowWithIssues: Partial<NotificationTemplateEntity>, | ||
userSessionData: UserSessionData | ||
) { | ||
await this.notificationTemplateRepository.update( | ||
{ | ||
_id: workflowWithIssues._id, | ||
_environmentId: userSessionData.environmentId, | ||
}, | ||
{ | ||
...workflowWithIssues, | ||
} | ||
); | ||
} | ||
|
||
private async fetchWorkflow(command: PatchWorkflowCommand) { | ||
return await this.getWorkflowByIdsUseCase.execute({ | ||
identifierOrInternalId: command.identifierOrInternalId, | ||
environmentId: command.user.environmentId, | ||
organizationId: command.user.organizationId, | ||
userId: command.user._id, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.