mirror of
https://github.com/stablyai/orca.git
synced 2026-11-17 19:34:29 +00:00
fix(cloud): AUDIT-AUTH-EVENTS design review: billing rows say the plan, seats, date and cap
This commit is contained in:
@@ -84,7 +84,10 @@ export async function pushBillingAudit(
|
||||
}
|
||||
}
|
||||
|
||||
type SubscriptionTerms = Pick<SubscriptionRow, 'status' | 'plan' | 'billing_interval' | 'seats' | 'cancel_at_period_end'>
|
||||
type SubscriptionTerms = Pick<SubscriptionRow, 'status' | 'plan' | 'billing_interval' | 'seats' | 'cancel_at_period_end'> & {
|
||||
/** 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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
)
|
||||
)
|
||||
|
||||
@@ -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' }]
|
||||
])
|
||||
|
||||
@@ -347,7 +347,7 @@ export function EnterpriseAuditLog(props: EnterpriseAuditLogProps): React.JSX.El
|
||||
className="flex items-center gap-3 py-2 text-xs"
|
||||
>
|
||||
<span className="min-w-0 flex-1 truncate text-foreground" title={event.action}>
|
||||
{auditEventSentence(event, t)}
|
||||
{auditEventSentence(event, t, locale)}
|
||||
</span>
|
||||
<span className="w-28 shrink-0 truncate text-muted-foreground">
|
||||
{auditCategoryLabel(event.category, t)}
|
||||
|
||||
@@ -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<string, unknown> | 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', () => {
|
||||
|
||||
@@ -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<string, unknown>
|
||||
/**
|
||||
* `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<string, ActionCopy> = {
|
||||
'member.invited': {
|
||||
@@ -62,23 +88,64 @@ export const ACCOUNT_AUDIT_ACTIONS: Record<string, ActionCopy> = {
|
||||
// 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<string, ActionCopy> = {
|
||||
},
|
||||
'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)
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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": {
|
||||
|
||||
Reference in New Issue
Block a user