zcatalyst-integ-cliq
Version:
Node.js SDK for integrating Zoho Catalyst with Zoho Cliq
61 lines (60 loc) • 2.47 kB
JavaScript
import { resolve, join } from 'path';
import { CatalystError } from './error.js';
import CONSTANTS from './constants.js';
import { getFunctionRoot, isEmpty, readConfig } from './util.js';
import { existsSync } from 'fs';
const { CLIQ, HANDLERS, CONFIGJSON, BOT, FUNCTION, COMMAND, MESSAGEACTION } = CONSTANTS;
export async function getConfigJson() {
const configFile = await readConfig(CONFIGJSON);
const configJSON = JSON.parse(configFile);
return validateConfig(configJSON);
}
async function validateConfig(config) {
if (isEmpty(config.deployment)) {
throw new CatalystError('Invalid deployment config');
}
if (isEmpty(config.deployment.integration_config)) {
throw new CatalystError('integration_config is missing');
}
const integConfig = config.deployment.integration_config.find((integ) => integ.service === CLIQ);
if (integConfig === undefined) {
throw new CatalystError('integration_config for service ZohoCliq is not found');
}
if (isEmpty(integConfig.handlers)) {
throw new CatalystError('Handlers is empty');
}
const handlersArr = Object.keys(integConfig.handlers);
const fnRoot = getFunctionRoot();
handlersArr.forEach((handler) => {
if (!Object.keys(HANDLERS).includes(handler)) {
throw new CatalystError('Unknown handler: ' + handler);
}
const handlerFile = integConfig.handlers[handler];
const handlerFilePath = resolve(join(fnRoot, handlerFile));
if (!existsSync(handlerFilePath)) {
throw new CatalystError(`Handler file ${handlerFilePath} provided for ${handler} does not exist`);
}
integConfig.handlers[handler] = handlerFilePath;
});
return integConfig;
}
export function preProcess(req) {
const params = req.params;
if (params === undefined) {
throw new CatalystError('No params found in request body', 2);
}
if (req.type === BOT) {
const name = req.name;
const uniqueName = req.unique_name;
const botContent = { name, unique_name: uniqueName };
if (req.handler.type === HANDLERS.bot_handler.ACTION_HANDLER) {
botContent.action_name = req.handler.name;
}
Object.assign(params, botContent);
}
else if (req.type === FUNCTION || req.type === COMMAND || req.type === MESSAGEACTION) {
const name = req.name;
Object.assign(params, { name });
}
return params;
}