fix(plugin-workflow): fix update workflow current property (#521)
This commit is contained in:
parent
151c3a32b8
commit
2b3f3bd5c3
@ -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': {
|
||||
|
@ -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
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
});
|
||||
});
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user