From 4f420c670f194fbf950c474a10964ad302045b47 Mon Sep 17 00:00:00 2001 From: chenos Date: Wed, 23 Feb 2022 18:39:36 +0800 Subject: [PATCH] fix(plugin-workflow): ts errors --- .../plugin-workflow/src/models/Execution.ts | 46 +++++++++---------- .../plugin-workflow/src/models/FlowNode.ts | 8 ++-- packages/plugin-workflow/src/models/Job.ts | 3 +- .../plugin-workflow/src/models/Workflow.ts | 12 ++--- 4 files changed, 33 insertions(+), 36 deletions(-) diff --git a/packages/plugin-workflow/src/models/Execution.ts b/packages/plugin-workflow/src/models/Execution.ts index df293abc8..0587c0abf 100644 --- a/packages/plugin-workflow/src/models/Execution.ts +++ b/packages/plugin-workflow/src/models/Execution.ts @@ -1,18 +1,12 @@ -import { - Model, - BelongsToGetAssociationMixin, - HasManyGetAssociationsMixin, - Transaction -} from 'sequelize'; +import { Database, Model } from '@nocobase/database'; import parse from 'json-templates'; - -import Database from '@nocobase/database'; - +import { BelongsToGetAssociationMixin, HasManyGetAssociationsMixin, Transaction } from 'sequelize'; import { EXECUTION_STATUS, JOB_STATUS } from '../constants'; import { getInstruction } from '../instructions'; -import WorkflowModel from './Workflow'; import FlowNodeModel from './FlowNode'; import JobModel from './Job'; +import WorkflowModel from './Workflow'; + export interface ExecutionOptions { transaction?: Transaction; @@ -54,11 +48,11 @@ export default class ExecutionModel extends Model { makeNodes(nodes = []) { this.nodes = nodes; - nodes.forEach(node => { + nodes.forEach((node) => { this.nodesMap.set(node.id, node); }); - nodes.forEach(node => { + nodes.forEach((node) => { if (node.upstreamId) { node.upstream = this.nodesMap.get(node.upstreamId); } @@ -70,7 +64,7 @@ export default class ExecutionModel extends Model { } makeJobs(jobs: Array) { - jobs.forEach(job => { + jobs.forEach((job) => { this.jobsMap.set(job.id, job); // TODO: should consider cycle, and from previous job this.jobsMapByNodeId[job.nodeId] = job.result; @@ -79,7 +73,8 @@ export default class ExecutionModel extends Model { async prepare(options, commit = false) { this.options = options || {}; - const { transaction = await (this.constructor).database.sequelize.transaction() } = this.options; + const { transaction = await (this.constructor).database.sequelize.transaction() } = + this.options; this.transaction = transaction; if (!this.workflow) { @@ -92,7 +87,7 @@ export default class ExecutionModel extends Model { const jobs = await this.getJobs({ order: [['id', 'ASC']], - transaction + transaction, }); this.makeJobs(jobs); @@ -108,7 +103,7 @@ export default class ExecutionModel extends Model { } await this.prepare(options); if (this.nodes.length) { - const head = this.nodes.find(item => !item.upstream); + const head = this.nodes.find((item) => !item.upstream); await this.exec(head, { result: this.context }); } else { await this.exit(null); @@ -144,7 +139,7 @@ export default class ExecutionModel extends Model { // for uncaught error, set to rejected job = { result: err instanceof Error ? err.toString() : err, - status: JOB_STATUS.REJECTED + status: JOB_STATUS.REJECTED, }; // if previous job is from resuming if (prevJob && prevJob.nodeId === node.id) { @@ -157,13 +152,13 @@ export default class ExecutionModel extends Model { // TODO(optimize): many checking of resuming or new could be improved // could be implemented separately in exec() / resume() if (job instanceof Model) { - savedJob = await job.save({ transaction: this.transaction }) as JobModel; + savedJob = (await job.save({ transaction: this.transaction })) as JobModel; } else { const upstreamId = prevJob instanceof Model ? prevJob.get('id') : null; savedJob = await this.saveJob({ nodeId: node.id, upstreamId, - ...job + ...job, }); } @@ -214,10 +209,13 @@ export default class ExecutionModel extends Model { async saveJob(payload) { const { database } = this.constructor; const { model } = database.getCollection('jobs'); - const [job] = await model.upsert({ - ...payload, - executionId: this.id - }, { transaction: this.transaction }) as [JobModel, boolean | null]; + const [job] = (await model.upsert( + { + ...payload, + executionId: this.id, + }, + { transaction: this.transaction }, + )) as [JobModel, boolean | null]; this.jobsMap.set(job.id, job); this.jobsMapByNodeId[job.nodeId] = job.result; @@ -256,7 +254,7 @@ export default class ExecutionModel extends Model { getParsedValue(value) { return parse(value)({ $context: this.context, - $jobsMapByNodeId: this.jobsMapByNodeId + $jobsMapByNodeId: this.jobsMapByNodeId, }); } } diff --git a/packages/plugin-workflow/src/models/FlowNode.ts b/packages/plugin-workflow/src/models/FlowNode.ts index e3b01a06a..19816ca5b 100644 --- a/packages/plugin-workflow/src/models/FlowNode.ts +++ b/packages/plugin-workflow/src/models/FlowNode.ts @@ -1,8 +1,8 @@ -import { Model, BelongsToGetAssociationMixin } from 'sequelize'; -import Database from '@nocobase/database'; - +import { Database, Model } from '@nocobase/database'; +import { BelongsToGetAssociationMixin } from 'sequelize'; import WorkflowModel from './Workflow'; + export default class FlowNodeModel extends Model { declare static readonly database: Database; @@ -20,4 +20,4 @@ export default class FlowNodeModel extends Model { declare workflow?: WorkflowModel; declare getWorkflow: BelongsToGetAssociationMixin; -} \ No newline at end of file +} diff --git a/packages/plugin-workflow/src/models/Job.ts b/packages/plugin-workflow/src/models/Job.ts index fc92306a4..cde654334 100644 --- a/packages/plugin-workflow/src/models/Job.ts +++ b/packages/plugin-workflow/src/models/Job.ts @@ -1,4 +1,5 @@ -import { Model, BelongsToGetAssociationMixin } from 'sequelize'; +import { Model } from '@nocobase/database'; +import { BelongsToGetAssociationMixin } from 'sequelize'; import FlowNodeModel from './FlowNode'; export default class JobModel extends Model { diff --git a/packages/plugin-workflow/src/models/Workflow.ts b/packages/plugin-workflow/src/models/Workflow.ts index 0af20d334..578081d08 100644 --- a/packages/plugin-workflow/src/models/Workflow.ts +++ b/packages/plugin-workflow/src/models/Workflow.ts @@ -1,9 +1,7 @@ -import { Model, HasManyGetAssociationsMixin, HasManyCreateAssociationMixin } from 'sequelize'; - -import Database from '@nocobase/database'; - -import { get as getTrigger } from '../triggers'; +import { Database, Model } from '@nocobase/database'; +import { HasManyCreateAssociationMixin, HasManyGetAssociationsMixin } from 'sequelize'; import { EXECUTION_STATUS } from '../constants'; +import { get as getTrigger } from '../triggers'; import ExecutionModel from './Execution'; import FlowNodeModel from './FlowNode'; @@ -31,7 +29,7 @@ export default class WorkflowModel extends Model { static async mount() { const collection = this.database.getCollection('workflows'); const workflows = await collection.repository.find({ - filter: { enabled: true } + filter: { enabled: true }, }); workflows.forEach((workflow: WorkflowModel) => { @@ -65,7 +63,7 @@ export default class WorkflowModel extends Model { const execution = await this.createExecution({ context, - status: EXECUTION_STATUS.STARTED + status: EXECUTION_STATUS.STARTED, }); execution.workflow = this;