Skip to content

Commit c9db5e8

Browse files
committed
fix(billing): a seat-row repair records no seat-change audit; deterministic latest-sync test helper
1 parent 4f1c786 commit c9db5e8

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

‎apps/sim/lib/billing/organizations/seats.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ import { captureServerEvent } from '@/lib/posthog/server'
1616
const logger = createLogger('OrganizationSeats')
1717

1818
export interface ReconcileOrganizationSeatsResult {
19+
/**
20+
* True only when the seat count changed. Repairing a row the Stripe plugin left stale, back to
21+
* the committed count, still rewrites the row and re-records the sync (`outboxEventId` is set)
22+
* but reports false and records no seat audit or analytics event.
23+
*/
1924
changed: boolean
2025
previousSeats?: number
2126
seats?: number
@@ -171,6 +176,15 @@ export async function reconcileOrganizationSeats({
171176
outboxEventId: outcome.outboxEventId,
172177
})
173178

179+
if (outcome.seats === outcome.previousSeats) {
180+
return {
181+
changed: false,
182+
previousSeats: outcome.previousSeats,
183+
seats: outcome.seats,
184+
outboxEventId: outcome.outboxEventId,
185+
}
186+
}
187+
174188
const increased = outcome.seats > outcome.previousSeats
175189
if (actorId) {
176190
recordAudit({

‎apps/sim/lib/billing/webhooks/stripe-sync-convergence.integration.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,11 @@ async function latestOutboxEventId(eventType: string, subscriptionId: string) {
265265
sql`${outboxEvent.payload} ->> 'subscriptionId' = ${subscriptionId}`
266266
)
267267
)
268-
.orderBy(desc(outboxEvent.createdAt), desc(outboxEvent.id))
268+
.orderBy(
269+
desc(outboxEvent.createdAt),
270+
sql`(${outboxEvent.payload} ->> 'committedAt')::numeric desc nulls last`,
271+
desc(outboxEvent.id)
272+
)
269273
.limit(1)
270274
if (!latest) throw new Error(`No ${eventType} event for ${subscriptionId}`)
271275
return latest.id

0 commit comments

Comments
 (0)