Show billing account identity in desktop settings
This commit is contained in:
parent
8f2712725a
commit
546537e678
|
|
@ -19,6 +19,7 @@ const current = (
|
|||
})
|
||||
|
||||
export const todayBillingState = {
|
||||
account_email: 'owner@sid5.example',
|
||||
auto_reload: {
|
||||
card: { kind: 'canonical' },
|
||||
enabled: true,
|
||||
|
|
|
|||
|
|
@ -83,6 +83,9 @@ describe('BillingSettings', () => {
|
|||
|
||||
expect(await screen.findByText('$996.47')).toBeTruthy()
|
||||
expect(screen.getByText('Ultra · $200/mo')).toBeTruthy()
|
||||
expect(screen.getByText('owner@sid5.example')).toBeTruthy()
|
||||
expect(screen.getByText('sid-5 · OWNER')).toBeTruthy()
|
||||
expect(screen.getByRole('button', { name: /Reconnect \/ switch account/ })).toBeTruthy()
|
||||
expect(screen.getByText('Visa •••• 3206')).toBeTruthy()
|
||||
expect(
|
||||
screen.getByText(
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ import { Progress } from '@/components/ui/progress'
|
|||
import { SegmentedControl } from '@/components/ui/segmented-control'
|
||||
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'
|
||||
import { Skeleton } from '@/components/ui/skeleton'
|
||||
import { BarChart3, CreditCard, ExternalLink, Package, Wrench } from '@/lib/icons'
|
||||
import { BarChart3, CreditCard, ExternalLink, Package, Users, Wrench } from '@/lib/icons'
|
||||
import { cn } from '@/lib/utils'
|
||||
|
||||
import { useRouteEnumParam } from '../../hooks/use-route-enum-param'
|
||||
|
|
@ -48,6 +48,7 @@ const BILLING_VIEWS = ['overview', 'plans'] as const
|
|||
type BillingSubView = (typeof BILLING_VIEWS)[number]
|
||||
|
||||
const FEATURE_BILLING_INVOICES = false
|
||||
const FALLBACK_PORTAL_URL = 'https://portal.nousresearch.com/billing'
|
||||
|
||||
const BILLING_DEV_FIXTURE_NAMES = import.meta.env.DEV
|
||||
? (Object.keys(billingDevFixtures) as BillingDevFixtureName[])
|
||||
|
|
@ -103,6 +104,45 @@ function NoticeCard({ notice }: { notice: BillingNoticeView }) {
|
|||
)
|
||||
}
|
||||
|
||||
function BillingIdentityCard({ billing }: { billing?: BillingStateResponse }) {
|
||||
if (!billing?.logged_in) {
|
||||
return null
|
||||
}
|
||||
|
||||
const email = billing.account_email?.trim()
|
||||
const org = billing.org_name?.trim()
|
||||
|
||||
const orgLabel = org || 'Personal workspace'
|
||||
const identityLabel = email || 'Account identity unavailable'
|
||||
|
||||
const caption = email
|
||||
? `${orgLabel}${billing.role ? ` · ${billing.role}` : ''}`
|
||||
: org
|
||||
? `${orgLabel} · email unavailable`
|
||||
: 'Email and workspace unavailable — reconnect to refresh the account context.'
|
||||
|
||||
return (
|
||||
<SettingsSection icon={Users} title="Billing account">
|
||||
<ListRow
|
||||
action={
|
||||
<Button
|
||||
onClick={() => openExternal(billing.portal_url ?? FALLBACK_PORTAL_URL)}
|
||||
size="sm"
|
||||
type="button"
|
||||
variant="outline"
|
||||
>
|
||||
Reconnect / switch account
|
||||
<ExternalLink className="size-3.5" />
|
||||
</Button>
|
||||
}
|
||||
below={<div className="mt-1 text-[length:var(--conversation-caption-font-size)] text-(--ui-text-tertiary)">{caption}</div>}
|
||||
description="The account and workspace used for the balance shown below."
|
||||
title={identityLabel}
|
||||
/>
|
||||
</SettingsSection>
|
||||
)
|
||||
}
|
||||
|
||||
// The payment method as it rides in the "Payment & credits" heading: the current
|
||||
// card (muted) plus a single underline text action (Update / Add payment method).
|
||||
function PaymentMethodAside({ row }: { row: BillingAccountRowView }) {
|
||||
|
|
@ -514,6 +554,8 @@ function BillingSettingsContent({
|
|||
</div>
|
||||
</div>
|
||||
|
||||
<BillingIdentityCard billing={billing} />
|
||||
|
||||
{view.plan && (
|
||||
<SettingsSection icon={Package} title="Plan">
|
||||
<CurrentPlanCard onViewPlans={() => setSubView('plans')} plan={view.plan} />
|
||||
|
|
|
|||
|
|
@ -193,6 +193,8 @@ export interface BillingAutoReload {
|
|||
}
|
||||
|
||||
export interface BillingStateResponse {
|
||||
/** Authenticated Nous user email for the SAME OAuth context used by billing.state. */
|
||||
account_email?: string | null
|
||||
auto_reload: BillingAutoReload | null
|
||||
balance_display: string
|
||||
balance_usd: string | null
|
||||
|
|
@ -213,7 +215,9 @@ export interface BillingStateResponse {
|
|||
min_usd: string | null
|
||||
monthly_cap: BillingMonthlyCap | null
|
||||
ok: boolean
|
||||
org_id?: string | null
|
||||
org_name: string | null
|
||||
org_slug?: string | null
|
||||
portal_url: string | null
|
||||
role: string | null
|
||||
// Shared dollar usage model (two-bar view), embedded by the gateway so /topup
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ import pytest
|
|||
|
||||
import tui_gateway.server as srv
|
||||
import hermes_cli.nous_billing as nb
|
||||
import hermes_cli.nous_account as na
|
||||
import agent.billing_view as bv
|
||||
from agent.billing_view import BillingState, CardInfo, MonthlyCap, PaymentMethodInfo
|
||||
|
||||
|
|
@ -33,6 +34,7 @@ def _call(method: str, params: dict) -> dict:
|
|||
def test_billing_state_serializes_decimals_as_strings(monkeypatch):
|
||||
state = BillingState(
|
||||
logged_in=True,
|
||||
org_id="org_1",
|
||||
org_name="Acme",
|
||||
role="OWNER",
|
||||
balance_usd=Decimal("142.5"),
|
||||
|
|
@ -52,8 +54,21 @@ def test_billing_state_serializes_decimals_as_strings(monkeypatch):
|
|||
portal_url="https://portal/billing?topup=open",
|
||||
)
|
||||
monkeypatch.setattr(bv, "build_billing_state", lambda *a, **kw: state)
|
||||
monkeypatch.setattr(
|
||||
na,
|
||||
"get_nous_portal_account_info",
|
||||
lambda *a, **kw: na.NousPortalAccountInfo(
|
||||
logged_in=True,
|
||||
source="account_api",
|
||||
fresh=True,
|
||||
org_id="org_1",
|
||||
email="owner@acme.example",
|
||||
),
|
||||
)
|
||||
res = _call("billing.state", {})
|
||||
assert res["ok"] is True and res["logged_in"] is True
|
||||
assert res["account_email"] == "owner@acme.example"
|
||||
assert res["org_id"] == "org_1"
|
||||
# Money on the wire is STRING, not float/number.
|
||||
assert res["balance_usd"] == "142.5"
|
||||
assert res["balance_display"] == "$142.50"
|
||||
|
|
|
|||
|
|
@ -8419,9 +8419,12 @@ def _serialize_billing_state(state) -> dict:
|
|||
"reload_to_display": format_money(ar.reload_to_usd),
|
||||
"card": card_out,
|
||||
}
|
||||
account_email = _billing_account_email(state)
|
||||
return {
|
||||
"ok": True,
|
||||
"logged_in": state.logged_in,
|
||||
"account_email": account_email,
|
||||
"org_id": state.org_id,
|
||||
"org_name": state.org_name,
|
||||
"org_slug": state.org_slug,
|
||||
"role": state.role,
|
||||
|
|
@ -8449,6 +8452,32 @@ def _serialize_billing_state(state) -> dict:
|
|||
}
|
||||
|
||||
|
||||
def _billing_account_email(state) -> Optional[str]:
|
||||
"""Best-effort Nous user email for the billing.state OAuth context.
|
||||
|
||||
The balance/org fields come from NAS through the same stored Nous OAuth
|
||||
credential. Fetching the normalized account snapshot here keeps the rendered
|
||||
Desktop identity in the same RPC generation as the balance, while failing
|
||||
open on older/offline auth states instead of blocking the billing overview.
|
||||
"""
|
||||
if not getattr(state, "logged_in", False):
|
||||
return None
|
||||
try:
|
||||
from hermes_cli.nous_account import get_nous_portal_account_info
|
||||
|
||||
info = get_nous_portal_account_info(force_fresh=True)
|
||||
except Exception:
|
||||
return None
|
||||
if not getattr(info, "logged_in", False):
|
||||
return None
|
||||
state_org_id = getattr(state, "org_id", None)
|
||||
info_org_id = getattr(info, "org_id", None)
|
||||
if state_org_id and info_org_id and state_org_id != info_org_id:
|
||||
return None
|
||||
email = getattr(info, "email", None)
|
||||
return email.strip() if isinstance(email, str) and email.strip() else None
|
||||
|
||||
|
||||
def _usage_payload(state) -> dict:
|
||||
"""Best-effort shared usage model for the /topup + /subscription overlay bars.
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue