mirror of
https://github.com/nocobase/nocobase
synced 2024-11-15 03:56:16 +00:00
fix(plugin-workflow): fix or condition bug in collection trigger (#4925)
This commit is contained in:
parent
c07084ca22
commit
00a7a52611
@ -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', () => {
|
describe('config.appends', () => {
|
||||||
it('non-appended association could not be accessed', async () => {
|
it('non-appended association could not be accessed', async () => {
|
||||||
const workflow = await WorkflowModel.create({
|
const workflow = await WorkflowModel.create({
|
||||||
|
@ -12,6 +12,7 @@ import Trigger from '.';
|
|||||||
import { toJSON } from '../utils';
|
import { toJSON } from '../utils';
|
||||||
import type { WorkflowModel } from '../types';
|
import type { WorkflowModel } from '../types';
|
||||||
import { ICollection, parseCollectionName } from '@nocobase/data-source-manager';
|
import { ICollection, parseCollectionName } from '@nocobase/data-source-manager';
|
||||||
|
import { isValidFilter } from '@nocobase/utils';
|
||||||
|
|
||||||
export interface CollectionChangeTriggerConfig {
|
export interface CollectionChangeTriggerConfig {
|
||||||
collection: string;
|
collection: string;
|
||||||
@ -65,8 +66,9 @@ async function handler(this: CollectionTrigger, workflow: WorkflowModel, data: M
|
|||||||
) {
|
) {
|
||||||
return;
|
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
|
// TODO: change to map filter format to calculation format
|
||||||
// const calculation = toCalculation(condition);
|
// const calculation = toCalculation(condition);
|
||||||
const count = await repository.count({
|
const count = await repository.count({
|
||||||
|
Loading…
Reference in New Issue
Block a user