summaryrefslogtreecommitdiff
path: root/src/pages/api/magento-product.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/pages/api/magento-product.ts')
-rw-r--r--src/pages/api/magento-product.ts29
1 files changed, 17 insertions, 12 deletions
diff --git a/src/pages/api/magento-product.ts b/src/pages/api/magento-product.ts
index c494b05d..32dd0e5c 100644
--- a/src/pages/api/magento-product.ts
+++ b/src/pages/api/magento-product.ts
@@ -54,19 +54,14 @@ export default async function handler(
let specsWithLabels: any[] = [];
- // Cek apakah ada custom_attributes
if (data.custom_attributes) {
- // Filter atribut yang kodenya dimulai dengan 'z'
- // FIX: Menghapus filter 'Pakai link' agar MSDS tetap muncul
const zAttributes = data.custom_attributes.filter((attr: any) =>
attr.attribute_code.startsWith('z')
);
- // Fetch detail label untuk setiap atribut secara paralel
specsWithLabels = await Promise.all(
zAttributes.map(async (attr: any) => {
try {
- // Endpoint untuk ambil detail atribut (Label)
const attrUrl = `https://pimdev.1211.my.id/rest/V1/products/attributes/${attr.attribute_code}`;
const attrRes = await fetch(attrUrl, {
@@ -80,7 +75,7 @@ export default async function handler(
if (attrRes.ok) {
const attrData = await attrRes.json();
return {
- code: attr.attribute_code, // FIX: Kirim code agar bisa dideteksi frontend (z_doc_)
+ code: attr.attribute_code,
label: attrData.default_frontend_label || attr.attribute_code,
value: attr.value
};
@@ -89,12 +84,11 @@ export default async function handler(
console.error(`Failed to fetch label for ${attr.attribute_code}`);
}
- // Fallback: Format manual
const fallbackLabel = attr.attribute_code
.substring(1).replace(/_/g, ' ').trim();
return {
- code: attr.attribute_code, // FIX: Kirim code di fallback juga
+ code: attr.attribute_code,
label: fallbackLabel,
value: attr.value
};
@@ -103,15 +97,24 @@ export default async function handler(
}
// =====================================================================
- // TAMBAHAN 2: AMBIL UP-SELLS (product_links)
+ // TAMBAHAN 2: AMBIL UP-SELLS (product_links type = upsell)
// =====================================================================
let upsellIds: number[] = [];
if (data.product_links && Array.isArray(data.product_links)) {
upsellIds = data.product_links
- // Filter hanya link type 'upsell'
.filter((link: any) => link.link_type === 'upsell')
- // Ambil SKU (yang isinya ID Odoo) dan ubah ke number
+ .map((link: any) => Number(link.linked_product_sku));
+ }
+
+ // =====================================================================
+ // TAMBAHAN 3: AMBIL RELATED PRODUCTS (product_links type = related)
+ // =====================================================================
+ let relatedIds: number[] = [];
+
+ if (data.product_links && Array.isArray(data.product_links)) {
+ relatedIds = data.product_links
+ .filter((link: any) => link.link_type === 'related')
.map((link: any) => Number(link.linked_product_sku));
}
@@ -121,10 +124,12 @@ export default async function handler(
const responseData = {
...data,
specs: specsWithLabels, // Data Spesifikasi (z_*)
- upsell_ids: upsellIds // Data Upsell ID (product_links)
+ upsell_ids: upsellIds, // Data Upsell ID
+ related_ids: relatedIds // Data Related ID (BARU)
};
res.status(200).json(responseData);
+ console.log(responseData);
} catch (error) {
console.error('Proxy Server Error:', error);