fix(plugin-workflow): use toJSON instead of get to get valid result (#1596)

This commit is contained in:
Junyi 2023-03-24 22:54:07 +07:00 committed by GitHub
parent 726b08e87a
commit cc50b38a12
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 34 additions and 8 deletions

View File

@ -1,10 +1,12 @@
import path from 'path';
import { get } from 'lodash';
import { ApplicationOptions } from '@nocobase/server';
import { MockServer, mockServer } from '@nocobase/test';
import Plugin from '..';
import { JOB_STATUS } from '../constants';
import FlowNodeModel from '../models/FlowNode';
export function sleep(ms: number) {
return new Promise(resolve => {
@ -23,10 +25,10 @@ export async function getApp({ manual, ...options }: MockAppOptions = {}): Promi
name: 'workflow',
instructions: {
echo: {
run(node, { result }, processor) {
run({ config = {} }: FlowNodeModel, { result }, processor) {
return {
status: JOB_STATUS.RESOLVED,
result
result: config.path == null ? result : get(result, config.path)
};
}
},

View File

@ -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', () => {
it('no changed config', async () => {
const workflow = await WorkflowModel.create({

View File

@ -18,7 +18,7 @@ export default {
transaction: processor.transaction
});
if (appends.length) {
if (result && appends.length) {
const includeFields = appends.filter(field => !result.get(field) || !result[field]);
const included = await model.findByPk(result[model.primaryKeyAttribute], {
attributes: [model.primaryKeyAttribute],
@ -26,13 +26,14 @@ export default {
transaction: processor.transaction
});
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 {
// NOTE: get() for non-proxied instance (#380)
result: result.get(),
result: result?.toJSON(),
status: JOB_STATUS.RESOLVED
};
}

View File

@ -78,11 +78,12 @@ async function handler(this: CollectionTrigger, workflow: WorkflowModel, data: M
transaction
});
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
});
}

View File

@ -344,7 +344,7 @@ ScheduleModes.set(SCHEDULE_MODE.COLLECTION_FIELD, {
instances.forEach(item => {
this.plugin.trigger(workflow, {
date: now,
data: item.get()
data: item.toJSON()
});
});
}