diff options
| author | trisusilo48 <tri.susilo@altama.co.id> | 2024-12-23 11:20:56 +0700 |
|---|---|---|
| committer | trisusilo48 <tri.susilo@altama.co.id> | 2024-12-23 11:20:56 +0700 |
| commit | 21f3d78e6c58d9b509f3ea234af462807ef1301d (patch) | |
| tree | 9bad9467325819888b73a86804222e313c6c9504 /src/pages/api | |
| parent | d1592286eef165533c21d52aec70dbb703cdcfd3 (diff) | |
| parent | 9962d114e590bfc5e6c865489ab4dcd84de81ad9 (diff) | |
Merge branch 'new-release' into feature/integrasi_biteship
Diffstat (limited to 'src/pages/api')
| -rw-r--r-- | src/pages/api/flashsale-header.js | 36 | ||||
| -rw-r--r-- | src/pages/api/hero-banner.js | 2 | ||||
| -rw-r--r-- | src/pages/api/search-flashsale.js | 26 |
3 files changed, 46 insertions, 18 deletions
diff --git a/src/pages/api/flashsale-header.js b/src/pages/api/flashsale-header.js index 31f8efdd..578801ae 100644 --- a/src/pages/api/flashsale-header.js +++ b/src/pages/api/flashsale-header.js @@ -15,23 +15,41 @@ export default async function handler(req, res) { try { await connectRedis(); const cacheKey = `flashsale_header`; - // await client.del(cacheKey); + + // Cek data yang ada di Redis let cachedData = await client.get(cacheKey); if (cachedData) { const data = JSON.parse(cachedData); + + // Periksa apakah data adalah array dan panjangnya 0 + if (!data || (Array.isArray(data) && data.length === 0)) { + await client.del(cacheKey); // Hapus kunci jika data kosong + return res.status(200).json({ data: [] }); + } + // Periksa apakah end_date lebih besar dari waktu saat ini + const currentTime = new Date(); + if (data[0].endDate && new Date(data[0].endDate) < currentTime) { + await client.del(cacheKey); // Hapus kunci jika end_date lebih kecil dari waktu saat ini + return res.status(200).json({ data: [] }); + } return res.status(200).json({ data }); } else { const flashSale = await odooApi('GET', `/api/v1/flashsale/header`); + if (flashSale.length === 0) { + return res.status(200).json({ data: [] }); + } else { + await client.set( + cacheKey, + JSON.stringify(flashSale), + 'EX', + flashSale.duration + ); - await client.set( - cacheKey, - JSON.stringify(flashSale), - 'EX', - flashSale.duration - ); - cachedData = await client.get(cacheKey); - return res.status(200).json({ data: cachedData }); + cachedData = await client.get(cacheKey); + const data = JSON.parse(cachedData); + return res.status(200).json({ data }); + } } } catch (error) { console.error('Error interacting with Redis or fetching data:', error); diff --git a/src/pages/api/hero-banner.js b/src/pages/api/hero-banner.js index 7a348cfa..b7f3c1c5 100644 --- a/src/pages/api/hero-banner.js +++ b/src/pages/api/hero-banner.js @@ -28,6 +28,8 @@ export default async function handler(req, res) { `/api/v1/banner?type=${type}` ); + if(!dataBannerSections) return res.status(200).json({ data: [] }); + // Simpan hasil fetch ke Redis dengan masa kadaluarsa 3 hari (259200 detik) await client.set( cacheKey, diff --git a/src/pages/api/search-flashsale.js b/src/pages/api/search-flashsale.js index d9e56c83..c00f6c64 100644 --- a/src/pages/api/search-flashsale.js +++ b/src/pages/api/search-flashsale.js @@ -23,20 +23,28 @@ export default async function handler(req, res) { if (cachedData) { const data = JSON.parse(cachedData); + // Periksa apakah data adalah array dan panjangnya 0 + if (!data || (Array.isArray(data) && data.length === 0)) { + await client.del(cacheKey); // Hapus kunci jika data kosong + return res.status(200).json({ data: [] }); + } return res.status(200).json({ data }); } else { const dataProductSearch = await axios( `${process.env.NEXT_PUBLIC_SELF_HOST}/api/shop/search?${query}&operation=${operation}]` ); - - await client.set( - cacheKey, - JSON.stringify(dataProductSearch.data), - 'EX', - duration - ); - cachedData = await client.get(cacheKey); - return res.status(200).json({ data: cachedData }); + if (dataProductSearch.data.length === 0) { + return res.status(200).json({ data: [] }); + } else { + await client.set( + cacheKey, + JSON.stringify(dataProductSearch.data), + 'EX', + duration + ); + cachedData = await client.get(cacheKey); + return res.status(200).json({ data: cachedData }); + } } } catch (error) { console.error('Error interacting with Redis or fetching data:', error); |
