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 => { 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 => { 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 => { 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 => { if (!ids || ids.length === 0) { return { products: [], num_found: 0, num_found_exact: true, start: 0 }; } const idQuery = ids.join(' OR '); const query = [ `q=*`, `fq=(id:(${idQuery}) OR product_id_i:(${idQuery}))`, 'rows=20', `source=upsell`, ]; const url = `${SELF_HOST}/api/shop/search?${query.join('&')}`; // Request const res = await fetch(url).then((res) => res.json()); // LOG 2: Hasil Pencarian SOLR console.group("🔍 2. [Solr Search Result]"); console.log("Request URL:", url); console.log("Requested IDs:", ids); const foundDocs = res.response?.docs || []; const foundIds = foundDocs.map((doc: any) => doc.id || doc.product_id_i); console.log("Found Products Count:", res.response?.numFound); console.log("Found IDs:", foundIds); // Cek ID mana yang hilang const missingIds = ids.filter((reqId) => !foundIds.includes(String(reqId)) && !foundIds.includes(Number(reqId))); if (missingIds.length > 0) { console.warn("⚠️ MISSING / NOT FOUND IDs:", missingIds); } else { console.log("✅ All IDs Found!"); } console.groupEnd(); return snakeCase(res.response); };