fix(plugin-workflow): add req context to processor (#936)

This commit is contained in:
Junyi 2022-10-19 19:01:33 +08:00 committed by chenos
parent 26c428a15b
commit cadaa8a2c2
8 changed files with 13 additions and 4 deletions

View File

@ -88,6 +88,7 @@ export type Values = any;
export interface CountOptions extends Omit<SequelizeCountOptions, 'distinct' | 'where' | 'include'>, Transactionable {
filter?: Filter;
context?: any;
}
export interface FilterByTk {

View File

@ -128,7 +128,7 @@ export default class WorkflowPlugin extends Plugin {
}
}
async trigger(workflow: WorkflowModel, context: Object, options: Transactionable = {}): Promise<ExecutionModel | null> {
async trigger(workflow: WorkflowModel, context: Object, options: Transactionable & { context?: any } = {}): Promise<ExecutionModel | null> {
// `null` means not to trigger
if (context === null) {
return null;
@ -186,7 +186,7 @@ export default class WorkflowPlugin extends Plugin {
execution.workflow = workflow;
const processor = this.createProcessor(execution, { transaction });
const processor = this.createProcessor(execution, { transaction, _context: options.context });
await processor.start();

View File

@ -13,6 +13,8 @@ import { EXECUTION_STATUS, JOB_STATUS } from './constants';
export interface ProcessorOptions extends Transactionable {
// TODO(temp): pass request context here for $isVar and other operators
_context?: any;
plugin: Plugin
}
@ -33,7 +35,7 @@ export default class Processor {
jobsMap = new Map<number, JobModel>();
jobsMapByNodeId: { [key: number]: any } = {};
constructor(public execution: ExecutionModel, private options: ProcessorOptions) {
constructor(public execution: ExecutionModel, public options: ProcessorOptions) {
}
// make dual linked nodes list then cache

View File

@ -12,6 +12,7 @@ export default {
const options = processor.getParsedValue(params);
const result = await repo.create({
...options,
context: processor.options._context,
transaction: processor.transaction
});

View File

@ -12,6 +12,7 @@ export default {
const options = processor.getParsedValue(params);
const result = await repo.destroy({
...options,
context: processor.options._context,
transaction: processor.transaction
});

View File

@ -14,6 +14,7 @@ export default {
const options = processor.getParsedValue(params);
const result = await (multiple ? repo.find : repo.findOne).call(repo, {
...options,
context: processor.options._context,
transaction: processor.transaction
});

View File

@ -14,6 +14,7 @@ export default {
const options = processor.getParsedValue(params);
const result = await repo.update({
...options,
context: processor.options._context,
transaction: processor.transaction
});

View File

@ -39,7 +39,7 @@ async function handler(this: CollectionTrigger, workflow: WorkflowModel, data: M
// TODO: change to map filter format to calculation format
// const calculation = toCalculation(condition);
const { repository, model } = (<typeof Model>data.constructor).database.getCollection(collection);
const { transaction } = options;
const { transaction, context } = options;
const count = await repository.count({
filter: {
$and: [
@ -47,6 +47,7 @@ async function handler(this: CollectionTrigger, workflow: WorkflowModel, data: M
{ [model.primaryKeyAttribute]: data[model.primaryKeyAttribute] }
]
},
context,
transaction
});
@ -56,6 +57,7 @@ async function handler(this: CollectionTrigger, workflow: WorkflowModel, data: M
}
return this.plugin.trigger(workflow, { data: data.get() }, {
context: options.context,
transaction: options.transaction
});
}