diff --git a/cloud/apps/billing/src/audit-push.ts b/cloud/apps/billing/src/audit-push.ts index 22208297b7d..7ef81401cad 100644 --- a/cloud/apps/billing/src/audit-push.ts +++ b/cloud/apps/billing/src/audit-push.ts @@ -84,7 +84,10 @@ export async function pushBillingAudit( } } -type SubscriptionTerms = Pick +type SubscriptionTerms = Pick & { + /** When the current period ends (ms): the day a scheduled cancellation takes effect. */ + current_period_end?: number +} const ENDED = new Set(['canceled', 'incomplete_expired']) /** Not a plan yet: Checkout made the subscription but the first payment has not gone through. */ @@ -116,7 +119,11 @@ export function subscriptionAuditEvents(before: SubscriptionTerms | null, after: events.push({ action: 'billing.seats_changed', at, detail: { seats: after.seats, previousSeats: before!.seats } }) } if (before!.cancel_at_period_end !== after.cancel_at_period_end) { - events.push({ action: after.cancel_at_period_end === 1 ? 'billing.cancel_scheduled' : 'billing.cancel_reverted', at }) + events.push( + after.cancel_at_period_end === 1 + ? { action: 'billing.cancel_scheduled', at, ...(after.current_period_end ? { detail: { endsAt: after.current_period_end } } : {}) } + : { action: 'billing.cancel_reverted', at } + ) } return events } diff --git a/cloud/apps/billing/src/webhook.ts b/cloud/apps/billing/src/webhook.ts index fce2ee98437..887e2cda185 100644 --- a/cloud/apps/billing/src/webhook.ts +++ b/cloud/apps/billing/src/webhook.ts @@ -167,7 +167,7 @@ export async function syncSubscription(env: Env, stripe: StripeClient, subscript accountId, subscriptionAuditEvents( known, - { status: subscription.status, plan: match.plan, billing_interval: match.interval, seats, cancel_at_period_end: subscription.cancel_at_period_end ? 1 : 0 }, + { status: subscription.status, plan: match.plan, billing_interval: match.interval, seats, cancel_at_period_end: subscription.cancel_at_period_end ? 1 : 0, current_period_end: periodEnd * 1000 }, now ) ) diff --git a/cloud/apps/billing/test/audit-push.test.ts b/cloud/apps/billing/test/audit-push.test.ts index 39c6b5d8204..d5f7829407a 100644 --- a/cloud/apps/billing/test/audit-push.test.ts +++ b/cloud/apps/billing/test/audit-push.test.ts @@ -68,7 +68,7 @@ describe('subscription changes', () => { expect(control.events().map((event) => [event.action, event.detail ?? null])).toEqual([ ['billing.plan_started', { plan: 'team', interval: 'month', seats: 2 }], ['billing.seats_changed', { seats: 5, previousSeats: 2 }], - ['billing.cancel_scheduled', null], + ['billing.cancel_scheduled', { endsAt: PERIOD_END * 1000 }], ['billing.cancel_reverted', null], ['billing.plan_canceled', { plan: 'team' }] ]) diff --git a/src/renderer/src/components/settings/enterprise/EnterpriseAuditLog.tsx b/src/renderer/src/components/settings/enterprise/EnterpriseAuditLog.tsx index a93d6afb4df..e3eca575956 100644 --- a/src/renderer/src/components/settings/enterprise/EnterpriseAuditLog.tsx +++ b/src/renderer/src/components/settings/enterprise/EnterpriseAuditLog.tsx @@ -347,7 +347,7 @@ export function EnterpriseAuditLog(props: EnterpriseAuditLogProps): React.JSX.El className="flex items-center gap-3 py-2 text-xs" > - {auditEventSentence(event, t)} + {auditEventSentence(event, t, locale)} {auditCategoryLabel(event.category, t)} diff --git a/src/renderer/src/components/settings/enterprise/EnterpriseSettingsView.test.tsx b/src/renderer/src/components/settings/enterprise/EnterpriseSettingsView.test.tsx index b840a90d946..bf72ac848ff 100644 --- a/src/renderer/src/components/settings/enterprise/EnterpriseSettingsView.test.tsx +++ b/src/renderer/src/components/settings/enterprise/EnterpriseSettingsView.test.tsx @@ -390,13 +390,53 @@ describe('copy', () => { expect(say('team.deleted', { type: 'team', id: null, label: 'Acme Labs' })).toBe( 'Deleted the team' ) - expect(say('billing.plan_started', { type: 'plan', id: null })).toBe('Plan started') - expect(say('billing.cancel_scheduled', { type: 'plan', id: null })).toBe( - 'Plan set to end at the next renewal' + }) + + it('puts what billing recorded into the sentence, and falls back when it recorded nothing', () => { + const say = (action: string, detail: Record | null): string => + auditEventSentence( + { + id: 'b', + at: FIXTURE_NOW, + category: 'billing', + action, + actor: { kind: 'system', userId: null, name: null, via: null }, + target: { type: 'plan', id: null }, + detail + }, + undefined, + 'en-US' + ) + expect(say('billing.plan_started', { plan: 'team', interval: 'month', seats: 2 })).toBe( + 'Plan started (Team, monthly)' ) - expect(say('billing.spend_cap_changed', { type: 'plan', id: null })).toBe( - 'Changed the overage spend cap' + expect(say('billing.plan_started', null)).toBe('Plan started') + expect(say('billing.plan_changed', { plan: 'team', interval: 'year' })).toBe( + 'Plan changed to Team, annual' ) + expect(say('billing.plan_changed', { plan: 'team' })).toBe('Plan changed') + expect(say('billing.seats_changed', { seats: 5, previousSeats: 3 })).toBe( + 'Seats changed from 3 to 5' + ) + expect(say('billing.seats_changed', { seats: 5 })).toBe('Seats changed') + expect(say('billing.plan_canceled', { plan: 'team' })).toBe('Plan ended') + expect(say('billing.cancel_scheduled', { endsAt: Date.UTC(2026, 11, 2, 12) })).toBe( + 'Plan set to end on Dec 2, 2026' + ) + expect(say('billing.cancel_scheduled', null)).toBe('Plan set to end at the next renewal') + expect(say('billing.cancel_reverted', null)).toBe('Plan set to renew again') + expect( + say('billing.spend_cap_changed', { spendCapCents: 5000, previousSpendCapCents: null }) + ).toBe('Changed the monthly spend cap to $50') + expect(say('billing.spend_cap_changed', { spendCapCents: 1250 })).toBe( + 'Changed the monthly spend cap to $12.50' + ) + // Back to the plan's default cap: no amount to show. + expect(say('billing.spend_cap_changed', { previousSpendCapCents: 5000 })).toBe( + 'Changed the monthly spend cap' + ) + expect(say('billing.overage_enabled', null)).toBe('Turned on overage') + expect(say('billing.overage_disabled', null)).toBe('Turned off overage') }) it('falls back to the plain sentence when the target has no name', () => { diff --git a/src/renderer/src/components/settings/enterprise/enterprise-audit-account-copy.ts b/src/renderer/src/components/settings/enterprise/enterprise-audit-account-copy.ts index dced67300bb..f5cd6efaa55 100644 --- a/src/renderer/src/components/settings/enterprise/enterprise-audit-account-copy.ts +++ b/src/renderer/src/components/settings/enterprise/enterprise-audit-account-copy.ts @@ -3,8 +3,34 @@ import type { Translate } from '@/i18n/use-translate' type Sentence = (t: Translate) => string -/** `named` takes the thing's name ({{name}}): a key, a secret, a teammate, a workspace. */ -export type ActionCopy = { plain: Sentence; named?: (t: Translate, name: string) => string } +type Detail = Record +/** + * `named` takes the thing's name ({{name}}): a key, a secret, a teammate, a workspace. + * `detailed` builds the sentence from what the event recorded (a plan, seat counts, an amount); + * it returns null when the event does not carry enough, and the plain sentence is used. + */ +export type ActionCopy = { + plain: Sentence + named?: (t: Translate, name: string) => string + detailed?: (t: Translate, detail: Detail, locale: string | undefined) => string | null +} + +/** Billing sends plan ids ("team"); the log shows them as names ("Team"). */ +function planName(detail: Detail, key = 'plan'): string | null { + const id = detail[key] + return typeof id === 'string' && id.length > 0 ? id.charAt(0).toUpperCase() + id.slice(1) : null +} + +function intervalWord(detail: Detail, t: Translate): string | null { + return detail.interval === 'month' + ? t('settings.enterprise.audit.interval.month', 'monthly') + : detail.interval === 'year' + ? t('settings.enterprise.audit.interval.year', 'annual') + : null +} + +const count = (value: unknown): number | null => + typeof value === 'number' && Number.isFinite(value) && value >= 0 ? value : null export const ACCOUNT_AUDIT_ACTIONS: Record = { 'member.invited': { @@ -62,23 +88,64 @@ export const ACCOUNT_AUDIT_ACTIONS: Record = { // Billing. Stripe reports plan and seat changes, so those read without a subject (the person // column says Orca); overage and the spend cap are changed by a person in Plan & billing. 'billing.plan_started': { - plain: (t) => t('settings.enterprise.audit.action.planStarted', 'Plan started') + plain: (t) => t('settings.enterprise.audit.action.planStarted', 'Plan started'), + detailed: (t, detail) => { + const plan = planName(detail) + const interval = intervalWord(detail, t) + return plan && interval + ? t( + 'settings.enterprise.audit.action.planStartedDetail', + 'Plan started ({{plan}}, {{interval}})', + { plan, interval } + ) + : null + } }, 'billing.plan_changed': { - plain: (t) => t('settings.enterprise.audit.action.planChanged', 'Plan changed') + plain: (t) => t('settings.enterprise.audit.action.planChanged', 'Plan changed'), + detailed: (t, detail) => { + const plan = planName(detail) + const interval = intervalWord(detail, t) + return plan && interval + ? t( + 'settings.enterprise.audit.action.planChangedDetail', + 'Plan changed to {{plan}}, {{interval}}', + { plan, interval } + ) + : null + } }, 'billing.plan_canceled': { plain: (t) => t('settings.enterprise.audit.action.planCanceled', 'Plan ended') }, 'billing.cancel_scheduled': { plain: (t) => - t('settings.enterprise.audit.action.cancelScheduled', 'Plan set to end at the next renewal') + t('settings.enterprise.audit.action.cancelScheduled', 'Plan set to end at the next renewal'), + detailed: (t, detail, locale) => { + const endsAt = count(detail.endsAt) + return endsAt + ? t('settings.enterprise.audit.action.cancelScheduledOn', 'Plan set to end on {{date}}', { + date: new Intl.DateTimeFormat(locale, { dateStyle: 'medium' }).format(endsAt) + }) + : null + } }, 'billing.cancel_reverted': { plain: (t) => t('settings.enterprise.audit.action.cancelReverted', 'Plan set to renew again') }, 'billing.seats_changed': { - plain: (t) => t('settings.enterprise.audit.action.seatsChanged', 'Seats changed') + plain: (t) => t('settings.enterprise.audit.action.seatsChanged', 'Seats changed'), + detailed: (t, detail) => { + const from = count(detail.previousSeats) + const to = count(detail.seats) + return from !== null && to !== null + ? t( + 'settings.enterprise.audit.action.seatsChangedDetail', + 'Seats changed from {{from}} to {{to}}', + { from: String(from), to: String(to) } + ) + : null + } }, 'billing.overage_enabled': { plain: (t) => t('settings.enterprise.audit.action.overageOn', 'Turned on overage') @@ -88,6 +155,23 @@ export const ACCOUNT_AUDIT_ACTIONS: Record = { }, 'billing.spend_cap_changed': { plain: (t) => - t('settings.enterprise.audit.action.spendCapChanged', 'Changed the overage spend cap') + t('settings.enterprise.audit.action.spendCapChanged2', 'Changed the monthly spend cap'), + detailed: (t, detail, locale) => { + // No amount (null) means the plan's default cap: say only that it changed. + const cents = count(detail.spendCapCents) + return cents === null + ? null + : t( + 'settings.enterprise.audit.action.spendCapChangedTo', + 'Changed the monthly spend cap to {{amount}}', + { + amount: new Intl.NumberFormat(locale, { + style: 'currency', + currency: 'USD', + minimumFractionDigits: cents % 100 === 0 ? 0 : 2 + }).format(cents / 100) + } + ) + } } } diff --git a/src/renderer/src/components/settings/enterprise/enterprise-audit-copy.ts b/src/renderer/src/components/settings/enterprise/enterprise-audit-copy.ts index d7ac2af733b..cf31667beda 100644 --- a/src/renderer/src/components/settings/enterprise/enterprise-audit-copy.ts +++ b/src/renderer/src/components/settings/enterprise/enterprise-audit-copy.ts @@ -248,11 +248,19 @@ function targetName(event: CloudAuditEvent): string | null { } /** "Created API key “CI deploy”"; an unknown action reads as its machine name. */ -export function auditEventSentence(event: CloudAuditEvent, t: Translate = translate): string { +export function auditEventSentence( + event: CloudAuditEvent, + t: Translate = translate, + locale?: string +): string { const copy = ACTIONS[event.action] if (!copy) { return event.action } + const detailed = event.detail ? copy.detailed?.(t, event.detail, locale) : null + if (detailed) { + return detailed + } const name = targetName(event) return name && copy.named ? copy.named(t, name) : copy.plain(t) } diff --git a/src/renderer/src/i18n/locales/en.json b/src/renderer/src/i18n/locales/en.json index da1f4e328ae..7bcf70342bd 100644 --- a/src/renderer/src/i18n/locales/en.json +++ b/src/renderer/src/i18n/locales/en.json @@ -1586,7 +1586,16 @@ "seatsChanged": "Seats changed", "overageOn": "Turned on overage", "overageOff": "Turned off overage", - "spendCapChanged": "Changed the overage spend cap" + "planStartedDetail": "Plan started ({{plan}}, {{interval}})", + "planChangedDetail": "Plan changed to {{plan}}, {{interval}}", + "cancelScheduledOn": "Plan set to end on {{date}}", + "seatsChangedDetail": "Seats changed from {{from}} to {{to}}", + "spendCapChanged2": "Changed the monthly spend cap", + "spendCapChangedTo": "Changed the monthly spend cap to {{amount}}" + }, + "interval": { + "month": "monthly", + "year": "annual" } }, "sso": {