Mark-Lasfar commited on
Commit
3b8241c
·
1 Parent(s): a978d80

Fix: Optimize loadStore function, fix preview mode and follow-status

Browse files
Files changed (1) hide show
  1. server.js +148 -3
server.js CHANGED
@@ -407,6 +407,9 @@ const excludedPaths = [
407
  '/api/ai/admin/stats',
408
  '/api/ai/admin/clear-cache',
409
 
 
 
 
410
  '/api/templates',
411
  '/api/templates/featured',
412
  '/api/templates/popular',
@@ -1753,7 +1756,13 @@ const storeSettingsSchema = new mongoose.Schema({
1753
  enableCompareProducts: { type: Boolean, default: false },
1754
  enableWishlist: { type: Boolean, default: false },
1755
  enableRecentlyViewed: { type: Boolean, default: true },
1756
- enableProductReviews: { type: Boolean, default: true }
 
 
 
 
 
 
1757
  },
1758
 
1759
  // ============================================
@@ -19259,6 +19268,141 @@ app.put('/api/store/settings', authenticateToken, async (req, res) => {
19259
  }
19260
  });
19261
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19262
  // ============================================
19263
  // POST /api/store/contact - إرسال رسالة لصاحب المتجر (محدث)
19264
  // ============================================
@@ -30143,12 +30287,12 @@ app.get('/api/store/:username', async (req, res) => {
30143
  content: section.content || {}
30144
  }))
30145
  .sort((a, b) => (a.order || 0) - (b.order || 0));
30146
-
30147
  const totalSales = await Order.aggregate([
30148
  { $match: { sellerId: user._id, status: 'completed' } },
30149
  { $group: { _id: null, total: { $sum: '$totalAmount' } } }
30150
  ]);
30151
-
30152
  const productsCount = await Product.countDocuments({ userId: user._id, isActive: true });
30153
  const followersCount = await StoreFollow.countDocuments({ storeId: user._id });
30154
 
@@ -30191,6 +30335,7 @@ app.get('/api/store/:username', async (req, res) => {
30191
  glassEffect: storeSettings.glassEffect || false,
30192
  backgroundType: storeSettings.backgroundType || 'default',
30193
  backgroundValue: storeSettings.backgroundValue || '',
 
30194
  customCss: storeSettings.customCss || '',
30195
  // ✅ إضافة إعدادات القالب
30196
  theme: theme ? {
 
407
  '/api/ai/admin/stats',
408
  '/api/ai/admin/clear-cache',
409
 
410
+ '/api/store/layout-template',
411
+ '/api/store/layout-template/reset',
412
+ '/api/store/layout-templates',
413
  '/api/templates',
414
  '/api/templates/featured',
415
  '/api/templates/popular',
 
1756
  enableCompareProducts: { type: Boolean, default: false },
1757
  enableWishlist: { type: Boolean, default: false },
1758
  enableRecentlyViewed: { type: Boolean, default: true },
1759
+ enableProductReviews: { type: Boolean, default: true },
1760
+ layoutTemplate: {
1761
+ type: String,
1762
+ enum: ['default', 'sidebar-left', 'sidebar-right', 'product-focused', 'minimal', 'magazine'],
1763
+ default: 'default'
1764
+ }
1765
+
1766
  },
1767
 
1768
  // ============================================
 
19268
  }
19269
  });
19270
 
19271
+
19272
+ // ============================================
19273
+ // PUT /api/store/layout-template - حفظ قالب التخطيط
19274
+ // ============================================
19275
+ app.put('/api/store/layout-template', authenticateToken, async (req, res) => {
19276
+ try {
19277
+ const { layoutTemplate } = req.body;
19278
+
19279
+ const validLayouts = ['default', 'sidebar-left', 'sidebar-right', 'product-focused', 'minimal', 'magazine'];
19280
+
19281
+ if (!validLayouts.includes(layoutTemplate)) {
19282
+ return res.status(400).json({ error: 'Invalid layout template' });
19283
+ }
19284
+
19285
+ const settings = await StoreSettings.findOneAndUpdate(
19286
+ { userId: req.user.userId },
19287
+ {
19288
+ $set: {
19289
+ 'layoutSettings.layoutTemplate': layoutTemplate,
19290
+ updatedAt: Date.now()
19291
+ }
19292
+ },
19293
+ { upsert: true, new: true }
19294
+ );
19295
+
19296
+ console.log(`✅ Layout template saved: ${layoutTemplate} for user ${req.user.userId}`);
19297
+
19298
+ res.json({
19299
+ success: true,
19300
+ layoutTemplate: layoutTemplate,
19301
+ message: 'Layout template saved successfully'
19302
+ });
19303
+
19304
+ } catch (error) {
19305
+ console.error('Error saving layout template:', error);
19306
+ res.status(500).json({ error: 'Failed to save layout template' });
19307
+ }
19308
+ });
19309
+
19310
+ // ============================================
19311
+ // POST /api/store/layout-template/reset - إعادة تعيين قالب التخطيط
19312
+ // ============================================
19313
+ app.post('/api/store/layout-template/reset', authenticateToken, async (req, res) => {
19314
+ try {
19315
+ const settings = await StoreSettings.findOneAndUpdate(
19316
+ { userId: req.user.userId },
19317
+ {
19318
+ $set: {
19319
+ 'layoutSettings.layoutTemplate': 'default',
19320
+ updatedAt: Date.now()
19321
+ }
19322
+ },
19323
+ { upsert: true, new: true }
19324
+ );
19325
+
19326
+ console.log(`✅ Layout template reset to default for user ${req.user.userId}`);
19327
+
19328
+ res.json({
19329
+ success: true,
19330
+ layoutTemplate: 'default',
19331
+ message: 'Layout template reset to default'
19332
+ });
19333
+
19334
+ } catch (error) {
19335
+ console.error('Error resetting layout template:', error);
19336
+ res.status(500).json({ error: 'Failed to reset layout template' });
19337
+ }
19338
+ });
19339
+
19340
+
19341
+ // ============================================
19342
+ // GET /api/store/layout-templates - جلب قوالب التخطيط المتاحة
19343
+ // ============================================
19344
+ app.get('/api/store/layout-templates', authenticateToken, async (req, res) => {
19345
+ try {
19346
+ const templates = [
19347
+ {
19348
+ id: 'default',
19349
+ name: 'Default Layout',
19350
+ description: 'Standard grid with 4 columns',
19351
+ previewImage: '/assets/layouts/default.svg',
19352
+ features: ['4 column grid', 'Full header', 'Standard footer', 'Products first']
19353
+ },
19354
+ {
19355
+ id: 'sidebar-left',
19356
+ name: 'Sidebar Left',
19357
+ description: 'Categories menu on the left side',
19358
+ previewImage: '/assets/layouts/sidebar-left.svg',
19359
+ features: ['Left sidebar', '3 column grid', 'Categories visible', 'Compact design']
19360
+ },
19361
+ {
19362
+ id: 'sidebar-right',
19363
+ name: 'Sidebar Right',
19364
+ description: 'Categories menu on the right side',
19365
+ previewImage: '/assets/layouts/sidebar-right.svg',
19366
+ features: ['Right sidebar', '3 column grid', 'Categories visible', 'Modern layout']
19367
+ },
19368
+ {
19369
+ id: 'product-focused',
19370
+ name: 'Product Focused',
19371
+ description: 'Large product cards in list view',
19372
+ previewImage: '/assets/layouts/product-focused.svg',
19373
+ features: ['List view layout', 'Large product images', 'Detailed descriptions', '2 column grid']
19374
+ },
19375
+ {
19376
+ id: 'minimal',
19377
+ name: 'Minimal Store',
19378
+ description: 'Clean and simple design',
19379
+ previewImage: '/assets/layouts/minimal.svg',
19380
+ features: ['2 column grid', 'Simple header', 'Minimal footer', 'Fast loading']
19381
+ },
19382
+ {
19383
+ id: 'magazine',
19384
+ name: 'Magazine Style',
19385
+ description: 'Editorial style with featured sections',
19386
+ previewImage: '/assets/layouts/magazine.svg',
19387
+ features: ['Hero section', 'Featured content', 'Blog style', '4 column grid']
19388
+ }
19389
+ ];
19390
+
19391
+ // جلب القالب الحالي للمستخدم
19392
+ const settings = await StoreSettings.findOne({ userId: req.user.userId });
19393
+ const currentLayout = settings?.layoutSettings?.layoutTemplate || 'default';
19394
+
19395
+ res.json({
19396
+ success: true,
19397
+ templates: templates,
19398
+ currentLayout: currentLayout
19399
+ });
19400
+
19401
+ } catch (error) {
19402
+ console.error('Error fetching layout templates:', error);
19403
+ res.status(500).json({ error: 'Failed to fetch layout templates' });
19404
+ }
19405
+ });
19406
  // ============================================
19407
  // POST /api/store/contact - إرسال رسالة لصاحب المتجر (محدث)
19408
  // ============================================
 
30287
  content: section.content || {}
30288
  }))
30289
  .sort((a, b) => (a.order || 0) - (b.order || 0));
30290
+
30291
  const totalSales = await Order.aggregate([
30292
  { $match: { sellerId: user._id, status: 'completed' } },
30293
  { $group: { _id: null, total: { $sum: '$totalAmount' } } }
30294
  ]);
30295
+
30296
  const productsCount = await Product.countDocuments({ userId: user._id, isActive: true });
30297
  const followersCount = await StoreFollow.countDocuments({ storeId: user._id });
30298
 
 
30335
  glassEffect: storeSettings.glassEffect || false,
30336
  backgroundType: storeSettings.backgroundType || 'default',
30337
  backgroundValue: storeSettings.backgroundValue || '',
30338
+ layoutTemplate: storeSettings.layoutSettings?.layoutTemplate || 'default',
30339
  customCss: storeSettings.customCss || '',
30340
  // ✅ إضافة إعدادات القالب
30341
  theme: theme ? {