Skip to content
This repository has been archived by the owner on Jan 7, 2020. It is now read-only.

Jsher pcpsave merge #42

Merged
merged 3 commits into from
Mar 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions modules/core/server/controllers/core.dbmodel.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -1035,14 +1035,14 @@ _.extend (DBModel.prototype, {
return new Promise (function (resolve, reject) {
if (self.err) return reject (self.err);
var q = _.extend ({}, self.baseQ, query);
console.log ('paginate.q = ' + JSON.stringify(q, null, 4));
console.log ('paginate.and = ' + JSON.stringify(and, null, 4));
console.log ('paginate.sort = ' + JSON.stringify(sort, null, 4));
console.log ('paginate.skip = ' + skip);
console.log ('paginate.limit = ' + limit);
console.log ('paginate.populate = ' + JSON.stringify(populate, null, 4));
console.log ('paginate.fields = ' + JSON.stringify(fields, null, 4));
console.log ('paginate.decorateCollection = ' + self.decorateCollection);
// console.log ('paginate.q = ' + JSON.stringify(q, null, 4));
// console.log ('paginate.and = ' + JSON.stringify(and, null, 4));
// console.log ('paginate.sort = ' + JSON.stringify(sort, null, 4));
// console.log ('paginate.skip = ' + skip);
// console.log ('paginate.limit = ' + limit);
// console.log ('paginate.populate = ' + JSON.stringify(populate, null, 4));
// console.log ('paginate.fields = ' + JSON.stringify(fields, null, 4));
// console.log ('paginate.decorateCollection = ' + self.decorateCollection);

self.model.find(q)
.and(and)
Expand All @@ -1053,13 +1053,13 @@ _.extend (DBModel.prototype, {
.select(fields)
.exec(function(error, data) {
if (!error) {
console.log('search.completed, get total count');
// console.log('search.completed, get total count');
self.model.find(q).and(and).count(function(e,c) {
if (e) {
console.log('search.count.error = ' + JSON.stringify(e));
self.complete(reject, 'search');
} else {
console.log('search.count.completed. total = ', c);
// console.log('search.count.completed. total = ', c);
resolve({data: data, count: c});
}
});
Expand Down
44 changes: 33 additions & 11 deletions modules/project-comments/client/config/commentperiod.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,35 @@ angular.module('comment').config(['$stateProvider', 'moment', "_", function ($st
period.periodType = 'Public';
period.commenterRoles = ['public'];

// store these, if they change, we need to update all child comment permissions...
var originalPeriodRoles = {
read: period.read,
write: period.write,
delete: period.delete,
commenterRoles: period.commenterRoles,
vettingRoles: period.vettingRoles,
classificationRoles: period.classificationRoles
};

var rolesChanged = function(period) {
var periodRoles = {
read: period.read,
write: period.write,
delete: period.delete,
commenterRoles: period.commenterRoles,
vettingRoles: period.vettingRoles,
classificationRoles: period.classificationRoles
};
return (JSON.stringify(originalPeriodRoles) !== JSON.stringify(periodRoles));
};

createEditCommonSetup($timeout, $scope, period, project, CodeLists);

// ESM-761: for edit - don't show project-system-admin roles for vet and classify..
period.vettingRoles = _.without(period.vettingRoles, 'project-system-admin');
period.classificationRoles = _.without(period.classificationRoles, 'project-system-admin');

$scope.busy = false;
$scope.hasErrors = false;
$scope.errorMessage = '';

Expand All @@ -236,26 +259,25 @@ angular.module('comment').config(['$stateProvider', 'moment', "_", function ($st
$scope.hasErrors = true;
$scope.errorMessage = 'Post, Vet and Classify Comments roles are all required. See Roles & Permissions tab.';
} else {
$scope.busy = true;

CommentPeriodModel.save($scope.period)
.then(function (model) {
// console.log ('period was saved',model);
// save the comments so that we pick up the (potential) changes to the period permissions...
return CommentModel.getAllCommentsForPeriod(model._id);
})
.then(function (comments) {
Promise.resolve()
.then(function () {
return comments.reduce(function (current, value, index) {
return CommentModel.save(value);
}, Promise.resolve());
});
if (!rolesChanged(model)) {
return;
} else {
// console.log ('period was saved, roles changed');
// save the comments so that we pick up the (potential) changes to the period permissions...
return CommentModel.commentPeriodCommentsSync(project._id, model._id, period.stats.total);
}
}).then(function () {
$scope.busy = false;
$state.transitionTo('p.commentperiod.list', {projectid: project.code}, {
reload: true, inherit: false, notify: true
});
})
.catch(function (err) {
$scope.busy = false;
console.error(err);
// alert (err.message);
});
Expand Down
68 changes: 66 additions & 2 deletions modules/project-comments/client/services/comment.model.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// is accessed through the front end
//
// =========================================================================
angular.module('comment').factory ('CommentModel', function (ModelBase, moment, _) {
angular.module('comment').factory ('CommentModel', ['$q', 'ModelBase', 'moment', '_', function ($q, ModelBase, moment, _) {
//
// build the model by extending the base model. the base model will
// have all the basic crud stuff built in
Expand All @@ -20,6 +20,70 @@ angular.module('comment').factory ('CommentModel', function (ModelBase, moment,
// get all the comments for a comment period
//
// -------------------------------------------------------------------------
commentPeriodCommentsSync: function (projectId, periodId, commentLength) {
var self = this;
// setting batch size (limit) to 10 because DEV was performing poorly. adjust this to try on different envs.
var start = 0, limit = 10;
var requests = [];

// build up however many requests we need to save all comments...
do {
requests.push({
// primary query...
periodId: periodId,
start: start,
limit: limit,
projectId: projectId
});
start = start + limit;
}
while (start < commentLength);

// sequential loop, one request at a time, wait until each step completes.
function sequential(items, callback) {
var i = 0, d = $q.defer();
next();
return d.promise;

function next() {
if( i < items.length ) {
callback(items[i], i, items).then(
function() {
i++;
next();
},
onError
);
}
else {
d.resolve();
}
}
function onError(reason) {
d.reject(reason);
}
}

// make the actual http request, return a promise..
function makeRequest(item, index, list) {
//console.log('pcp comments sync request ', (index+1), ' of ', list.length);
return self.put ('/api/comments/period/' + item.periodId + '/perms/sync', item);
}


return new Promise(function(resolve, reject) {
sequential(requests, makeRequest)
.then(function() {
//console.log('pcp comments sync done.');
resolve();
})
.catch(function(err) {
console.log('pcp comments sync error... reject ', JSON.stringify(err));
reject(err);
});
});

},
getAllCommentsForPeriod: function (periodId) {
return this.get ('/api/comments/period/'+periodId+'/all');
},
Expand Down Expand Up @@ -211,6 +275,6 @@ angular.module('comment').factory ('CommentModel', function (ModelBase, moment,
}
});
return new Class ();
});
}]);


7 changes: 5 additions & 2 deletions modules/project-comments/client/views/period-edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
<div class="view-title-container flex-row">
<h1><span ng-show="project.name">Add/Edit Comment Period</h1>
<div class="actions">
<a class="btn btn-default btn-sm" ui-sref="p.commentperiod.list">Cancel</a>
<a class="btn btn-primary btn-sm" href ng-click="save()">Save</a>
<a class="btn btn-default btn-sm" ui-sref="p.commentperiod.list" ng-disabled="busy">Cancel</a>
<a class="btn btn-primary btn-sm" href ng-click="save()" ng-disabled="busy">Save</a>
</div>
</div>

<div class="view-body-container">
<div class="spinner-container" ng-show="busy">
<div class="spinner-new rotating"></div>
</div>

<div ng-show="hasErrors" class="alert alert-danger flex-row">
<div class="alert-icon"><span class="glyphicon glyphicon-exclamation-sign"></span></div>
Expand Down
130 changes: 130 additions & 0 deletions modules/project-comments/server/controllers/comment.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var DBModel = require (path.resolve ('./modules/core/server/controllers/core.d
var _ = require ('lodash');
// var Roles = require (path.resolve('./modules/roles/server/controllers/role.controller'));
var DocumentClass = require (path.resolve('./modules/documents/server/controllers/core.document.controller'));
var ProjectController = require (path.resolve('./modules/projects/server/controllers/project.controller'));

module.exports = DBModel.extend ({
name : 'Comment',
Expand Down Expand Up @@ -115,6 +116,9 @@ module.exports = DBModel.extend ({
if (comment.valuedComponents.length === 0) {
comment.proponentStatus = 'Unclassified';
}
if (_.isEmpty(comment.proponentStatus)) {
comment.proponentStatus = 'Unclassified';
}
return new Promise (function (resolve, reject) {
//
// get the period
Expand Down Expand Up @@ -382,6 +386,132 @@ module.exports = DBModel.extend ({
})
.then (resolve, reject);
});
},
updatePermissionBatch: function(projectId, periodId, skip, limit) {
var self = this;
var projectCtrl = new ProjectController(this.opts);

return new Promise(function (resolve, reject) {
projectCtrl.findById(projectId)
.then(function(project) {
if (project && project.userCan.createCommentPeriod) {
// ok, let them find all the comments and update them... make them act like admin...
self.isAdmin = true;
self.user.roles.push('admin'); // need this so the documents controller in preprocessUpdate will act as admin
return self.getCommentsForPeriod(periodId, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, skip, limit, undefined);
} else {
// can't createCommentPeriod, so don't allow them to do this comment/document processing...
return [];
}
})
.then(function (results) {
var a = _.map(results.data, function (d) {
// update which will call preprocessUpdate where the logic really is...
return self.update(d, d);
});
return Promise.all(a);
})
.then(resolve, reject);
});
},
getPeriodPaginate: function (body) {
var self = this;
// base query / filter
var periodId;
var eaoStatus;
var proponentStatus;
var isPublished;

// filter By Fields...
var commentId;
var authorComment;
var location;
var pillar;
var topic;

// pagination stuff
var skip = 0;
var limit = 50;
var sortby = {};

if (body) {
// base query / filter
if (!_.isEmpty(body.periodId)) {
periodId = body.periodId;
}
if (!_.isEmpty(body.eaoStatus)) {
eaoStatus = body.eaoStatus;
}
if (!_.isEmpty(body.proponentStatus)) {
proponentStatus = body.proponentStatus;
}
if (body.isPublished !== undefined) {
isPublished = Boolean(body.isPublished);
}
// filter By Fields...
if (!_.isEmpty(body.commentId)) {
try {
commentId = parseInt(body.commentId);
} catch(e) {

}
}
if (!_.isEmpty(body.authorComment)) {
authorComment = body.authorComment;
}
if (!_.isEmpty(body.location)) {
location = body.location;
}
if (!_.isEmpty(body.pillar)) {
pillar = body.pillar;
}
if (!_.isEmpty(body.topic)) {
topic = body.topic;
}
// pagination stuff
try {
skip = parseInt(body.start);
limit = parseInt(body.limit);
} catch(e) {
console.log("Non-critical error:", e);
}
if (body.orderBy) {
sortby[body.orderBy] = body.reverse ? -1 : 1;
}
}

return self.getCommentsForPeriod (periodId, eaoStatus, proponentStatus, isPublished, commentId, authorComment, location, pillar, topic, skip, limit, sortby);
},
getPeriodPermsSync: function (body) {
var self = this;
// base query / filter
var periodId;

// pagination stuff
var skip = 0;
var limit = 50;

var projectId; // will need this to check for createCommentPeriod permission

if (body) {
// base query / filter
if (!_.isEmpty(body.periodId)) {
periodId = body.periodId;
}
// pagination stuff
try {
skip = parseInt(body.start);
limit = parseInt(body.limit);
} catch(e) {
console.log('Invalid skip/start or limit value passed in (skip/start =', body.start, ', limit = ', body.limit, '); using defaults: skip/start = ', skip, ', limit =', limit);
}

if (!_.isEmpty(body.projectId)) {
projectId = body.projectId;
}

}
return self.updatePermissionBatch(projectId, periodId, skip, limit);
}
});

Loading