File size: 3,201 Bytes
b89a86e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/**
 * Script to add placeholder images to existing categories and products
 * Uses Lorem Picsum for free placeholder images
 */

import { storage } from './storage';

// Generate random Lorem Picsum image URL
function getPlaceholderImageUrl(width: number = 400, height: number = 300): string {
  const randomId = Math.floor(Math.random() * 1000) + 1;
  return `https://picsum.photos/${width}/${height}?random=${randomId}`;
}

// Get category-specific image URL
function getCategoryImageUrl(categoryName: string): string {
  const randomId = Math.floor(Math.random() * 1000) + 1;
  return `https://picsum.photos/200/200?random=${randomId}`;
}

// Get product images array (3 images per product)
function getProductImages(): string[] {
  const images: string[] = [];
  for (let i = 0; i < 3; i++) {
    const randomId = Math.floor(Math.random() * 1000) + 1;
    images.push(`https://picsum.photos/400/400?random=${randomId}`);
  }
  return images;
}

export async function addPlaceholderImages() {
  console.log('๐Ÿ–ผ๏ธ  Adding placeholder images to products and categories...\n');
  
  try {
    // Update categories
    console.log('Fetching categories...');
    const categories = await storage.getAllCategories();
    console.log(`Found ${categories.length} categories`);
    
    for (const category of categories) {
      if (!category.imageUrl) {
        const imageUrl = getCategoryImageUrl(category.name);
        
        console.log(`Updating category "${category.name}" with image: ${imageUrl}`);
        
        await storage.updateCategory(category.id, { imageUrl });
        console.log(`โœ“ Updated category: ${category.name}`);
      } else {
        console.log(`Category "${category.name}" already has an image`);
      }
    }
    
    console.log('');
    
    // Update products
    console.log('Fetching products...');
    const products = await storage.getAllProducts();
    console.log(`Found ${products.length} products`);
    
    for (const product of products) {
      if (!product.images || product.images.length === 0) {
        const images = getProductImages();
        
        console.log(`Updating product "${product.title}" with ${images.length} images`);
        
        await storage.updateProduct(product.id, { images });
        console.log(`โœ“ Updated product: ${product.title}`);
      } else {
        console.log(`Product "${product.title}" already has ${product.images.length} image(s)`);
      }
    }
    
    console.log('\nโœ… Finished adding placeholder images!');
    return { success: true, message: 'Placeholder images added successfully' };
    
  } catch (error) {
    console.error('Error adding placeholder images:', error);
    return { success: false, error: error instanceof Error ? error.message : 'Unknown error' };
  }
}

// Add API endpoint to trigger this function
export function addPlaceholderImagesRoute(app: any) {
  app.post('/api/admin/add-placeholder-images', async (req: any, res: any) => {
    try {
      const result = await addPlaceholderImages();
      res.json(result);
    } catch (error) {
      res.status(500).json({ success: false, error: error instanceof Error ? error.message : 'Unknown error' });
    }
  });
}