Update stock fetch to use Dropbox URL for Item Availability

This commit is contained in:
Christian Vidal Wolf
2026-02-03 08:55:45 +01:00
parent ceb389cfb0
commit c4ad999619
2 changed files with 19 additions and 27 deletions
+2 -2
View File
@@ -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();
+17 -25
View File
@@ -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 });
}
}