fix(plugin-workflow): fix update workflow current property (#521)

This commit is contained in:
Junyi 2022-06-21 21:43:30 +08:00 committed by GitHub
parent 151c3a32b8
commit 2b3f3bd5c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 23 deletions

View File

@ -49,7 +49,8 @@ export default {
{ value: 2, label: '{{t("After record updated")}}' }, { value: 2, label: '{{t("After record updated")}}' },
{ value: 3, label: '{{t("After record added or updated")}}' }, { value: 3, label: '{{t("After record added or updated")}}' },
{ value: 4, label: '{{t("After record deleted")}}' } { value: 4, label: '{{t("After record deleted")}}' }
] ],
placeholder: '{{t("Trigger on")}}'
}, },
required: true required: true
}, },
@ -62,6 +63,7 @@ export default {
'x-component': 'FieldsSelect', 'x-component': 'FieldsSelect',
'x-component-props': { 'x-component-props': {
mode: 'multiple', mode: 'multiple',
placeholder: '{{t("Select Field")}}'
} }
}, },
'config.condition': { 'config.condition': {

View File

@ -17,16 +17,13 @@ import { EXECUTION_STATUS } from './constants';
async function setCurrent(instance: WorkflowModel, options) { async function setCurrent(instance: WorkflowModel, options) {
const updates: { enabled?: boolean, current?: boolean } = {}; const updates: { enabled?: boolean, current?: boolean } = {};
if (!instance.changed('enabled')) { if (!instance.changed('enabled') || !instance.enabled) {
return; return;
} }
if (instance.enabled) {
instance.set('current', true); instance.set('current', true);
updates.enabled = false; updates.enabled = false;
}
if (instance.current) {
// NOTE: set to `null` but not `false` will not violate the unique index // NOTE: set to `null` but not `false` will not violate the unique index
updates.current = null; updates.current = null;
const previous = await (<typeof WorkflowModel>instance.constructor).findOne({ const previous = await (<typeof WorkflowModel>instance.constructor).findOne({
@ -41,7 +38,6 @@ async function setCurrent(instance: WorkflowModel, options) {
transaction: options.transaction transaction: options.transaction
}); });
} }
}
} }
export default class WorkflowPlugin extends Plugin { export default class WorkflowPlugin extends Plugin {

View File

@ -33,12 +33,31 @@ describe('workflow > workflow', () => {
collection: 'posts' collection: 'posts'
} }
}); });
expect(workflow.current).toBe(true);
const p1 = await PostRepo.create({ values: { title: 't1' } });
const executions1 = await workflow.getExecutions();
expect(executions1.length).toBe(1);
const w = await WorkflowModel.findOne({
where: {
current: true
}
});
expect(w).not.toBeNull();
expect(w.current).toBe(true);
await workflow.update({ enabled: false }); await workflow.update({ enabled: false });
const post = await PostRepo.create({ values: { title: 't1' } }); const p2 = await PostRepo.create({ values: { title: 't2' } });
const executions = await workflow.getExecutions(); const executions2 = await workflow.getExecutions();
expect(executions.length).toBe(0); expect(executions2.length).toBe(1);
const workflows = await WorkflowModel.findAll({
where: {
current: true
}
});
expect(workflows.length).toBe(1);
}); });
}); });