import { onRequest } from "firebase-functions/v2/https"; import { Request } from "firebase-functions/v2/https"; import { getCorsHandler } from "../../../shared/middleware"; import { getAdmin, getLogger } from "../../../shared/config"; import { InvoiceService } from "./invoiceService"; const admin = getAdmin(); const logger = getLogger(); const corsHandler = getCorsHandler(); const invoiceService = new InvoiceService(); export const getInvoiceUrl = onRequest({ region: '#{SERVICES_RGN}#' }, async (request: Request, response) => { return corsHandler(request, response, async () => { try { const authHeader = request.headers.authorization || ''; if (!authHeader || !authHeader.startsWith('Bearer ')) { response.status(401).json({ error: 'Unauthorized' }); return; } const idToken = authHeader.split('Bearer ')[1]; try { await admin.auth().verifyIdToken(idToken); const { invoicePath } = request.query; if (!invoicePath) { response.status(400).json({ success: false, error: 'Missing invoice path' }); return; } const downloadUrl = await invoiceService.getInvoiceDownloadUrl(invoicePath as string); response.json({ success: true, downloadUrl }); } catch (authError: any) { logger.error('Authentication error:', authError); response.status(401).json({ success: false, error: 'Invalid authentication token', details: authError.message }); } } catch (error: any) { logger.error('Error getting invoice URL:', error); response.status(500).json({ success: false, error: 'Failed to get invoice URL', details: error.message }); } }); });