@dainamite/billing 0.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +658 -0
- package/LICENSE +21 -0
- package/README.md +77 -0
- package/dist/modules/billing/acl.js +20 -0
- package/dist/modules/billing/acl.js.map +7 -0
- package/dist/modules/billing/api/accounts/route.js +182 -0
- package/dist/modules/billing/api/accounts/route.js.map +7 -0
- package/dist/modules/billing/api/export/account/[id]/route.js +170 -0
- package/dist/modules/billing/api/export/account/[id]/route.js.map +7 -0
- package/dist/modules/billing/api/invoices/[id]/route.js +85 -0
- package/dist/modules/billing/api/invoices/[id]/route.js.map +7 -0
- package/dist/modules/billing/api/invoices/add-line/route.js +69 -0
- package/dist/modules/billing/api/invoices/add-line/route.js.map +7 -0
- package/dist/modules/billing/api/invoices/edit-line/route.js +78 -0
- package/dist/modules/billing/api/invoices/edit-line/route.js.map +7 -0
- package/dist/modules/billing/api/invoices/post/route.js +74 -0
- package/dist/modules/billing/api/invoices/post/route.js.map +7 -0
- package/dist/modules/billing/api/invoices/remove-line/route.js +69 -0
- package/dist/modules/billing/api/invoices/remove-line/route.js.map +7 -0
- package/dist/modules/billing/api/invoices/route.js +109 -0
- package/dist/modules/billing/api/invoices/route.js.map +7 -0
- package/dist/modules/billing/api/items/bulk/route.js +74 -0
- package/dist/modules/billing/api/items/bulk/route.js.map +7 -0
- package/dist/modules/billing/api/items/route.js +224 -0
- package/dist/modules/billing/api/items/route.js.map +7 -0
- package/dist/modules/billing/api/openapi.js +25 -0
- package/dist/modules/billing/api/openapi.js.map +7 -0
- package/dist/modules/billing/api/run-outcomes/route.js +122 -0
- package/dist/modules/billing/api/run-outcomes/route.js.map +7 -0
- package/dist/modules/billing/api/runs/retry-failed/route.js +70 -0
- package/dist/modules/billing/api/runs/retry-failed/route.js.map +7 -0
- package/dist/modules/billing/api/runs/route.js +143 -0
- package/dist/modules/billing/api/runs/route.js.map +7 -0
- package/dist/modules/billing/api/test-invoices/wipe/route.js +65 -0
- package/dist/modules/billing/api/test-invoices/wipe/route.js.map +7 -0
- package/dist/modules/billing/api/usage/route.js +138 -0
- package/dist/modules/billing/api/usage/route.js.map +7 -0
- package/dist/modules/billing/backend/billing/accounts/[id]/page.js +402 -0
- package/dist/modules/billing/backend/billing/accounts/[id]/page.js.map +7 -0
- package/dist/modules/billing/backend/billing/accounts/[id]/page.meta.js +25 -0
- package/dist/modules/billing/backend/billing/accounts/[id]/page.meta.js.map +7 -0
- package/dist/modules/billing/backend/billing/accounts/create/page.js +82 -0
- package/dist/modules/billing/backend/billing/accounts/create/page.js.map +7 -0
- package/dist/modules/billing/backend/billing/accounts/create/page.meta.js +25 -0
- package/dist/modules/billing/backend/billing/accounts/create/page.meta.js.map +7 -0
- package/dist/modules/billing/backend/billing/accounts/page.js +173 -0
- package/dist/modules/billing/backend/billing/accounts/page.js.map +7 -0
- package/dist/modules/billing/backend/billing/accounts/page.meta.js +18 -0
- package/dist/modules/billing/backend/billing/accounts/page.meta.js.map +7 -0
- package/dist/modules/billing/backend/billing/invoices/[id]/page.js +500 -0
- package/dist/modules/billing/backend/billing/invoices/[id]/page.js.map +7 -0
- package/dist/modules/billing/backend/billing/invoices/[id]/page.meta.js +25 -0
- package/dist/modules/billing/backend/billing/invoices/[id]/page.meta.js.map +7 -0
- package/dist/modules/billing/backend/billing/invoices/page.js +187 -0
- package/dist/modules/billing/backend/billing/invoices/page.js.map +7 -0
- package/dist/modules/billing/backend/billing/invoices/page.meta.js +18 -0
- package/dist/modules/billing/backend/billing/invoices/page.meta.js.map +7 -0
- package/dist/modules/billing/backend/billing/items/[id]/page.js +265 -0
- package/dist/modules/billing/backend/billing/items/[id]/page.js.map +7 -0
- package/dist/modules/billing/backend/billing/items/[id]/page.meta.js +25 -0
- package/dist/modules/billing/backend/billing/items/[id]/page.meta.js.map +7 -0
- package/dist/modules/billing/backend/billing/items/create/page.js +90 -0
- package/dist/modules/billing/backend/billing/items/create/page.js.map +7 -0
- package/dist/modules/billing/backend/billing/items/create/page.meta.js +25 -0
- package/dist/modules/billing/backend/billing/items/create/page.meta.js.map +7 -0
- package/dist/modules/billing/backend/billing/items/page.js +208 -0
- package/dist/modules/billing/backend/billing/items/page.js.map +7 -0
- package/dist/modules/billing/backend/billing/items/page.meta.js +18 -0
- package/dist/modules/billing/backend/billing/items/page.meta.js.map +7 -0
- package/dist/modules/billing/backend/billing/runs/[id]/page.js +293 -0
- package/dist/modules/billing/backend/billing/runs/[id]/page.js.map +7 -0
- package/dist/modules/billing/backend/billing/runs/[id]/page.meta.js +21 -0
- package/dist/modules/billing/backend/billing/runs/[id]/page.meta.js.map +7 -0
- package/dist/modules/billing/backend/billing/runs/page.js +341 -0
- package/dist/modules/billing/backend/billing/runs/page.js.map +7 -0
- package/dist/modules/billing/backend/billing/runs/page.meta.js +18 -0
- package/dist/modules/billing/backend/billing/runs/page.meta.js.map +7 -0
- package/dist/modules/billing/backend/billing/usage/create/page.js +100 -0
- package/dist/modules/billing/backend/billing/usage/create/page.js.map +7 -0
- package/dist/modules/billing/backend/billing/usage/create/page.meta.js +25 -0
- package/dist/modules/billing/backend/billing/usage/create/page.meta.js.map +7 -0
- package/dist/modules/billing/ce.js +56 -0
- package/dist/modules/billing/ce.js.map +7 -0
- package/dist/modules/billing/cli.js +67 -0
- package/dist/modules/billing/cli.js.map +7 -0
- package/dist/modules/billing/commands/accounts.js +157 -0
- package/dist/modules/billing/commands/accounts.js.map +7 -0
- package/dist/modules/billing/commands/index.js +6 -0
- package/dist/modules/billing/commands/index.js.map +7 -0
- package/dist/modules/billing/commands/invoices.js +401 -0
- package/dist/modules/billing/commands/invoices.js.map +7 -0
- package/dist/modules/billing/commands/items.js +317 -0
- package/dist/modules/billing/commands/items.js.map +7 -0
- package/dist/modules/billing/commands/runs.js +123 -0
- package/dist/modules/billing/commands/runs.js.map +7 -0
- package/dist/modules/billing/commands/usage.js +88 -0
- package/dist/modules/billing/commands/usage.js.map +7 -0
- package/dist/modules/billing/components/AccountForm.js +420 -0
- package/dist/modules/billing/components/AccountForm.js.map +7 -0
- package/dist/modules/billing/components/DetailFields.js +37 -0
- package/dist/modules/billing/components/DetailFields.js.map +7 -0
- package/dist/modules/billing/components/ItemForm.js +440 -0
- package/dist/modules/billing/components/ItemForm.js.map +7 -0
- package/dist/modules/billing/components/LineFormDialog.js +195 -0
- package/dist/modules/billing/components/LineFormDialog.js.map +7 -0
- package/dist/modules/billing/components/UsageForm.js +262 -0
- package/dist/modules/billing/components/UsageForm.js.map +7 -0
- package/dist/modules/billing/data/entities.js +433 -0
- package/dist/modules/billing/data/entities.js.map +7 -0
- package/dist/modules/billing/data/entityIds.js +12 -0
- package/dist/modules/billing/data/entityIds.js.map +7 -0
- package/dist/modules/billing/data/validators.js +396 -0
- package/dist/modules/billing/data/validators.js.map +7 -0
- package/dist/modules/billing/di.js +6 -0
- package/dist/modules/billing/di.js.map +7 -0
- package/dist/modules/billing/events.js +35 -0
- package/dist/modules/billing/events.js.map +7 -0
- package/dist/modules/billing/i18n/en.json +336 -0
- package/dist/modules/billing/i18n/pl.json +336 -0
- package/dist/modules/billing/index.js +17 -0
- package/dist/modules/billing/index.js.map +7 -0
- package/dist/modules/billing/lib/billPeriod.js +101 -0
- package/dist/modules/billing/lib/billPeriod.js.map +7 -0
- package/dist/modules/billing/lib/billRunEngine.js +256 -0
- package/dist/modules/billing/lib/billRunEngine.js.map +7 -0
- package/dist/modules/billing/lib/billRunLabel.js +8 -0
- package/dist/modules/billing/lib/billRunLabel.js.map +7 -0
- package/dist/modules/billing/lib/idempotency.js +13 -0
- package/dist/modules/billing/lib/idempotency.js.map +7 -0
- package/dist/modules/billing/lib/invoiceStatus.js +38 -0
- package/dist/modules/billing/lib/invoiceStatus.js.map +7 -0
- package/dist/modules/billing/lib/invoiceWriter.js +153 -0
- package/dist/modules/billing/lib/invoiceWriter.js.map +7 -0
- package/dist/modules/billing/lib/itemSelector.js +77 -0
- package/dist/modules/billing/lib/itemSelector.js.map +7 -0
- package/dist/modules/billing/lib/seeds/demo.js +130 -0
- package/dist/modules/billing/lib/seeds/demo.js.map +7 -0
- package/dist/modules/billing/lib/tenantLock.js +32 -0
- package/dist/modules/billing/lib/tenantLock.js.map +7 -0
- package/dist/modules/billing/lib/usageRater.js +150 -0
- package/dist/modules/billing/lib/usageRater.js.map +7 -0
- package/dist/modules/billing/lib/usageRunner.js +82 -0
- package/dist/modules/billing/lib/usageRunner.js.map +7 -0
- package/dist/modules/billing/migrations/Migration20260519000000_billing.js +144 -0
- package/dist/modules/billing/migrations/Migration20260519000000_billing.js.map +7 -0
- package/dist/modules/billing/setup.js +190 -0
- package/dist/modules/billing/setup.js.map +7 -0
- package/dist/modules/billing/subscribers/payment-captured-to-paid.js +47 -0
- package/dist/modules/billing/subscribers/payment-captured-to-paid.js.map +7 -0
- package/dist/modules/billing/translations.js +5 -0
- package/dist/modules/billing/translations.js.map +7 -0
- package/dist/modules/billing/workers/reap-stale-bill-runs.js +31 -0
- package/dist/modules/billing/workers/reap-stale-bill-runs.js.map +7 -0
- package/package.json +148 -0
- package/src/modules/billing/__integration__/TC-BILL-001.spec.ts +116 -0
- package/src/modules/billing/__integration__/TC-BILL-002.spec.ts +169 -0
- package/src/modules/billing/__integration__/TC-BILL-003.spec.ts +28 -0
- package/src/modules/billing/__integration__/meta.ts +4 -0
- package/src/modules/billing/__tests__/commands/invoices.test.ts +464 -0
- package/src/modules/billing/__tests__/commands/itemsBulk.test.ts +284 -0
- package/src/modules/billing/__tests__/lib/billPeriod.test.ts +131 -0
- package/src/modules/billing/__tests__/lib/billRunEngine.test.ts +561 -0
- package/src/modules/billing/__tests__/lib/idempotency.test.ts +81 -0
- package/src/modules/billing/__tests__/lib/invoiceStatus.test.ts +97 -0
- package/src/modules/billing/__tests__/lib/invoiceWriter.test.ts +119 -0
- package/src/modules/billing/__tests__/lib/itemSelector.test.ts +192 -0
- package/src/modules/billing/__tests__/lib/seedsDemo.test.ts +86 -0
- package/src/modules/billing/__tests__/lib/tenantLock.test.ts +126 -0
- package/src/modules/billing/__tests__/lib/usageRater.test.ts +190 -0
- package/src/modules/billing/__tests__/package-isolation.test.ts +223 -0
- package/src/modules/billing/__tests__/setup.test.ts +107 -0
- package/src/modules/billing/__tests__/validators.test.ts +594 -0
- package/src/modules/billing/acl.ts +16 -0
- package/src/modules/billing/api/accounts/route.ts +198 -0
- package/src/modules/billing/api/export/account/[id]/route.ts +233 -0
- package/src/modules/billing/api/invoices/[id]/route.ts +143 -0
- package/src/modules/billing/api/invoices/add-line/route.ts +74 -0
- package/src/modules/billing/api/invoices/edit-line/route.ts +97 -0
- package/src/modules/billing/api/invoices/post/route.ts +97 -0
- package/src/modules/billing/api/invoices/remove-line/route.ts +75 -0
- package/src/modules/billing/api/invoices/route.ts +167 -0
- package/src/modules/billing/api/items/bulk/route.ts +92 -0
- package/src/modules/billing/api/items/route.ts +255 -0
- package/src/modules/billing/api/openapi.ts +33 -0
- package/src/modules/billing/api/run-outcomes/route.ts +149 -0
- package/src/modules/billing/api/runs/retry-failed/route.ts +88 -0
- package/src/modules/billing/api/runs/route.ts +170 -0
- package/src/modules/billing/api/test-invoices/wipe/route.ts +87 -0
- package/src/modules/billing/api/usage/route.ts +148 -0
- package/src/modules/billing/backend/billing/accounts/[id]/page.meta.ts +23 -0
- package/src/modules/billing/backend/billing/accounts/[id]/page.tsx +574 -0
- package/src/modules/billing/backend/billing/accounts/create/page.meta.ts +23 -0
- package/src/modules/billing/backend/billing/accounts/create/page.tsx +101 -0
- package/src/modules/billing/backend/billing/accounts/page.meta.ts +16 -0
- package/src/modules/billing/backend/billing/accounts/page.tsx +222 -0
- package/src/modules/billing/backend/billing/invoices/[id]/page.meta.ts +23 -0
- package/src/modules/billing/backend/billing/invoices/[id]/page.tsx +613 -0
- package/src/modules/billing/backend/billing/invoices/page.meta.ts +16 -0
- package/src/modules/billing/backend/billing/invoices/page.tsx +245 -0
- package/src/modules/billing/backend/billing/items/[id]/page.meta.ts +23 -0
- package/src/modules/billing/backend/billing/items/[id]/page.tsx +366 -0
- package/src/modules/billing/backend/billing/items/create/page.meta.ts +23 -0
- package/src/modules/billing/backend/billing/items/create/page.tsx +110 -0
- package/src/modules/billing/backend/billing/items/page.meta.ts +16 -0
- package/src/modules/billing/backend/billing/items/page.tsx +272 -0
- package/src/modules/billing/backend/billing/runs/[id]/page.meta.ts +19 -0
- package/src/modules/billing/backend/billing/runs/[id]/page.tsx +404 -0
- package/src/modules/billing/backend/billing/runs/page.meta.ts +16 -0
- package/src/modules/billing/backend/billing/runs/page.tsx +426 -0
- package/src/modules/billing/backend/billing/usage/create/page.meta.ts +23 -0
- package/src/modules/billing/backend/billing/usage/create/page.tsx +120 -0
- package/src/modules/billing/ce.ts +74 -0
- package/src/modules/billing/cli.ts +89 -0
- package/src/modules/billing/commands/accounts.ts +205 -0
- package/src/modules/billing/commands/index.ts +13 -0
- package/src/modules/billing/commands/invoices.ts +542 -0
- package/src/modules/billing/commands/items.ts +438 -0
- package/src/modules/billing/commands/runs.ts +166 -0
- package/src/modules/billing/commands/usage.ts +118 -0
- package/src/modules/billing/components/AccountForm.tsx +517 -0
- package/src/modules/billing/components/DetailFields.tsx +60 -0
- package/src/modules/billing/components/ItemForm.tsx +558 -0
- package/src/modules/billing/components/LineFormDialog.tsx +253 -0
- package/src/modules/billing/components/UsageForm.tsx +326 -0
- package/src/modules/billing/data/entities.ts +533 -0
- package/src/modules/billing/data/entityIds.ts +25 -0
- package/src/modules/billing/data/validators.ts +562 -0
- package/src/modules/billing/di.ts +14 -0
- package/src/modules/billing/events.ts +47 -0
- package/src/modules/billing/i18n/en.json +336 -0
- package/src/modules/billing/i18n/pl.json +336 -0
- package/src/modules/billing/index.ts +22 -0
- package/src/modules/billing/lib/billPeriod.ts +181 -0
- package/src/modules/billing/lib/billRunEngine.ts +416 -0
- package/src/modules/billing/lib/billRunLabel.ts +14 -0
- package/src/modules/billing/lib/idempotency.ts +46 -0
- package/src/modules/billing/lib/invoiceStatus.ts +61 -0
- package/src/modules/billing/lib/invoiceWriter.ts +260 -0
- package/src/modules/billing/lib/itemSelector.ts +154 -0
- package/src/modules/billing/lib/seeds/demo.ts +199 -0
- package/src/modules/billing/lib/tenantLock.ts +87 -0
- package/src/modules/billing/lib/usageRater.ts +250 -0
- package/src/modules/billing/lib/usageRunner.ts +189 -0
- package/src/modules/billing/migrations/Migration20260519000000_billing.ts +175 -0
- package/src/modules/billing/setup.ts +252 -0
- package/src/modules/billing/subscribers/payment-captured-to-paid.ts +89 -0
- package/src/modules/billing/translations.ts +9 -0
- package/src/modules/billing/workers/reap-stale-bill-runs.ts +57 -0
|
@@ -0,0 +1,404 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
import * as React from 'react'
|
|
3
|
+
import Link from 'next/link'
|
|
4
|
+
import { useParams } from 'next/navigation'
|
|
5
|
+
import type { ColumnDef } from '@tanstack/react-table'
|
|
6
|
+
import { useT } from '@open-mercato/shared/lib/i18n/context'
|
|
7
|
+
import { Page, PageBody, PageHeader } from '@open-mercato/ui/backend/Page'
|
|
8
|
+
import { FormHeader } from '@open-mercato/ui/backend/forms'
|
|
9
|
+
import { DataTable } from '@open-mercato/ui/backend/DataTable'
|
|
10
|
+
import { Tag } from '@open-mercato/ui/primitives/tag'
|
|
11
|
+
import { Button } from '@open-mercato/ui/primitives/button'
|
|
12
|
+
import { LoadingMessage, ErrorMessage } from '@open-mercato/ui/backend/detail'
|
|
13
|
+
import { flash } from '@open-mercato/ui/backend/FlashMessages'
|
|
14
|
+
import {
|
|
15
|
+
apiCallOrThrow,
|
|
16
|
+
readApiResultOrThrow,
|
|
17
|
+
} from '@open-mercato/ui/backend/utils/apiCall'
|
|
18
|
+
import { normalizeCrudServerError } from '@open-mercato/ui/backend/utils/serverErrors'
|
|
19
|
+
import { billRunName } from '../../../../lib/billRunLabel'
|
|
20
|
+
import {
|
|
21
|
+
DetailCard,
|
|
22
|
+
DetailField,
|
|
23
|
+
Stat,
|
|
24
|
+
StatCard,
|
|
25
|
+
} from '../../../../components/DetailFields'
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Bill Run detail — header card + outcomes list.
|
|
29
|
+
*
|
|
30
|
+
* The "Retry failed accounts" button is enabled only when the run
|
|
31
|
+
* has at least one failed outcome. It POSTs the
|
|
32
|
+
* `billing.runs.retry_failed` endpoint, which creates a new run
|
|
33
|
+
* scoped to those accounts (the new run shows up in the list at
|
|
34
|
+
* `/backend/billing/runs`).
|
|
35
|
+
*/
|
|
36
|
+
|
|
37
|
+
// API list rows are snake_case (see `api/runs/route.ts` +
|
|
38
|
+
// `api/run-outcomes/route.ts` `fields`).
|
|
39
|
+
type BillRun = {
|
|
40
|
+
id: string
|
|
41
|
+
triggered_by: 'schedule' | 'manual'
|
|
42
|
+
parent_run_id: string | null
|
|
43
|
+
dry_run: boolean
|
|
44
|
+
test_mode: boolean
|
|
45
|
+
catch_up: boolean
|
|
46
|
+
as_of_date: string
|
|
47
|
+
started_at: string | null
|
|
48
|
+
finished_at: string | null
|
|
49
|
+
status: 'running' | 'completed' | 'partial_failure' | 'failed'
|
|
50
|
+
summary: {
|
|
51
|
+
accounts_processed?: number
|
|
52
|
+
drafts_created?: number
|
|
53
|
+
drafts_skipped_existing?: number
|
|
54
|
+
accounts_failed?: number
|
|
55
|
+
accounts_with_warnings?: number
|
|
56
|
+
usage_records_rated?: number
|
|
57
|
+
} | null
|
|
58
|
+
organization_id: string
|
|
59
|
+
tenant_id: string
|
|
60
|
+
created_at: string
|
|
61
|
+
updated_at: string
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
type BillRunOutcome = {
|
|
65
|
+
id: string
|
|
66
|
+
bill_run_id: string
|
|
67
|
+
bill_account_id: string
|
|
68
|
+
bill_account_name: string | null
|
|
69
|
+
status: 'success' | 'success_with_warnings' | 'skipped_existing_draft' | 'failed'
|
|
70
|
+
error_message: string | null
|
|
71
|
+
warnings: Record<string, unknown> | null
|
|
72
|
+
draft_invoice_id: string | null
|
|
73
|
+
invoice_number: string | null
|
|
74
|
+
created_at: string
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
type ListResponse<T> = {
|
|
78
|
+
items: T[]
|
|
79
|
+
total: number
|
|
80
|
+
totalPages: number
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function formatDate(value: string | null | undefined): string {
|
|
84
|
+
if (!value) return '—'
|
|
85
|
+
const parsed = new Date(value)
|
|
86
|
+
return Number.isNaN(parsed.getTime()) ? value : parsed.toLocaleString()
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function runStatusVariant(
|
|
90
|
+
status: BillRun['status'],
|
|
91
|
+
): 'default' | 'success' | 'warning' | 'error' {
|
|
92
|
+
switch (status) {
|
|
93
|
+
case 'completed':
|
|
94
|
+
return 'success'
|
|
95
|
+
case 'partial_failure':
|
|
96
|
+
return 'warning'
|
|
97
|
+
case 'failed':
|
|
98
|
+
return 'error'
|
|
99
|
+
default:
|
|
100
|
+
return 'default'
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function outcomeStatusVariant(
|
|
105
|
+
status: BillRunOutcome['status'],
|
|
106
|
+
): 'default' | 'success' | 'warning' | 'error' {
|
|
107
|
+
switch (status) {
|
|
108
|
+
case 'success':
|
|
109
|
+
return 'success'
|
|
110
|
+
case 'success_with_warnings':
|
|
111
|
+
return 'warning'
|
|
112
|
+
case 'failed':
|
|
113
|
+
return 'error'
|
|
114
|
+
case 'skipped_existing_draft':
|
|
115
|
+
default:
|
|
116
|
+
return 'default'
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export default function BillRunDetailPage(props: { params?: { id?: string } }) {
|
|
121
|
+
const t = useT()
|
|
122
|
+
// OM serves backend pages through a catch-all route — the dynamic
|
|
123
|
+
// `[id]` segment arrives as a page prop; `useParams()` is the fallback.
|
|
124
|
+
const urlParams = useParams<{ id: string }>()
|
|
125
|
+
const runId = (props.params?.id ?? urlParams?.id ?? '') as string
|
|
126
|
+
|
|
127
|
+
const [run, setRun] = React.useState<BillRun | null>(null)
|
|
128
|
+
const [runError, setRunError] = React.useState<string | null>(null)
|
|
129
|
+
const [outcomes, setOutcomes] = React.useState<BillRunOutcome[]>([])
|
|
130
|
+
const [outcomesPage, setOutcomesPage] = React.useState(1)
|
|
131
|
+
const [outcomesTotal, setOutcomesTotal] = React.useState(0)
|
|
132
|
+
const [outcomesTotalPages, setOutcomesTotalPages] = React.useState(1)
|
|
133
|
+
const [outcomesLoading, setOutcomesLoading] = React.useState(true)
|
|
134
|
+
const [retrying, setRetrying] = React.useState(false)
|
|
135
|
+
|
|
136
|
+
const loadRun = React.useCallback(async () => {
|
|
137
|
+
if (!runId) return
|
|
138
|
+
setRunError(null)
|
|
139
|
+
try {
|
|
140
|
+
const result = await readApiResultOrThrow<ListResponse<BillRun>>(
|
|
141
|
+
`/api/billing/runs?id=${runId}&pageSize=1`,
|
|
142
|
+
)
|
|
143
|
+
const found = result.items?.[0]
|
|
144
|
+
if (!found) {
|
|
145
|
+
setRunError(t('billing.runs.detail.error.not_found', 'Run not found'))
|
|
146
|
+
setRun(null)
|
|
147
|
+
return
|
|
148
|
+
}
|
|
149
|
+
setRun(found)
|
|
150
|
+
} catch (err) {
|
|
151
|
+
const { message } = normalizeCrudServerError(err)
|
|
152
|
+
setRunError(message || t('billing.runs.detail.error.load', 'Failed to load run'))
|
|
153
|
+
}
|
|
154
|
+
}, [runId, t])
|
|
155
|
+
|
|
156
|
+
const loadOutcomes = React.useCallback(async () => {
|
|
157
|
+
if (!runId) return
|
|
158
|
+
setOutcomesLoading(true)
|
|
159
|
+
try {
|
|
160
|
+
const params = new URLSearchParams({
|
|
161
|
+
billRunId: runId,
|
|
162
|
+
page: String(outcomesPage),
|
|
163
|
+
pageSize: '50',
|
|
164
|
+
})
|
|
165
|
+
const result = await readApiResultOrThrow<ListResponse<BillRunOutcome>>(
|
|
166
|
+
`/api/billing/run-outcomes?${params.toString()}`,
|
|
167
|
+
)
|
|
168
|
+
setOutcomes(result.items ?? [])
|
|
169
|
+
setOutcomesTotal(result.total ?? 0)
|
|
170
|
+
setOutcomesTotalPages(result.totalPages ?? 1)
|
|
171
|
+
} finally {
|
|
172
|
+
setOutcomesLoading(false)
|
|
173
|
+
}
|
|
174
|
+
}, [outcomesPage, runId])
|
|
175
|
+
|
|
176
|
+
React.useEffect(() => {
|
|
177
|
+
void loadRun()
|
|
178
|
+
}, [loadRun])
|
|
179
|
+
React.useEffect(() => {
|
|
180
|
+
void loadOutcomes()
|
|
181
|
+
}, [loadOutcomes])
|
|
182
|
+
|
|
183
|
+
const failedCount = run?.summary?.accounts_failed ?? 0
|
|
184
|
+
|
|
185
|
+
const handleRetryFailed = React.useCallback(async () => {
|
|
186
|
+
if (!run || failedCount === 0) return
|
|
187
|
+
setRetrying(true)
|
|
188
|
+
try {
|
|
189
|
+
const call = await apiCallOrThrow<{ billRunId: string; outcomesCount: number }>(
|
|
190
|
+
'/api/billing/runs/retry-failed',
|
|
191
|
+
{
|
|
192
|
+
method: 'POST',
|
|
193
|
+
headers: { 'Content-Type': 'application/json' },
|
|
194
|
+
body: JSON.stringify({ billRunId: run.id }),
|
|
195
|
+
},
|
|
196
|
+
)
|
|
197
|
+
const outcomesCount = call.result?.outcomesCount ?? 0
|
|
198
|
+
flash(
|
|
199
|
+
t('billing.runs.retry.success', 'Retry run created') +
|
|
200
|
+
` (${outcomesCount} accounts)`,
|
|
201
|
+
'success',
|
|
202
|
+
)
|
|
203
|
+
// Refresh — the new run shows up in the list view; the current
|
|
204
|
+
// run's outcomes are immutable so no reload of `loadOutcomes`
|
|
205
|
+
// needed, but loadRun is cheap.
|
|
206
|
+
void loadRun()
|
|
207
|
+
} catch (err) {
|
|
208
|
+
const { message } = normalizeCrudServerError(err)
|
|
209
|
+
flash(message || t('billing.runs.retry.error', 'Retry failed'), 'error')
|
|
210
|
+
} finally {
|
|
211
|
+
setRetrying(false)
|
|
212
|
+
}
|
|
213
|
+
}, [failedCount, loadRun, run, t])
|
|
214
|
+
|
|
215
|
+
const outcomeColumns = React.useMemo<ColumnDef<BillRunOutcome>[]>(
|
|
216
|
+
() => [
|
|
217
|
+
{
|
|
218
|
+
accessorKey: 'bill_account_id',
|
|
219
|
+
header: t('billing.runs.outcomes.columns.account', 'Account'),
|
|
220
|
+
cell: ({ row }) => (
|
|
221
|
+
<Link
|
|
222
|
+
href={`/backend/billing/accounts/${row.original.bill_account_id}`}
|
|
223
|
+
className="text-sm text-primary hover:underline"
|
|
224
|
+
>
|
|
225
|
+
{row.original.bill_account_name ?? row.original.bill_account_id}
|
|
226
|
+
</Link>
|
|
227
|
+
),
|
|
228
|
+
},
|
|
229
|
+
{
|
|
230
|
+
accessorKey: 'status',
|
|
231
|
+
header: t('billing.runs.outcomes.columns.status', 'Status'),
|
|
232
|
+
cell: ({ row }) => (
|
|
233
|
+
<Tag variant={outcomeStatusVariant(row.original.status)}>
|
|
234
|
+
{row.original.status}
|
|
235
|
+
</Tag>
|
|
236
|
+
),
|
|
237
|
+
},
|
|
238
|
+
{
|
|
239
|
+
accessorKey: 'draft_invoice_id',
|
|
240
|
+
header: t('billing.runs.outcomes.columns.invoice', 'Invoice'),
|
|
241
|
+
cell: ({ row }) =>
|
|
242
|
+
row.original.draft_invoice_id ? (
|
|
243
|
+
<Link
|
|
244
|
+
href={`/backend/billing/invoices/${row.original.draft_invoice_id}`}
|
|
245
|
+
className="text-sm text-primary hover:underline"
|
|
246
|
+
>
|
|
247
|
+
{row.original.invoice_number ?? row.original.draft_invoice_id}
|
|
248
|
+
</Link>
|
|
249
|
+
) : (
|
|
250
|
+
'—'
|
|
251
|
+
),
|
|
252
|
+
},
|
|
253
|
+
{
|
|
254
|
+
accessorKey: 'error_message',
|
|
255
|
+
header: t('billing.runs.outcomes.columns.error', 'Error'),
|
|
256
|
+
cell: ({ row }) => row.original.error_message ?? '—',
|
|
257
|
+
},
|
|
258
|
+
{
|
|
259
|
+
accessorKey: 'warnings',
|
|
260
|
+
header: t('billing.runs.outcomes.columns.warnings', 'Warnings'),
|
|
261
|
+
cell: ({ row }) => {
|
|
262
|
+
if (!row.original.warnings) return '—'
|
|
263
|
+
return (
|
|
264
|
+
<span className="font-mono text-xs">{JSON.stringify(row.original.warnings)}</span>
|
|
265
|
+
)
|
|
266
|
+
},
|
|
267
|
+
},
|
|
268
|
+
],
|
|
269
|
+
[t],
|
|
270
|
+
)
|
|
271
|
+
|
|
272
|
+
if (runError) {
|
|
273
|
+
return (
|
|
274
|
+
<Page>
|
|
275
|
+
<PageHeader title={t('billing.runs.detail.title', 'Bill Run')} />
|
|
276
|
+
<PageBody>
|
|
277
|
+
<ErrorMessage label={runError} />
|
|
278
|
+
</PageBody>
|
|
279
|
+
</Page>
|
|
280
|
+
)
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
if (!run) {
|
|
284
|
+
return (
|
|
285
|
+
<Page>
|
|
286
|
+
<PageHeader title={t('billing.runs.detail.title', 'Bill Run')} />
|
|
287
|
+
<PageBody>
|
|
288
|
+
<LoadingMessage label={t('billing.runs.detail.loading', 'Loading run…')} />
|
|
289
|
+
</PageBody>
|
|
290
|
+
</Page>
|
|
291
|
+
)
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
return (
|
|
295
|
+
<Page>
|
|
296
|
+
<FormHeader
|
|
297
|
+
mode="detail"
|
|
298
|
+
backHref="/backend/billing/runs"
|
|
299
|
+
entityTypeLabel={t('billing.runs.detail.title', 'Bill Run')}
|
|
300
|
+
title={billRunName(run.as_of_date)}
|
|
301
|
+
statusBadge={
|
|
302
|
+
<Tag variant={runStatusVariant(run.status)}>{run.status}</Tag>
|
|
303
|
+
}
|
|
304
|
+
actionsContent={
|
|
305
|
+
failedCount > 0 ? (
|
|
306
|
+
<Button disabled={retrying} onClick={handleRetryFailed}>
|
|
307
|
+
{retrying
|
|
308
|
+
? t('billing.runs.retry.in_progress', 'Retrying…')
|
|
309
|
+
: t('billing.runs.retry.action', 'Retry failed accounts')}
|
|
310
|
+
{` (${failedCount})`}
|
|
311
|
+
</Button>
|
|
312
|
+
) : null
|
|
313
|
+
}
|
|
314
|
+
/>
|
|
315
|
+
<PageBody>
|
|
316
|
+
<div className="space-y-6">
|
|
317
|
+
<DetailCard>
|
|
318
|
+
<DetailField label={t('billing.runs.detail.field.run_id', 'Run ID')}>
|
|
319
|
+
<span className="font-mono text-xs">{run.id}</span>
|
|
320
|
+
</DetailField>
|
|
321
|
+
<DetailField label={t('billing.runs.detail.field.triggered_by', 'Trigger')}>
|
|
322
|
+
{run.triggered_by}
|
|
323
|
+
</DetailField>
|
|
324
|
+
<DetailField label={t('billing.runs.detail.field.mode', 'Mode')}>
|
|
325
|
+
<div className="flex flex-wrap gap-1">
|
|
326
|
+
{run.dry_run ? (
|
|
327
|
+
<Tag variant="warning">{t('billing.runs.mode.dry_run', 'Dry-run')}</Tag>
|
|
328
|
+
) : run.test_mode ? (
|
|
329
|
+
<Tag variant="warning">{t('billing.runs.mode.test', 'Test')}</Tag>
|
|
330
|
+
) : (
|
|
331
|
+
<Tag variant="default">{t('billing.runs.mode.real', 'Real')}</Tag>
|
|
332
|
+
)}
|
|
333
|
+
{run.catch_up ? (
|
|
334
|
+
<Tag variant="default">{t('billing.runs.mode.catch_up', 'Catch-up')}</Tag>
|
|
335
|
+
) : null}
|
|
336
|
+
</div>
|
|
337
|
+
</DetailField>
|
|
338
|
+
<DetailField label={t('billing.runs.detail.field.as_of_date', 'As-of date')}>
|
|
339
|
+
{run.as_of_date?.slice(0, 10) ?? '—'}
|
|
340
|
+
</DetailField>
|
|
341
|
+
<DetailField label={t('billing.runs.detail.field.started_at', 'Started')}>
|
|
342
|
+
{formatDate(run.started_at)}
|
|
343
|
+
</DetailField>
|
|
344
|
+
<DetailField label={t('billing.runs.detail.field.finished_at', 'Finished')}>
|
|
345
|
+
{formatDate(run.finished_at)}
|
|
346
|
+
</DetailField>
|
|
347
|
+
{run.parent_run_id ? (
|
|
348
|
+
<DetailField label={t('billing.runs.detail.field.parent_run', 'Parent run')}>
|
|
349
|
+
<span className="font-mono text-xs">{run.parent_run_id}</span>
|
|
350
|
+
</DetailField>
|
|
351
|
+
) : null}
|
|
352
|
+
</DetailCard>
|
|
353
|
+
|
|
354
|
+
{run.summary ? (
|
|
355
|
+
<StatCard title={t('billing.runs.summary.title', 'Summary')}>
|
|
356
|
+
<Stat
|
|
357
|
+
label={t('billing.runs.summary.accounts', 'Accounts')}
|
|
358
|
+
value={run.summary.accounts_processed ?? 0}
|
|
359
|
+
/>
|
|
360
|
+
<Stat
|
|
361
|
+
label={t('billing.runs.summary.drafts', 'Drafts')}
|
|
362
|
+
value={run.summary.drafts_created ?? 0}
|
|
363
|
+
/>
|
|
364
|
+
<Stat
|
|
365
|
+
label={t('billing.runs.summary.skipped', 'Skipped')}
|
|
366
|
+
value={run.summary.drafts_skipped_existing ?? 0}
|
|
367
|
+
/>
|
|
368
|
+
<Stat
|
|
369
|
+
label={t('billing.runs.summary.warnings', 'Warnings')}
|
|
370
|
+
value={run.summary.accounts_with_warnings ?? 0}
|
|
371
|
+
/>
|
|
372
|
+
<Stat
|
|
373
|
+
label={t('billing.runs.summary.failed', 'Failed')}
|
|
374
|
+
value={run.summary.accounts_failed ?? 0}
|
|
375
|
+
/>
|
|
376
|
+
<Stat
|
|
377
|
+
label={t('billing.runs.summary.usage', 'Usage rated')}
|
|
378
|
+
value={run.summary.usage_records_rated ?? 0}
|
|
379
|
+
/>
|
|
380
|
+
</StatCard>
|
|
381
|
+
) : null}
|
|
382
|
+
|
|
383
|
+
<div>
|
|
384
|
+
<h2 className="text-base font-semibold mb-2">
|
|
385
|
+
{t('billing.runs.outcomes.title', 'Per-account outcomes')}
|
|
386
|
+
</h2>
|
|
387
|
+
<DataTable
|
|
388
|
+
columns={outcomeColumns}
|
|
389
|
+
data={outcomes}
|
|
390
|
+
isLoading={outcomesLoading}
|
|
391
|
+
pagination={{
|
|
392
|
+
page: outcomesPage,
|
|
393
|
+
pageSize: 50,
|
|
394
|
+
total: outcomesTotal,
|
|
395
|
+
totalPages: outcomesTotalPages,
|
|
396
|
+
onPageChange: setOutcomesPage,
|
|
397
|
+
}}
|
|
398
|
+
/>
|
|
399
|
+
</div>
|
|
400
|
+
</div>
|
|
401
|
+
</PageBody>
|
|
402
|
+
</Page>
|
|
403
|
+
)
|
|
404
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import React from 'react'
|
|
2
|
+
import { PlayCircle } from 'lucide-react'
|
|
3
|
+
|
|
4
|
+
const runsIcon = React.createElement(PlayCircle, { size: 16 })
|
|
5
|
+
|
|
6
|
+
export const metadata = {
|
|
7
|
+
requireAuth: true,
|
|
8
|
+
requireFeatures: ['billing.run.view'],
|
|
9
|
+
pageTitle: 'Bill Runs',
|
|
10
|
+
pageTitleKey: 'billing.runs.title',
|
|
11
|
+
pageGroup: 'Billing',
|
|
12
|
+
pageGroupKey: 'billing.nav.group',
|
|
13
|
+
pageOrder: 30,
|
|
14
|
+
icon: runsIcon,
|
|
15
|
+
breadcrumb: [{ label: 'Bill Runs', labelKey: 'billing.runs.title' }],
|
|
16
|
+
} as const
|