fix(plugin-workflow-action-trigger): fix appends loading (#3659)

* fix(plugin-workflow-action-trigger): fix appends loading

* fix(plugin-workflow-action-trigger): fix test case
This commit is contained in:
Junyi 2024-03-08 21:06:10 +08:00 committed by GitHub
parent 4b1a33912e
commit 677eb152e0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 98 additions and 14 deletions

View File

@ -1,9 +1,14 @@
import { get } from 'lodash';
import { BelongsTo, HasOne } from 'sequelize';
import { Model, modelAssociationByKey } from '@nocobase/database';
import { DefaultContext } from '@nocobase/server';
import { ActionContext } from '@nocobase/resourcer';
import { Next } from '@nocobase/actions';
import WorkflowPlugin, { Trigger, WorkflowModel, toJSON } from '@nocobase/plugin-workflow';
interface Context extends ActionContext, DefaultContext {}
export default class extends Trigger {
constructor(workflow: WorkflowPlugin) {
super(workflow);
@ -11,7 +16,7 @@ export default class extends Trigger {
workflow.app.resourcer.use(this.middleware);
}
async triggerAction(context, next) {
async triggerAction(context: Context, next: Next) {
const { triggerWorkflows } = context.action.params;
if (!triggerWorkflows) {
@ -24,7 +29,7 @@ export default class extends Trigger {
this.trigger(context);
}
middleware = async (context, next) => {
middleware = async (context: Context, next: Next) => {
const {
resourceName,
actionName,
@ -48,7 +53,7 @@ export default class extends Trigger {
return this.trigger(context);
};
private async trigger(context) {
private async trigger(context: Context) {
const { triggerWorkflows = '', values } = context.action.params;
const { currentUser, currentRole } = context.state;
@ -59,17 +64,20 @@ export default class extends Trigger {
const triggers = triggerWorkflows.split(',').map((trigger) => trigger.split('!'));
const workflowRepo = this.workflow.db.getRepository('workflows');
const workflows = await workflowRepo.find({
filter: {
key: triggers.map((trigger) => trigger[0]),
current: true,
type: 'action',
enabled: true,
},
});
const workflows = (
await workflowRepo.find({
filter: {
key: triggers.map((trigger) => trigger[0]),
current: true,
type: 'action',
enabled: true,
},
})
).filter((workflow) => Boolean(workflow.config.collection));
const syncGroup = [];
const asyncGroup = [];
for (const workflow of workflows) {
const { collection, appends = [] } = workflow.config;
const trigger = triggers.find((trigger) => trigger[0] == workflow.key);
const event = [workflow];
if (context.action.resourceName !== 'workflows') {
@ -90,7 +98,6 @@ export default class extends Trigger {
}
}
}
const { collection, appends = [] } = workflow.config;
const model = payload.constructor;
if (payload instanceof Model) {
if (collection !== model.collection.name) {
@ -107,7 +114,15 @@ export default class extends Trigger {
event.push({ data: toJSON(payload), ...userInfo });
}
} else {
const data = trigger[1] ? get(values, trigger[1]) : values;
const { model, repository } = context.db.getCollection(collection);
let data = trigger[1] ? get(values, trigger[1]) : values;
const pk = get(data, model.primaryKeyAttribute);
if (appends.length && pk != null) {
data = await repository.findOne({
filterByTk: pk,
appends,
});
}
// this.workflow.trigger(workflow, {
// data,
// ...userInfo,

View File

@ -214,6 +214,7 @@ describe('workflow > action-trigger', () => {
type: 'action',
config: {
collection: 'posts',
appends: ['createdBy'],
},
});
@ -238,6 +239,7 @@ describe('workflow > action-trigger', () => {
const [e2] = await workflow.getExecutions();
expect(e2.status).toBe(EXECUTION_STATUS.RESOLVED);
expect(e2.context.data).toHaveProperty('title', 't2');
expect(e2.context.data).toHaveProperty('createdBy');
});
});
@ -281,7 +283,7 @@ describe('workflow > action-trigger', () => {
});
describe('directly trigger', () => {
it('trigger data', async () => {
it('no collection configured should not be triggered', async () => {
const workflow = await WorkflowModel.create({
enabled: true,
type: 'action',
@ -295,20 +297,78 @@ describe('workflow > action-trigger', () => {
await sleep(500);
const e1s = await workflow.getExecutions();
expect(e1s.length).toBe(0);
});
it('trigger on form data', async () => {
const workflow = await WorkflowModel.create({
enabled: true,
type: 'action',
config: {
collection: 'posts',
appends: ['createdBy'],
},
});
const res1 = await userAgents[0].resource('workflows').trigger({
values: { title: 't1' },
triggerWorkflows: `${workflow.key}`,
});
expect(res1.status).toBe(202);
await sleep(500);
const [e1] = await workflow.getExecutions();
expect(e1.status).toBe(EXECUTION_STATUS.RESOLVED);
expect(e1.context.data).toMatchObject({ title: 't1' });
expect(e1.context.data.createdBy).toBeUndefined();
});
it('trigger on record data', async () => {
const workflow = await WorkflowModel.create({
enabled: true,
type: 'action',
config: {
collection: 'posts',
appends: ['createdBy'],
},
});
const post = await PostRepo.create({
values: { title: 't1', createdBy: users[0].id },
});
const res1 = await userAgents[0].resource('workflows').trigger({
values: post.toJSON(),
triggerWorkflows: `${workflow.key}`,
});
expect(res1.status).toBe(202);
await sleep(500);
const [e1] = await workflow.getExecutions();
expect(e1.status).toBe(EXECUTION_STATUS.RESOLVED);
expect(e1.context.data).toMatchObject({ title: 't1' });
expect(e1.context.data).toHaveProperty('createdBy');
expect(e1.context.data.createdBy.id).toBe(users[0].id);
});
it('multi trigger', async () => {
const w1 = await WorkflowModel.create({
enabled: true,
type: 'action',
config: {
collection: 'posts',
},
});
const w2 = await WorkflowModel.create({
enabled: true,
type: 'action',
config: {
collection: 'posts',
},
});
const res1 = await userAgents[0].resource('workflows').trigger({
@ -358,6 +418,9 @@ describe('workflow > action-trigger', () => {
const workflow = await WorkflowModel.create({
enabled: true,
type: 'action',
config: {
collection: 'posts',
},
});
const res1 = await userAgents[0].resource('workflows').trigger({
@ -377,6 +440,9 @@ describe('workflow > action-trigger', () => {
const workflow = await WorkflowModel.create({
enabled: true,
type: 'action',
config: {
collection: 'posts',
},
});
const res1 = await userAgents[0].resource('workflows').trigger({
@ -398,6 +464,9 @@ describe('workflow > action-trigger', () => {
const w1 = await WorkflowModel.create({
enabled: true,
type: 'action',
config: {
collection: 'posts',
},
});
const res1 = await userAgents[0].resource('workflows').trigger({