Files
CrazeAnalytix/api/fetch-vendor-stock.ts
T

50 lines
1.8 KiB
TypeScript

import type { VercelRequest, VercelResponse } from '@vercel/node';
import fs from 'fs';
import path from 'path';
export default async function handler(req: VercelRequest, res: VercelResponse) {
// CORS headers
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-vendor-stock] Reading Vendor Stock file from local storage...');
// Try multiple possible paths to be robust
const pathsToTry = [
path.join(process.cwd(), 'Vendor Stock.xlsx'),
path.join(process.cwd(), 'public', 'Vendor Stock.xlsx'),
path.join('/Users/christianvidalwolf/github/CrazeAnalytix', 'Vendor Stock.xlsx')
];
let buffer = null;
let foundPath = '';
for (const p of pathsToTry) {
console.log(`[fetch-vendor-stock] Checking path: ${p}`);
if (fs.existsSync(p)) {
buffer = fs.readFileSync(p);
foundPath = p;
break;
}
}
if (!buffer) {
throw new Error(`Vendor Stock file 'Vendor Stock.xlsx' not found in any of: ${pathsToTry.join(', ')}`);
}
console.log(`[fetch-vendor-stock] Successfully read Vendor Stock Excel from ${foundPath}, size: ${buffer.byteLength}`);
res.setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
res.status(200).send(buffer);
} catch (error: any) {
console.error('[fetch-vendor-stock] Error:', error);
res.status(500).json({ error: error.message });
}
}