@nocobase/plugin-workflow-notification
Version:
Send notification in workflow.
146 lines (144 loc) • 5.72 kB
JavaScript
/**
* This file is part of the NocoBase (R) project.
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
* Authors: NocoBase Team.
*
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
* For more information, please refer to: https://www.nocobase.com/agreement.
*/
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var NotificationInstruction_exports = {};
__export(NotificationInstruction_exports, {
default: () => NotificationInstruction_default
});
module.exports = __toCommonJS(NotificationInstruction_exports);
var import_plugin_notification_manager = __toESM(require("@nocobase/plugin-notification-manager"));
var import_plugin_workflow = require("@nocobase/plugin-workflow");
class NotificationInstruction_default extends import_plugin_workflow.Instruction {
async run(node, prevJob, processor) {
const { ignoreFail, ...config } = node.config;
const options = processor.getParsedValue(config, node.id);
const scope = processor.getScope(node.id);
const sendParams = {
channelName: options.channelName,
message: { ...options, content: config.content },
triggerFrom: "workflow",
data: scope
};
const notificationServer = this.workflow.pm.get(import_plugin_notification_manager.default);
const { workflow } = processor.execution;
const sync = this.workflow.isWorkflowSync(workflow);
if (sync) {
try {
const result = await notificationServer.send(sendParams);
if (result.status === "success") {
return {
status: import_plugin_workflow.JOB_STATUS.RESOLVED,
result
};
} else {
return {
status: ignoreFail ? import_plugin_workflow.JOB_STATUS.RESOLVED : import_plugin_workflow.JOB_STATUS.FAILED,
result
};
}
} catch (error) {
return {
status: ignoreFail ? import_plugin_workflow.JOB_STATUS.RESOLVED : import_plugin_workflow.JOB_STATUS.ERROR,
result: error
};
}
}
const { id } = processor.saveJob({
status: import_plugin_workflow.JOB_STATUS.PENDING,
nodeId: node.id,
nodeKey: node.key,
upstreamId: (prevJob == null ? void 0 : prevJob.id) ?? null
});
await processor.exit();
const jobDone = { status: import_plugin_workflow.JOB_STATUS.PENDING };
try {
processor.logger.info(`notification (#${node.id}) sent, waiting for response...`);
const result = await notificationServer.send(sendParams);
if (result.status === "success") {
processor.logger.info(`notification (#${node.id}) sent successfully.`);
jobDone.status = import_plugin_workflow.JOB_STATUS.RESOLVED;
jobDone.result = result;
} else {
processor.logger.info(`notification (#${node.id}) sent failed.`);
jobDone.status = ignoreFail ? import_plugin_workflow.JOB_STATUS.RESOLVED : import_plugin_workflow.JOB_STATUS.FAILED;
jobDone.result = result;
}
} catch (error) {
processor.logger.warn(`notification (#${node.id}) sent failed: ${error.message}`);
jobDone.status = ignoreFail ? import_plugin_workflow.JOB_STATUS.RESOLVED : import_plugin_workflow.JOB_STATUS.FAILED;
jobDone.result = error;
} finally {
const job = await this.workflow.app.db.getRepository("jobs").findOne({
filterByTk: id
});
job.set(jobDone);
processor.logger.debug(`notification (#${node.id}) sending ended, resume workflow...`);
setImmediate(() => {
this.workflow.resume(job);
});
}
}
async resume(node, job, processor) {
return job;
}
async test(config) {
const sendParams = {
channelName: config.channelName,
message: config,
triggerFrom: "workflow",
data: {}
};
const notificationServer = this.workflow.pm.get(import_plugin_notification_manager.default);
try {
const result = await notificationServer.send(sendParams);
if (result.status === "success") {
return {
status: import_plugin_workflow.JOB_STATUS.RESOLVED,
result
};
} else {
return {
status: import_plugin_workflow.JOB_STATUS.FAILED,
result
};
}
} catch (error) {
return {
status: import_plugin_workflow.JOB_STATUS.FAILED,
result: error
};
}
}
}