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

Commit

Permalink
Add Joi valildation to middleware, hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
solocommand committed Jul 26, 2022
1 parent 1d1faec commit 1caed66
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 56 deletions.
52 changes: 31 additions & 21 deletions packages/marko-web-omeda-identity-x/add-integration-hooks.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@

const Joi = require('@parameter1/joi');
const { validate } = require('@parameter1/joi/utils');
const { get } = require('@parameter1/base-cms-object-path');
const IdXConfig = require('@parameter1/base-cms-marko-web-identity-x/config');
const {
onAuthenticationSuccess,
onLoginLinkSent,
Expand All @@ -11,25 +14,33 @@ const {
* @param {object} params
* @param {IdentityXConfiguration} params.idxConfig
*/
module.exports = ({
idxConfig,
brandKey,
omedaGraphQLProp = '$omedaGraphQLClient',
idxOmedaRapidIdentifyProp = '$idxOmedaRapidIdentify',
} = {}) => {
if (!idxConfig) throw new Error('The IdentityX configuration instances is required to add Omeda+IdentityX integration hooks.');
module.exports = (params = {}) => {
const {
idxConfig,
brandKey,
omedaGraphQLClientProp,
idxOmedaRapidIdentifyProp,
omedaPromoCodeCookieName,
omedaPromoCodeDefault,
} = validate(Joi.object({
brandKey: Joi.string().required(),
idxConfig: Joi.object().instance(IdXConfig).required(),
idxOmedaRapidIdentifyProp: Joi.string().required(),
omedaGraphQLClientProp: Joi.string().required(),
omedaPromoCodeCookieName: Joi.string().required(),
omedaPromoCodeDefault: Joi.string(),
}), params);

idxConfig.addHook({
name: 'onLoginLinkSent',
shouldAwait: false,
fn: async args => onLoginLinkSent({
...args,
brandKey,
omedaGraphQLProp,
idxOmedaRapidIdentifyProp,

req: args.req,
service: args.service,
user: args.user,
idxOmedaRapidIdentify: get(args, `req.${idxOmedaRapidIdentifyProp}`),
omedaGraphQLClient: get(args, `req.${omedaGraphQLClientProp}`),
omedaPromoCodeCookieName,
omedaPromoCodeDefault,
}),
});

Expand All @@ -39,9 +50,6 @@ module.exports = ({
fn: async args => onAuthenticationSuccess({
...args,
brandKey,
res: args.res,
req: args.req,
user: args.user,
}),
});

Expand All @@ -50,16 +58,18 @@ module.exports = ({
shouldAwait: false,
fn: async args => onUserProfileUpdate({
...args,
idxOmedaRapidIdentifyProp,
req: args.req,
user: args.user,
idxOmedaRapidIdentify: get(args, `req.${idxOmedaRapidIdentifyProp}`),
omedaPromoCodeCookieName,
omedaPromoCodeDefault,
}),
});

idxConfig.addHook({
name: 'onLogout',
shouldAwait: true,
fn: async ({ res }) => onLogout({ res }),
fn: async args => onLogout({
...args,
}),
});

return idxConfig;
Expand Down
8 changes: 5 additions & 3 deletions packages/marko-web-omeda-identity-x/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,12 @@ module.exports = (app, params = {}) => {

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

// attach the identity-x rapid identification wrapper middleware
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
const Joi = require('@parameter1/joi');
const { validate } = require('@parameter1/joi/utils');
const olyticsCookie = require('@parameter1/base-cms-marko-web-omeda/olytics/customer-cookie');
const findEncryptedId = require('../external-id/find-encrypted-customer-id');

module.exports = async ({
brandKey,
user,
res,
}) => {
module.exports = async (params = {}) => {
const {
brandKey,
user,
res,
} = validate(Joi.object({
brandKey: Joi.string().required(),
user: Joi.object().required(),
res: Joi.object().required(),
}).unknown(true), params);
const encryptedId = findEncryptedId({ externalIds: user.externalIds, brandKey });
if (!encryptedId) return;
olyticsCookie.setTo(res, encryptedId);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const Joi = require('@parameter1/joi');
const { validate } = require('@parameter1/joi/utils');
const extractPromoCode = require('../utils/extract-promo-code');
const {
getAnsweredQuestionMap,
Expand All @@ -6,22 +8,28 @@ const {
updateIdentityX,
} = require('../omeda-data');

module.exports = async ({
brandKey,
omedaGraphQLProp = '$omedaGraphQLClient',
idxOmedaRapidIdentifyProp = '$idxOmedaRapidIdentify',
omedaPromoCodeCookieName = 'omeda_promo_code',
omedaPromoCodeDefault,
promoCode: hookDataPromoCode,

req,
service: identityX,
user,
}) => {
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}`);
module.exports = async (params = {}) => {
const {
brandKey,
idxOmedaRapidIdentify,
omedaGraphQLClient,
omedaPromoCodeCookieName,
omedaPromoCodeDefault,
promoCode: hookDataPromoCode,
req,
service: identityX,
user,
} = validate(Joi.object({
brandKey: Joi.string().required(),
idxOmedaRapidIdentify: Joi.function().required(),
omedaGraphQLClient: Joi.object().required(),
omedaPromoCodeCookieName: Joi.string().required(),
omedaPromoCodeDefault: Joi.string(),
promoCode: Joi.string(),
req: Joi.object().required(),
service: Joi.object().required(),
user: Joi.object().required(),
}).unknown(), params);

const promoCode = extractPromoCode({
promoCode: hookDataPromoCode,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
const Joi = require('@parameter1/joi');
const { validate } = require('@parameter1/joi/utils');
const olyticsCookie = require('@parameter1/base-cms-marko-web-omeda/olytics/customer-cookie');

module.exports = ({ res }) => {
module.exports = (params = {}) => {
const {
res,
} = validate(Joi.object({
res: Joi.object().required(),
}).unknown(), params);
olyticsCookie.clearFrom(res);
};
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
const Joi = require('@parameter1/joi');
const { validate } = require('@parameter1/joi/utils');
const extractPromoCode = require('../utils/extract-promo-code');

module.exports = async ({
idxOmedaRapidIdentifyProp = '$idxOmedaRapidIdentify',
omedaPromoCodeCookieName = 'omeda_promo_code',
omedaPromoCodeDefault,
req,
user,
promoCode: hookDataPromoCode,
}) => {
const idxOmedaRapidIdentify = req[idxOmedaRapidIdentifyProp];
if (!idxOmedaRapidIdentify) throw new Error(`Unable to find the IdentityX+Omeda rapid identifier on the request using ${idxOmedaRapidIdentifyProp}`);
module.exports = async (params = {}) => {
const {
idxOmedaRapidIdentify,
omedaPromoCodeCookieName,
omedaPromoCodeDefault,
promoCode: hookDataPromoCode,
req,
user,
} = validate(Joi.object({
idxOmedaRapidIdentify: Joi.function().required(),
omedaPromoCodeCookieName: Joi.string().required(),
omedaPromoCodeDefault: Joi.string(),
promoCode: Joi.string(),
req: Joi.object().required(),
user: Joi.object().required(),
}).unknown(), params);

const promoCode = extractPromoCode({
promoCode: hookDataPromoCode,
Expand Down

0 comments on commit 1caed66

Please sign in to comment.