-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prevent workflow version from bad update (#6848)
Closes #6840 - Add query-hooks folder in common. Will be followed by hooks for workflows and runs - When updating a version, ensure the status is draft and that the status is not manually updated
- Loading branch information
Showing
6 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
packages/twenty-server/src/modules/workflow/common/query-hooks/workflow-query-hook.module.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,14 @@ | ||
import { Module } from '@nestjs/common'; | ||
|
||
import { WorkflowVersionUpdateOnePreQueryHook } from 'src/modules/workflow/common/query-hooks/workflow-version/workflow-version-update-one.pre-query-hook'; | ||
import { WorkflowVersionValidationWorkspaceService } from 'src/modules/workflow/common/query-hooks/workflow-version/workflow-version-validation.workspace-service'; | ||
import { WorkflowCommonWorkspaceService } from 'src/modules/workflow/common/workflow-common.workspace-service'; | ||
|
||
@Module({ | ||
providers: [ | ||
WorkflowVersionUpdateOnePreQueryHook, | ||
WorkflowVersionValidationWorkspaceService, | ||
WorkflowCommonWorkspaceService, | ||
], | ||
}) | ||
export class WorkflowQueryHookModule {} |
28 changes: 28 additions & 0 deletions
28
...orkflow/common/query-hooks/workflow-version/workflow-version-update-one.pre-query-hook.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,28 @@ | ||
import { WorkspaceQueryHookInstance } from 'src/engine/api/graphql/workspace-query-runner/workspace-query-hook/interfaces/workspace-query-hook.interface'; | ||
import { UpdateOneResolverArgs } from 'src/engine/api/graphql/workspace-resolver-builder/interfaces/workspace-resolvers-builder.interface'; | ||
|
||
import { WorkspaceQueryHook } from 'src/engine/api/graphql/workspace-query-runner/workspace-query-hook/decorators/workspace-query-hook.decorator'; | ||
import { AuthContext } from 'src/engine/core-modules/auth/types/auth-context.type'; | ||
import { WorkflowVersionValidationWorkspaceService } from 'src/modules/workflow/common/query-hooks/workflow-version/workflow-version-validation.workspace-service'; | ||
import { WorkflowVersionWorkspaceEntity } from 'src/modules/workflow/common/standard-objects/workflow-version.workspace-entity'; | ||
|
||
@WorkspaceQueryHook(`workflowVersion.updateOne`) | ||
export class WorkflowVersionUpdateOnePreQueryHook | ||
implements WorkspaceQueryHookInstance | ||
{ | ||
constructor( | ||
private readonly workflowVersionValidationWorkspaceService: WorkflowVersionValidationWorkspaceService, | ||
) {} | ||
|
||
async execute( | ||
_authContext: AuthContext, | ||
_objectName: string, | ||
payload: UpdateOneResolverArgs<WorkflowVersionWorkspaceEntity>, | ||
): Promise<UpdateOneResolverArgs<WorkflowVersionWorkspaceEntity>> { | ||
await this.workflowVersionValidationWorkspaceService.validateWorkflowVersionForUpdateOne( | ||
payload, | ||
); | ||
|
||
return payload; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...les/workflow/common/query-hooks/workflow-version/workflow-version-validation.exception.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,11 @@ | ||
import { CustomException } from 'src/utils/custom-exception'; | ||
|
||
export class WorkflowVersionValidationException extends CustomException { | ||
constructor(message: string, code: WorkflowVersionValidationExceptionCode) { | ||
super(message, code); | ||
} | ||
} | ||
|
||
export enum WorkflowVersionValidationExceptionCode { | ||
FORBIDDEN = 'FORBIDDEN', | ||
} |
43 changes: 43 additions & 0 deletions
43
...flow/common/query-hooks/workflow-version/workflow-version-validation.workspace-service.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,43 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
|
||
import { UpdateOneResolverArgs } from 'src/engine/api/graphql/workspace-resolver-builder/interfaces/workspace-resolvers-builder.interface'; | ||
|
||
import { | ||
WorkflowVersionValidationException, | ||
WorkflowVersionValidationExceptionCode, | ||
} from 'src/modules/workflow/common/query-hooks/workflow-version/workflow-version-validation.exception'; | ||
import { | ||
WorkflowVersionStatus, | ||
WorkflowVersionWorkspaceEntity, | ||
} from 'src/modules/workflow/common/standard-objects/workflow-version.workspace-entity'; | ||
import { WorkflowCommonWorkspaceService } from 'src/modules/workflow/common/workflow-common.workspace-service'; | ||
|
||
@Injectable() | ||
export class WorkflowVersionValidationWorkspaceService { | ||
constructor( | ||
private readonly workflowCommonWorkspaceService: WorkflowCommonWorkspaceService, | ||
) {} | ||
|
||
async validateWorkflowVersionForUpdateOne( | ||
payload: UpdateOneResolverArgs<WorkflowVersionWorkspaceEntity>, | ||
) { | ||
const workflowVersion = | ||
await this.workflowCommonWorkspaceService.getWorkflowVersionOrFail( | ||
payload.id, | ||
); | ||
|
||
if (workflowVersion.status !== WorkflowVersionStatus.DRAFT) { | ||
throw new WorkflowVersionValidationException( | ||
'Only draft workflow versions can be updated', | ||
WorkflowVersionValidationExceptionCode.FORBIDDEN, | ||
); | ||
} | ||
|
||
if (payload.data.status !== workflowVersion.status) { | ||
throw new WorkflowVersionValidationException( | ||
'Cannot update workflow version status manually', | ||
WorkflowVersionValidationExceptionCode.FORBIDDEN, | ||
); | ||
} | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
packages/twenty-server/src/modules/workflow/common/workflow-common.module.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