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 { export interface CountOptions extends Omit<SequelizeCountOptions, 'distinct' | 'where' | 'include'>, Transactionable {
filter?: Filter; filter?: Filter;
context?: any;
} }
export interface FilterByTk { 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 // `null` means not to trigger
if (context === null) { if (context === null) {
return null; return null;
@ -186,7 +186,7 @@ export default class WorkflowPlugin extends Plugin {
execution.workflow = workflow; execution.workflow = workflow;
const processor = this.createProcessor(execution, { transaction }); const processor = this.createProcessor(execution, { transaction, _context: options.context });
await processor.start(); await processor.start();

View File

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

View File

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

View File

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

View File

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

View File

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