Skip to content
This repository has been archived by the owner on Dec 9, 2024. It is now read-only.

Commit

Permalink
Create rapid identify JSON route/router
Browse files Browse the repository at this point in the history
  • Loading branch information
zarathustra323 committed Apr 14, 2021
1 parent 57ba9bd commit edea334
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/marko-web-omeda-identity-x/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
40 changes: 40 additions & 0 deletions packages/marko-web-omeda-identity-x/routes/rapid-identify.js
Original file line number Diff line number Diff line change
@@ -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;
};

0 comments on commit edea334

Please sign in to comment.