From 56ba7cb194e8a664cb0144e7091159e10b8c56ab Mon Sep 17 00:00:00 2001 From: Miqdad Date: Fri, 6 Jun 2025 13:20:29 +0700 Subject: private banner section --- src/pages/api/banner-section.js | 74 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 66 insertions(+), 8 deletions(-) (limited to 'src/pages/api') diff --git a/src/pages/api/banner-section.js b/src/pages/api/banner-section.js index 7d7040c0..ab48f3f4 100644 --- a/src/pages/api/banner-section.js +++ b/src/pages/api/banner-section.js @@ -12,22 +12,81 @@ const connectRedis = async () => { }; export default async function handler(req, res) { + if (req.method !== 'GET') { + return res.status(405).json({ error: 'Method not allowed' }); + } + try { await connectRedis(); - const cacheKey = 'hero-banner'; - // await client.del(cacheKey); + + 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', - '/api/v1/banner?type=home-banner' - ); + const dataBannerSections = await odooApi('GET', apiEndpoint); - // Simpan hasil fetch ke Redis dengan masa kadaluarsa 3 hari (259200 detik) await client.set( cacheKey, JSON.stringify(dataBannerSections), @@ -38,7 +97,6 @@ export default async function handler(req, res) { return res.status(200).json({ data: dataBannerSections }); } } catch (error) { - console.error('Error interacting with Redis or fetching data:', error); return res.status(500).json({ error: 'Internal Server Error' }); } } -- cgit v1.2.3