refactor(plugin-workflow): change to (#3871)

This commit is contained in:
Junyi 2024-03-29 23:36:49 +08:00 committed by GitHub
parent 4adc116386
commit 6d0d21ede2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 15 additions and 9 deletions

View File

@ -11,7 +11,7 @@ export class CreateInstruction extends Instruction {
const { collection, params: { appends = [], ...params } = {} } = node.config; const { collection, params: { appends = [], ...params } = {} } = node.config;
const [dataSourceName, collectionName] = parseCollectionName(collection); const [dataSourceName, collectionName] = parseCollectionName(collection);
const { repository, model } = this.workflow.app.dataSourceManager.dataSources const { repository, filterTargetKey } = this.workflow.app.dataSourceManager.dataSources
.get(dataSourceName) .get(dataSourceName)
.collectionManager.getCollection(collectionName); .collectionManager.getCollection(collectionName);
const options = processor.getParsedValue(params, node.id); const options = processor.getParsedValue(params, node.id);
@ -33,7 +33,7 @@ export class CreateInstruction extends Instruction {
return set; return set;
}, new Set()); }, new Set());
result = await repository.findOne({ result = await repository.findOne({
filterByTk: created[model.primaryKeyAttribute], filterByTk: created[filterTargetKey],
appends: Array.from(includeFields), appends: Array.from(includeFields),
transaction, transaction,
}); });

View File

@ -42,7 +42,7 @@ async function handler(this: CollectionTrigger, workflow: WorkflowModel, data: M
.get(dataSourceName) .get(dataSourceName)
.collectionManager.getCollection(collectionName); .collectionManager.getCollection(collectionName);
const { transaction, context } = options; const { transaction, context } = options;
const { repository, model } = collection; const { repository, filterTargetKey } = collection;
// NOTE: if no configured fields changed, do not trigger // NOTE: if no configured fields changed, do not trigger
if ( if (
@ -62,7 +62,7 @@ async function handler(this: CollectionTrigger, workflow: WorkflowModel, data: M
// const calculation = toCalculation(condition); // const calculation = toCalculation(condition);
const count = await repository.count({ const count = await repository.count({
filter: { filter: {
$and: [condition, { [model.primaryKeyAttribute]: data[model.primaryKeyAttribute] }], $and: [condition, { [filterTargetKey]: data[filterTargetKey] }],
}, },
context, context,
transaction, transaction,
@ -82,7 +82,7 @@ async function handler(this: CollectionTrigger, workflow: WorkflowModel, data: M
return set; return set;
}, new Set()); }, new Set());
result = await repository.findOne({ result = await repository.findOne({
filterByTk: data[model.primaryKeyAttribute], filterByTk: data[filterTargetKey],
appends: Array.from(includeFields), appends: Array.from(includeFields),
transaction, transaction,
}); });

View File

@ -283,8 +283,11 @@ export default class ScheduleTrigger {
} }
schedule(workflow: WorkflowModel, record, nextTime, toggle = true, options = {}) { schedule(workflow: WorkflowModel, record, nextTime, toggle = true, options = {}) {
const { model } = this.workflow.app.db.getCollection(workflow.config.collection); const [dataSourceName, collectionName] = parseCollectionName(workflow.config.collection);
const recordPk = record.get(model.primaryKeyAttribute); const { filterTargetKey } = this.workflow.app.dataSourceManager.dataSources
.get(dataSourceName)
.collectionManager.getCollection(collectionName);
const recordPk = record.get(filterTargetKey);
if (toggle) { if (toggle) {
const nextInterval = Math.max(0, nextTime - Date.now()); const nextInterval = Math.max(0, nextTime - Date.now());
const key = `${workflow.id}:${recordPk}@${nextTime}`; const key = `${workflow.id}:${recordPk}@${nextTime}`;
@ -307,8 +310,11 @@ export default class ScheduleTrigger {
} }
async trigger(workflow: WorkflowModel, record, nextTime, { transaction }: Transactionable = {}) { async trigger(workflow: WorkflowModel, record, nextTime, { transaction }: Transactionable = {}) {
const { repository, model } = this.workflow.app.db.getCollection(workflow.config.collection); const [dataSourceName, collectionName] = parseCollectionName(workflow.config.collection);
const recordPk = record.get(model.primaryKeyAttribute); const { repository, filterTargetKey } = this.workflow.app.dataSourceManager.dataSources
.get(dataSourceName)
.collectionManager.getCollection(collectionName);
const recordPk = record.get(filterTargetKey);
const data = await repository.findOne({ const data = await repository.findOne({
filterByTk: recordPk, filterByTk: recordPk,
appends: workflow.config.appends, appends: workflow.config.appends,