@mediarithmics/plugins-nodejs-sdk
Version:
This is the mediarithmics nodejs to help plugin developers bootstrapping their plugin without having to deal with most of the plugin boilerplate
108 lines (107 loc) • 5.33 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const _ = require("lodash");
const index_1 = require("../../../index");
class EmailRouterPlugin extends index_1.BasePlugin {
fetchEmailRouterProperties(id) {
const _super = name => super[name];
return __awaiter(this, void 0, void 0, function* () {
const response = yield _super("requestGatewayHelper").call(this, "GET", `${this.outboundPlatformUrl}/v1/email_routers/${id}/properties`);
this.logger.debug(`Fetched Email Router Properties: ${id} - ${JSON.stringify(response.data)}`);
return response.data;
});
}
// Method to build an instance context
// To be overriden to get a cutom behavior
// This is a default provided implementation
instanceContextBuilder(routerId) {
return __awaiter(this, void 0, void 0, function* () {
const emailRouterProps = yield this.fetchEmailRouterProperties(routerId);
const context = {
routerProperties: emailRouterProps
};
return context;
});
}
getInstanceContext(emailRouterId) {
return __awaiter(this, void 0, void 0, function* () {
if (!this.pluginCache.get(emailRouterId)) {
this.pluginCache.put(emailRouterId, this.instanceContextBuilder(emailRouterId), this.INSTANCE_CONTEXT_CACHE_EXPIRATION);
}
return yield this.pluginCache.get(emailRouterId);
});
}
initEmailRouting() {
this.app.post("/v1/email_routing", this.asyncMiddleware((req, res) => __awaiter(this, void 0, void 0, function* () {
if (!req.body || _.isEmpty(req.body)) {
const msg = {
error: "Missing request body"
};
this.logger.error("POST /v1/email_routing : %s", JSON.stringify(msg));
return res.status(500).json(msg);
}
else {
this.logger.debug(`POST /v1/email_routing ${JSON.stringify(req.body)}`);
const emailRoutingRequest = req.body;
if (!this.onEmailRouting) {
const errMsg = "No Email Routing listener registered!";
this.logger.error(errMsg);
return res.status(500).json({ error: errMsg });
}
const instanceContext = yield this.getInstanceContext(emailRoutingRequest.email_router_id);
const pluginResponse = yield this.onEmailRouting(emailRoutingRequest, instanceContext);
this.logger.debug(`Returning: ${JSON.stringify(pluginResponse)}`);
res.status(200).send(JSON.stringify(pluginResponse));
}
})));
}
initEmailCheck() {
this.app.post("/v1/email_router_check", (req, res) => {
if (!req.body || _.isEmpty(req.body)) {
const msg = {
error: "Missing request body"
};
this.logger.error("POST /v1/email_router_check : %s", JSON.stringify(msg));
res.status(500).json(msg);
}
else {
this.logger.debug(`POST /v1/email_router_check ${JSON.stringify(req.body)}`);
const emailCheckRequest = req.body;
if (!this.onEmailRouting) {
throw new Error("No Email Check listener registered!");
}
if (!this.pluginCache.get(emailCheckRequest.email_router_id)) {
this.pluginCache.put(emailCheckRequest.email_router_id, this.instanceContextBuilder(emailCheckRequest.email_router_id), this.INSTANCE_CONTEXT_CACHE_EXPIRATION);
}
this.pluginCache
.get(emailCheckRequest.email_router_id)
.then((instanceContext) => {
return this.onEmailCheck(emailCheckRequest, instanceContext).then(response => {
this.logger.debug(`Returning: ${JSON.stringify(response)}`);
res.status(200).send(JSON.stringify(response));
});
})
.catch((error) => {
this.logger.error(`Something bad happened : ${error.message} - ${error.stack}`);
return res.status(500).send(error.message + "\n" + error.stack);
});
}
});
}
constructor() {
super();
// We init the specific route to listen for activity analysis requests
this.initEmailRouting();
this.initEmailCheck();
this.setErrorHandler();
}
}
exports.EmailRouterPlugin = EmailRouterPlugin;