From 53ccc8b4e5d0228e7d42d271c3d1e37808d82e37 Mon Sep 17 00:00:00 2001 From: Jeevan Kishore Date: Fri, 24 Feb 2023 16:32:32 +0530 Subject: [PATCH] Implement webengage route (#354) --- package-lock.json | 4 +-- package.json | 4 +-- server/handlers/webengage-notifications.js | 30 ++++++++++++++++++++++ server/routes.js | 10 ++++++++ 4 files changed, 44 insertions(+), 4 deletions(-) create mode 100644 server/handlers/webengage-notifications.js diff --git a/package-lock.json b/package-lock.json index b0a59a46..9f0f75f4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@quintype/framework", - "version": "7.18.7", + "version": "7.18.8-webengage.3", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@quintype/framework", - "version": "7.18.7", + "version": "7.18.8-webengage.3", "license": "ISC", "dependencies": { "@ampproject/toolbox-optimizer": "2.8.3", diff --git a/package.json b/package.json index 4e492700..bdd908da 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@quintype/framework", - "version": "7.18.7", + "version": "7.18.8-webengage.3", "description": "Libraries to help build Quintype Node.js apps", "main": "index.js", "engines": { @@ -14,7 +14,7 @@ "coverage": "nyc --all npm test", "coverage-html": "nyc --all --reporter=html npm test", "docs": "rimraf docs && jsdoc -c jsdoc.json", - "sync-files-to": "npx onchange --verbose --wait --await-write-finish 'client/**/*' 'server/**/*' 'isomorphic/**/*' -- ./bin-dev-scripts/sync-to.sh " + "sync-files-to": "npx onchange --verbose --await-write-finish=2000 'client/**/*' 'server/**/*' 'isomorphic/**/*' -- ./bin-dev-scripts/sync-to.sh " }, "repository": { "type": "git", diff --git a/server/handlers/webengage-notifications.js b/server/handlers/webengage-notifications.js new file mode 100644 index 00000000..35810b67 --- /dev/null +++ b/server/handlers/webengage-notifications.js @@ -0,0 +1,30 @@ +const get = require("lodash/get"); +const request = require("request-promise"); + +exports.triggerWebengageNotifications = async function triggerWebengageNotifications( + req, + res, + next, + { apiKey, licenseCode } +) { + const eventName = get(req, ["body", "v1", "event", "name"], ""); + const url = `https://api.webengage.com/v2/accounts/${licenseCode}/business/save-event`; + + try { + await request({ + uri: url, + method: "POST", + headers: { + Authorization: `Bearer ${apiKey}`, + "content-type": "application/json", + }, + body: { eventName: eventName, eventData: { ...req.body, user_type: "malibu_user" } }, + json: true, + }); + res.status(200).send("webengage event triggered successfully"); + return; + } catch (error) { + res.status(500).send("webengage event failed"); + return; + } +}; diff --git a/server/routes.js b/server/routes.js index 0eff4eea..c60c0b66 100644 --- a/server/routes.js +++ b/server/routes.js @@ -23,6 +23,7 @@ const { redirectStory } = require("./handlers/story-redirect"); const { simpleJsonHandler } = require("./handlers/simple-json-handler"); const { makePickComponentSync } = require("../isomorphic/impl/make-pick-component-sync"); const { registerFCMTopic } = require("./handlers/fcm-registration-handler"); +const { triggerWebengageNotifications } = require("./handlers/webengage-notifications"); const rp = require("request-promise"); const bodyParser = require("body-parser"); const get = require("lodash/get"); @@ -332,6 +333,7 @@ exports.isomorphicRoutes = function isomorphicRoutes( sMaxAge = 900, appLoadingPlaceholder = "", fcmServerKey = "", + webengageConfig = {}, } ) { const withConfig = withConfigPartial(getClient, logError, publisherConfig, configWrapper); @@ -426,6 +428,14 @@ exports.isomorphicRoutes = function isomorphicRoutes( app.post("/register-fcm-topic", bodyParser.json(), withConfig(registerFCMTopic, { publisherConfig, fcmServerKey })); + if (webengageConfig.enableWebengage) { + app.post( + "/integrations/webengage/trigger-notification", + bodyParser.json(), + withConfig(triggerWebengageNotifications, webengageConfig) + ); + } + if (manifestFn) { app.get("/manifest.json", withConfig(handleManifest, { manifestFn, logError })); }