Update lottery winner by id.

Endpoint, controller and repository function for updating lottery winner
by id and json payload.
Keeps previous definition for undefined/null values.
This commit is contained in:
2021-02-18 21:05:40 +01:00
parent fc029f80df
commit d0fa89b92b
2 changed files with 54 additions and 0 deletions

View File

@@ -43,6 +43,28 @@ const winnerById = (id, isAdmin = false) => {
});
};
const updateWinnerById = (id, updateModel) => {
return VirtualWinner.findOne({ id: id }).then(winner => {
if (winner == null) {
throw new WinnerNotFound();
}
const updatedWinner = {
name: updateModel.name != null ? updateModel.name : winner.name,
phoneNumber: updateModel.phoneNumber != null ? updateModel.phoneNumber : winner.phoneNumber,
red: updateModel.red != null ? updateModel.red : winner.red,
green: updateModel.green != null ? updateModel.green : winner.green,
blue: updateModel.blue != null ? updateModel.blue : winner.blue,
yellow: updateModel.yellow != null ? updateModel.yellow : winner.yellow,
timestamp_drawn: updateModel.timestamp_drawn != null ? updateModel.timestamp_drawn : winner.timestamp_drawn,
timestamp_limit: updateModel.timestamp_limit != null ? updateModel.timestamp_limit : winner.timestamp_limit,
timestamp_sent: updateModel.timestamp_sent != null ? updateModel.timestamp_sent : winner.timestamp_sent
};
return VirtualWinner.updateOne({ id: id }, updatedWinner).then(_ => updatedWinner);
});
};
const deleteWinnerById = id => {
return VirtualWinner.findOne({ _id: id }).then(winner => {
if (winner == null) {
@@ -61,6 +83,7 @@ module.exports = {
addWinners,
allWinners,
winnerById,
updateWinnerById,
deleteWinnerById,
deleteWinners
};