-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* ✨ Store cart to user db * 🐛 Dont clear cart on add cart items * 🐛 Overwrite cart item on recover * 🚸 Clear local cart on account restore * ♻️ Combine cart API path
- Loading branch information
1 parent
6866503
commit 4b4ef47
Showing
6 changed files
with
119 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
const { Router } = require('express'); | ||
|
||
const { authenticateV2Login } = require('../../../middleware/auth'); | ||
const { | ||
FieldValue, | ||
walletUserCollection, | ||
} = require('../../../../modules/firebase'); | ||
|
||
const router = Router(); | ||
|
||
router.get('/cart', authenticateV2Login, async (req, res) => { | ||
const { user } = req.session; | ||
const cartDoc = await walletUserCollection.doc(user).get(); | ||
const { cart } = cartDoc.data(); | ||
res.json({ cart: cart || [] }); | ||
}); | ||
|
||
router.post('/cart', authenticateV2Login, async (req, res) => { | ||
const { user } = req.session; | ||
const { cart: inputCart } = req.body; | ||
if (!Array.isArray(inputCart)) { | ||
res.status(400).send('INVALID_CART'); | ||
return; | ||
} | ||
const cart = Object.values( | ||
inputCart.reduce((acc, item) => { | ||
acc[item.productId] = item; | ||
return acc; | ||
}, {}) | ||
).map(item => { | ||
const { | ||
productId, | ||
collectionId, | ||
classId, | ||
coupon, | ||
customPriceInDecimal, | ||
from, | ||
timestamp, | ||
quantity, | ||
priceIndex, | ||
} = item; | ||
return JSON.parse( | ||
JSON.stringify({ | ||
productId, | ||
collectionId, | ||
classId, | ||
coupon, | ||
customPriceInDecimal, | ||
from, | ||
timestamp, | ||
quantity, | ||
priceIndex, | ||
}) | ||
); | ||
}); | ||
await walletUserCollection.doc(user).update({ | ||
cart: cart?.length ? cart : FieldValue.delete(), | ||
cartUpdateTimestamp: FieldValue.serverTimestamp(), | ||
}); | ||
res.sendStatus(200); | ||
}); | ||
|
||
router.delete('/cart', authenticateV2Login, async (req, res) => { | ||
const { user } = req.session; | ||
await walletUserCollection.doc(user).update({ | ||
cart: FieldValue.delete(), | ||
cartUpdateTimestamp: FieldValue.serverTimestamp(), | ||
}); | ||
res.sendStatus(200); | ||
}); | ||
|
||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters