import { UsageEntry } from '../usage'; export const METER_EVENT_NAME = 'zappier.api_cents'; export interface MeterEventClient { createMeterEvent(params: { eventName: string; customerId: string; value: string; /** Stripe-side dedup key; events with the same identifier are dropped. */ identifier?: string; }): Promise; } export function computeBillableCents(entries: UsageEntry[], monthlyCreditCents: number): number { const totalCents = entries.reduce((sum, e) => sum + e.cents, 0); return Math.max(0, totalCents - monthlyCreditCents); } export function computeDelta(billableCents: number, previouslyReportedCents: number): number { return Math.max(0, billableCents - previouslyReportedCents); } // Public API: retained for API compatibility. The monthly billing job now uses // the delta-based path in src/jobs/report-usage.ts (reportMonthlyUsage), which // shares computeBillableCents with this function. Do not remove. export async function reportUsage( client: MeterEventClient, stripeCustomerId: string, entries: UsageEntry[], monthlyCreditCents: number, ): Promise { const billable = computeBillableCents(entries, monthlyCreditCents); if (billable <= 0) return 0; await client.createMeterEvent({ eventName: METER_EVENT_NAME, customerId: stripeCustomerId, value: String(billable), }); return billable; }