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

Set statuses on workflows #6792

Merged
merged 7 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ export class WorkflowTriggerResolver {
) {}

@Mutation(() => Boolean)
async enableWorkflowTrigger(
async activateWorkflowVersion(
@Args('workflowVersionId') workflowVersionId: string,
) {
return await this.workflowTriggerWorkspaceService.enableWorkflowTrigger(
return await this.workflowTriggerWorkspaceService.activateWorkflowVersion(
workflowVersionId,
);
}

@Mutation(() => Boolean)
async disableWorkflowTrigger(
async deactivateWorkflowVersion(
@Args('workflowVersionId') workflowVersionId: string,
) {
return await this.workflowTriggerWorkspaceService.disableWorkflowTrigger(
return await this.workflowTriggerWorkspaceService.deactivateWorkflowVersion(
workflowVersionId,
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ export enum WorkflowVersionStatus {
DRAFT = 'DRAFT',
ACTIVE = 'ACTIVE',
DEACTIVATED = 'DEACTIVATED',
ARCHIVED = 'ARCHIVED',
}

export const WorkflowVersionStatusOptions = [
const WorkflowVersionStatusOptions = [
{
value: WorkflowVersionStatus.DRAFT,
label: 'Draft',
Expand All @@ -47,7 +48,13 @@ export const WorkflowVersionStatusOptions = [
value: WorkflowVersionStatus.DEACTIVATED,
label: 'Deactivated',
position: 2,
color: 'gray',
color: 'red',
},
{
value: WorkflowVersionStatus.ARCHIVED,
label: 'Archived',
position: 3,
color: 'grey',
},
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,34 @@ import { STANDARD_OBJECT_IDS } from 'src/engine/workspace-manager/workspace-sync
import { FavoriteWorkspaceEntity } from 'src/modules/favorite/standard-objects/favorite.workspace-entity';
import { WorkflowEventListenerWorkspaceEntity } from 'src/modules/workflow/common/standard-objects/workflow-event-listener.workspace-entity';
import { WorkflowRunWorkspaceEntity } from 'src/modules/workflow/common/standard-objects/workflow-run.workspace-entity';
import {
WorkflowVersionStatus,
WorkflowVersionStatusOptions,
WorkflowVersionWorkspaceEntity,
} from 'src/modules/workflow/common/standard-objects/workflow-version.workspace-entity';
import { WorkflowVersionWorkspaceEntity } from 'src/modules/workflow/common/standard-objects/workflow-version.workspace-entity';

export enum WorkflowStatus {
DRAFT = 'DRAFT',
ACTIVE = 'ACTIVE',
DEACTIVATED = 'DEACTIVATED',
}

const WorkflowStatusOptions = [
{
value: WorkflowStatus.DRAFT,
label: 'Draft',
position: 0,
color: 'yellow',
},
{
value: WorkflowStatus.ACTIVE,
label: 'Active',
position: 1,
color: 'green',
},
{
value: WorkflowStatus.DEACTIVATED,
label: 'Deactivated',
position: 2,
color: 'grey',
},
];

@WorkspaceEntity({
standardId: STANDARD_OBJECT_IDS.workflow,
Expand Down Expand Up @@ -61,10 +84,10 @@ export class WorkflowWorkspaceEntity extends BaseWorkspaceEntity {
type: FieldMetadataType.MULTI_SELECT,
label: 'Statuses',
description: 'The current statuses of the workflow versions',
options: WorkflowVersionStatusOptions,
options: WorkflowStatusOptions,
})
@WorkspaceIsNullable()
statuses: WorkflowVersionStatus[] | null;
statuses: WorkflowStatus[] | null;

@WorkspaceField({
standardId: WORKFLOW_STANDARD_FIELD_IDS.position,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ export class WorkflowCommonWorkspaceService {
},
});

return this.getValidWorkflowVersionOrFail(workflowVersion);
}

async getValidWorkflowVersionOrFail(
workflowVersion: WorkflowVersionWorkspaceEntity | null,
): Promise<
Omit<WorkflowVersionWorkspaceEntity, 'trigger'> & {
trigger: WorkflowTrigger;
}
> {
if (!workflowVersion) {
throw new WorkflowTriggerException(
'Workflow version not found',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,268 @@
import { Test, TestingModule } from '@nestjs/testing';

import { TwentyORMManager } from 'src/engine/twenty-orm/twenty-orm.manager';
import { WorkflowVersionStatus } from 'src/modules/workflow/common/standard-objects/workflow-version.workspace-entity';
import { WorkflowStatus } from 'src/modules/workflow/common/standard-objects/workflow.workspace-entity';
import {
WorkflowStatusesUpdateJob,
WorkflowVersionBatchEvent,
WorkflowVersionEventType,
} from 'src/modules/workflow/workflow-status/jobs/workflow-statuses-update.job';

describe('WorkflowStatusesUpdate', () => {
let job: WorkflowStatusesUpdateJob;

const mockWorkflowRepository = {
findOneOrFail: jest.fn(),
update: jest.fn(),
};

const mockTwentyORMManager = {
getRepository: jest.fn().mockResolvedValue(mockWorkflowRepository),
};

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
WorkflowStatusesUpdateJob,
{
provide: TwentyORMManager,
useValue: mockTwentyORMManager,
},
],
}).compile();

job = await module.resolve<WorkflowStatusesUpdateJob>(
WorkflowStatusesUpdateJob,
);
});

it('should be defined', () => {
expect(job).toBeDefined();
});

describe('handle', () => {
describe('when event type is CREATE', () => {
it('when already a draft, do not change anything', async () => {
const event: WorkflowVersionBatchEvent = {
workspaceId: '1',
type: WorkflowVersionEventType.CREATE,
workflowIds: ['1'],
};

const mockWorkflow = {
statuses: [WorkflowStatus.DRAFT],
};

mockWorkflowRepository.findOneOrFail.mockResolvedValue(mockWorkflow);

await job.handle(event);

expect(mockWorkflowRepository.findOneOrFail).toHaveBeenCalledTimes(1);
expect(mockWorkflowRepository.update).toHaveBeenCalledTimes(0);
});

it('when no draft yet, update statuses', async () => {
const event: WorkflowVersionBatchEvent = {
workspaceId: '1',
type: WorkflowVersionEventType.CREATE,
workflowIds: ['1'],
};

const mockWorkflow = {
id: '1',
statuses: [WorkflowStatus.ACTIVE],
};

mockWorkflowRepository.findOneOrFail.mockResolvedValue(mockWorkflow);

await job.handle(event);

expect(mockWorkflowRepository.findOneOrFail).toHaveBeenCalledTimes(1);
expect(mockWorkflowRepository.update).toHaveBeenCalledWith(
{ id: '1' },
{ statuses: [WorkflowStatus.ACTIVE, WorkflowStatus.DRAFT] },
);
});
});

describe('when event type is STATUS_UPDATE', () => {
test('when status is the same, should not do anything', async () => {
const event: WorkflowVersionBatchEvent = {
workspaceId: '1',
type: WorkflowVersionEventType.STATUS_UPDATE,
statusUpdates: [
{
workflowId: '1',
previousStatus: WorkflowVersionStatus.ACTIVE,
newStatus: WorkflowVersionStatus.ACTIVE,
},
],
};

const mockWorkflow = {
statuses: [WorkflowStatus.ACTIVE],
};

mockWorkflowRepository.findOneOrFail.mockResolvedValue(mockWorkflow);

await job.handle(event);

expect(mockWorkflowRepository.findOneOrFail).toHaveBeenCalledTimes(1);
expect(mockWorkflowRepository.update).toHaveBeenCalledTimes(0);
});

test('when update that should be impossible, do not do anything', async () => {
const event: WorkflowVersionBatchEvent = {
workspaceId: '1',
type: WorkflowVersionEventType.STATUS_UPDATE,
statusUpdates: [
{
workflowId: '1',
previousStatus: WorkflowVersionStatus.ACTIVE,
newStatus: WorkflowVersionStatus.DRAFT,
},
],
};

const mockWorkflow = {
statuses: [WorkflowStatus.ACTIVE],
};

mockWorkflowRepository.findOneOrFail.mockResolvedValue(mockWorkflow);

await job.handle(event);

expect(mockWorkflowRepository.findOneOrFail).toHaveBeenCalledTimes(1);
expect(mockWorkflowRepository.update).toHaveBeenCalledTimes(0);
});

test('when WorkflowVersionStatus.DEACTIVATED to WorkflowVersionStatus.ACTIVE, should activate', async () => {
const event: WorkflowVersionBatchEvent = {
workspaceId: '1',
type: WorkflowVersionEventType.STATUS_UPDATE,
statusUpdates: [
{
workflowId: '1',
previousStatus: WorkflowVersionStatus.DEACTIVATED,
newStatus: WorkflowVersionStatus.ACTIVE,
},
],
};

const mockWorkflow = {
statuses: [WorkflowStatus.DEACTIVATED],
};

mockWorkflowRepository.findOneOrFail.mockResolvedValue(mockWorkflow);

await job.handle(event);

expect(mockWorkflowRepository.findOneOrFail).toHaveBeenCalledTimes(1);
expect(mockWorkflowRepository.update).toHaveBeenCalledWith(
{ id: '1' },
{ statuses: [WorkflowStatus.ACTIVE] },
);
});

test('when WorkflowVersionStatus.ACTIVE to WorkflowVersionStatus.DEACTIVATED, should deactivate', async () => {
const event: WorkflowVersionBatchEvent = {
workspaceId: '1',
type: WorkflowVersionEventType.STATUS_UPDATE,
statusUpdates: [
{
workflowId: '1',
previousStatus: WorkflowVersionStatus.ACTIVE,
newStatus: WorkflowVersionStatus.DEACTIVATED,
},
],
};

const mockWorkflow = {
statuses: [WorkflowStatus.ACTIVE],
};

mockWorkflowRepository.findOneOrFail.mockResolvedValue(mockWorkflow);

await job.handle(event);

expect(mockWorkflowRepository.findOneOrFail).toHaveBeenCalledTimes(1);
expect(mockWorkflowRepository.update).toHaveBeenCalledWith(
{ id: '1' },
{ statuses: [WorkflowStatus.DEACTIVATED] },
);
});

test('when WorkflowVersionStatus.DRAFT to WorkflowVersionStatus.ACTIVE, should activate', async () => {
const event: WorkflowVersionBatchEvent = {
workspaceId: '1',
type: WorkflowVersionEventType.STATUS_UPDATE,
statusUpdates: [
{
workflowId: '1',
previousStatus: WorkflowVersionStatus.DRAFT,
newStatus: WorkflowVersionStatus.ACTIVE,
},
],
};

const mockWorkflow = {
statuses: [WorkflowStatus.DRAFT],
};

mockWorkflowRepository.findOneOrFail.mockResolvedValue(mockWorkflow);

await job.handle(event);

expect(mockWorkflowRepository.findOneOrFail).toHaveBeenCalledTimes(1);
expect(mockWorkflowRepository.update).toHaveBeenCalledWith(
{ id: '1' },
{ statuses: [WorkflowStatus.ACTIVE] },
);
});
});

describe('when event type is DELETE', () => {
test('when status is not draft, should not do anything', async () => {
const event: WorkflowVersionBatchEvent = {
workspaceId: '1',
type: WorkflowVersionEventType.DELETE,
workflowIds: ['1'],
};

const mockWorkflow = {
statuses: [WorkflowStatus.ACTIVE],
};

mockWorkflowRepository.findOneOrFail.mockResolvedValue(mockWorkflow);

await job.handle(event);

expect(mockWorkflowRepository.findOneOrFail).toHaveBeenCalledTimes(1);
expect(mockWorkflowRepository.update).toHaveBeenCalledTimes(0);
});

test('when status is draft, should delete', async () => {
const event: WorkflowVersionBatchEvent = {
workspaceId: '1',
type: WorkflowVersionEventType.DELETE,
workflowIds: ['1'],
};

const mockWorkflow = {
statuses: [WorkflowStatus.DRAFT],
};

mockWorkflowRepository.findOneOrFail.mockResolvedValue(mockWorkflow);

await job.handle(event);

expect(mockWorkflowRepository.findOneOrFail).toHaveBeenCalledTimes(1);
expect(mockWorkflowRepository.update).toHaveBeenCalledWith(
{ id: '1' },
{ statuses: [] },
);
});
});
});
});
Loading
Loading