This repository has been archived by the owner on Dec 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #66 from zarathustra323/omeda-idx-middleware
Create Omeda+IdentityX Rapid Identification Router
- Loading branch information
Showing
4 changed files
with
58 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -88,4 +88,5 @@ module.exports = async ({ | |
namespace, | ||
}), | ||
]); | ||
return { id, encryptedCustomerId }; | ||
}; |
40 changes: 40 additions & 0 deletions
40
packages/marko-web-omeda-identity-x/routes/rapid-identify.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
13 changes: 13 additions & 0 deletions
13
packages/marko-web-omeda-identity-x/utils/find-omeda-encrypted-id.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
}; |