fix(plugin-workflow): use toJSON instead of get to get valid result (#1596)
This commit is contained in:
parent
726b08e87a
commit
cc50b38a12
@ -1,10 +1,12 @@
|
|||||||
import path from 'path';
|
import path from 'path';
|
||||||
|
import { get } from 'lodash';
|
||||||
|
|
||||||
import { ApplicationOptions } from '@nocobase/server';
|
import { ApplicationOptions } from '@nocobase/server';
|
||||||
import { MockServer, mockServer } from '@nocobase/test';
|
import { MockServer, mockServer } from '@nocobase/test';
|
||||||
|
|
||||||
import Plugin from '..';
|
import Plugin from '..';
|
||||||
import { JOB_STATUS } from '../constants';
|
import { JOB_STATUS } from '../constants';
|
||||||
|
import FlowNodeModel from '../models/FlowNode';
|
||||||
|
|
||||||
export function sleep(ms: number) {
|
export function sleep(ms: number) {
|
||||||
return new Promise(resolve => {
|
return new Promise(resolve => {
|
||||||
@ -23,10 +25,10 @@ export async function getApp({ manual, ...options }: MockAppOptions = {}): Promi
|
|||||||
name: 'workflow',
|
name: 'workflow',
|
||||||
instructions: {
|
instructions: {
|
||||||
echo: {
|
echo: {
|
||||||
run(node, { result }, processor) {
|
run({ config = {} }: FlowNodeModel, { result }, processor) {
|
||||||
return {
|
return {
|
||||||
status: JOB_STATUS.RESOLVED,
|
status: JOB_STATUS.RESOLVED,
|
||||||
result
|
result: config.path == null ? result : get(result, config.path)
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -54,6 +54,28 @@ describe('workflow > triggers > collection', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('model context', () => {
|
||||||
|
it('with association', async () => {
|
||||||
|
const workflow = await WorkflowModel.create({
|
||||||
|
enabled: true,
|
||||||
|
type: 'collection',
|
||||||
|
config: {
|
||||||
|
mode: 1,
|
||||||
|
collection: 'posts'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const post = await PostRepo.create({ values: { title: 't1', category: { title: 'c1' } } });
|
||||||
|
|
||||||
|
await sleep(500);
|
||||||
|
|
||||||
|
const executions = await workflow.getExecutions();
|
||||||
|
expect(executions.length).toBe(1);
|
||||||
|
expect(executions[0].context.data.title).toBe('t1');
|
||||||
|
expect(executions[0].context.data.category.title).toBe('c1');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('config.changed', () => {
|
describe('config.changed', () => {
|
||||||
it('no changed config', async () => {
|
it('no changed config', async () => {
|
||||||
const workflow = await WorkflowModel.create({
|
const workflow = await WorkflowModel.create({
|
||||||
|
@ -18,7 +18,7 @@ export default {
|
|||||||
transaction: processor.transaction
|
transaction: processor.transaction
|
||||||
});
|
});
|
||||||
|
|
||||||
if (appends.length) {
|
if (result && appends.length) {
|
||||||
const includeFields = appends.filter(field => !result.get(field) || !result[field]);
|
const includeFields = appends.filter(field => !result.get(field) || !result[field]);
|
||||||
const included = await model.findByPk(result[model.primaryKeyAttribute], {
|
const included = await model.findByPk(result[model.primaryKeyAttribute], {
|
||||||
attributes: [model.primaryKeyAttribute],
|
attributes: [model.primaryKeyAttribute],
|
||||||
@ -26,13 +26,14 @@ export default {
|
|||||||
transaction: processor.transaction
|
transaction: processor.transaction
|
||||||
});
|
});
|
||||||
includeFields.forEach(field => {
|
includeFields.forEach(field => {
|
||||||
result.set(field, included!.get(field), { raw: true });
|
const value = included!.get(field);
|
||||||
|
result.set(field, Array.isArray(value) ? value.map(item => item.toJSON()) : value.toJSON(), { raw: true });
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
// NOTE: get() for non-proxied instance (#380)
|
// NOTE: get() for non-proxied instance (#380)
|
||||||
result: result.get(),
|
result: result?.toJSON(),
|
||||||
status: JOB_STATUS.RESOLVED
|
status: JOB_STATUS.RESOLVED
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -78,11 +78,12 @@ async function handler(this: CollectionTrigger, workflow: WorkflowModel, data: M
|
|||||||
transaction
|
transaction
|
||||||
});
|
});
|
||||||
includeFields.forEach(field => {
|
includeFields.forEach(field => {
|
||||||
data.set(field, included!.get(field), { raw: true });
|
const value = included!.get(field);
|
||||||
|
data.set(field, Array.isArray(value) ? value.map(item => item.toJSON()) : value.toJSON(), { raw: true });
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
this.plugin.trigger(workflow, { data: data.get() }, {
|
this.plugin.trigger(workflow, { data: data.toJSON() }, {
|
||||||
context
|
context
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -344,7 +344,7 @@ ScheduleModes.set(SCHEDULE_MODE.COLLECTION_FIELD, {
|
|||||||
instances.forEach(item => {
|
instances.forEach(item => {
|
||||||
this.plugin.trigger(workflow, {
|
this.plugin.trigger(workflow, {
|
||||||
date: now,
|
date: now,
|
||||||
data: item.get()
|
data: item.toJSON()
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user