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

View File

@ -17,30 +17,26 @@ import { EXECUTION_STATUS } from './constants';
async function setCurrent(instance: WorkflowModel, options) {
const updates: { enabled?: boolean, current?: boolean } = {};
if (!instance.changed('enabled')) {
if (!instance.changed('enabled') || !instance.enabled) {
return;
}
if (instance.enabled) {
instance.set('current', true);
updates.enabled = false;
}
instance.set('current', true);
updates.enabled = false;
if (instance.current) {
// NOTE: set to `null` but not `false` will not violate the unique index
updates.current = null;
const previous = await (<typeof WorkflowModel>instance.constructor).findOne({
where: {
key: instance.key,
current: true
}
});
if (previous) {
await previous.update(updates, {
transaction: options.transaction
});
// NOTE: set to `null` but not `false` will not violate the unique index
updates.current = null;
const previous = await (<typeof WorkflowModel>instance.constructor).findOne({
where: {
key: instance.key,
current: true
}
});
if (previous) {
await previous.update(updates, {
transaction: options.transaction
});
}
}

View File

@ -33,12 +33,31 @@ describe('workflow > workflow', () => {
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 });
const post = await PostRepo.create({ values: { title: 't1' } });
const p2 = await PostRepo.create({ values: { title: 't2' } });
const executions = await workflow.getExecutions();
expect(executions.length).toBe(0);
const executions2 = await workflow.getExecutions();
expect(executions2.length).toBe(1);
const workflows = await WorkflowModel.findAll({
where: {
current: true
}
});
expect(workflows.length).toBe(1);
});
});