Mark-Lasfar commited on
Commit
d12ffa9
·
1 Parent(s): a4e0b05

Fix cart productId comparison

Browse files
Files changed (1) hide show
  1. server.js +51 -32
server.js CHANGED
@@ -18277,6 +18277,9 @@ app.post('/api/checkout/:storeId', authenticateToken, async (req, res) => {
18277
  // ============================================
18278
  cart.stores.delete(storeId);
18279
  await cart.calculateTotals();
 
 
 
18280
  await cart.save();
18281
 
18282
  // ============================================
@@ -18774,7 +18777,7 @@ app.post('/api/cart/add', authenticateToken, async (req, res) => {
18774
 
18775
  // ✅ البحث عن المنتج الموجود
18776
  const existingItemIndex = storeCart.items.findIndex(
18777
- item => item.productId === productId
18778
  );
18779
 
18780
  if (existingItemIndex !== -1) {
@@ -18810,6 +18813,9 @@ app.post('/api/cart/add', authenticateToken, async (req, res) => {
18810
  // ✅ حساب إجماليات السلة الكلية
18811
  await cart.calculateTotals();
18812
 
 
 
 
18813
  await cart.save();
18814
 
18815
  // ✅ إرجاع السلة المحدثة
@@ -18862,7 +18868,9 @@ app.put('/api/cart/update', authenticateToken, async (req, res) => {
18862
  return res.status(404).json({ error: 'Store not found in cart' });
18863
  }
18864
 
18865
- const itemIndex = storeCart.items.findIndex(item => item.productId === productId);
 
 
18866
  if (itemIndex === -1) {
18867
  return res.status(404).json({ error: 'Product not found in cart' });
18868
  }
@@ -18883,6 +18891,9 @@ app.put('/api/cart/update', authenticateToken, async (req, res) => {
18883
 
18884
  cart.stores.set(storeId, storeCart);
18885
  await cart.calculateTotals();
 
 
 
18886
  await cart.save();
18887
 
18888
  res.json({
@@ -18928,7 +18939,9 @@ app.delete('/api/cart/remove', authenticateToken, async (req, res) => {
18928
  }
18929
 
18930
  // حذف المنتج
18931
- storeCart.items = storeCart.items.filter(item => item.productId !== productId);
 
 
18932
 
18933
  if (storeCart.items.length === 0) {
18934
  // إذا كانت السلة فارغة، احذف المتجر بالكامل
@@ -18948,6 +18961,9 @@ app.delete('/api/cart/remove', authenticateToken, async (req, res) => {
18948
  }
18949
 
18950
  await cart.calculateTotals();
 
 
 
18951
  await cart.save();
18952
 
18953
  res.json({
@@ -18983,6 +18999,9 @@ app.delete('/api/cart/clear-store', authenticateToken, async (req, res) => {
18983
 
18984
  cart.stores.delete(storeId);
18985
  await cart.calculateTotals();
 
 
 
18986
  await cart.save();
18987
 
18988
  res.json({
@@ -19000,35 +19019,6 @@ app.delete('/api/cart/clear-store', authenticateToken, async (req, res) => {
19000
  }
19001
  });
19002
 
19003
- // GET /api/cart/store/:storeId - جلب سلة متجر واحد
19004
- app.get('/api/cart/store/:storeId', authenticateToken, async (req, res) => {
19005
- try {
19006
- const { storeId } = req.params;
19007
- const cart = await Cart.findOne({ userId: req.user.userId });
19008
-
19009
- if (!cart) {
19010
- return res.json({ success: true, storeCart: null });
19011
- }
19012
-
19013
- const storeCart = cart.stores.get(storeId);
19014
- if (!storeCart) {
19015
- return res.json({ success: true, storeCart: null });
19016
- }
19017
-
19018
- res.json({
19019
- success: true,
19020
- storeCart: {
19021
- storeId: storeId,
19022
- storeName: storeCart.storeName,
19023
- items: storeCart.items,
19024
- subtotal: storeCart.subtotal,
19025
- itemCount: storeCart.itemCount
19026
- }
19027
- });
19028
- } catch (error) {
19029
- res.status(500).json({ error: 'Failed to fetch store cart' });
19030
- }
19031
- });
19032
 
19033
  // ============================================
19034
  // 6. DELETE /api/cart/clear-all - تفريغ كل السلة
@@ -19091,6 +19081,35 @@ app.get('/api/cart/summary', authenticateToken, async (req, res) => {
19091
  }
19092
  });
19093
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19094
 
19095
 
19096
 
 
18277
  // ============================================
18278
  cart.stores.delete(storeId);
18279
  await cart.calculateTotals();
18280
+
18281
+ cart.markModified('stores');
18282
+
18283
  await cart.save();
18284
 
18285
  // ============================================
 
18777
 
18778
  // ✅ البحث عن المنتج الموجود
18779
  const existingItemIndex = storeCart.items.findIndex(
18780
+ item => item.productId?.toString() === productId?.toString()
18781
  );
18782
 
18783
  if (existingItemIndex !== -1) {
 
18813
  // ✅ حساب إجماليات السلة الكلية
18814
  await cart.calculateTotals();
18815
 
18816
+
18817
+ cart.markModified('stores');
18818
+
18819
  await cart.save();
18820
 
18821
  // ✅ إرجاع السلة المحدثة
 
18868
  return res.status(404).json({ error: 'Store not found in cart' });
18869
  }
18870
 
18871
+ const itemIndex = storeCart.items.findIndex(
18872
+ item => item.productId?.toString() === productId?.toString()
18873
+ );
18874
  if (itemIndex === -1) {
18875
  return res.status(404).json({ error: 'Product not found in cart' });
18876
  }
 
18891
 
18892
  cart.stores.set(storeId, storeCart);
18893
  await cart.calculateTotals();
18894
+
18895
+ cart.markModified('stores');
18896
+
18897
  await cart.save();
18898
 
18899
  res.json({
 
18939
  }
18940
 
18941
  // حذف المنتج
18942
+ storeCart.items = storeCart.items.filter(
18943
+ item => item.productId?.toString() !== productId?.toString()
18944
+ );
18945
 
18946
  if (storeCart.items.length === 0) {
18947
  // إذا كانت السلة فارغة، احذف المتجر بالكامل
 
18961
  }
18962
 
18963
  await cart.calculateTotals();
18964
+
18965
+ cart.markModified('stores');
18966
+
18967
  await cart.save();
18968
 
18969
  res.json({
 
18999
 
19000
  cart.stores.delete(storeId);
19001
  await cart.calculateTotals();
19002
+
19003
+ cart.markModified('stores');
19004
+
19005
  await cart.save();
19006
 
19007
  res.json({
 
19019
  }
19020
  });
19021
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19022
 
19023
  // ============================================
19024
  // 6. DELETE /api/cart/clear-all - تفريغ كل السلة
 
19081
  }
19082
  });
19083
 
19084
+ // GET /api/cart/store/:storeId - جلب سلة متجر واحد
19085
+ app.get('/api/cart/store/:storeId', authenticateToken, async (req, res) => {
19086
+ try {
19087
+ const { storeId } = req.params;
19088
+ const cart = await Cart.findOne({ userId: req.user.userId });
19089
+
19090
+ if (!cart) {
19091
+ return res.json({ success: true, storeCart: null });
19092
+ }
19093
+
19094
+ const storeCart = cart.stores.get(storeId);
19095
+ if (!storeCart) {
19096
+ return res.json({ success: true, storeCart: null });
19097
+ }
19098
+
19099
+ res.json({
19100
+ success: true,
19101
+ storeCart: {
19102
+ storeId: storeId,
19103
+ storeName: storeCart.storeName,
19104
+ items: storeCart.items,
19105
+ subtotal: storeCart.subtotal,
19106
+ itemCount: storeCart.itemCount
19107
+ }
19108
+ });
19109
+ } catch (error) {
19110
+ res.status(500).json({ error: 'Failed to fetch store cart' });
19111
+ }
19112
+ });
19113
 
19114
 
19115