node-mongo-demo

node.js and mongodb demo

git clone https://9o.is/git/node-mongo-demo.git

commit 239c80412779bfb2f78ce19c2c0fc8ffe5c4c950
parent 8109abd1e649a589b208c6e45cab5a97d2c97876
Author: Jul <jul@9o.is>
Date:   Tue, 28 Jan 2025 06:14:30 -0500

remove win and add timestamps to bet model

Diffstat:
Mbackend/src/models/lucky7-bet.js | 12+++++++++---
1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/backend/src/models/lucky7-bet.js b/backend/src/models/lucky7-bet.js @@ -6,8 +6,8 @@ const lucky7BetSchema = mongoose.Schema({ rollAt: { type: Date, required: true }, lucky: { type: Boolean, required: true }, roll: { type: [Number], required: true }, - win: { type: Boolean, required: true }, }, { + timestamps: true, optimisticConcurrency: true, virtuals: { state: { @@ -15,6 +15,14 @@ const lucky7BetSchema = mongoose.Schema({ return Date.now() < this.rollAt ? 'pending' : 'finished'; }, }, + win: { + get() { + if (!this.played()) return undefined; + + const seven = 7 === this.roll[0] + this.roll[1]; + return (this.lucky && seven) || (!this.lucky && !seven); + }, + }, }, methods: { played() { @@ -22,8 +30,6 @@ const lucky7BetSchema = mongoose.Schema({ }, play() { this.roll = [randomInt(1,7), randomInt(1,7)]; - const seven = 7 === this.roll[0] + this.roll[1]; - this.win = (this.lucky && seven) || (!this.lucky && !seven); }, toRedactedObject() { const { id, rollAt, lucky, roll, win, state } = this;