Skip to content

Commit

Permalink
commit for version 2.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
vamsee committed Apr 30, 2020
2 parents 0651310 + 8256a3b commit e4e92bf
Show file tree
Hide file tree
Showing 16 changed files with 462 additions and 246 deletions.
13 changes: 7 additions & 6 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/client/
/coverage/
/node_modules/
server/dropdb.js
/lib/expression-language/expression-syntax-parser.js
/test/
build/
client/
coverage/
node_modules/
test/
drop.js
Gruntfile.js
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@
"no-undefined": 1, // http://eslint.org/docs/rules/no-undefined
"no-with": 2, // http://eslint.org/docs/rules/no-with
"handle-callback-err": 1, // http://eslint.org/docs/rules/handle-callback-err
"radix": 2, // http://eslint.org/docs/rules/radix
"radix": 0, // http://eslint.org/docs/rules/radix
"wrap-iife": [2, "any"], // http://eslint.org/docs/rules/wrap-iife
"yoda": 2, // http://eslint.org/docs/rules/yoda

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ out/
*.zip
/common/models/test
package-lock.json
oracle-user.sh
7 changes: 2 additions & 5 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module.exports = function GruntConfig(grunt) {
// Project configuration.
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),

clean: {
coverage: {
src: ['coverage/']
Expand Down Expand Up @@ -39,12 +40,8 @@ module.exports = function GruntConfig(grunt) {
});

// Add the grunt-mocha-test tasks.
grunt.loadNpmTasks('grunt-mocha-test');
grunt.loadNpmTasks('grunt-mocha-istanbul');
grunt.loadNpmTasks('grunt-contrib-clean');

grunt.loadNpmTasks('grunt-mkdir');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-mocha-istanbul');

grunt.registerTask('test-with-coverage', ['clean:coverage', 'mocha_istanbul']);
};
61 changes: 59 additions & 2 deletions lib/service-personalizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ var exprLang = require('oe-expression/lib/expression-language.js');
var mergeQuery = require('loopback-datasource-juggler/lib/utils').mergeQuery;
var logger = require('oe-logger');
var log = logger('service-personalizer');
var customFunction;


/**
* This function returns the necessary sorting logic to
Expand Down Expand Up @@ -130,6 +132,12 @@ var applyPersonalizationRule = function applyPersonalizationRuleFn(ctx, p13nRule
});
// arr.push(async.apply(addSort, ctx, p13nRule[instruction]));
break;
case 'postCustomFunction':
arr.push({
type: 'postCustomFunction',
fn: async.apply(executeCustomFunction, ctx, p13nRule[instruction])
});
break;
case 'union':
// unionResults(ctx, p13nRule[instruction]);
break;
Expand All @@ -151,7 +159,7 @@ var applyPersonalizationRule = function applyPersonalizationRuleFn(ctx, p13nRule
}
}
}
return arr.sort(sortFactoryFn()).map(x => x.fn);
return arr.sort(sortFactoryFn());
};

function execute(arr, callback) {
Expand Down Expand Up @@ -196,6 +204,12 @@ var applyReversePersonalizationRule = function applyReversePersonalizationRuleFn
});
// arr.push(async.apply(addReverseFieldValueReplace, ctx, p13nRule[instruction]));
break;
case 'preCustomFunction':
arr.push({
type: 'preCustomFunction',
fn: async.apply(executeCustomFunction, ctx, p13nRule[instruction])
});
break;
default:
}
}
Expand Down Expand Up @@ -441,6 +455,26 @@ function reverseFieldValueReplacementFn(ctx, rule, cb) {
}
}

// old code
function executeCustomFunctionFn(ctx, customFunctionName) {
// TODO: Security check
// var custFn = new Function('ctx', customFunction);
var custFn = function customFnn(ctx, customFunction) {
customFunction(ctx);
};

log.debug(ctx.options, 'function - ', customFunction);
custFn(ctx);
}

// execute custom function
function executeCustomFunction(ctx, instruction, cb) {
let customFunctionName = instruction.functionName;
customFunction[customFunctionName](ctx);
cb();
}


/**
* Processes a 'filter' instruction. This method checks if underlying datasource to which the model is attached to
* supports query based filtering. If yes, it adds a 'where' clause in the query. Otherwise creates a post processing
Expand Down Expand Up @@ -605,6 +639,27 @@ function addSort(ctx, instruction, cb) {
}
}

/**
* Custom function
*/
// getCustom function
function loadCustomFunction(fnCache) {
customFunction = fnCache;
}

function getCustomFunction() {
return customFunction;
}

/* eslint-disable */
function addCustomFunction(ctx, instruction, cb) {
// instruction has the customFunction Name
// Datasource does not support field name replacement. Add it as a post processing function
addPostProcessingFunction(ctx, 'customFunction', instruction, executeCustomFunctionFn);
cb();
}
/* eslint-enable */

function createOrderExp(instruction, tempKeys) {
if (!Array.isArray(instruction)) {
instruction = [instruction];
Expand Down Expand Up @@ -797,5 +852,7 @@ module.exports = {
getPersonalizationRuleForModel: getPersonalizationRuleForModel,
applyPersonalizationRule: applyPersonalizationRule,
applyReversePersonalizationRule: applyReversePersonalizationRule,
execute: execute
execute: execute,
loadCustomFunction,
getCustomFunction
};
16 changes: 6 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "oe-service-personalization",
"version": "2.1.0",
"version": "2.2.0",
"description": "oe-cloud modularization project",
"engines": {
"node": ">=6"
Expand All @@ -24,25 +24,21 @@
"devDependencies": {
"babel-eslint": "7.2.3",
"chai": "3.4.1",
"chai-datetime": "1.4.0",
"chai-things": "0.2.0",
"chalk": "1.1.1",
"eslint": "4.10.0",
"grunt": "1.0.4",
"grunt-banner": "0.6.0",
"grunt-cli": "1.3.2",
"grunt-contrib-clean": "2.0.0",
"grunt-contrib-copy": "1.0.0",
"grunt-jsbeautifier": "0.2.13",
"grunt-mkdir": "1.0.0",
"grunt-mocha-istanbul": "5.0.2",
"grunt-mocha-test": "0.13.3",
"istanbul": "0.4.5",
"md5": "^2.2.1",
"mocha": "5.2.0",
"oe-connector-mongodb": "^2.0.0",
"oe-connector-oracle": "^2.0.0",
"oe-connector-postgresql": "^2.0.0",
"superagent-defaults": "0.1.14",
"supertest": "3.4.2",
"loopback-connector-mongodb": "3.9.2",
"oe-connector-postgresql": "^2.0.0"
"supertest": "3.4.2"
},
"repository": {
"type": "git",
Expand Down
33 changes: 31 additions & 2 deletions server/boot/service-personalization.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ var personalizationRuleModel;
module.exports = function ServicePersonalization(app, cb) {
log.debug(log.defaultContext(), 'In service-personalization.js boot script.');
personalizationRuleModel = app.models.PersonalizationRule;
// requiring customFunction
let servicePersoConfig = app.get('servicePersonalization');
if (servicePersoConfig && 'customFunctionPath' in servicePersoConfig) {
try {
servicePersonalizer.loadCustomFunction(require(servicePersoConfig.customFunctionPath));
} catch (e) {
log.error(log.defaultContext(), 'require customFunction', e);
}
}
// Creating 'before save' and 'after save' observer hooks for PersonlizationRule model
personalizationRuleModel.observe('before save', personalizationRuleBeforeSave);
personalizationRuleModel.observe('after save', personalizationRuleAfterSave);
Expand Down Expand Up @@ -80,7 +89,21 @@ function personalizationRuleBeforeSave(ctx, next) {
// It is good to have if we have a declarative way of validating model existence.
var modelName = data.modelName;
if (loopback.findModel(modelName, ctx.options)) {
next();
var nextFlag = true;
if (data.personalizationRule.postCustomFunction) {
if (!(Object.keys(servicePersonalizer.getCustomFunction()).indexOf(data.personalizationRule.postCustomFunction.functionName) > -1) && nextFlag) {
next(new Error('Module \'' + data.personalizationRule.postCustomFunction.functionName + '\' doesn\'t exists.'));
nextFlag = false;
}
}
if (data.personalizationRule.preCustomFunction) {
if (!(Object.keys(servicePersonalizer.getCustomFunction()).indexOf(data.personalizationRule.preCustomFunction.functionName) > -1) && nextFlag) {
next(new Error('Module \'' + data.personalizationRule.precustomFunction.functionName + '\' doesn\'t exists.'));
}
}
if (nextFlag) {
next();
}
} else {
// Not sure it is the right way to construct error object to sent in the response.
var err = new Error('Model \'' + modelName + '\' doesn\'t exists.');
Expand Down Expand Up @@ -257,6 +280,8 @@ function beforeRemoteFindHook(model) {
if (rule !== null && typeof rule !== 'undefined') {
log.debug(ctx.req.callContext, 'beforeRemoteFindHook personalization rule found , rule: ', rule);
var fns = servicePersonalizer.applyPersonalizationRule(ctx, rule.personalizationRule);
fns = fns.filter(x => x.type !== 'postCustomFunction');
fns = fns.map(x => x.fn);
servicePersonalizer.execute(fns, function (err) {
if (err) {
return next(err);
Expand All @@ -279,6 +304,8 @@ function beforeRemoteFindOneHook(model) {
if (rule !== null && typeof rule !== 'undefined') {
log.debug(ctx.req.callContext, 'beforeRemoteFindOneHook personalization rule found , rule: ', rule);
var fns = servicePersonalizer.applyPersonalizationRule(ctx, rule.personalizationRule);
fns = fns.filter(x => x.type !== 'postCustomFunction');
fns = fns.map(x => x.fn);
servicePersonalizer.execute(fns, function (err) {
if (err) {
return next(err);
Expand All @@ -301,6 +328,8 @@ function beforeRemoteFindByIdHook(model) {
if (rule !== null && typeof rule !== 'undefined') {
log.debug(ctx.req.callContext, 'beforeRemoteFindByIdHook personalization rule found , rule: ', rule);
var fns = servicePersonalizer.applyPersonalizationRule(ctx, rule.personalizationRule);
fns = fns.filter(x => x.type !== 'postCustomFunction');
fns = fns.map(x => x.fn);
servicePersonalizer.execute(fns, function (err) {
if (err) {
return next(err);
Expand Down Expand Up @@ -329,9 +358,9 @@ function afterRemotePersonalizationExec(model, ctx, next) {
if (rule !== null && typeof rule !== 'undefined') {
log.debug(ctx.req.callContext, 'afterRemotePersonalizationExec personalization rule found , rule: ', rule);
log.debug(ctx.req.callContext, 'applying PersonalizationRule now');

log.debug(ctx.req.callContext, 'beforeRemoteFindHook personalization rule found , rule: ', rule);
var fns = servicePersonalizer.applyPersonalizationRule(ctx, rule.personalizationRule);
fns = fns.map(x => x.fn);
servicePersonalizer.execute(fns, function (err) {
if (err) {
return next(err);
Expand Down
5 changes: 4 additions & 1 deletion test/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,8 @@
"cors": false,
"handleErrors": false
},
"legacyExplorer": false
"legacyExplorer": false,
"servicePersonalization" : {
"customFunctionPath": "D:\\Repos\\oecloud.io\\oe-service-personalization_master\\test\\customFunction"
}
}
14 changes: 14 additions & 0 deletions test/config.local.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
var config = require('./config.json');

// add customFunctionPath to config if environment variable is present
if (process.env.custom_function_path && !config.servicePersonalization) {
config.servicePersonalization = new Object({
customFunctionPath: process.env.custom_function_path
});
} else if (process.env.custom_function_path && config.servicePersonalization) {
config.servicePersonalization.customFunctionPath = process.env.custom_function_path;
} else {
config.servicePersonalization.customFunctionPath = config.servicePersonalization.customFunctionPath;
}

module.exports = config;
23 changes: 23 additions & 0 deletions test/customFunction/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
var md5 = require('md5');

function customFn(ctx) {
if (ctx.result && ctx.result.length > 1) {
ctx.result = ctx.result.map((item) => {
item.name = md5(item.name);
return item;
});
} else {
ctx.result.name = md5(ctx.result.name);
}
}

function hashReqBody(ctx) {
if (ctx.req.body) {
ctx.req.body.name = md5(ctx.req.body.name);
}
}

module.exports = {
customFn,
hashReqBody
};
2 changes: 1 addition & 1 deletion test/datasources.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"database": "oe-service-personalization-test",
"password": "admin",
"name": "db",
"connector": "mongodb",
"connector": "oe-connector-mongodb",
"user": "admin",
"connectionTimeout": 500000
}
Expand Down
34 changes: 16 additions & 18 deletions test/datasources.mongo.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,25 @@
*
*/
var mongoHost = process.env.MONGO_HOST || 'localhost';
var mongoPort = process.env.MONGO_PORT ? parseInt(process.env.MONGO_PORT) : 27017;
var dbName = process.env.DB_NAME || 'oe-service-personalization-test';
module.exports =
{
"memdb": {
"name": "memdb",
"connector": "memory"
module.exports = {
'memdb': {
'name': 'memdb',
'connector': 'memory'
},
"transient": {
"name": "transient",
"connector": "transient"
'transient': {
'name': 'transient',
'connector': 'transient'
},
"db": {
"host": mongoHost,
"port": 27017,
"url": "mongodb://" + mongoHost + ":27017/" + dbName,
"database": dbName,
"password": "admin",
"name": "db",
"connector": "mongodb",
"user": "admin",
"connectionTimeout": 500000
'db': {
'host': mongoHost,
'port': mongoPort,
'url': 'mongodb://' + mongoHost + ':' + mongoPort + '/' + dbName,
'database': dbName,
'name': 'db',
'connector': 'oe-connector-mongodb',
'connectionTimeout': 500000
}
};

10 changes: 2 additions & 8 deletions test/datasources.oracle.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,10 @@
* Bangalore, India. All Rights Reserved.
*
*/
/**
**
** ©2016-2017 EdgeVerve Systems Limited (a fully owned Infosys subsidiary),
** Bangalore, India. All Rights Reserved.
**
**/

var oracleSID = process.env.ORACLE_SID || 'orclpdb.ad.infosys.com';
var oracleSID = process.env.ORACLE_SID || 'ORCLCDB';
var oracleHost = process.env.ORACLE_HOST || 'localhost';
var oraclePort = process.env.ORACLE_PORT || 1521;
var oraclePort = process.env.ORACLE_PORT ? parseInt(process.env.ORACLE_PORT) : 1521;
var oracleUserName = process.env.ORACLE_USERNAME || 'oeadmin';
var oracleUserPassword = process.env.ORACLE_PASSWORD || 'oeadmin';

Expand Down
Loading

0 comments on commit e4e92bf

Please sign in to comment.