summaryrefslogtreecommitdiff
path: root/src/pages
diff options
context:
space:
mode:
authorMiqdad <ahmadmiqdad27@gmail.com>2025-06-06 13:20:29 +0700
committerMiqdad <ahmadmiqdad27@gmail.com>2025-06-06 13:20:29 +0700
commit56ba7cb194e8a664cb0144e7091159e10b8c56ab (patch)
treea8ee81b818067a01434d21a5835b8d72b09bb81b /src/pages
parentf42a816422a0c7fab40a98123cadf8268e05d37e (diff)
<miqdad> private banner section
Diffstat (limited to 'src/pages')
-rw-r--r--src/pages/api/banner-section.js74
1 files changed, 66 insertions, 8 deletions
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' });
}
}