Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
vamsee committed Oct 6, 2020
2 parents e4e92bf + 9283700 commit 81a2c48
Show file tree
Hide file tree
Showing 20 changed files with 3,369 additions and 1,087 deletions.
830 changes: 825 additions & 5 deletions README.md

Large diffs are not rendered by default.

55 changes: 55 additions & 0 deletions common/mixins/service-personalization-mixin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
*
* ©2016-2020 EdgeVerve Systems Limited (a fully owned Infosys subsidiary),
* Bangalore, India. All Rights Reserved.
*
*/

/**
* This mixin will attach beforeRemote and afterRemote
* hooks and decide if the data needs to be service
* personalized.
*
* Therefore, it is necessary to enable the mixin
* configuration on the corresponding model definition,
* even if it does not directly participate in the
* service personalization (viz is the case with any
* form of relations - or related models).
*
* This will only personalize data for the remote endpoints.
*/


const logger = require('oe-logger');
const log = logger('service-personalization-mixin');
const { runPersonalizations } = require('./../../lib/service-personalizer');
const { slice } = require('./../../lib/utils');

module.exports = function ServicePersonalizationMixin(TargetModel) {
log.debug(log.defaultContext(), `Applying service personalization for ${TargetModel.definition.name}`);
TargetModel.afterRemote('**', function ServicePersonalizationAfterRemoteHook() {
let args = slice(arguments);
let ctx = args[0];
let next = args[args.length - 1];
// let callCtx = ctx.req.callContext;
log.debug(ctx, `afterRemote: (enter) MethodString: ${ctx.methodString}`);
runPersonalizations(ctx, false, function (err) {
log.debug(ctx, `afterRemote: (leave${err ? '- with error' : ''}) MethodString: ${ctx.methodString}`);
next(err);
});
});

TargetModel.beforeRemote('**', function ServicePersonalizationBeforeRemoteHook() {
let args = slice(arguments);
let ctx = args[0];
let next = args[args.length - 1];

log.debug(ctx, `beforeRemote: (enter) MethodString: ${ctx.methodString}`);

// let ctxInfo = parseMethodString(ctx.methodString);
runPersonalizations(ctx, true, function (err) {
log.debug(ctx, `beforeRemote: (leave${err ? '- with error' : ''}) MethodString: ${ctx.methodString}`);
next(err);
});
});
};
8 changes: 7 additions & 1 deletion common/models/personalization-rule.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,16 @@
"personalizationRule": {
"type": "object",
"required": true
},
"methodName" : {
"type": "string",
"default": "**",
"description": "The model methodName this rule should apply to. Should be the methodName (static/instance) or wildcards you specify in a afterRemote()/beforeRemote(). Default '**'"
}
},
"validations": [],
"relations": {},
"acls": [],
"methods": {}
"methods": {},
"mixins": {}
}
25 changes: 25 additions & 0 deletions lib/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const { performServicePersonalizations, applyServicePersonalization } = require('./service-personalizer');
module.exports = {
/**
* Standard api for personalization
*/
performServicePersonalizations,

/**
* Api for personalization. Rules can
* be manually passed as arguments to
* this function.
*
* @param {string} modelName - the model name.
* @param {*} data - object or array
* @param {array} personalizationRecords - the personalization rule as an array.
* @param {object} options - personalization options
* @param {function} done - callback to signal completion. Takes only one argument - error.
* @returns {undefined} - nothing
*/
applyServicePersonalization: function applyServicePersonalizationWrapper(modelName, data, personalizationRecords, options, done) {
let { context } = options;
context._personalizationCache = {};
applyServicePersonalization(modelName, data, personalizationRecords, options, done);
}
};
Loading

0 comments on commit 81a2c48

Please sign in to comment.