Some checks failed
Deploy FitLien services to Dev / Deploy to Dev (push) Failing after 55s
Co-authored-by: AllenTJ7 <163137620+AllenTJ7@users.noreply.github.com> Reviewed-on: #27
84 lines
2.4 KiB
TypeScript
84 lines
2.4 KiB
TypeScript
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 processInvoice = 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 {
|
|
membershipId,
|
|
paymentId,
|
|
invoiceData,
|
|
emailOptions
|
|
} = request.body;
|
|
|
|
if (!membershipId || !paymentId || !invoiceData) {
|
|
response.status(400).json({
|
|
success: false,
|
|
error: 'Missing required fields'
|
|
});
|
|
return;
|
|
}
|
|
|
|
const result = await invoiceService.processInvoice(
|
|
membershipId,
|
|
paymentId,
|
|
invoiceData,
|
|
emailOptions
|
|
);
|
|
|
|
if (!result.success) {
|
|
response.status(400).json({
|
|
success: false,
|
|
error: result.error || 'Failed to process invoice'
|
|
});
|
|
return;
|
|
}
|
|
|
|
response.json({
|
|
success: true,
|
|
message: 'Invoice processed successfully',
|
|
invoicePath: result.invoicePath,
|
|
downloadUrl: result.downloadUrl,
|
|
emailSent: result.emailSent
|
|
});
|
|
|
|
} 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 processing invoice:', error);
|
|
response.status(500).json({
|
|
success: false,
|
|
error: 'Failed to process invoice',
|
|
details: error.message
|
|
});
|
|
}
|
|
});
|
|
});
|