fix(plugin-workflow): fix or condition bug in collection trigger (#4925)

This commit is contained in:
Junyi 2024-07-22 16:47:02 +08:00 committed by GitHub
parent c07084ca22
commit 00a7a52611
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 160 additions and 2 deletions

View File

@ -280,6 +280,162 @@ describe('workflow > triggers > collection', () => {
});
});
describe('config.condition', () => {
it('empty condition', async () => {
const workflow = await WorkflowModel.create({
enabled: true,
type: 'collection',
config: {
mode: 1,
collection: 'posts',
condition: {},
},
});
const post = await PostRepo.create({ values: { title: 't1' } });
await sleep(500);
const executions = await workflow.getExecutions();
expect(executions.length).toBe(1);
expect(executions[0].context.data.title).toBe('t1');
});
it('and empty condition', async () => {
const workflow = await WorkflowModel.create({
enabled: true,
type: 'collection',
config: {
mode: 1,
collection: 'posts',
condition: {
$and: [],
},
},
});
const post = await PostRepo.create({ values: { title: 't1' } });
await sleep(500);
const executions = await workflow.getExecutions();
expect(executions.length).toBe(1);
expect(executions[0].context.data.title).toBe('t1');
});
it('and deep empty condition', async () => {
const workflow = await WorkflowModel.create({
enabled: true,
type: 'collection',
config: {
mode: 1,
collection: 'posts',
condition: {
$and: [{}],
},
},
});
const post = await PostRepo.create({ values: { title: 't1' } });
await sleep(500);
const executions = await workflow.getExecutions();
expect(executions.length).toBe(1);
expect(executions[0].context.data.title).toBe('t1');
});
it('and condition', async () => {
const workflow = await WorkflowModel.create({
enabled: true,
type: 'collection',
config: {
mode: 1,
collection: 'posts',
condition: {
$and: [{ title: 't1' }],
},
},
});
const post1 = await PostRepo.create({ values: { title: 't1' } });
const post2 = await PostRepo.create({ values: { title: 't2' } });
await sleep(500);
const executions = await workflow.getExecutions();
expect(executions.length).toBe(1);
expect(executions[0].context.data.title).toBe('t1');
});
it('or empty condition', async () => {
const workflow = await WorkflowModel.create({
enabled: true,
type: 'collection',
config: {
mode: 1,
collection: 'posts',
condition: {
$or: [],
},
},
});
const post = await PostRepo.create({ values: { title: 't1' } });
await sleep(500);
const executions = await workflow.getExecutions();
expect(executions.length).toBe(1);
expect(executions[0].context.data.title).toBe('t1');
});
it('or deep empty condition', async () => {
const workflow = await WorkflowModel.create({
enabled: true,
type: 'collection',
config: {
mode: 1,
collection: 'posts',
condition: {
$or: [{}],
},
},
});
const post = await PostRepo.create({ values: { title: 't1' } });
await sleep(500);
const executions = await workflow.getExecutions();
expect(executions.length).toBe(1);
expect(executions[0].context.data.title).toBe('t1');
});
it('or condition', async () => {
const workflow = await WorkflowModel.create({
enabled: true,
type: 'collection',
config: {
mode: 1,
collection: 'posts',
condition: {
$or: [{ title: { $notEmpty: true } }],
},
},
});
const post1 = await PostRepo.create({ values: { title: 't1' } });
const post2 = await PostRepo.create({ values: {} });
await sleep(500);
const executions = await workflow.getExecutions();
expect(executions.length).toBe(1);
expect(executions[0].context.data.title).toBe('t1');
});
});
describe('config.appends', () => {
it('non-appended association could not be accessed', async () => {
const workflow = await WorkflowModel.create({

View File

@ -12,6 +12,7 @@ import Trigger from '.';
import { toJSON } from '../utils';
import type { WorkflowModel } from '../types';
import { ICollection, parseCollectionName } from '@nocobase/data-source-manager';
import { isValidFilter } from '@nocobase/utils';
export interface CollectionChangeTriggerConfig {
collection: string;
@ -65,8 +66,9 @@ async function handler(this: CollectionTrigger, workflow: WorkflowModel, data: M
) {
return;
}
// NOTE: if no configured condition match, do not trigger
if (condition && condition.$and?.length) {
// NOTE: if no configured condition, or not match, do not trigger
if (isValidFilter(condition)) {
// TODO: change to map filter format to calculation format
// const calculation = toCalculation(condition);
const count = await repository.count({