Added unsetContestantAndWinner

This commit is contained in:
Felix Albrigtsen 2022-04-23 11:46:39 +02:00
parent 61e4193455
commit 9f58f902b8
2 changed files with 43 additions and 0 deletions

View File

@ -282,6 +282,25 @@ api.post("/match/:matchId/setWinner", (req, res) => {
.then(match => res.send({"status": "OK", "data": match}))
.catch(err => res.send({"status": "error", "data": err}));
});
api.post("/match/:matchId/unsetContestant", (req, res) => {
let matchId = req.params.matchId;
let contestantId = req.body.teamId;
if (isNaN(matchId)) {
res.json({"status": "error", "data": "matchId must be a number"});
return
}
if (contestantId == undefined || isNaN(contestantId)) {
res.json({"status": "error", "data": "contestantId must be a number"});
return
}
matchId = parseInt(matchId);
contestantId = parseInt(contestantId);
tmdb.unsetContestantAndWinner(matchId, contestantId)
.then(match => res.send({"status": "OK", "data": match}))
.catch(err => res.send({"status": "error", "data": err}));
});
// #endregion
// #region team/:teamId

View File

@ -11,6 +11,7 @@ module.exports = {
deleteTeam: deleteTeam,
getMatch: getMatch,
setMatchWinner: setMatchWinner,
unsetContestantAndWinner: unsetContestantAndWinner,
createTournament: createTournament,
deleteTournament: deleteTournament,
editTournament: editTournament,
@ -161,6 +162,29 @@ async function setMatchWinner(matchId, winnerId) {
});
}
async function unsetContestantAndWinner(matchId, teamId) {
let match = await getMatch(matchId);
return new Promise(function(resolve, reject) {
unsetContestant(matchId, teamId)
.then(() => {
// Find what the child match that supplied the team
connection.query("SELECT * FROM matches WHERE parentMatchId = ? AND winnerId = ?", [matchId, teamId], (err, childMatches) => {
if (err) { console.log(err); reject(err); }
if (childMatches.length != 1) {
reject("Error: Could not find the correct child match");
return;
}
let childMatch = childMatches[0];
// Remove the winner from the child match
setMatchWinner(childMatch.id, null)
.then(() => { resolve(); })
.catch(err => { reject(err); });
});
})
.catch(err => { reject(err); });
});
}
// #endregion
// #region tournament