mirror of
https://github.com/christianvidalwolf-prog/CrazeAnalytix.git
synced 2026-08-03 13:45:23 +02:00
refactor: consolidate 3 MKT fetch routes into fetch-mkt-data to stay within Vercel 12-function limit
Replace fetch-deals, fetch-promos, fetch-chargebacks with a single /api/fetch-mkt-data?file=deals|promos|chargebacks endpoint. Update MktDataView fetch calls accordingly. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
49498c5c51
commit
6a1cb0e8dd
@@ -1,37 +0,0 @@
|
|||||||
import type { VercelRequest, VercelResponse } from '@vercel/node';
|
|
||||||
|
|
||||||
const CHARGEBACKS_DROPBOX_URL = "https://www.dropbox.com/scl/fi/m2qr2cxhtgkc48acjrrgx/Amazon-Operational-Chargebacks-2025.xlsx?rlkey=vlrz4qfydsjtikgh0ylivocjq&st=zdhmjvaz&dl=1";
|
|
||||||
|
|
||||||
export default async function handler(req: VercelRequest, res: VercelResponse) {
|
|
||||||
res.setHeader('Access-Control-Allow-Origin', '*');
|
|
||||||
res.setHeader('Access-Control-Allow-Methods', 'GET, OPTIONS');
|
|
||||||
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
|
|
||||||
|
|
||||||
if (req.method === 'OPTIONS') {
|
|
||||||
return res.status(200).end();
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
console.log('[fetch-chargebacks] Fetching Chargebacks from Dropbox...');
|
|
||||||
const response = await fetch(CHARGEBACKS_DROPBOX_URL, {
|
|
||||||
cache: 'no-store',
|
|
||||||
headers: {
|
|
||||||
'Pragma': 'no-cache',
|
|
||||||
'Cache-Control': 'no-cache'
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
throw new Error(`Dropbox responded with ${response.status}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const buffer = await response.arrayBuffer();
|
|
||||||
console.log('[fetch-chargebacks] Successfully fetched Chargebacks Excel, size:', buffer.byteLength);
|
|
||||||
|
|
||||||
res.setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
|
|
||||||
res.status(200).send(Buffer.from(buffer));
|
|
||||||
} catch (error: any) {
|
|
||||||
console.error('[fetch-chargebacks] Error:', error);
|
|
||||||
res.status(500).json({ error: error.message });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,37 +0,0 @@
|
|||||||
import type { VercelRequest, VercelResponse } from '@vercel/node';
|
|
||||||
|
|
||||||
const DEALS_DROPBOX_URL = "https://www.dropbox.com/scl/fi/3mm5m4e04myhqt59piefs/Deals-Amazon-2025.xlsx?rlkey=6llr0xhs6di0c3d8o96ashzmv&st=ggu0z7hu&dl=1";
|
|
||||||
|
|
||||||
export default async function handler(req: VercelRequest, res: VercelResponse) {
|
|
||||||
res.setHeader('Access-Control-Allow-Origin', '*');
|
|
||||||
res.setHeader('Access-Control-Allow-Methods', 'GET, OPTIONS');
|
|
||||||
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
|
|
||||||
|
|
||||||
if (req.method === 'OPTIONS') {
|
|
||||||
return res.status(200).end();
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
console.log('[fetch-deals] Fetching Deals from Dropbox...');
|
|
||||||
const response = await fetch(DEALS_DROPBOX_URL, {
|
|
||||||
cache: 'no-store',
|
|
||||||
headers: {
|
|
||||||
'Pragma': 'no-cache',
|
|
||||||
'Cache-Control': 'no-cache'
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
throw new Error(`Dropbox responded with ${response.status}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const buffer = await response.arrayBuffer();
|
|
||||||
console.log('[fetch-deals] Successfully fetched Deals Excel, size:', buffer.byteLength);
|
|
||||||
|
|
||||||
res.setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
|
|
||||||
res.status(200).send(Buffer.from(buffer));
|
|
||||||
} catch (error: any) {
|
|
||||||
console.error('[fetch-deals] Error:', error);
|
|
||||||
res.status(500).json({ error: error.message });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
import type { VercelRequest, VercelResponse } from '@vercel/node';
|
||||||
|
|
||||||
|
const FILE_URLS: Record<string, string> = {
|
||||||
|
deals: "https://www.dropbox.com/scl/fi/3mm5m4e04myhqt59piefs/Deals-Amazon-2025.xlsx?rlkey=6llr0xhs6di0c3d8o96ashzmv&st=ggu0z7hu&dl=1",
|
||||||
|
promos: "https://www.dropbox.com/scl/fi/ufhej9do9w839oyyfrpk3/Promos_Amazon_2025_all-countries.xlsx?rlkey=4foano9dt5h3sl58l35vqg89d&st=wu0t56wh&dl=1",
|
||||||
|
chargebacks: "https://www.dropbox.com/scl/fi/m2qr2cxhtgkc48acjrrgx/Amazon-Operational-Chargebacks-2025.xlsx?rlkey=vlrz4qfydsjtikgh0ylivocjq&st=zdhmjvaz&dl=1",
|
||||||
|
};
|
||||||
|
|
||||||
|
export default async function handler(req: VercelRequest, res: VercelResponse) {
|
||||||
|
res.setHeader('Access-Control-Allow-Origin', '*');
|
||||||
|
res.setHeader('Access-Control-Allow-Methods', 'GET, OPTIONS');
|
||||||
|
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
|
||||||
|
|
||||||
|
if (req.method === 'OPTIONS') {
|
||||||
|
return res.status(200).end();
|
||||||
|
}
|
||||||
|
|
||||||
|
const file = req.query.file as string;
|
||||||
|
const url = FILE_URLS[file];
|
||||||
|
|
||||||
|
if (!url) {
|
||||||
|
return res.status(400).json({ error: `Unknown file: "${file}". Valid options: deals, promos, chargebacks` });
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
console.log(`[fetch-mkt-data] Fetching ${file} from Dropbox...`);
|
||||||
|
const response = await fetch(url, {
|
||||||
|
cache: 'no-store',
|
||||||
|
headers: { 'Pragma': 'no-cache', 'Cache-Control': 'no-cache' }
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(`Dropbox responded with ${response.status}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const buffer = await response.arrayBuffer();
|
||||||
|
console.log(`[fetch-mkt-data] Successfully fetched ${file}, size:`, buffer.byteLength);
|
||||||
|
|
||||||
|
res.setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
|
||||||
|
res.status(200).send(Buffer.from(buffer));
|
||||||
|
} catch (error: any) {
|
||||||
|
console.error(`[fetch-mkt-data] Error fetching ${file}:`, error);
|
||||||
|
res.status(500).json({ error: error.message });
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,37 +0,0 @@
|
|||||||
import type { VercelRequest, VercelResponse } from '@vercel/node';
|
|
||||||
|
|
||||||
const PROMOS_DROPBOX_URL = "https://www.dropbox.com/scl/fi/ufhej9do9w839oyyfrpk3/Promos_Amazon_2025_all-countries.xlsx?rlkey=4foano9dt5h3sl58l35vqg89d&st=wu0t56wh&dl=1";
|
|
||||||
|
|
||||||
export default async function handler(req: VercelRequest, res: VercelResponse) {
|
|
||||||
res.setHeader('Access-Control-Allow-Origin', '*');
|
|
||||||
res.setHeader('Access-Control-Allow-Methods', 'GET, OPTIONS');
|
|
||||||
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
|
|
||||||
|
|
||||||
if (req.method === 'OPTIONS') {
|
|
||||||
return res.status(200).end();
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
console.log('[fetch-promos] Fetching Promos from Dropbox...');
|
|
||||||
const response = await fetch(PROMOS_DROPBOX_URL, {
|
|
||||||
cache: 'no-store',
|
|
||||||
headers: {
|
|
||||||
'Pragma': 'no-cache',
|
|
||||||
'Cache-Control': 'no-cache'
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
throw new Error(`Dropbox responded with ${response.status}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const buffer = await response.arrayBuffer();
|
|
||||||
console.log('[fetch-promos] Successfully fetched Promos Excel, size:', buffer.byteLength);
|
|
||||||
|
|
||||||
res.setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
|
|
||||||
res.status(200).send(Buffer.from(buffer));
|
|
||||||
} catch (error: any) {
|
|
||||||
console.error('[fetch-promos] Error:', error);
|
|
||||||
res.status(500).json({ error: error.message });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -109,9 +109,9 @@ export default function MktDataView({ rawData, adsData }: MktDataViewProps) {
|
|||||||
setMktLoading(true);
|
setMktLoading(true);
|
||||||
try {
|
try {
|
||||||
const [dealsRes, promosRes, chargesRes] = await Promise.all([
|
const [dealsRes, promosRes, chargesRes] = await Promise.all([
|
||||||
fetch('/api/fetch-deals'),
|
fetch('/api/fetch-mkt-data?file=deals'),
|
||||||
fetch('/api/fetch-promos'),
|
fetch('/api/fetch-mkt-data?file=promos'),
|
||||||
fetch('/api/fetch-chargebacks'),
|
fetch('/api/fetch-mkt-data?file=chargebacks'),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
if (cancelled) return;
|
if (cancelled) return;
|
||||||
@@ -120,21 +120,21 @@ export default function MktDataView({ rawData, adsData }: MktDataViewProps) {
|
|||||||
const buf = await dealsRes.arrayBuffer();
|
const buf = await dealsRes.arrayBuffer();
|
||||||
if (!cancelled) setDealsMap(parseDealsExcel(buf));
|
if (!cancelled) setDealsMap(parseDealsExcel(buf));
|
||||||
} else {
|
} else {
|
||||||
console.warn('[MktDataView] fetch-deals failed:', dealsRes.status);
|
console.warn('[MktDataView] fetch-mkt-data?file=deals failed:', dealsRes.status);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (promosRes.ok) {
|
if (promosRes.ok) {
|
||||||
const buf = await promosRes.arrayBuffer();
|
const buf = await promosRes.arrayBuffer();
|
||||||
if (!cancelled) setPromosMap(parsePromosExcel(buf));
|
if (!cancelled) setPromosMap(parsePromosExcel(buf));
|
||||||
} else {
|
} else {
|
||||||
console.warn('[MktDataView] fetch-promos failed:', promosRes.status);
|
console.warn('[MktDataView] fetch-mkt-data?file=promos failed:', promosRes.status);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (chargesRes.ok) {
|
if (chargesRes.ok) {
|
||||||
const buf = await chargesRes.arrayBuffer();
|
const buf = await chargesRes.arrayBuffer();
|
||||||
if (!cancelled) setChargebacksMap(parseChargebacksExcel(buf));
|
if (!cancelled) setChargebacksMap(parseChargebacksExcel(buf));
|
||||||
} else {
|
} else {
|
||||||
console.warn('[MktDataView] fetch-chargebacks failed:', chargesRes.status);
|
console.warn('[MktDataView] fetch-mkt-data?file=chargebacks failed:', chargesRes.status);
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('[MktDataView] Error fetching marketing data:', e);
|
console.error('[MktDataView] Error fetching marketing data:', e);
|
||||||
|
|||||||
Reference in New Issue
Block a user