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 allWines = () => {
return PreLotteryWine.find();
return PreLotteryWine.find().populate("winner");
};
const allWinesWithoutWinner = () => {
return PreLotteryWine.find({ winner: { $exists: false } });
};
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 => {
return PreLotteryWine.findOne({ _id: id }).then(wine => {
if (wine == null) {
@@ -82,8 +92,10 @@ const wineSchema = () => {
module.exports = {
allWines,
allWinesWithoutWinner,
addWines,
wineById,
addWinnerToWine,
updateWineById,
deleteWineById,
deleteWines,

View File

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

View File

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

View File

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