summaryrefslogtreecommitdiff
path: root/src/pages/api/banner-section.js
blob: ab48f3f4a0d2e631aaecd1ea9dacf2a07bcd3999 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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' });
  }
}