From c4ad999619197d438c65c4be62d470ed9550a694 Mon Sep 17 00:00:00 2001 From: Christian Vidal Wolf Date: Tue, 3 Feb 2026 08:55:45 +0100 Subject: [PATCH] Update stock fetch to use Dropbox URL for Item Availability --- App.tsx | 4 ++-- api/fetch-stock.ts | 42 +++++++++++++++++------------------------- 2 files changed, 19 insertions(+), 27 deletions(-) diff --git a/App.tsx b/App.tsx index 12ac8c7..c3d208f 100644 --- a/App.tsx +++ b/App.tsx @@ -170,8 +170,8 @@ const App: React.FC = () => { const handleStockFetch = useCallback(async () => { try { - console.log('[App] Fetching stock from /Item Availability.xlsx...'); - const response = await fetch('/Item Availability.xlsx'); + console.log('[App] Fetching stock from /api/fetch-stock...'); + const response = await fetch('/api/fetch-stock'); if (!response.ok) throw new Error(`Failed to fetch stock: ${response.status}`); const buffer = await response.arrayBuffer(); diff --git a/api/fetch-stock.ts b/api/fetch-stock.ts index ce0f6a9..a7233dc 100644 --- a/api/fetch-stock.ts +++ b/api/fetch-stock.ts @@ -1,7 +1,8 @@ import type { VercelRequest, VercelResponse } from '@vercel/node'; -import fs from 'fs'; -import path from 'path'; + +// Item Availability from Dropbox (using dl=1 for direct download) +const STOCK_DROPBOX_URL = "https://www.dropbox.com/scl/fi/jaxc1y01ot7wj4ksswaoe/Item-Availability.xlsx?rlkey=v428jlri5jf1z5ggiebze7ovo&st=3uzg6csf&dl=1"; export default async function handler(req: VercelRequest, res: VercelResponse) { // CORS headers @@ -14,36 +15,27 @@ export default async function handler(req: VercelRequest, res: VercelResponse) { } try { - console.log('[fetch-stock] Reading Stock file from local storage...'); - // Try multiple possible paths to be robust - const pathsToTry = [ - path.join(process.cwd(), 'Item Availability.xlsx'), - path.join(process.cwd(), 'public', 'Item Availability.xlsx'), - path.join('/Users/christianvidalwolf/github/CrazeAnalytix', 'Item Availability.xlsx') - ]; - - let buffer = null; - let foundPath = ''; - - for (const p of pathsToTry) { - console.log(`[fetch-stock] Checking path: ${p}`); - if (fs.existsSync(p)) { - buffer = fs.readFileSync(p); - foundPath = p; - break; + console.log('[fetch-stock] Fetching Stock from Dropbox...'); + const response = await fetch(STOCK_DROPBOX_URL, { + cache: 'no-store', + headers: { + 'Pragma': 'no-cache', + 'Cache-Control': 'no-cache' } + }); + + if (!response.ok) { + throw new Error(`Dropbox responded with ${response.status}`); } - if (!buffer) { - throw new Error(`Stock file 'Item Availability.xlsx' not found in any of: ${pathsToTry.join(', ')}`); - } - - console.log(`[fetch-stock] Successfully read Stock Excel from ${foundPath}, size: ${buffer.byteLength}`); + const buffer = await response.arrayBuffer(); + console.log('[fetch-stock] Successfully fetched Stock Excel, size:', buffer.byteLength); res.setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); - res.status(200).send(buffer); + res.status(200).send(Buffer.from(buffer)); } catch (error: any) { console.error('[fetch-stock] Error:', error); res.status(500).json({ error: error.message }); } } +