fitlien-services/functions/src/payments/phonepe/invoice/sendInvoiceEmail.ts
2025-05-19 12:52:06 +05:30

92 lines
2.7 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, EmailOptions } from "./invoiceService";
const admin = getAdmin();
const logger = getLogger();
const corsHandler = getCorsHandler();
const invoiceService = new InvoiceService();
export const sendInvoiceEmail = 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,
recipientEmail,
recipientName,
subject,
customHtml,
gymName,
planName,
amount,
transactionId,
paymentDate,
paymentMethod
} = request.body;
if (!invoicePath || !recipientEmail) {
response.status(400).json({
success: false,
error: 'Missing required fields'
});
return;
}
const emailOptions: EmailOptions = {
recipientEmail,
recipientName,
subject,
customHtml,
additionalData: {
gymName,
planName,
amount,
transactionId,
paymentDate: paymentDate ? new Date(paymentDate) : undefined,
paymentMethod
}
};
const emailSent = await invoiceService.sendInvoiceEmail(invoicePath, emailOptions);
const downloadUrl = await invoiceService.getInvoiceDownloadUrl(invoicePath);
response.json({
success: true,
message: emailSent ? 'Invoice email sent successfully' : 'Failed to send email but URL generated',
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 sending invoice email:', error);
response.status(500).json({
success: false,
error: 'Failed to send invoice email',
details: error.message
});
}
});
});