mirror of
https://github.com/christianvidalwolf-prog/CrazeAnalytix.git
synced 2026-08-03 13:05:24 +02:00
Update stock fetch to use Dropbox URL for Item Availability
This commit is contained in:
+17
-25
@@ -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 });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user