summaryrefslogtreecommitdiff
path: root/src-migrate/services/product.ts
blob: f77fc3eccbc66d622aabddfda4f2ce73e6c5a090 (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
103
104
import { IProduct, IProductDetail } from '~/types/product';
import snakeCase from 'snakecase-keys';
import odooApi from '~/libs/odooApi';
import { ICategoryBreadcrumb } from '~/types/category';

const SELF_HOST = process.env.NEXT_PUBLIC_SELF_HOST;

export const getProductById = async (
  id: string,
  tier: string
): Promise<IProductDetail | null> => {
  const url = `${SELF_HOST}/api/shop/product-detail`;
  const params = new URLSearchParams({ id, auth: tier });
  return await fetch(`${url}?${params.toString()}`)
    .then((res) => res.json())
    .then((res) => {
      if (res.length > 0) return snakeCase(res[0]) as IProductDetail;
      return null;
    });
};

export interface GetProductSimilarProps {
  name: string;
  except?: {
    productId?: number;
    manufactureId?: number;
  };
  limit?: number;
}

export interface GetProductSimilarRes {
  products: IProduct[];
  num_found: number;
  num_found_exact: boolean;
  start: number;
}

export const getProductSimilar = async ({
  name,
  except,
  limit = 30,
}: GetProductSimilarProps): Promise<GetProductSimilarRes> => {
  const query = [
    `q=${name}`,
    'page=1',
    'operation=OR',
    // 'priceFrom=1',
    `source=similar`,
  ];

  if (except?.productId) query.push(`fq=-product_id_i:${except.productId}`);
  if (except?.manufactureId)
    query.push(`fq=-manufacture_id_i:${except.manufactureId}`);

  const url = `${SELF_HOST}/api/shop/search?${query.join('&')}`;

  return await fetch(url)
    .then((res) => res.json())
    .then((res) => snakeCase(res.response));
};

export const getProductCategoryBreadcrumb = async (
  id: number
): Promise<ICategoryBreadcrumb[]> => {
  return await odooApi('GET', `/api/v1/product/${id}/category-breadcrumb`);
};

// =================================================================
// TAMBAHAN BARU: SERVICE FETCH BY LIST ID (UNTUK UPSELL/RELATED)
// =================================================================

export interface GetProductsByIdsProps {
  ids: number[];
}

export const getProductsByIds = async ({
  ids,
}: GetProductsByIdsProps): Promise<GetProductSimilarRes> => {
  // Jika array ID kosong, kembalikan object kosong agar tidak error
  if (!ids || ids.length === 0) {
    return {
        products: [],
        num_found: 0,
        num_found_exact: true,
        start: 0
    };
  }

  // Buat query Solr format: product_id_i:(224102 OR 88019 OR ...)
  const idQuery = ids.join(' OR ');

  const query = [
    `q=*`, // Query wildcard (ambil semua)
    `fq=product_id_i:(${idQuery})`, // TAPI difilter hanya ID yang ada di list upsell
    'rows=20',
    `source=upsell`,
  ];

  const url = `${SELF_HOST}/api/shop/search?${query.join('&')}`;

  return await fetch(url)
    .then((res) => res.json())
    .then((res) => snakeCase(res.response));
};