Skip to content

Commit

Permalink
Merge branch 'connector' into 'master'
Browse files Browse the repository at this point in the history
oeConnector pass multiple arguments as array

See merge request !25
  • Loading branch information
kvsrohit committed Aug 23, 2018
2 parents 42d908a + 2e89dc7 commit 7d7cd7d
Show file tree
Hide file tree
Showing 15 changed files with 67 additions and 10,053 deletions.
67 changes: 47 additions & 20 deletions common/models/lib/workflow-nodes/service-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ module.exports.run = function run(options, flowObject, message, process, token,
var evaluateJSON = function evaluateJSON(data, incomingMsg, process, options) {
var sandbox = {
msg: incomingMsg,
options: options,
pv: function pv(name) {
if (name === 'accessToken') {
return options.accessToken;
Expand Down Expand Up @@ -87,7 +88,7 @@ module.exports.evaluateJSON = evaluateJSON;
*/
function evaluateFTConnector(options, flowObject, message, process, done) {
var variableType = flowObject.variableType;
var status = 'approved';
var status = 'approved';

if (variableType === 'ProcessVariable' || variableType === 'processvariable') {
status = process._processVariables[flowObject.variableValue];
Expand Down Expand Up @@ -208,7 +209,7 @@ function makeRESTCalls(urlOptions, retry, callback) {
message.statusMessage = response.statusMessage;
}

if (response && response.statusCode >= 500 && retry > 0 ) {
if (response && response.statusCode >= 500 && retry > 0) {
log.debug(log.defaultContext(), 'making a retry attempt to url : ' + urlOptions.url);
makeRESTCalls(urlOptions, retry - 1, callback);
} else {
Expand Down Expand Up @@ -243,7 +244,7 @@ function makeRESTCalls(urlOptions, retry, callback) {
function evaluateOEConnector(options, flowObject, message, process, done) {
var modelName = evaluateProp(flowObject.props.model, message, process, options);
// var modelName = flowObject.props.model;
var operation = flowObject.props.method;
var operationName = flowObject.props.method;
try {
var model = loopback.getModel(modelName, options);
} catch (err) {
Expand All @@ -253,34 +254,60 @@ function evaluateOEConnector(options, flowObject, message, process, done) {
});
}
var data = flowObject.props.data || {};
if (operation && model && model[operation]) {
data = evaluateJSON(data, message, process, options);
model[operation](data[0], options, function evalCB(err, res) {
if (operationName && model && model[operationName]) {
let operationArguments = evaluateJSON(data, message, process, options);

let operation = model[operationName];

let evalCB = function evalCB(err, result) {
if (err) {
log.error(options, err);
return done(null, {
error: err
});
}
var result = res;
if (result && typeof result === 'object' && result.constructor.name !== 'Array') {
if (typeof result.toObject !== 'undefined') {
return done(null, result.toObject());
}
return done(null, result);
}
var _res = [];
for (var i = 0; i < res.length; i++) {
_res.push(res[i].toObject());

if (result && Array.isArray(result)) {
result = result.map(v => {
return (typeof v.toObject === 'function') ? v.toObject() : v;
});
} else if (result && typeof result === 'object' && typeof result.toObject === 'function') {
result = result.toObject();
}
return done(null, _res);
});
return done(null, result);
};

if (!Array.isArray(operationArguments)) {
operationArguments = [operationArguments];
}
operationArguments.push(evalCB);
operation.apply(model, operationArguments);
// model[operation](data[0], options, function evalCB(err, res) {
// if (err) {
// log.error(options, err);
// return done(null, {
// error: err
// });
// }
// var result = res;
// if (result && typeof result === 'object' && result.constructor.name !== 'Array') {
// if (typeof result.toObject !== 'undefined') {
// return done(null, result.toObject());
// }
// return done(null, result);
// }
// var _res = [];
// for (var i = 0; i < res.length; i++) {
// _res.push(res[i].toObject());
// }
// return done(null, _res);
// });
} else {
return done(null, {error: new Error('Invalid operation ' + operation + ' on model ' + modelName )});
return done(null, { error: new Error('Invalid operation ' + operationName + ' on model ' + modelName) });
}
}


// eslint-disable-next-line no-unused-vars
function evaluateProp(data, incomingMsg, process, options) {
// check if prop needs to be evaluated
if (data && (data.indexOf('pv(') > -1 || data.indexOf('msg(') > -1)) {
Expand Down
Loading

0 comments on commit 7d7cd7d

Please sign in to comment.