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

Commit

Permalink
Merge pull request #190 from zarathustra323/consolidate-omeda-idx
Browse files Browse the repository at this point in the history
Consolidate Omeda, IdentityX and Omeda+IdentityX installation
  • Loading branch information
zarathustra323 authored Nov 13, 2021
2 parents d5b3cb9 + 33c17e1 commit 4116cd2
Show file tree
Hide file tree
Showing 16 changed files with 295 additions and 83 deletions.
5 changes: 5 additions & 0 deletions packages/marko-web-identity-x/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
const routes = require('./routes');
const middleware = require('./middleware');

/**
*
* @param {object} app The express app instance
* @param {IdentityXConfiguration} config The IdentityX config
*/
module.exports = (app, config) => {
app.use(middleware(config));
app.use('/__idx', routes);
Expand Down
18 changes: 6 additions & 12 deletions packages/marko-web-omeda-identity-x/add-integration-hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,17 @@ const {
module.exports = ({
idxConfig,
brandKey,
productId,
omedaGraphQLProp = '$omeda',
omedaGraphQLProp = '$omedaGraphQLClient',
idxOmedaRapidIdentifyProp = '$idxOmedaRapidIdentify',
} = {}) => {
if (!idxConfig) throw new Error('The IdentityX configuration instances is required to add Omeda+IdentityX integration hooks.');
if (!brandKey) throw new Error('An Omeda brand key is required to add Omeda+IdentityX integration hooks.');
if (!productId) throw new Error('An Omeda rapid identification product ID is required to add Omeda+IdentityX integration hooks.');
idxConfig.addHook({
name: 'onLoginLinkSent',
shouldAwait: false,
fn: async ({ req, service, user }) => onLoginLinkSent({
brandKey,
productId,
omedaGraphQLProp,
idxOmedaRapidIdentifyProp,

req,
service,
Expand All @@ -43,14 +41,10 @@ module.exports = ({
idxConfig.addHook({
name: 'onUserProfileUpdate',
shouldAwait: false,
fn: async ({ user, service, req }) => onUserProfileUpdate({
brandKey,
productId,
omedaGraphQLProp,

user,
service,
fn: async ({ user, req }) => onUserProfileUpdate({
idxOmedaRapidIdentifyProp,
req,
user,
}),
});

Expand Down
65 changes: 65 additions & 0 deletions packages/marko-web-omeda-identity-x/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
const omeda = require('@parameter1/base-cms-marko-web-omeda');
const identityX = require('@parameter1/base-cms-marko-web-identity-x');
const addOmedaHooksToIdentityXConfig = require('./add-integration-hooks');
const stripOlyticsParam = require('./middleware/strip-olytics-param');
const rapidIdentify = require('./middleware/rapid-identify');

module.exports = (app, {
brandKey: brand,
clientKey,
appId,
inputId,

rapidIdentProductId,
omedaGraphQLClientProp = '$omedaGraphQLClient',
omedaRapidIdentifyProp = '$omedaRapidIdentify',

idxConfig,
idxOmedaRapidIdentifyProp = '$idxOmedaRapidIdentify',
} = {}) => {
if (!brand) throw new Error('The Omeda `brandKey` is required.');
if (!appId) throw new Error('The Omeda `appId` is required.');
if (!rapidIdentProductId) throw new Error('The Omeda `rapidIdentProductId` is required.');

// consistently pass brand key
const brandKey = brand.trim().toLowerCase();

// install omeda middleware
omeda(app, {
brandKey,
clientKey,
appId,
inputId,
rapidIdentProductId,
omedaGraphQLClientProp,
omedaRapidIdentifyProp,
});

// add appropiate identity-x to omeda integration hooks
addOmedaHooksToIdentityXConfig({
idxConfig,
brandKey,
productId: rapidIdentProductId,
omedaGraphQLProp: omedaGraphQLClientProp,
});

// install identity x
identityX(app, idxConfig);

// attach the identity-x rapid identification wrapper middleware
app.use(rapidIdentify({
brandKey,
productId: rapidIdentProductId,
prop: idxOmedaRapidIdentifyProp,
omedaRapidIdentifyProp,
}));

// register the rapid identify AJAX route
app.use('/__idx/omeda-rapid-ident', rapidIdentify({
brandKey,
idxOmedaRapidIdentifyProp,
}));

// strip `oly_enc_id` when identity-x user is logged-in
app.use(stripOlyticsParam());
};
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const gql = require('graphql-tag');
const { get, getAsArray } = require('@parameter1/base-cms-object-path');
const isOmedaDemographicId = require('../external-id/is-demographic-id');
const rapidIdentify = require('../rapid-identify');

const FIELD_QUERY = gql`
query GetCustomFields {
Expand Down Expand Up @@ -58,13 +57,13 @@ const SET_OMEDA_DEMOGRAPHIC_DATA = gql`
}
`;

const getOmedaCustomerRecord = async (omedaGraphQL, encryptedCustomerId) => {
const getOmedaCustomerRecord = async (omedaGraphQLClient, encryptedCustomerId) => {
const variables = { id: encryptedCustomerId };
const { data } = await omedaGraphQL.query({ query: CUSTOMER_QUERY, variables });
const { data } = await omedaGraphQLClient.query({ query: CUSTOMER_QUERY, variables });
return data.customerByEncryptedId;
};

const setOmedaData = async ({ service, user, omedaCustomer }) => {
const setOmedaData = async ({ identityX, user, omedaCustomer }) => {
const input = {
email: user.email,

Expand All @@ -77,14 +76,14 @@ const setOmedaData = async ({ service, user, omedaCustomer }) => {
regionCode: get(omedaCustomer, 'primaryPostalAddress.regionCode'),
postalCode: get(omedaCustomer, 'primaryPostalAddress.postalCode'),
};
return service.client.mutate({
return identityX.client.mutate({
mutation: SET_OMEDA_DATA,
variables: { input },
});
};

const setOmedaDemographics = async ({
service,
identityX,
user,
omedaCustomer,
omedaLinkedFields,
Expand Down Expand Up @@ -118,35 +117,33 @@ const setOmedaDemographics = async ({
answerMap.forEach((optionIdSet, fieldId) => {
answers.push({ fieldId, optionIds: [...optionIdSet] });
});
await service.client.mutate({
await identityX.client.mutate({
mutation: SET_OMEDA_DEMOGRAPHIC_DATA,
variables: { input: { id: user.id, answers } },
context: { apiToken: service.config.getApiToken() },
context: { apiToken: identityX.config.getApiToken() },
});
}
};

module.exports = async ({
brandKey,
productId,
omedaGraphQLProp = '$omeda',
omedaGraphQLProp = '$omedaGraphQLClient',
idxOmedaRapidIdentifyProp = '$idxOmedaRapidIdentify',

req,
service,
service: identityX,
user,
}) => {
const omedaGraphQL = req[omedaGraphQLProp];
if (!omedaGraphQL) throw new Error(`Unable to load the Omeda GraphQL API from the request using prop ${omedaGraphQLProp}`);
const omedaGraphQLClient = req[omedaGraphQLProp];
if (!omedaGraphQLClient) throw new Error(`Unable to load the Omeda GraphQL API from the request using prop ${omedaGraphQLProp}`);
const idxOmedaRapidIdentify = req[idxOmedaRapidIdentifyProp];
if (!idxOmedaRapidIdentify) throw new Error(`Unable to find the IdentityX+Omeda rapid identifier on the request using ${idxOmedaRapidIdentifyProp}`);

// get omeda customer id (via rapid identity) and load omeda custom field data
const [{ data }, { encryptedCustomerId }] = await Promise.all([
service.client.query({ query: FIELD_QUERY }),
rapidIdentify({
brandKey,
productId,
appUser: user.verified ? user : { id: user.id, email: user.email },
identityX: service,
omedaGraphQL,
identityX.client.query({ query: FIELD_QUERY }),
idxOmedaRapidIdentify({
user: user.verified ? user : { id: user.id, email: user.email },
}),
]);

Expand All @@ -170,12 +167,12 @@ module.exports = async ({
}

// find the omeda customer record to "prime" the identity-x user.
const omedaCustomer = await getOmedaCustomerRecord(omedaGraphQL, encryptedCustomerId);
const omedaCustomer = await getOmedaCustomerRecord(omedaGraphQLClient, encryptedCustomerId);
const promises = [];
if (!user.verified) promises.push(setOmedaData({ service, user, omedaCustomer }));
if (!user.verified) promises.push(setOmedaData({ identityX, user, omedaCustomer }));
if (!hasAnsweredAllOmedaQuestions) {
promises.push(setOmedaDemographics({
service,
identityX,
user,
omedaCustomer,
omedaLinkedFields,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
const rapidIdentify = require('../rapid-identify');

module.exports = async ({
brandKey,
productId,
omedaGraphQLProp = '$omeda',

user,
service,
idxOmedaRapidIdentifyProp = '$idxOmedaRapidIdentify',
req,
user,
}) => {
const omedaGraphQL = req[omedaGraphQLProp];
if (!omedaGraphQL) throw new Error(`Unable to load the Omeda GraphQL API from the request using prop ${omedaGraphQLProp}`);
return rapidIdentify({
brandKey,
productId,
appUser: user,
identityX: service,
omedaGraphQL,
});
const idxOmedaRapidIdentify = req[idxOmedaRapidIdentifyProp];
if (!idxOmedaRapidIdentify) throw new Error(`Unable to find the IdentityX+Omeda rapid identifier on the request using ${idxOmedaRapidIdentifyProp}`);
return idxOmedaRapidIdentify({ user });
};
30 changes: 30 additions & 0 deletions packages/marko-web-omeda-identity-x/middleware/rapid-identify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const idxOmedaRapidIdentify = require('../rapid-identify');

module.exports = ({
brandKey,
productId,

prop = '$idxOmedaRapidIdentify',
omedaRapidIdentifyProp = '$omedaRapidIdentify',
}) => {
if (!prop) throw new Error('An Omeda + IdentityX rapid identifcation prop is required.');
if (!omedaRapidIdentifyProp) throw new Error('The Omeda rapid identifcation prop is required.');

return (req, res, next) => {
const omedaRapidIdentify = req[omedaRapidIdentifyProp];
if (!omedaRapidIdentify) throw new Error(`Unable to find the Omeda rapid identifier on the request using ${omedaRapidIdentifyProp}`);

const handler = async ({ user } = {}) => idxOmedaRapidIdentify({
brandKey,
productId,
appUser: user,

identityX: req.identityX,
omedaRapidIdentify,
});

req[prop] = handler;
res.locals[prop] = handler;
next();
};
};
2 changes: 1 addition & 1 deletion packages/marko-web-omeda-identity-x/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"test": "yarn compile && yarn lint"
},
"dependencies": {
"@parameter1/base-cms-marko-web-identity-x": "^2.53.1",
"@parameter1/base-cms-marko-web-omeda": "^2.52.0",
"@parameter1/base-cms-object-path": "^2.45.0",
"@parameter1/base-cms-utils": "^2.22.2",
Expand All @@ -19,7 +20,6 @@
},
"peerDependencies": {
"@parameter1/base-cms-marko-web": "^2.13.0",
"@parameter1/base-cms-marko-web-identity-x": "^2.16.0",
"express": "^4.17.1"
},
"publishConfig": {
Expand Down
20 changes: 5 additions & 15 deletions packages/marko-web-omeda-identity-x/rapid-identify.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@ const ALPHA3_CODE = gql`
}
`;

const RAPID_IDENT = gql`
mutation RapidIdentityX($input: RapidCustomerIdentificationMutationInput!) {
result: rapidCustomerIdentification(input: $input) { id encryptedCustomerId }
}
`;

const getAlpha3CodeFor = async (alpha2, identityX) => {
const { data } = await identityX.client.query({
query: ALPHA3_CODE,
Expand All @@ -29,14 +23,15 @@ const getAlpha3CodeFor = async (alpha2, identityX) => {
* @param {number} params.productId The Omeda product ID to associate with the identification
* @param {object} params.appUser The IdentityX user
* @param {IdentityX} params.identityX The Marko web IdentityX service
* @param {ApolloClient} params.omedaGraphQL The Omeda GraphQL client
* @param {function} params.omedaRapidIdentify The Omeda rapid identifcation action
*/
module.exports = async ({
brandKey,
productId,
appUser,

identityX,
omedaGraphQL,
omedaRapidIdentify,
} = {}) => {
const {
givenName,
Expand Down Expand Up @@ -68,9 +63,9 @@ module.exports = async ({
};
});

const input = {
productId,
const { id, encryptedCustomerId } = await omedaRapidIdentify({
email: appUser.email,
productId,
...(givenName && { firstName: givenName }),
...(familyName && { lastName: familyName }),
...(organization && { companyName: organization }),
Expand All @@ -79,12 +74,7 @@ module.exports = async ({
...(regionCode && { regionCode }),
...(postalCode && { postalCode }),
...(demographics.length && { demographics }),
};
const { data } = await omedaGraphQL.mutate({
mutation: RAPID_IDENT,
variables: { input },
});
const { id, encryptedCustomerId } = data.result;

const namespace = { provider: 'omeda', tenant: brandKey.toLowerCase(), type: 'customer' };
await Promise.all([
Expand Down
Loading

0 comments on commit 4116cd2

Please sign in to comment.