Skip to content

Commit

Permalink
Merge tag 'v1.6.0' of http://10.73.97.24/oecloud.io/oe-workflow into …
Browse files Browse the repository at this point in the history
…release

PI11 Release
  • Loading branch information
vamsee committed Oct 25, 2018
2 parents 032045f + 7d7cd7d commit 5216ddb
Show file tree
Hide file tree
Showing 20 changed files with 768 additions and 10,086 deletions.
5 changes: 4 additions & 1 deletion common/mixins/maker-checker-mixin-v2.js
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,9 @@ function addOERemoteMethods(Model) {
var modelName = Model.definition.name;
var ChangeWorkflowRequest = app.models.ChangeWorkflowRequest;

var inputPV = data.pv;
delete data.pv;

var idName = Model.definition.idName();
var id = data[idName] || 'this_id_wont_exist';
ChangeWorkflowRequest.find({
Expand Down Expand Up @@ -856,7 +859,7 @@ function addOERemoteMethods(Model) {
var mapping = res[0];

let workflowBody = mapping.workflowBody;
workflowBody.processVariables = workflowBody.processVariables || {};
workflowBody.processVariables = Object.assign(workflowBody.processVariables || {}, inputPV);
workflowBody.processVariables._operation = mData.operation;
workflowBody.processVariables._modifiers = mData._modifiers;
workflowBody.processVariables._modelInstance = mData.data;
Expand Down
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
116 changes: 85 additions & 31 deletions common/models/task.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ module.exports = function Task(Task) {
}

function groupMatch(group, candidateGroups, excludedGroups) {
if (candidateGroups.indexOf(group) !== -1 ) {
if (candidateGroups.indexOf(group) !== -1) {
// group found
return 1;
} else if (excludedGroups.indexOf(group) !== -1) {
Expand Down Expand Up @@ -155,7 +155,7 @@ module.exports = function Task(Task) {
}

function userMatch(user, candidateUsers, excludedUsers) {
if (candidateUsers.indexOf(user) !== -1 ) {
if (candidateUsers.indexOf(user) !== -1) {
// user found
return 1;
} else if (excludedUsers.indexOf(user) !== -1) {
Expand Down Expand Up @@ -237,6 +237,14 @@ module.exports = function Task(Task) {
Task.prototype.complete = function complete(data, options, next) {
var self = this;
var tname = self.name;

if (self.status !== 'pending') {
let error = new Error('Task already completed');
error.code = 'TASK_ALREADY_COMPLETED';
error.status = error.statusCode = 409;
return next(error);
}

self.processInstance({}, options, function fetchProcessDef(err, process) {
if (err) {
log.error(options, err);
Expand All @@ -254,7 +262,7 @@ module.exports = function Task(Task) {
if (taskObj.isMultiMaker) {
// this task is a maker user task, so no need to have pv and msg and directly take obj as update
var updates = data;
pdata = {__comments__: data.__comments__};
pdata = { __comments__: data.__comments__ };
if (typeof data.pv !== 'undefined') {
pdata.pv = data.pv;
delete updates.pv;
Expand Down Expand Up @@ -324,7 +332,7 @@ module.exports = function Task(Task) {
log.error(options, err);
return next(err);
}
// process._processVariables._modelInstance = instx;
// process._processVariables._modelInstance = instx;
var xdata = {};
xdata.pv = pdata.pv || {};
xdata.pv._modifiers = modifiers;
Expand All @@ -343,18 +351,18 @@ module.exports = function Task(Task) {
WorkflowManager = loopback.getModel('WorkflowManager', options);
workflowInstanceId = process._processVariables._workflowInstanceId;

if ( typeof data.__action__ === 'undefined' ) {
if (typeof data.__action__ === 'undefined') {
let err = new Error('__action__ not provided. Checker enabled task requires this field.');
log.error(options, err);
return next(err);
}

let validActArr = [ 'approved', 'rejected' ];
if ( taskObj.stepVariables && taskObj.stepVariables.__action__ ) {
let validActArr = ['approved', 'rejected'];
if (taskObj.stepVariables && taskObj.stepVariables.__action__) {
validActArr = validActArr.concat(taskObj.stepVariables.__action__);
}

let isValid = ( validActArr.indexOf(data.__action__) > -1 );
let isValid = (validActArr.indexOf(data.__action__) > -1);
if (!isValid) {
let err = new Error('Provided action is not valid. Possible valid actions : ' + JSON.stringify(validActArr));
log.error(options, err);
Expand Down Expand Up @@ -406,18 +414,18 @@ module.exports = function Task(Task) {
WorkflowManager = loopback.getModel('WorkflowManager', options);
workflowInstanceId = process._processVariables._workflowInstanceId;

if ( typeof data.__action__ === 'undefined' ) {
if (typeof data.__action__ === 'undefined') {
let err = new Error('__action__ not provided. Checker enabled task requires this field.');
log.error(options, err);
return next(err);
}

let validActArr = [ 'approved', 'rejected' ];
if ( self.stepVariables && self.stepVariables.__action__ ) {
let validActArr = ['approved', 'rejected'];
if (self.stepVariables && self.stepVariables.__action__) {
validActArr = validActArr.concat(self.stepVariables.__action__);
}

let isValid = ( validActArr.indexOf(data.__action__) > -1 );
let isValid = (validActArr.indexOf(data.__action__) > -1);
if (!isValid) {
let err = new Error('Provided action is not valid. Possible valid actions : ' + JSON.stringify(validActArr));
log.error(options, err);
Expand Down Expand Up @@ -449,7 +457,7 @@ module.exports = function Task(Task) {
pdata.pv.__action__ = data.__action__;
/* Set __comments__ for updating Remarks*/
options.__comments__ = data.__comments__;
if (['approved', 'rejected'].indexOf(data.__action__) > -1 ) {
if (['approved', 'rejected'].indexOf(data.__action__) > -1) {
WorkflowManager.endAttachWfRequest(postData, options, function completeMakerCheckerRequest(err, res) {
delete options.__comments__;
if (err) {
Expand Down Expand Up @@ -533,7 +541,7 @@ module.exports = function Task(Task) {
}
}
// self.status = status;
var updates = {'status': status, comments: data.__comments__, '_version': self._version};
var updates = { 'status': status, comments: data.__comments__, '_version': self._version };
self.updateAttributes(updates, options, function saveTask(saveError, instance) {
if (err || saveError) {
log.error(options, err, saveError);
Expand All @@ -547,20 +555,30 @@ module.exports = function Task(Task) {


/**
* REST endpoint for completing User-Task
* REST endpoint for assigning task to another user or role.
* @param {Object} data Process-Variables & Message data
* @param {Object} options Options
* @param {Function} next Callback
* @returns {void}
*/
Task.prototype.delegate = function delegate(data, options, next) {
var self = this;
var updates = {
'candidateUsers': [],
'candidateRoles': [],
'candidateGroups': [],
'id': self.id,
'_version': self._version
};

var assignee;
if (data && data.assignee) {
assignee = data.assignee;
updates.candidateUsers = [data.assignee];
} else if (data && data.role) {
updates.candidateRoles = [data.role];
} else if (data && data.group) {
updates.candidateRoles = [data.group];
} else {
var error = new Error('Assignee is required to delegate task.');
var error = new Error('Assignee/role/group is required to delegate task.');
log.error(options, error);
return next(error);
}
Expand All @@ -571,19 +589,7 @@ module.exports = function Task(Task) {
return next(errorx);
}

var updates = {
'candidateUsers': [
assignee
],
'candidateRoles': [],
'candidateGroups': [],
'excludedUsers': [],
'excludedRoles': [],
'excludedGroups': [],
'id': self.id,
'_version': self._version
};

data.comments && (updates.comments = data.comments);
self.updateAttributes(updates, options, function cb(err, inst) {
if (err) {
log.error(options, err);
Expand All @@ -593,6 +599,33 @@ module.exports = function Task(Task) {
});
};

/**
* REST endpoint for updating user comments
* @param {objet} data user comments
* @param {Object} options Options
* @param {Function} next Callback
* @returns {void}
*/
Task.prototype.updateComments = function comments(data, options, next) {
if (data && data.comments) {
var updates = {
_version: this._version,
comments: data.comments
};
} else {
var error = new Error('comments are required for update');
return next(error);
}

this.updateAttributes(updates, options, function updateAttributesCbFn(err, data) {
if (err) {
next(err);
} else {
next(null, data);
}
});
};

Task.remoteMethod('completeTask', {
accessType: 'WRITE',
accepts: [
Expand Down Expand Up @@ -661,4 +694,25 @@ module.exports = function Task(Task) {
root: true
}
});

Task.remoteMethod('updateComments', {
accessType: 'WRITE',
accepts: {
arg: 'data',
type: 'object',
required: true,
description: 'Task instance',
http: { source: 'body' }
},
description: 'Sends a request to update task comments',
http: {
verb: 'put',
path: '/updateComments/'
},
isStatic: false,
returns: {
type: 'object',
root: true
}
});
};
Loading

0 comments on commit 5216ddb

Please sign in to comment.