summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorFIN-IT_AndriFP <andrifebriyadiputra@gmail.com>2025-11-28 09:36:15 +0700
committerFIN-IT_AndriFP <andrifebriyadiputra@gmail.com>2025-11-28 09:36:15 +0700
commita5e695f82e03577cc85c4a1dded9f6021f0235fc (patch)
tree854eb7e74f4beb94bf494979ad0cb31870ce06db /src
parent629133beeb7f5668b63db61e415cf844d513cde7 (diff)
(andri) try get detail product from magento
Diffstat (limited to 'src')
-rw-r--r--src/pages/api/magento-product.ts114
1 files changed, 114 insertions, 0 deletions
diff --git a/src/pages/api/magento-product.ts b/src/pages/api/magento-product.ts
new file mode 100644
index 00000000..c906079e
--- /dev/null
+++ b/src/pages/api/magento-product.ts
@@ -0,0 +1,114 @@
+import type { NextApiRequest, NextApiResponse } from 'next';
+
+export default async function handler(
+ req: NextApiRequest,
+ res: NextApiResponse
+) {
+ const { sku } = req.query;
+
+ if (!sku) {
+ return res.status(400).json({ error: 'SKU is required' });
+ }
+
+ // Token Magento
+ const token = 'vxrtcjvztv1icgjzsui45de9kmwlz0lf';
+ const baseUrl = 'https://pimdev.1211.my.id/rest/V1/products';
+
+ try {
+ // 1. Pastikan SKU menjadi string dan hapus spasi kiri/kanan
+ const cleanSku = String(sku).trim();
+
+ // 2. Encode SKU
+ const encodedSku = encodeURIComponent(cleanSku);
+
+ // 3. Bentuk URL Final
+ const finalUrl = `${baseUrl}/${encodedSku}`;
+
+ // --- DEBUGGING LOG ---
+ console.log('Fetching URL:', finalUrl);
+
+ // Request ke Product Endpoint
+ const response = await fetch(finalUrl, {
+ method: 'GET',
+ headers: {
+ 'Content-Type': 'application/json',
+ 'Authorization': `Bearer ${token}`,
+ },
+ });
+
+ if (!response.ok) {
+ console.error(`Magento Error: ${response.status} ${response.statusText}`);
+ return res.status(response.status).json({
+ error: 'Failed to fetch from Magento',
+ magentoStatus: response.status,
+ checkedUrl: finalUrl
+ });
+ }
+
+ const data = await response.json();
+
+ // =====================================================================
+ // TAMBAHAN: FETCH LABEL ATRIBUT (z_*)
+ // =====================================================================
+
+ let specsWithLabels: any[] = [];
+
+ // Cek apakah ada custom_attributes
+ if (data.custom_attributes) {
+ // Filter atribut yang kodenya dimulai dengan 'z'
+ 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): /V1/products/attributes/{attributeCode}
+ const attrUrl = `${baseUrl}/attributes/${attr.attribute_code}`;
+
+ const attrRes = await fetch(attrUrl, {
+ method: 'GET',
+ headers: {
+ 'Authorization': `Bearer ${token}`,
+ 'Content-Type': 'application/json',
+ }
+ });
+
+ if (attrRes.ok) {
+ const attrData = await attrRes.json();
+ // AMBIL NILAI 'default_frontend_label'
+ return {
+ label: attrData.default_frontend_label || attr.attribute_code,
+ value: attr.value
+ };
+ }
+ } catch (err) {
+ console.error(`Failed to fetch label for ${attr.attribute_code}`);
+ }
+
+ // Fallback: Jika gagal ambil label, format manual dari kode
+ const fallbackLabel = attr.attribute_code
+ .substring(1).replace(/_/g, ' ').trim(); // z_size_ml -> size ml
+
+ return {
+ label: fallbackLabel,
+ value: attr.value
+ };
+ })
+ );
+ }
+
+ // Gabungkan data asli dengan data specs yang sudah ada labelnya
+ const responseData = {
+ ...data,
+ specs: specsWithLabels // Frontend tinggal pakai field ini
+ };
+
+ res.status(200).json(responseData);
+
+ } catch (error) {
+ console.error('Proxy Server Error:', error);
+ res.status(500).json({ error: 'Internal Server Error' });
+ }
+} \ No newline at end of file