From d7ef457d315830f225480e2803309b764f042278 Mon Sep 17 00:00:00 2001 From: Jacob Bare Date: Wed, 14 Apr 2021 09:21:17 -0400 Subject: [PATCH 1/3] Return customer ID and ecrypted ID from rapid identify --- packages/marko-web-omeda-identity-x/rapid-identify.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/marko-web-omeda-identity-x/rapid-identify.js b/packages/marko-web-omeda-identity-x/rapid-identify.js index 3d045635e..be84afa97 100644 --- a/packages/marko-web-omeda-identity-x/rapid-identify.js +++ b/packages/marko-web-omeda-identity-x/rapid-identify.js @@ -88,4 +88,5 @@ module.exports = async ({ namespace, }), ]); + return { id, encryptedCustomerId }; }; From 57ba9bd3e18434e7e510ceb77a6f0c87f06412f3 Mon Sep 17 00:00:00 2001 From: Jacob Bare Date: Wed, 14 Apr 2021 09:21:43 -0400 Subject: [PATCH 2/3] Create find encrypted ID utility --- packages/marko-web-omeda-identity-x/package.json | 1 + .../utils/find-omeda-encrypted-id.js | 13 +++++++++++++ 2 files changed, 14 insertions(+) create mode 100644 packages/marko-web-omeda-identity-x/utils/find-omeda-encrypted-id.js diff --git a/packages/marko-web-omeda-identity-x/package.json b/packages/marko-web-omeda-identity-x/package.json index 618cdb03c..4c92b56a3 100644 --- a/packages/marko-web-omeda-identity-x/package.json +++ b/packages/marko-web-omeda-identity-x/package.json @@ -12,6 +12,7 @@ }, "dependencies": { "@parameter1/base-cms-object-path": "^2.5.0", + "@parameter1/base-cms-utils": "^2.4.2", "graphql": "^14.7.0", "graphql-tag": "^2.11.0" }, diff --git a/packages/marko-web-omeda-identity-x/utils/find-omeda-encrypted-id.js b/packages/marko-web-omeda-identity-x/utils/find-omeda-encrypted-id.js new file mode 100644 index 000000000..a7d5d70b1 --- /dev/null +++ b/packages/marko-web-omeda-identity-x/utils/find-omeda-encrypted-id.js @@ -0,0 +1,13 @@ +const { asArray } = require('@parameter1/base-cms-utils'); + +module.exports = ({ externalIds = [], brandKey } = {}) => { + const externalId = asArray(externalIds).find((eid) => { + const { namespace, identifier } = eid; + return namespace.provider === 'omeda' + && namespace.tenant === brandKey.toLowerCase() + && namespace.type === 'customer' + && identifier.type === 'encrypted' + && identifier.value; + }); + return externalId ? externalId.identifier.value : null; +}; From edea33423c6c2da499d24283f21009fa4c089465 Mon Sep 17 00:00:00 2001 From: Jacob Bare Date: Wed, 14 Apr 2021 09:22:10 -0400 Subject: [PATCH 3/3] Create rapid identify JSON route/router --- .../marko-web-omeda-identity-x/package.json | 4 +- .../routes/rapid-identify.js | 40 +++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 packages/marko-web-omeda-identity-x/routes/rapid-identify.js diff --git a/packages/marko-web-omeda-identity-x/package.json b/packages/marko-web-omeda-identity-x/package.json index 4c92b56a3..af3d76e71 100644 --- a/packages/marko-web-omeda-identity-x/package.json +++ b/packages/marko-web-omeda-identity-x/package.json @@ -17,7 +17,9 @@ "graphql-tag": "^2.11.0" }, "peerDependencies": { - "@parameter1/base-cms-marko-web-identity-x": "^2.16.0" + "@parameter1/base-cms-marko-web": "^2.13.0", + "@parameter1/base-cms-marko-web-identity-x": "^2.16.0", + "express": "^4.17.1" }, "publishConfig": { "access": "public" diff --git a/packages/marko-web-omeda-identity-x/routes/rapid-identify.js b/packages/marko-web-omeda-identity-x/routes/rapid-identify.js new file mode 100644 index 000000000..4ea5eb44d --- /dev/null +++ b/packages/marko-web-omeda-identity-x/routes/rapid-identify.js @@ -0,0 +1,40 @@ +const { Router } = require('express'); +const { getAsObject } = require('@parameter1/base-cms-object-path'); +const { asyncRoute } = require('@parameter1/base-cms-utils'); +const jsonErrorHandler = require('@parameter1/base-cms-marko-web/express/json-error-handler'); +const omedaRapidIdentityX = require('../rapid-identify'); +const findOmedaEncryptedId = require('../utils/find-omeda-encrypted-id'); + +module.exports = ({ brandKey, productId } = {}) => { + if (!brandKey) throw new Error('An Omeda brand key is required to use this middleware.'); + if (!productId) throw new Error('An Omeda rapid identification product ID is required to use this middleware.'); + const router = Router(); + router.get('/', asyncRoute(async (req, res) => { + const data = { encryptedId: null, source: null }; + const { identityX } = req; + const context = await identityX.loadActiveContext(); + const user = getAsObject(context, 'user'); + if (!user.id) return res.json(data); + // determine if an encrypted ID already exists for this user and brand. + const encryptedId = findOmedaEncryptedId({ externalIds: user.externalIds, brandKey }); + if (encryptedId) { + data.encryptedId = encryptedId; + data.source = 'existing'; + } else { + // no omeda identifier found for this user, rapidly identify. + const { encryptedCustomerId } = await omedaRapidIdentityX({ + brandKey, + productId, + appUser: user, + identityX, + omedaGraphQL: req.$omeda, + }); + data.encryptedId = encryptedCustomerId; + data.source = 'new'; + } + return res.json(data); + })); + + router.use(jsonErrorHandler()); + return router; +};