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:
parent
4b1a33912e
commit
677eb152e0
@ -1,9 +1,14 @@
|
|||||||
import { get } from 'lodash';
|
import { get } from 'lodash';
|
||||||
import { BelongsTo, HasOne } from 'sequelize';
|
import { BelongsTo, HasOne } from 'sequelize';
|
||||||
import { Model, modelAssociationByKey } from '@nocobase/database';
|
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';
|
import WorkflowPlugin, { Trigger, WorkflowModel, toJSON } from '@nocobase/plugin-workflow';
|
||||||
|
|
||||||
|
interface Context extends ActionContext, DefaultContext {}
|
||||||
|
|
||||||
export default class extends Trigger {
|
export default class extends Trigger {
|
||||||
constructor(workflow: WorkflowPlugin) {
|
constructor(workflow: WorkflowPlugin) {
|
||||||
super(workflow);
|
super(workflow);
|
||||||
@ -11,7 +16,7 @@ export default class extends Trigger {
|
|||||||
workflow.app.resourcer.use(this.middleware);
|
workflow.app.resourcer.use(this.middleware);
|
||||||
}
|
}
|
||||||
|
|
||||||
async triggerAction(context, next) {
|
async triggerAction(context: Context, next: Next) {
|
||||||
const { triggerWorkflows } = context.action.params;
|
const { triggerWorkflows } = context.action.params;
|
||||||
|
|
||||||
if (!triggerWorkflows) {
|
if (!triggerWorkflows) {
|
||||||
@ -24,7 +29,7 @@ export default class extends Trigger {
|
|||||||
this.trigger(context);
|
this.trigger(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
middleware = async (context, next) => {
|
middleware = async (context: Context, next: Next) => {
|
||||||
const {
|
const {
|
||||||
resourceName,
|
resourceName,
|
||||||
actionName,
|
actionName,
|
||||||
@ -48,7 +53,7 @@ export default class extends Trigger {
|
|||||||
return this.trigger(context);
|
return this.trigger(context);
|
||||||
};
|
};
|
||||||
|
|
||||||
private async trigger(context) {
|
private async trigger(context: Context) {
|
||||||
const { triggerWorkflows = '', values } = context.action.params;
|
const { triggerWorkflows = '', values } = context.action.params;
|
||||||
|
|
||||||
const { currentUser, currentRole } = context.state;
|
const { currentUser, currentRole } = context.state;
|
||||||
@ -59,17 +64,20 @@ export default class extends Trigger {
|
|||||||
|
|
||||||
const triggers = triggerWorkflows.split(',').map((trigger) => trigger.split('!'));
|
const triggers = triggerWorkflows.split(',').map((trigger) => trigger.split('!'));
|
||||||
const workflowRepo = this.workflow.db.getRepository('workflows');
|
const workflowRepo = this.workflow.db.getRepository('workflows');
|
||||||
const workflows = await workflowRepo.find({
|
const workflows = (
|
||||||
|
await workflowRepo.find({
|
||||||
filter: {
|
filter: {
|
||||||
key: triggers.map((trigger) => trigger[0]),
|
key: triggers.map((trigger) => trigger[0]),
|
||||||
current: true,
|
current: true,
|
||||||
type: 'action',
|
type: 'action',
|
||||||
enabled: true,
|
enabled: true,
|
||||||
},
|
},
|
||||||
});
|
})
|
||||||
|
).filter((workflow) => Boolean(workflow.config.collection));
|
||||||
const syncGroup = [];
|
const syncGroup = [];
|
||||||
const asyncGroup = [];
|
const asyncGroup = [];
|
||||||
for (const workflow of workflows) {
|
for (const workflow of workflows) {
|
||||||
|
const { collection, appends = [] } = workflow.config;
|
||||||
const trigger = triggers.find((trigger) => trigger[0] == workflow.key);
|
const trigger = triggers.find((trigger) => trigger[0] == workflow.key);
|
||||||
const event = [workflow];
|
const event = [workflow];
|
||||||
if (context.action.resourceName !== 'workflows') {
|
if (context.action.resourceName !== 'workflows') {
|
||||||
@ -90,7 +98,6 @@ export default class extends Trigger {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const { collection, appends = [] } = workflow.config;
|
|
||||||
const model = payload.constructor;
|
const model = payload.constructor;
|
||||||
if (payload instanceof Model) {
|
if (payload instanceof Model) {
|
||||||
if (collection !== model.collection.name) {
|
if (collection !== model.collection.name) {
|
||||||
@ -107,7 +114,15 @@ export default class extends Trigger {
|
|||||||
event.push({ data: toJSON(payload), ...userInfo });
|
event.push({ data: toJSON(payload), ...userInfo });
|
||||||
}
|
}
|
||||||
} else {
|
} 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, {
|
// this.workflow.trigger(workflow, {
|
||||||
// data,
|
// data,
|
||||||
// ...userInfo,
|
// ...userInfo,
|
||||||
|
@ -214,6 +214,7 @@ describe('workflow > action-trigger', () => {
|
|||||||
type: 'action',
|
type: 'action',
|
||||||
config: {
|
config: {
|
||||||
collection: 'posts',
|
collection: 'posts',
|
||||||
|
appends: ['createdBy'],
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -238,6 +239,7 @@ describe('workflow > action-trigger', () => {
|
|||||||
const [e2] = await workflow.getExecutions();
|
const [e2] = await workflow.getExecutions();
|
||||||
expect(e2.status).toBe(EXECUTION_STATUS.RESOLVED);
|
expect(e2.status).toBe(EXECUTION_STATUS.RESOLVED);
|
||||||
expect(e2.context.data).toHaveProperty('title', 't2');
|
expect(e2.context.data).toHaveProperty('title', 't2');
|
||||||
|
expect(e2.context.data).toHaveProperty('createdBy');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -281,7 +283,7 @@ describe('workflow > action-trigger', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('directly trigger', () => {
|
describe('directly trigger', () => {
|
||||||
it('trigger data', async () => {
|
it('no collection configured should not be triggered', async () => {
|
||||||
const workflow = await WorkflowModel.create({
|
const workflow = await WorkflowModel.create({
|
||||||
enabled: true,
|
enabled: true,
|
||||||
type: 'action',
|
type: 'action',
|
||||||
@ -295,20 +297,78 @@ describe('workflow > action-trigger', () => {
|
|||||||
|
|
||||||
await sleep(500);
|
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();
|
const [e1] = await workflow.getExecutions();
|
||||||
expect(e1.status).toBe(EXECUTION_STATUS.RESOLVED);
|
expect(e1.status).toBe(EXECUTION_STATUS.RESOLVED);
|
||||||
expect(e1.context.data).toMatchObject({ title: 't1' });
|
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 () => {
|
it('multi trigger', async () => {
|
||||||
const w1 = await WorkflowModel.create({
|
const w1 = await WorkflowModel.create({
|
||||||
enabled: true,
|
enabled: true,
|
||||||
type: 'action',
|
type: 'action',
|
||||||
|
config: {
|
||||||
|
collection: 'posts',
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const w2 = await WorkflowModel.create({
|
const w2 = await WorkflowModel.create({
|
||||||
enabled: true,
|
enabled: true,
|
||||||
type: 'action',
|
type: 'action',
|
||||||
|
config: {
|
||||||
|
collection: 'posts',
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const res1 = await userAgents[0].resource('workflows').trigger({
|
const res1 = await userAgents[0].resource('workflows').trigger({
|
||||||
@ -358,6 +418,9 @@ describe('workflow > action-trigger', () => {
|
|||||||
const workflow = await WorkflowModel.create({
|
const workflow = await WorkflowModel.create({
|
||||||
enabled: true,
|
enabled: true,
|
||||||
type: 'action',
|
type: 'action',
|
||||||
|
config: {
|
||||||
|
collection: 'posts',
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const res1 = await userAgents[0].resource('workflows').trigger({
|
const res1 = await userAgents[0].resource('workflows').trigger({
|
||||||
@ -377,6 +440,9 @@ describe('workflow > action-trigger', () => {
|
|||||||
const workflow = await WorkflowModel.create({
|
const workflow = await WorkflowModel.create({
|
||||||
enabled: true,
|
enabled: true,
|
||||||
type: 'action',
|
type: 'action',
|
||||||
|
config: {
|
||||||
|
collection: 'posts',
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const res1 = await userAgents[0].resource('workflows').trigger({
|
const res1 = await userAgents[0].resource('workflows').trigger({
|
||||||
@ -398,6 +464,9 @@ describe('workflow > action-trigger', () => {
|
|||||||
const w1 = await WorkflowModel.create({
|
const w1 = await WorkflowModel.create({
|
||||||
enabled: true,
|
enabled: true,
|
||||||
type: 'action',
|
type: 'action',
|
||||||
|
config: {
|
||||||
|
collection: 'posts',
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const res1 = await userAgents[0].resource('workflows').trigger({
|
const res1 = await userAgents[0].resource('workflows').trigger({
|
||||||
|
Loading…
Reference in New Issue
Block a user