"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const express_1 = require("express"); const middleware_1 = require("../auth/middleware"); const migrate_1 = require("../db/migrate"); const router = (0, express_1.Router)(); router.use(middleware_1.authMiddleware); // Get dashboard stats router.get('/stats', async (req, res) => { try { // Store stats const storesResult = await migrate_1.pool.query(` SELECT COUNT(*) as total, COUNT(*) FILTER (WHERE active = true) as active, MIN(last_scraped_at) as oldest_scrape, MAX(last_scraped_at) as latest_scrape FROM stores `); // Product stats const productsResult = await migrate_1.pool.query(` SELECT COUNT(*) as total, COUNT(*) FILTER (WHERE in_stock = true) as in_stock, COUNT(*) FILTER (WHERE local_image_path IS NOT NULL) as with_images FROM products `); // Campaign stats const campaignsResult = await migrate_1.pool.query(` SELECT COUNT(*) as total, COUNT(*) FILTER (WHERE active = true) as active FROM campaigns `); // Recent clicks (last 24 hours) const clicksResult = await migrate_1.pool.query(` SELECT COUNT(*) as clicks_24h FROM clicks WHERE clicked_at >= NOW() - INTERVAL '24 hours' `); // Recent products added (last 24 hours) const recentProductsResult = await migrate_1.pool.query(` SELECT COUNT(*) as new_products_24h FROM products WHERE first_seen_at >= NOW() - INTERVAL '24 hours' `); // Proxy stats const proxiesResult = await migrate_1.pool.query(` SELECT COUNT(*) as total, COUNT(*) FILTER (WHERE active = true) as active, COUNT(*) FILTER (WHERE is_anonymous = true) as anonymous FROM proxies `); res.json({ stores: storesResult.rows[0], products: productsResult.rows[0], campaigns: campaignsResult.rows[0], clicks: clicksResult.rows[0], recent: recentProductsResult.rows[0], proxies: proxiesResult.rows[0] }); } catch (error) { console.error('Error fetching dashboard stats:', error); res.status(500).json({ error: 'Failed to fetch dashboard stats' }); } }); // Get recent activity router.get('/activity', async (req, res) => { try { const { limit = 20 } = req.query; // Recent scrapes const scrapesResult = await migrate_1.pool.query(` SELECT s.name, s.last_scraped_at, COUNT(p.id) as product_count FROM stores s LEFT JOIN products p ON s.id = p.store_id AND p.last_seen_at = s.last_scraped_at WHERE s.last_scraped_at IS NOT NULL GROUP BY s.id, s.name, s.last_scraped_at ORDER BY s.last_scraped_at DESC LIMIT $1 `, [limit]); // Recent products const productsResult = await migrate_1.pool.query(` SELECT p.name, p.price, s.name as store_name, p.first_seen_at FROM products p JOIN stores s ON p.store_id = s.id ORDER BY p.first_seen_at DESC LIMIT $1 `, [limit]); res.json({ recent_scrapes: scrapesResult.rows, recent_products: productsResult.rows }); } catch (error) { console.error('Error fetching dashboard activity:', error); res.status(500).json({ error: 'Failed to fetch dashboard activity' }); } }); exports.default = router;