mirror of
https://github.com/christianvidalwolf-prog/CrazeAnalytix.git
synced 2026-08-03 11:55:22 +02:00
Update stock fetch to use Dropbox URL for Item Availability
This commit is contained in:
@@ -170,8 +170,8 @@ const App: React.FC = () => {
|
|||||||
|
|
||||||
const handleStockFetch = useCallback(async () => {
|
const handleStockFetch = useCallback(async () => {
|
||||||
try {
|
try {
|
||||||
console.log('[App] Fetching stock from /Item Availability.xlsx...');
|
console.log('[App] Fetching stock from /api/fetch-stock...');
|
||||||
const response = await fetch('/Item Availability.xlsx');
|
const response = await fetch('/api/fetch-stock');
|
||||||
if (!response.ok) throw new Error(`Failed to fetch stock: ${response.status}`);
|
if (!response.ok) throw new Error(`Failed to fetch stock: ${response.status}`);
|
||||||
|
|
||||||
const buffer = await response.arrayBuffer();
|
const buffer = await response.arrayBuffer();
|
||||||
|
|||||||
+17
-25
@@ -1,7 +1,8 @@
|
|||||||
|
|
||||||
import type { VercelRequest, VercelResponse } from '@vercel/node';
|
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) {
|
export default async function handler(req: VercelRequest, res: VercelResponse) {
|
||||||
// CORS headers
|
// CORS headers
|
||||||
@@ -14,36 +15,27 @@ export default async function handler(req: VercelRequest, res: VercelResponse) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
console.log('[fetch-stock] Reading Stock file from local storage...');
|
console.log('[fetch-stock] Fetching Stock from Dropbox...');
|
||||||
// Try multiple possible paths to be robust
|
const response = await fetch(STOCK_DROPBOX_URL, {
|
||||||
const pathsToTry = [
|
cache: 'no-store',
|
||||||
path.join(process.cwd(), 'Item Availability.xlsx'),
|
headers: {
|
||||||
path.join(process.cwd(), 'public', 'Item Availability.xlsx'),
|
'Pragma': 'no-cache',
|
||||||
path.join('/Users/christianvidalwolf/github/CrazeAnalytix', 'Item Availability.xlsx')
|
'Cache-Control': 'no-cache'
|
||||||
];
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(`Dropbox responded with ${response.status}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!buffer) {
|
const buffer = await response.arrayBuffer();
|
||||||
throw new Error(`Stock file 'Item Availability.xlsx' not found in any of: ${pathsToTry.join(', ')}`);
|
console.log('[fetch-stock] Successfully fetched Stock Excel, size:', buffer.byteLength);
|
||||||
}
|
|
||||||
|
|
||||||
console.log(`[fetch-stock] Successfully read Stock Excel from ${foundPath}, size: ${buffer.byteLength}`);
|
|
||||||
|
|
||||||
res.setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
|
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) {
|
} catch (error: any) {
|
||||||
console.error('[fetch-stock] Error:', error);
|
console.error('[fetch-stock] Error:', error);
|
||||||
res.status(500).json({ error: error.message });
|
res.status(500).json({ error: error.message });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user