fix Telegram order alert: aggregate whole batch (total + all services)

Was reading only updated.rows[0] -> reported a single line item's net as the
'Total' and showed just one service for multi-service batches (e.g. Paul Wilson's
3-service $218 PayPal batch showed as 'mcs150-update $34.50'). Now sums
service_fee - discount + surcharge + gov_fee across all rows and lists every
service.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
justin 2026-05-31 19:46:35 -05:00
parent 2fab98c0a8
commit f9c4f6783b

View file

@ -1263,22 +1263,28 @@ export async function handlePaymentComplete(
const botToken = process.env.TELEGRAM_BOT_TOKEN;
const chatId = process.env.TELEGRAM_CHAT_ID;
if (botToken && chatId) {
// Aggregate the WHOLE batch — updated.rows holds every line item, not just one.
const rows = updated.rows as Record<string, unknown>[];
const customerName = (order.customer_name as string) || "Unknown";
const customerEmail = (order.customer_email as string) || "";
const serviceName = (order.service_slug as string)
|| (order.service_name as string)
|| order_type;
// Calculate total in dollars (after discount + surcharge)
const feeCents = Number(order.service_fee_cents || order.total_cents || 0);
const discountCents = Number(order.discount_cents || 0);
const surchargeCents = Number(order.surcharge_cents || 0);
const formationCents = Number(order.formation_fee_cents || 0);
const stateCents = Number(order.state_fee_cents || 0);
const addonCents = Number(order.addon_fee_cents || 0);
const pucCents = Number(order.puc_fee_cents || 0);
const totalCents = feeCents - discountCents + surchargeCents + formationCents + stateCents + addonCents + pucCents;
let totalCents = 0;
let discountCents = 0;
const serviceNames: string[] = [];
for (const r of rows) {
const fee = Number(r.service_fee_cents || r.total_cents || 0);
const disc = Number(r.discount_cents || 0);
const sur = Number(r.surcharge_cents || 0);
const gov = Number(r.gov_fee_cents || 0);
totalCents += fee - disc + sur + gov;
discountCents += disc;
serviceNames.push((r.service_name as string) || (r.service_slug as string) || "");
}
const totalDollars = (totalCents / 100).toFixed(2);
const serviceLine = serviceNames.length <= 1
? `Service: ${serviceNames[0] || order_type}\n`
: `Services (${serviceNames.length}):\n • ${serviceNames.join("\n • ")}\n`;
const discountLine = discountCents > 0
? `Discount: -$${(discountCents / 100).toFixed(2)} (${order.discount_code || "promo"})\n`
: "";
@ -1293,7 +1299,7 @@ export async function handlePaymentComplete(
+ `Customer: ${customerName}\n`
+ `Email: ${customerEmail}\n`
+ idLine
+ `Service: ${serviceName}\n`
+ serviceLine
+ `Total: $${totalDollars}\n`
+ discountLine
+ `Payment: ${paymentMethod}\n`