@tachybase/plugin-workflow-approval
Version:
Approval base in Workflow
102 lines (101 loc) • 4.14 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var approvalRecords_exports = {};
__export(approvalRecords_exports, {
approvalRecords: () => approvalRecords
});
module.exports = __toCommonJS(approvalRecords_exports);
var import_module_workflow = require("@tachybase/module-workflow");
var import_server = require("@tego/server");
var import_error_code = require("../constants/error-code");
var import_status = require("../constants/status");
const approvalRecords = {
async listCentralized(context, next) {
const centralizedApprovalFlow = await context.db.getRepository("workflows").find({
filter: {
type: "approval",
"config.centralized": true
},
fields: ["id"]
});
context.action.mergeParams({
filter: {
workflowId: centralizedApprovalFlow.map((item) => item.id)
}
});
return import_server.actions.list(context, next);
},
async submit(context, next) {
var _a, _b;
const repository = import_server.utils.getRepositoryFromParams(context);
const { filterByTk, values } = context.action.params;
const { data, status, needUpdateRecord } = values || {};
const { currentUser } = context.state;
if (!currentUser) {
return context.throw(401);
}
const approvalRecord = await repository.findOne({
filterByTk,
filter: {
userId: currentUser == null ? void 0 : currentUser.id
},
appends: ["job", "node", "execution", "workflow", "approval"],
context
});
if (!approvalRecord) {
return context.throw(404);
}
switch (true) {
// case !approvalRecord.workflow.enabled:
// return context.throw(400, ERROR_CODE_MAP['not_enable_workflow']);
case ((_a = approvalRecord.execution) == null ? void 0 : _a.status):
return context.throw(400, import_error_code.ERROR_CODE_MAP["execution_finished"]);
case ((_b = approvalRecord.job) == null ? void 0 : _b.status):
return context.throw(400, import_error_code.ERROR_CODE_MAP["job_finished"]);
case approvalRecord.status !== import_status.APPROVAL_ACTION_STATUS.PENDING:
return context.throw(400, import_error_code.ERROR_CODE_MAP["not_approval_pending"]);
case (!needUpdateRecord && !(approvalRecord.node.config.actions ?? []).includes(status)):
return context.throw(400, import_error_code.ERROR_CODE_MAP["not_need_update"]);
default:
break;
}
await approvalRecord.update({
status,
comment: data.comment,
snapshot: approvalRecord.approval.data,
summary: approvalRecord.approval.summary,
collectionName: approvalRecord.approval.collectionName
});
context.body = approvalRecord.get();
context.status = 202;
await next();
approvalRecord.execution.workflow = approvalRecord.workflow;
approvalRecord.job.execution = approvalRecord.execution;
approvalRecord.job.latestUserJob = approvalRecord.get();
const workflow = context.app.getPlugin(import_module_workflow.PluginWorkflow);
const processor = workflow.createProcessor(approvalRecord.execution);
processor.logger.info(
`approval node (${approvalRecord.nodeId}) action trigger execution (${approvalRecord.execution.id}) to resume`
);
workflow.resume(approvalRecord.job);
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
approvalRecords
});