From 8869b6ff157c8d3c38793b5b955f95395caae7a6 Mon Sep 17 00:00:00 2001 From: Arnaud97234 Date: Wed, 20 Dec 2023 15:09:17 +0100 Subject: [PATCH 1/4] fix db schema --- backend/models/packs.js | 2 +- backend/routes/pack.js | 6 +++--- frontend/reducers/users.js | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/backend/models/packs.js b/backend/models/packs.js index 98a7415..d74c958 100644 --- a/backend/models/packs.js +++ b/backend/models/packs.js @@ -2,7 +2,7 @@ const mongoose = require('mongoose') const packPriceSchema = mongoose.Schema({ userToken: { type: mongoose.Schema.Types.String, ref: 'users' }, - price: Number, + price: Number }) const packSchema = mongoose.Schema({ diff --git a/backend/routes/pack.js b/backend/routes/pack.js index 5207901..6482409 100644 --- a/backend/routes/pack.js +++ b/backend/routes/pack.js @@ -17,7 +17,6 @@ router.get("/marketPacks", (req, res) => { router.patch("/openPack/:userToken/:userPack", async (req, res) => { // find the pack to open const user = await User.findOne({ "token": req.params.userToken }) - console.log("User cards: ", user.cardsId) const userId = await user._id const pack = await Pack.findOne({ "_id": req.params.userPack, "packPrices.userToken": req.params.userToken }) // Decrease pack Stock @@ -37,8 +36,9 @@ router.patch("/openPack/:userToken/:userPack", async (req, res) => { await Card.updateOne({ "_id": card._id }, { $push: { "cardPrices": { - price: 3000, - userId: userId + price: 0, + userId: userId, + userToken: req.params.userToken } } }) diff --git a/frontend/reducers/users.js b/frontend/reducers/users.js index 2424a9c..0548660 100644 --- a/frontend/reducers/users.js +++ b/frontend/reducers/users.js @@ -1,7 +1,7 @@ import { createSlice } from '@reduxjs/toolkit' const initialState = { - value: { token: null, username: null, email: null, cardsList: [], packsList: []} + value: { token: null, username: null, email: null, cardsList: [], packsList: [] } } export const usersSlice = createSlice({ From 395b42e404293ebbf1a210840f0a372c18cf2af2 Mon Sep 17 00:00:00 2001 From: Arnaud97234 Date: Wed, 20 Dec 2023 19:39:51 +0100 Subject: [PATCH 2/4] some fix --- backend/models/packs.js | 2 +- backend/models/users.js | 4 ++-- backend/routes/pack.js | 3 +-- backend/routes/users.js | 3 ++- frontend/components/modals/SigninModal.js | 22 +++++++++++----------- 5 files changed, 17 insertions(+), 17 deletions(-) diff --git a/backend/models/packs.js b/backend/models/packs.js index d74c958..1cde483 100644 --- a/backend/models/packs.js +++ b/backend/models/packs.js @@ -1,7 +1,7 @@ const mongoose = require('mongoose') const packPriceSchema = mongoose.Schema({ - userToken: { type: mongoose.Schema.Types.String, ref: 'users' }, + userToken: String, price: Number }) diff --git a/backend/models/users.js b/backend/models/users.js index a42d9ca..dafd42a 100644 --- a/backend/models/users.js +++ b/backend/models/users.js @@ -7,8 +7,8 @@ const userSchema = mongoose.Schema({ stock: Number, token: String, credits: Number, - cardsId: [{type: mongoose.Schema.Types.ObjectId, ref: 'cards'}], - packsId: [{type: mongoose.Schema.Types.ObjectId, ref: 'packs'}], + cardsId: [{ type: mongoose.Schema.Types.ObjectId, ref: 'cards' }], + packsId: [{ type: mongoose.Schema.Types.ObjectId, ref: 'packs' }], }) const User = mongoose.model('users', userSchema) diff --git a/backend/routes/pack.js b/backend/routes/pack.js index 6482409..2704094 100644 --- a/backend/routes/pack.js +++ b/backend/routes/pack.js @@ -17,7 +17,7 @@ router.get("/marketPacks", (req, res) => { router.patch("/openPack/:userToken/:userPack", async (req, res) => { // find the pack to open const user = await User.findOne({ "token": req.params.userToken }) - const userId = await user._id + //const userId = await user._id const pack = await Pack.findOne({ "_id": req.params.userPack, "packPrices.userToken": req.params.userToken }) // Decrease pack Stock const packToUpdate = pack.packPrices[0]._id @@ -37,7 +37,6 @@ router.patch("/openPack/:userToken/:userPack", async (req, res) => { $push: { "cardPrices": { price: 0, - userId: userId, userToken: req.params.userToken } } diff --git a/backend/routes/users.js b/backend/routes/users.js index ed0126a..92fe039 100644 --- a/backend/routes/users.js +++ b/backend/routes/users.js @@ -28,7 +28,7 @@ router.post("/signup", (req, res) => { email: req.body.email, password: hash, token: token, - credits: Math.floor(Math.random()*89999+10000), + credits: Math.floor(Math.random() * 89999 + 10000), gamesId: [], cardsId: [], packsId: ["657ad5ea8f96e935b9b105a4"], @@ -69,6 +69,7 @@ router.post("/signin", (req, res) => { res.json({ result: false, error: "Wrong password" }); return; } + console.log(data.packsList) res.json({ result: true, token: data.token, diff --git a/frontend/components/modals/SigninModal.js b/frontend/components/modals/SigninModal.js index f65c59d..167f3cf 100644 --- a/frontend/components/modals/SigninModal.js +++ b/frontend/components/modals/SigninModal.js @@ -22,17 +22,17 @@ function SigninModal() { headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ email: signinEmail, password: signinPassword }), }) - .then(response => response.json()) - .then(data => { - if(data.result) { - setSigninEmail('') - setSigninPassword('') - addUser({ token: data.token, email: signinEmail, username: data.username, cardsList: data.cardsList, gamesList: data.gamesList, packsList: data.packsList }) - router.push("/home"); - } else { - setErrorMessage(data.error) - } - }) + .then(response => response.json()) + .then(data => { + if (data.result) { + setSigninEmail('') + setSigninPassword('') + addUser({ token: data.token, email: signinEmail, username: data.username, cardsList: data.cardsList, gamesList: data.gamesList, packsList: data.packsList }) + router.push("/home"); + } else { + setErrorMessage(data.error) + } + }) } const state = useSelector((state) => state.users.value) return ( From bb8b41b8f0f85b73047211db63a7af3c846fa32f Mon Sep 17 00:00:00 2001 From: Arnaud97234 Date: Wed, 20 Dec 2023 23:33:37 +0100 Subject: [PATCH 3/4] add qty field to User packs --- backend/models/users.js | 7 ++++++- backend/routes/pack.js | 43 ++++++++++++++++++++++++++++++++--------- 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/backend/models/users.js b/backend/models/users.js index dafd42a..5a0a94b 100644 --- a/backend/models/users.js +++ b/backend/models/users.js @@ -1,5 +1,10 @@ const mongoose = require('mongoose') +const packIdSchema = mongoose.Schema({ + packId: [{ type: mongoose.Schema.Types.ObjectId, ref: 'packs' }], + quantity: Number +}) + const userSchema = mongoose.Schema({ email: String, username: String, @@ -8,7 +13,7 @@ const userSchema = mongoose.Schema({ token: String, credits: Number, cardsId: [{ type: mongoose.Schema.Types.ObjectId, ref: 'cards' }], - packsId: [{ type: mongoose.Schema.Types.ObjectId, ref: 'packs' }], + packsId: [packIdSchema] }) const User = mongoose.model('users', userSchema) diff --git a/backend/routes/pack.js b/backend/routes/pack.js index 2704094..ad9e9f8 100644 --- a/backend/routes/pack.js +++ b/backend/routes/pack.js @@ -11,24 +11,49 @@ router.get("/marketPacks", (req, res) => { Pack.find({ 'packPrices.price': { $gt: 0 } }) .then((pack) => { res.json({ result: true, pack: pack }) - }); -}); + }) +}) router.patch("/openPack/:userToken/:userPack", async (req, res) => { // find the pack to open - const user = await User.findOne({ "token": req.params.userToken }) - //const userId = await user._id - const pack = await Pack.findOne({ "_id": req.params.userPack, "packPrices.userToken": req.params.userToken }) - // Decrease pack Stock - const packToUpdate = pack.packPrices[0]._id + const pack = await Pack.findOne({ "_id": req.params.userPack }) + + // Decrease pack's Stock const newStock = Number(pack.stock) - 1 - await Pack.updateOne({ "packPrices.userToken": req.params.userToken }, { "stock": newStock }) + await Pack.updateOne({ "_id": req.params.userPack }, { "stock": newStock }) + const packsEligible = [] + pack.packPrices.map((e) => { + if (e.userToken == req.params.userToken) { + packsEligible.push(e) + } + }) + const packToremove = packsEligible[0]._id // Remove user from Pack await Pack.updateOne( { "_id": req.params.userPack }, - { $pull: { "packPrices": { "_id": packToUpdate } } } + { $pull: { "packPrices": { "_id": packToremove } } } ) + + // Decrease pack quantity for user + + const packQtyToDecrease = await User.findOne({ "token": req.params.userToken }) + + packQtyToDecrease.packsId.map((e) => { + if (e.packId == req.params.userPack) { + User.updateOne({ + "packsId": { + "packId": req.params.userPack + } + }, { + "packsId": { + "quantity": packsEligible.length + } + }) + } + }) + + // Give 5 random cards to user const cardsToAdd = await Card.aggregate([ { $sample: { size: 5 } } ]) From 5cd70ff51b14750c56b1d76a890332893aac3bea Mon Sep 17 00:00:00 2001 From: Arnaud97234 Date: Thu, 21 Dec 2023 12:46:52 +0100 Subject: [PATCH 4/4] put response --- backend/routes/pack.js | 1 + 1 file changed, 1 insertion(+) diff --git a/backend/routes/pack.js b/backend/routes/pack.js index ad9e9f8..2404f32 100644 --- a/backend/routes/pack.js +++ b/backend/routes/pack.js @@ -68,6 +68,7 @@ router.patch("/openPack/:userToken/:userPack", async (req, res) => { }) await User.updateOne({ "token": req.params.userToken }, { $push: { "cardsId": card._id } }) + res.json({ result: true, newCards: cardsToAdd }) }) })