Permalink
Browse files

Allow for win status to be toggled, per-candidate, when managing offices

  • Loading branch information...
jzblee committed Apr 7, 2017
1 parent afeea5f commit 115dad53cc9ffcfa211c5b159c9639db21765059
Showing with 65 additions and 0 deletions.
  1. +34 −0 public/javascripts/controllers/OfficesController.js
  2. +27 −0 routes/candidates.js
  3. +4 −0 views/partials/offices.html
@@ -297,6 +297,40 @@ app.controller('OfficesController', ['$scope', '$route', '$routeParams', '$locat
}
};
+ /**
+ * Removes a candidate from a given office
+ * @param rcsId
+ * @param officeId
+ */
+ $scope.toggleWon = function (rcsId, officeId) {
+ var confirmation = confirm("Are you sure you want to toggle win status for " + rcsId + "?");
+
+ if (confirmation) {
+ $scope.offices.forEach(function (o) {
+ var title;
+ var indexToToggle = -1;
+ if (o.id === officeId) {
+ title = o.title;
+ o.candidates.forEach(function (c, index) {
+ if (c.rcsId === rcsId) {
+ indexToToggle = index;
+ }
+ });
+ }
+ if (indexToToggle > -1) {
+ var status;
+ if (o.candidates[indexToToggle].winner) status = '0';
+ else status = '1';
+ $http.put('/api/candidates/update/' + rcsId + '/' + officeId + '/' + status).then(function () {
+ addNewAlert("success", rcsId + " had win status toggled", "toggle_candidate_win");
+ }, function (response) {
+ addNewAlert("error", response.statusText + " (code: " + response.status + ")", "toggle_candidate_win");
+ });
+ }
+ });
+ }
+ };
+
$scope.addCandidateKeypressEvent = function (keyEvent) {
if (keyEvent.which === 13 && $scope.newCandidate.rcs) {
$scope.addCandidate();
View
@@ -51,6 +51,7 @@ var queries = {
"`middle_name`, `last_name`, `greek_affiliated`, `entry_date`, `class_by_credit`, `grad_date`, `rin`) VALUES ",
remove: "DELETE FROM " + functions.dbName() + ".`candidates` ",
update: "UPDATE " + functions.dbName() + ".`candidate_data` SET <> WHERE rcs_id = ",
+ updateWinner: "UPDATE " + functions.dbName() + ".`candidates` SET `winner` = <> WHERE rcs_id = ",
updateParty: "UPDATE " + functions.dbName() + ".`candidates` SET `party_id` = <> WHERE rcs_id = ",
duplicateRCS: " ON DUPLICATE KEY UPDATE rcs_id = ",
@@ -330,6 +331,32 @@ router.put('/update/:rcs_id', function (req, res) {
});
});
+router.put('/update/:rcs_id/:office_id/:status', function (req, res) {
+ var userData = functions.verifyPermissions(req);
+ if (!userData.admin && userData.username != req.params.rcs_id) {
+ res.sendStatus(401);
+ return;
+ }
+
+ var connection = functions.dbConnect(res);
+
+ var rcs_id = req.params.rcs_id,
+ office_id = req.params.office_id,
+ status = req.params.status,
+ data = req.body;
+
+ if (!data) res.status(204);
+
+ query = queries.updateWinner.replace(/<>/g, mysql.escape(status)) + mysql.escape(rcs_id) +
+ ' AND office_id = ' + mysql.escape(office_id);
+
+ connection.query(query, functions.defaultJSONCallback(res));
+
+ logger.write(connection, req.session.cas_user, "CANDIDATE_MODIFY", "Modified " + rcs_id);
+
+ connection.end();
+});
+
router.delete('/delete/:rcs_id/:office_id', function (req, res) {
if (!functions.verifyPermissions(req).admin) {
res.sendStatus(401);
@@ -307,6 +307,10 @@ <h3>Candidates</h3>
<li class="list-group-item" ng-repeat="c in o.candidates | orderBy:'-nominations'"
ng-class="{'list-group-item-success': c.nominations >= o.nominationsRequired,
'list-group-item-warning': c.nominations > 0 && c.nominations < o.nominationsRequired}">
+ <button class="btn btn-default btn-xs pull-right" ng-click="toggleWon(c.rcsId, o.id)">
+ <span class="fa fa-trophy"></span>
+ </button>
+
<button class="btn btn-default btn-xs pull-right" ng-click="removeCandidate(c.rcsId, o.id)">
<span class="fa fa-times"></span>
</button>

0 comments on commit 115dad5

Please sign in to comment.