Files
CrazeAnalytix/api/fetch-forecast.ts
T

50 lines
1.8 KiB
TypeScript
Raw Normal View History

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-forecast] Reading Forecast file from local storage...');
// Try multiple possible paths to be robust
const pathsToTry = [
path.join(process.cwd(), 'fc 26.xlsx'),
path.join(process.cwd(), 'public', 'fc 26.xlsx'),
path.join('/Users/christianvidalwolf/github/CrazeAnalytix', 'fc 26.xlsx') // Direct path as fallback for this environment
];
let buffer = null;
let foundPath = '';
for (const p of pathsToTry) {
console.log(`[fetch-forecast] Checking path: ${p}`);
if (fs.existsSync(p)) {
buffer = fs.readFileSync(p);
foundPath = p;
break;
}
}
if (!buffer) {
throw new Error(`Forecast file 'fc 26.xlsx' not found in any of: ${pathsToTry.join(', ')}`);
}
console.log(`[fetch-forecast] Successfully read Forecast 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-forecast] Error:', error);
res.status(500).json({ error: error.message });
}
}