feat(plugin-workflow): add user variable to form trigger context (#2477)

This commit is contained in:
Junyi 2023-08-17 20:07:57 +07:00 committed by GitHub
parent d58d0d697c
commit 2e74a31aa9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 66 additions and 3 deletions

View File

@ -65,12 +65,21 @@ export default {
title: lang('Trigger data'),
},
},
{
collectionName: 'users',
name: 'user',
type: 'hasOne',
target: 'users',
uiSchema: {
title: lang('User submitted form'),
},
},
];
const result = getCollectionFieldOptions({
// depth,
...options,
fields: rootFields,
appends: ['data', ...(config.appends?.map((item) => `data.${item}`) || [])],
appends: ['data', 'user', ...(config.appends?.map((item) => `data.${item}`) || [])],
compile,
getCollectionFields,
});

View File

@ -33,6 +33,7 @@ export default {
'Form data model': '表单数据模型',
'Use a collection to match form data.': '使用一个数据表来匹配表单数据。',
'Associations to use': '待使用的关系数据',
'User submitted form': '提交表单的用户',
'Bind workflows': '绑定工作流',
'Workflow will be triggered after submitting succeeded.': '提交成功后触发工作流。',
'Workflow will be triggered after saving succeeded.': '保存成功后触发工作流。',

View File

@ -180,6 +180,29 @@ describe('workflow > triggers > form', () => {
expect(e1[0].status).toBe(EXECUTION_STATUS.RESOLVED);
expect(e1[0].context.data).toHaveProperty('createdBy');
});
it('user submitted form', async () => {
const workflow = await WorkflowModel.create({
enabled: true,
type: 'form',
config: {
collection: 'posts',
},
});
const res1 = await userAgents[0].resource('posts').create({
values: { title: 't1' },
triggerWorkflows: `${workflow.key}`,
});
expect(res1.status).toBe(200);
await sleep(500);
const [e1] = await workflow.getExecutions();
expect(e1.status).toBe(EXECUTION_STATUS.RESOLVED);
expect(e1.context.user).toBeDefined();
expect(e1.context.user.id).toBe(users[0].id);
});
});
describe('update', () => {
@ -263,6 +286,30 @@ describe('workflow > triggers > form', () => {
expect(e2.status).toBe(EXECUTION_STATUS.RESOLVED);
expect(e2.context.data).toMatchObject({ title: 't1' });
});
it('user submitted form', async () => {
const workflow = await WorkflowModel.create({
enabled: true,
type: 'form',
config: {
collection: 'posts',
appends: ['createdBy'],
},
});
const res1 = await userAgents[0].resource('posts').create({
values: { title: 't1' },
triggerWorkflows: `${workflow.key}`,
});
expect(res1.status).toBe(200);
await sleep(500);
const [e1] = await workflow.getExecutions();
expect(e1.status).toBe(EXECUTION_STATUS.RESOLVED);
expect(e1.context.user).toBeDefined();
expect(e1.context.user.id).toBe(users[0].id);
});
});
describe('context data path', () => {

View File

@ -48,6 +48,8 @@ export default class FormTrigger extends Trigger {
return;
}
const { currentUser } = context.state;
const triggers = triggerWorkflows.split(',').map((trigger) => trigger.split('!'));
const workflowRepo = this.plugin.db.getRepository('workflows');
const workflows = await workflowRepo.find({
@ -86,10 +88,14 @@ export default class FormTrigger extends Trigger {
appends,
});
}
this.plugin.trigger(workflow, { data: toJSON(payload) });
this.plugin.trigger(workflow, { data: payload, user: currentUser });
});
} else {
this.plugin.trigger(workflow, { data: trigger[1] ? get(values, trigger[1]) : values });
const data = trigger[1] ? get(values, trigger[1]) : values;
this.plugin.trigger(workflow, {
data,
user: currentUser,
});
}
});
}