import odooApi from '@/core/api/odooApi'; import { createClient } from 'redis'; const client = createClient(); client.on('error', (err) => console.error('Redis Client Error', err)); const connectRedis = async () => { if (!client.isOpen) { await client.connect(); } }; export default async function handler(req, res) { if (req.method !== 'GET') { return res.status(405).json({ error: 'Method not allowed' }); } try { await connectRedis(); const type = req.query.type || 'home-banner'; if (type === 'private-brand') { // Handle multiple private brand banner types const bannerTypes = [ 'banner-brand-footer', 'banner-brand-tengah-footer', 'banner-brand-kanan-footer', ]; const allBanners = []; for (const brandType of bannerTypes) { const brandCacheKey = `homepage_bannerSection_${brandType}`; let cachedData = await client.get(brandCacheKey); if (cachedData) { const data = JSON.parse(cachedData); allBanners.push(...(data || [])); } else { try { const dataBannerSections = await odooApi( 'GET', `/api/v1/banner?type=${brandType}` ); if (dataBannerSections && dataBannerSections.length > 0) { await client.set( brandCacheKey, JSON.stringify(dataBannerSections), 'EX', 259200 ); allBanners.push(...dataBannerSections); } } catch (error) { continue; } } } return res.status(200).json({ data: allBanners, total: allBanners.length, }); } // Handle home-banner and other single types let cacheKey; let apiEndpoint; if (type === 'home-banner') { cacheKey = 'hero-banner'; apiEndpoint = '/api/v1/banner?type=home-banner'; } else { cacheKey = `homepage_bannerSection_${type}`; apiEndpoint = `/api/v1/banner?type=${type}`; } let cachedData = await client.get(cacheKey); if (cachedData) { const data = JSON.parse(cachedData); return res.status(200).json({ data }); } else { const dataBannerSections = await odooApi('GET', apiEndpoint); await client.set( cacheKey, JSON.stringify(dataBannerSections), 'EX', 259200 ); return res.status(200).json({ data: dataBannerSections }); } } catch (error) { return res.status(500).json({ error: 'Internal Server Error' }); } }