Skip to content

Commit

Permalink
Allow for win status to be toggled, per-candidate, when managing offices
Browse files Browse the repository at this point in the history
  • Loading branch information
jzblee committed Apr 7, 2017
1 parent afeea5f commit 115dad5
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
34 changes: 34 additions & 0 deletions public/javascripts/controllers/OfficesController.js
Expand Up @@ -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) { $scope.addCandidateKeypressEvent = function (keyEvent) {
if (keyEvent.which === 13 && $scope.newCandidate.rcs) { if (keyEvent.which === 13 && $scope.newCandidate.rcs) {
$scope.addCandidate(); $scope.addCandidate();
Expand Down
27 changes: 27 additions & 0 deletions routes/candidates.js
Expand Up @@ -51,6 +51,7 @@ var queries = {
"`middle_name`, `last_name`, `greek_affiliated`, `entry_date`, `class_by_credit`, `grad_date`, `rin`) VALUES ", "`middle_name`, `last_name`, `greek_affiliated`, `entry_date`, `class_by_credit`, `grad_date`, `rin`) VALUES ",
remove: "DELETE FROM " + functions.dbName() + ".`candidates` ", remove: "DELETE FROM " + functions.dbName() + ".`candidates` ",
update: "UPDATE " + functions.dbName() + ".`candidate_data` SET <> WHERE rcs_id = ", 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 = ", updateParty: "UPDATE " + functions.dbName() + ".`candidates` SET `party_id` = <> WHERE rcs_id = ",


duplicateRCS: " ON DUPLICATE KEY UPDATE rcs_id = ", duplicateRCS: " ON DUPLICATE KEY UPDATE rcs_id = ",
Expand Down Expand Up @@ -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) { router.delete('/delete/:rcs_id/:office_id', function (req, res) {
if (!functions.verifyPermissions(req).admin) { if (!functions.verifyPermissions(req).admin) {
res.sendStatus(401); res.sendStatus(401);
Expand Down
4 changes: 4 additions & 0 deletions views/partials/offices.html
Expand Up @@ -307,6 +307,10 @@ <h3>Candidates</h3>
<li class="list-group-item" ng-repeat="c in o.candidates | orderBy:'-nominations'" <li class="list-group-item" ng-repeat="c in o.candidates | orderBy:'-nominations'"
ng-class="{'list-group-item-success': c.nominations >= o.nominationsRequired, ng-class="{'list-group-item-success': c.nominations >= o.nominationsRequired,
'list-group-item-warning': c.nominations > 0 && 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)"> <button class="btn btn-default btn-xs pull-right" ng-click="removeCandidate(c.rcsId, o.id)">
<span class="fa fa-times"></span> <span class="fa fa-times"></span>
</button> </button>
Expand Down

0 comments on commit 115dad5

Please sign in to comment.