When selecting prize, add winner to wine.

Changed the way we register a prize for winner.
Now we have a new prize_selected boolean field on a winner. This is
used to filter on when finding what winners have not selected a prize
yet. This also replaces the previous method of removing virtualWinners
after they selected a prize.

PrelotteryWine also get's a winner reference. This is used to filter on
when finding what prizes are left, and also makes it easier to
archive/register a lottery when the wine has a winner attached.
This commit is contained in:
2021-02-18 20:50:30 +01:00
parent 4bd3b688e9
commit a28a8ccacb
4 changed files with 30 additions and 9 deletions

View File

@@ -4,7 +4,11 @@ const PreLotteryWine = require(path.join(__dirname, "/schemas/PreLotteryWine"));
const { WineNotFound } = require(path.join(__dirname, "/vinlottisErrors")); const { WineNotFound } = require(path.join(__dirname, "/vinlottisErrors"));
const allWines = () => { const allWines = () => {
return PreLotteryWine.find(); return PreLotteryWine.find().populate("winner");
};
const allWinesWithoutWinner = () => {
return PreLotteryWine.find({ winner: { $exists: false } });
}; };
const addWines = wines => { const addWines = wines => {
@@ -56,6 +60,12 @@ const updateWineById = (id, updateModel) => {
}); });
}; };
const addWinnerToWine = (wine, winner) => {
wine.winner = winner;
winner.prize_selected = true;
return Promise.all(wine.save(), winner.save());
};
const deleteWineById = id => { const deleteWineById = id => {
return PreLotteryWine.findOne({ _id: id }).then(wine => { return PreLotteryWine.findOne({ _id: id }).then(wine => {
if (wine == null) { if (wine == null) {
@@ -82,8 +92,10 @@ const wineSchema = () => {
module.exports = { module.exports = {
allWines, allWines,
allWinesWithoutWinner,
addWines, addWines,
wineById, wineById,
addWinnerToWine,
updateWineById, updateWineById,
deleteWineById, deleteWineById,
deleteWines, deleteWines,

View File

@@ -24,10 +24,11 @@ const verifyWinnerNextInLine = async id => {
} }
let allWinners = await VirtualWinner.find().sort({ timestamp_drawn: 1 }); let allWinners = await VirtualWinner.find().sort({ timestamp_drawn: 1 });
if ( if (
allWinners[0].id != foundWinner.id ||
foundWinner.timestamp_limit == undefined || foundWinner.timestamp_limit == undefined ||
foundWinner.timestamp_sent == undefined foundWinner.timestamp_sent == undefined ||
foundWinner.prize_selected == true
) { ) {
throw new WineSelectionWinnerNotNextInLine(); throw new WineSelectionWinnerNotNextInLine();
} }
@@ -47,8 +48,8 @@ const claimPrize = (winner, wine) => {
const notifyNextWinner = async () => { const notifyNextWinner = async () => {
let nextWinner = undefined; let nextWinner = undefined;
const winnersLeft = await VirtualWinner.find().sort({ timestamp_drawn: 1 }); const winnersLeft = await VirtualWinner.find({ prize_selected: false }).sort({ timestamp_drawn: 1 });
const winesLeft = await PreLotteryWine.find(); const winesLeft = await PreLotteryWine.find({ winner: { $exists: false } });
if (winnersLeft.length > 1) { if (winnersLeft.length > 1) {
console.log("multiple winners left, choose next in line"); console.log("multiple winners left, choose next in line");
@@ -60,7 +61,7 @@ const notifyNextWinner = async () => {
console.log("one winner and one wine left, choose for user"); console.log("one winner and one wine left, choose for user");
nextWinner = winnersLeft[0]; // one winner and one wine left, choose for user nextWinner = winnersLeft[0]; // one winner and one wine left, choose for user
wine = winesLeft[0]; wine = winesLeft[0];
return claimPrize(nextWinner, wine); return claimPrize(wine, nextWinner);
} }
if (nextWinner) { if (nextWinner) {
@@ -82,7 +83,7 @@ function startTimeout(id) {
console.log(`Starting timeout for user ${id}.`); console.log(`Starting timeout for user ${id}.`);
console.log(`Timeout duration: ${minutesForTimeout * minute}`); console.log(`Timeout duration: ${minutesForTimeout * minute}`);
setTimeout(async () => { setTimeout(async () => {
let virtualWinner = await VirtualWinner.findOne({ id: id }); let virtualWinner = await VirtualWinner.findOne({ id: id, prize_selected: false });
if (!virtualWinner) { if (!virtualWinner) {
console.log(`Timeout done for user ${id}, but user has already sent data.`); console.log(`Timeout done for user ${id}, but user has already sent data.`);
return; return;
@@ -96,7 +97,7 @@ function startTimeout(id) {
virtualWinner.timestamp_sent = null; virtualWinner.timestamp_sent = null;
await virtualWinner.save(); await virtualWinner.save();
findAndNotifyNextWinner(); notifyNextWinner();
}, minutesForTimeout * minute); }, minutesForTimeout * minute);
return Promise.resolve(); return Promise.resolve();

View File

@@ -9,7 +9,11 @@ const PreLotteryWine = new Schema({
year: Number, year: Number,
image: String, image: String,
price: String, price: String,
country: String country: String,
winner: {
type: Schema.Types.ObjectId,
ref: "VirtualWinner"
}
}); });
module.exports = mongoose.model("PreLotteryWine", PreLotteryWine); module.exports = mongoose.model("PreLotteryWine", PreLotteryWine);

View File

@@ -10,6 +10,10 @@ const VirtualWinner = new Schema({
red: Number, red: Number,
yellow: Number, yellow: Number,
id: String, id: String,
prize_selected: {
type: Boolean,
default: false
},
timestamp_drawn: Number, timestamp_drawn: Number,
timestamp_sent: Number, timestamp_sent: Number,
timestamp_limit: Number timestamp_limit: Number