fix: Archive but save set winners as winners
If archiving a lottery, let archive-functionality circumvent SMS-service if the winners have been set on the archive page. - Also remove unused file
This commit is contained in:
@@ -33,7 +33,6 @@ const addWinnerWithWine = async (winner, wine) => {
|
|||||||
wine: savedWine,
|
wine: savedWine,
|
||||||
color: winner.color
|
color: winner.color
|
||||||
};
|
};
|
||||||
|
|
||||||
if (exisitingWinner == undefined) {
|
if (exisitingWinner == undefined) {
|
||||||
const newWinner = new Winner({
|
const newWinner = new Winner({
|
||||||
name: winner.name,
|
name: winner.name,
|
||||||
|
|||||||
@@ -5,10 +5,13 @@ const Attendee = require(path.join(__dirname, "/schemas/Attendee"));
|
|||||||
const PreLotteryWine = require(path.join(__dirname, "/schemas/PreLotteryWine"));
|
const PreLotteryWine = require(path.join(__dirname, "/schemas/PreLotteryWine"));
|
||||||
const VirtualWinner = require(path.join(__dirname, "/schemas/VirtualWinner"));
|
const VirtualWinner = require(path.join(__dirname, "/schemas/VirtualWinner"));
|
||||||
const Lottery = require(path.join(__dirname, "/schemas/Purchase"));
|
const Lottery = require(path.join(__dirname, "/schemas/Purchase"));
|
||||||
|
const { WineNotFound } = require(path.join(__dirname, "/vinlottisErrors"));
|
||||||
|
|
||||||
const Message = require(path.join(__dirname, "/message"));
|
const Message = require(path.join(__dirname, "/message"));
|
||||||
const historyRepository = require(path.join(__dirname, "/history"));
|
const historyRepository = require(path.join(__dirname, "/history"));
|
||||||
const wineRepository = require(path.join(__dirname, "/wine"));
|
const wineRepository = require(path.join(__dirname, "/wine"));
|
||||||
|
const winnerRepository = require(path.join(__dirname, "/winner"));
|
||||||
|
const prelotteryWineRepository = require(path.join(__dirname, "/prelotteryWine"));
|
||||||
|
|
||||||
const {
|
const {
|
||||||
WinnerNotFound,
|
WinnerNotFound,
|
||||||
@@ -17,11 +20,36 @@ const {
|
|||||||
LotteryByDateNotFound
|
LotteryByDateNotFound
|
||||||
} = require(path.join(__dirname, "/vinlottisErrors"));
|
} = require(path.join(__dirname, "/vinlottisErrors"));
|
||||||
|
|
||||||
|
const moveUnfoundPrelotteryWineToWines = async (error, tempWine) => {
|
||||||
|
if(!(error instanceof WineNotFound)) {
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!tempWine.winner) {
|
||||||
|
throw new WinnerNotFound()
|
||||||
|
}
|
||||||
|
|
||||||
|
const prelotteryWine = await prelotteryWineRepository.wineById(tempWine._id);
|
||||||
|
const winner = await winnerRepository.winnerById(tempWine.winner.id, true);
|
||||||
|
|
||||||
|
return wineRepository
|
||||||
|
.addWine(prelotteryWine)
|
||||||
|
.then(_ => prelotteryWineRepository.addWinnerToWine(prelotteryWine, winner)) // prelotteryWine.deleteById
|
||||||
|
.then(_ => historyRepository.addWinnerWithWine(winner, prelotteryWine))
|
||||||
|
.then(_ => winnerRepository.setWinnerChosenById(winner.id))
|
||||||
|
}
|
||||||
|
|
||||||
const archive = (date, raffles, stolen, wines) => {
|
const archive = (date, raffles, stolen, wines) => {
|
||||||
const { blue, red, yellow, green } = raffles;
|
const { blue, red, yellow, green } = raffles;
|
||||||
const bought = blue + red + yellow + green;
|
const bought = blue + red + yellow + green;
|
||||||
|
|
||||||
return Promise.all(wines.map(wine => wineRepository.findWine(wine))).then(resolvedWines => {
|
return Promise.all(
|
||||||
|
wines.map(wine => wineRepository
|
||||||
|
.findWine(wine)
|
||||||
|
.catch(error => moveUnfoundPrelotteryWineToWines(error, wine)
|
||||||
|
.then(_ => wineRepository.findWine(wine))
|
||||||
|
))
|
||||||
|
).then(resolvedWines => {
|
||||||
const lottery = new Lottery({
|
const lottery = new Lottery({
|
||||||
date,
|
date,
|
||||||
blue,
|
blue,
|
||||||
|
|||||||
@@ -1,35 +0,0 @@
|
|||||||
const path = require("path");
|
|
||||||
const Highscore = require(path.join(__dirname, "/schemas/Highscore"));
|
|
||||||
|
|
||||||
async function findSavePerson(foundWinner, wonWine, date) {
|
|
||||||
let person = await Highscore.findOne({
|
|
||||||
name: foundWinner.name
|
|
||||||
});
|
|
||||||
|
|
||||||
if (person == undefined) {
|
|
||||||
let newPerson = new Highscore({
|
|
||||||
name: foundWinner.name,
|
|
||||||
wins: [
|
|
||||||
{
|
|
||||||
color: foundWinner.color,
|
|
||||||
date: date,
|
|
||||||
wine: wonWine
|
|
||||||
}
|
|
||||||
]
|
|
||||||
});
|
|
||||||
|
|
||||||
await newPerson.save();
|
|
||||||
} else {
|
|
||||||
person.wins.push({
|
|
||||||
color: foundWinner.color,
|
|
||||||
date: date,
|
|
||||||
wine: wonWine
|
|
||||||
});
|
|
||||||
person.markModified("wins");
|
|
||||||
await person.save();
|
|
||||||
}
|
|
||||||
|
|
||||||
return person;
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports.findSavePerson = findSavePerson;
|
|
||||||
@@ -87,6 +87,9 @@ router.get("/chat/history", chatController.getAllHistory);
|
|||||||
router.delete("/chat/history", mustBeAuthenticated, chatController.deleteHistory);
|
router.delete("/chat/history", mustBeAuthenticated, chatController.deleteHistory);
|
||||||
|
|
||||||
router.post("/login", userController.login);
|
router.post("/login", userController.login);
|
||||||
|
|
||||||
|
// We should have a check here if we are dev/prod,
|
||||||
|
// and disable the mustBeAuthentacted for the register
|
||||||
router.post("/register", mustBeAuthenticated, userController.register);
|
router.post("/register", mustBeAuthenticated, userController.register);
|
||||||
router.get("/logout", userController.logout);
|
router.get("/logout", userController.logout);
|
||||||
|
|
||||||
|
|||||||
@@ -10,17 +10,19 @@ const redactWinnerInfoMapper = winner => {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const addWinner = winner => {
|
||||||
|
let newWinner = new VirtualWinner({
|
||||||
|
name: winner.name,
|
||||||
|
color: winner.color,
|
||||||
|
timestamp_drawn: new Date().getTime()
|
||||||
|
});
|
||||||
|
|
||||||
|
return newWinner.save()
|
||||||
|
}
|
||||||
|
|
||||||
const addWinners = winners => {
|
const addWinners = winners => {
|
||||||
return Promise.all(
|
return Promise.all(
|
||||||
winners.map(winner => {
|
winners.map(winner => addWinner(winner))
|
||||||
let newWinnerElement = new VirtualWinner({
|
|
||||||
name: winner.name,
|
|
||||||
color: winner.color,
|
|
||||||
timestamp_drawn: new Date().getTime()
|
|
||||||
});
|
|
||||||
|
|
||||||
return newWinnerElement.save();
|
|
||||||
})
|
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -49,6 +51,14 @@ const winnerById = (id, isAdmin = false) => {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const setWinnerChosenById = (id) => {
|
||||||
|
return VirtualWinner.findOne({id: id}).then(winner => {
|
||||||
|
winner.prize_selected = true
|
||||||
|
winner.markModified("wins")
|
||||||
|
return winner.save()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
const updateWinnerById = (id, updateModel) => {
|
const updateWinnerById = (id, updateModel) => {
|
||||||
return VirtualWinner.findOne({ id: id }).then(winner => {
|
return VirtualWinner.findOne({ id: id }).then(winner => {
|
||||||
if (winner == null) {
|
if (winner == null) {
|
||||||
@@ -86,10 +96,12 @@ const deleteWinners = () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
addWinner,
|
||||||
addWinners,
|
addWinners,
|
||||||
allWinners,
|
allWinners,
|
||||||
winnerById,
|
winnerById,
|
||||||
updateWinnerById,
|
updateWinnerById,
|
||||||
deleteWinnerById,
|
deleteWinnerById,
|
||||||
deleteWinners
|
deleteWinners,
|
||||||
|
setWinnerChosenById
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -95,3 +95,4 @@ app.use("/subscription", subscriptionApi);
|
|||||||
app.use("/", (req, res) => res.sendFile(path.join(__dirname + "/public/dist/index.html")));
|
app.use("/", (req, res) => res.sendFile(path.join(__dirname + "/public/dist/index.html")));
|
||||||
|
|
||||||
server.listen(30030);
|
server.listen(30030);
|
||||||
|
console.log("Server listening on :30030")
|
||||||
|
|||||||
Reference in New Issue
Block a user