@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,110 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
import * as React from 'react'
|
|
3
|
+
import { useRouter, useSearchParams } from 'next/navigation'
|
|
4
|
+
import { useT } from '@open-mercato/shared/lib/i18n/context'
|
|
5
|
+
import { Page, PageBody, PageHeader } from '@open-mercato/ui/backend/Page'
|
|
6
|
+
import { LoadingMessage } from '@open-mercato/ui/backend/detail'
|
|
7
|
+
import { flash } from '@open-mercato/ui/backend/FlashMessages'
|
|
8
|
+
import {
|
|
9
|
+
apiCallOrThrow,
|
|
10
|
+
readApiResultOrThrow,
|
|
11
|
+
} from '@open-mercato/ui/backend/utils/apiCall'
|
|
12
|
+
import { normalizeCrudServerError } from '@open-mercato/ui/backend/utils/serverErrors'
|
|
13
|
+
import { ItemForm, type AccountOption, type AssembledItemPayload } from '../../../../components/ItemForm'
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* `/backend/billing/items/create` — create a Billing Item.
|
|
17
|
+
*
|
|
18
|
+
* Loads the account list once on mount to drive the account picker.
|
|
19
|
+
* Supports `?billAccountId=<id>` deep-link so the parent account
|
|
20
|
+
* page (when we add an "Add item" CTA there) can pre-select.
|
|
21
|
+
*
|
|
22
|
+
* POSTs `/api/billing/items`. On success, navigates to the item's
|
|
23
|
+
* detail page so the operator can edit further if needed.
|
|
24
|
+
*/
|
|
25
|
+
export default function CreateBillingItemPage() {
|
|
26
|
+
const t = useT()
|
|
27
|
+
const router = useRouter()
|
|
28
|
+
const searchParams = useSearchParams()
|
|
29
|
+
const preselectedAccountId = searchParams.get('billAccountId') ?? ''
|
|
30
|
+
|
|
31
|
+
const [accounts, setAccounts] = React.useState<AccountOption[] | null>(null)
|
|
32
|
+
const [submitting, setSubmitting] = React.useState(false)
|
|
33
|
+
|
|
34
|
+
React.useEffect(() => {
|
|
35
|
+
let cancelled = false
|
|
36
|
+
void (async () => {
|
|
37
|
+
try {
|
|
38
|
+
const result = await readApiResultOrThrow<{ items: AccountOption[] }>(
|
|
39
|
+
'/api/billing/accounts?pageSize=100&sortField=name&sortDir=asc',
|
|
40
|
+
)
|
|
41
|
+
if (!cancelled) setAccounts(result.items ?? [])
|
|
42
|
+
} catch {
|
|
43
|
+
if (!cancelled) setAccounts([])
|
|
44
|
+
}
|
|
45
|
+
})()
|
|
46
|
+
return () => {
|
|
47
|
+
cancelled = true
|
|
48
|
+
}
|
|
49
|
+
}, [])
|
|
50
|
+
|
|
51
|
+
const handleSubmit = React.useCallback(
|
|
52
|
+
async (payload: AssembledItemPayload) => {
|
|
53
|
+
setSubmitting(true)
|
|
54
|
+
try {
|
|
55
|
+
const call = await apiCallOrThrow<{ id: string; deduplicated: boolean }>(
|
|
56
|
+
'/api/billing/items',
|
|
57
|
+
{
|
|
58
|
+
method: 'POST',
|
|
59
|
+
headers: { 'Content-Type': 'application/json' },
|
|
60
|
+
body: JSON.stringify(payload),
|
|
61
|
+
},
|
|
62
|
+
)
|
|
63
|
+
const id = call.result?.id
|
|
64
|
+
flash(t('billing.items.create.success', 'Billing item created'), 'success')
|
|
65
|
+
if (id) {
|
|
66
|
+
router.push(`/backend/billing/items/${id}`)
|
|
67
|
+
} else {
|
|
68
|
+
router.push('/backend/billing/items')
|
|
69
|
+
}
|
|
70
|
+
} catch (err) {
|
|
71
|
+
const { message } = normalizeCrudServerError(err)
|
|
72
|
+
flash(
|
|
73
|
+
message || t('billing.items.create.error', 'Failed to create item'),
|
|
74
|
+
'error',
|
|
75
|
+
)
|
|
76
|
+
} finally {
|
|
77
|
+
setSubmitting(false)
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
[router, t],
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
const handleCancel = React.useCallback(() => {
|
|
84
|
+
router.push('/backend/billing/items')
|
|
85
|
+
}, [router])
|
|
86
|
+
|
|
87
|
+
return (
|
|
88
|
+
<Page>
|
|
89
|
+
<PageHeader title={t('billing.items.create.title', 'Create Billing Item')} />
|
|
90
|
+
<PageBody>
|
|
91
|
+
{accounts === null ? (
|
|
92
|
+
<LoadingMessage label={t('billing.items.create.loading', 'Loading accounts…')} />
|
|
93
|
+
) : (
|
|
94
|
+
<ItemForm
|
|
95
|
+
mode="create"
|
|
96
|
+
accountOptions={accounts}
|
|
97
|
+
initial={
|
|
98
|
+
preselectedAccountId
|
|
99
|
+
? { billAccountId: preselectedAccountId }
|
|
100
|
+
: undefined
|
|
101
|
+
}
|
|
102
|
+
submitting={submitting}
|
|
103
|
+
onSubmit={handleSubmit}
|
|
104
|
+
onCancel={handleCancel}
|
|
105
|
+
/>
|
|
106
|
+
)}
|
|
107
|
+
</PageBody>
|
|
108
|
+
</Page>
|
|
109
|
+
)
|
|
110
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import React from 'react'
|
|
2
|
+
import { Receipt } from 'lucide-react'
|
|
3
|
+
|
|
4
|
+
const itemsIcon = React.createElement(Receipt, { size: 16 })
|
|
5
|
+
|
|
6
|
+
export const metadata = {
|
|
7
|
+
requireAuth: true,
|
|
8
|
+
requireFeatures: ['billing.item.view'],
|
|
9
|
+
pageTitle: 'Billing Items',
|
|
10
|
+
pageTitleKey: 'billing.items.title',
|
|
11
|
+
pageGroup: 'Billing',
|
|
12
|
+
pageGroupKey: 'billing.nav.group',
|
|
13
|
+
pageOrder: 20,
|
|
14
|
+
icon: itemsIcon,
|
|
15
|
+
breadcrumb: [{ label: 'Billing Items', labelKey: 'billing.items.title' }],
|
|
16
|
+
} as const
|
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
"use client"
|
|
2
|
+
import * as React from 'react'
|
|
3
|
+
import Link from 'next/link'
|
|
4
|
+
import { useRouter, useSearchParams } from 'next/navigation'
|
|
5
|
+
import { Plus } from 'lucide-react'
|
|
6
|
+
import type { ColumnDef } from '@tanstack/react-table'
|
|
7
|
+
import { useT } from '@open-mercato/shared/lib/i18n/context'
|
|
8
|
+
import type { FilterDef, FilterValues } from '@open-mercato/ui/backend/FilterBar'
|
|
9
|
+
import { Page, PageBody } from '@open-mercato/ui/backend/Page'
|
|
10
|
+
import { DataTable } from '@open-mercato/ui/backend/DataTable'
|
|
11
|
+
import { Tag } from '@open-mercato/ui/primitives/tag'
|
|
12
|
+
import { Button } from '@open-mercato/ui/primitives/button'
|
|
13
|
+
import { readApiResultOrThrow } from '@open-mercato/ui/backend/utils/apiCall'
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Billing Items admin list.
|
|
17
|
+
*
|
|
18
|
+
* Filters: type (one_time / recurring / usage), bill account, active.
|
|
19
|
+
* Items are typically integration-driven (CPQ connector creates them
|
|
20
|
+
* on subscription events), so the most common UI use is *inspecting*
|
|
21
|
+
* what's there — but the "New item" CTA is available for manual
|
|
22
|
+
* entry / one-off adjustments.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
// API list rows are snake_case (see `api/items/route.ts` `fields`).
|
|
26
|
+
type BillingItemRow = {
|
|
27
|
+
id: string
|
|
28
|
+
bill_account_id: string
|
|
29
|
+
bill_account_name: string | null
|
|
30
|
+
type: 'one_time' | 'recurring' | 'usage'
|
|
31
|
+
description: string
|
|
32
|
+
bill_start_date: string
|
|
33
|
+
bill_end_date: string | null
|
|
34
|
+
uom_code: string | null
|
|
35
|
+
subscription_id: string | null
|
|
36
|
+
source_ref: string | null
|
|
37
|
+
is_active: boolean
|
|
38
|
+
created_at: string
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
type ListResponse = {
|
|
42
|
+
items: BillingItemRow[]
|
|
43
|
+
total: number
|
|
44
|
+
totalPages: number
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function formatDate(value: string | null | undefined): string {
|
|
48
|
+
if (!value) return '—'
|
|
49
|
+
const parsed = new Date(value)
|
|
50
|
+
return Number.isNaN(parsed.getTime()) ? value : parsed.toLocaleDateString()
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function typeVariant(
|
|
54
|
+
type: BillingItemRow['type'],
|
|
55
|
+
): 'default' | 'success' | 'warning' | 'info' {
|
|
56
|
+
switch (type) {
|
|
57
|
+
case 'recurring':
|
|
58
|
+
return 'success'
|
|
59
|
+
case 'one_time':
|
|
60
|
+
return 'warning'
|
|
61
|
+
case 'usage':
|
|
62
|
+
return 'info'
|
|
63
|
+
default:
|
|
64
|
+
return 'default'
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export default function BillingItemsListPage() {
|
|
69
|
+
const t = useT()
|
|
70
|
+
const router = useRouter()
|
|
71
|
+
const searchParams = useSearchParams()
|
|
72
|
+
// Deep-link from the account detail page: `?billAccountId=<id>` pre-
|
|
73
|
+
// filters the list so the operator lands on "items for this account".
|
|
74
|
+
const initialAccountId = searchParams.get('billAccountId') ?? ''
|
|
75
|
+
|
|
76
|
+
const [rows, setRows] = React.useState<BillingItemRow[]>([])
|
|
77
|
+
const [page, setPage] = React.useState(1)
|
|
78
|
+
const [pageSize] = React.useState(25)
|
|
79
|
+
const [filters, setFilters] = React.useState<FilterValues>(
|
|
80
|
+
initialAccountId ? { billAccountId: initialAccountId } : {},
|
|
81
|
+
)
|
|
82
|
+
const [total, setTotal] = React.useState(0)
|
|
83
|
+
const [totalPages, setTotalPages] = React.useState(1)
|
|
84
|
+
const [loading, setLoading] = React.useState(true)
|
|
85
|
+
const [search, setSearch] = React.useState('')
|
|
86
|
+
|
|
87
|
+
// Resolve account id → name from the loaded rows (each carries
|
|
88
|
+
// `bill_account_name` from the items route's afterList hook) so the
|
|
89
|
+
// active `billAccountId` filter chip shows a name, not a UUID.
|
|
90
|
+
const accountNames = React.useMemo(() => {
|
|
91
|
+
const map: Record<string, string> = {}
|
|
92
|
+
for (const r of rows) {
|
|
93
|
+
if (r.bill_account_name) map[r.bill_account_id] = r.bill_account_name
|
|
94
|
+
}
|
|
95
|
+
return map
|
|
96
|
+
}, [rows])
|
|
97
|
+
|
|
98
|
+
const filterDefs = React.useMemo<FilterDef[]>(
|
|
99
|
+
() => [
|
|
100
|
+
{
|
|
101
|
+
id: 'type',
|
|
102
|
+
label: t('billing.items.filters.type', 'Type'),
|
|
103
|
+
type: 'select',
|
|
104
|
+
options: [
|
|
105
|
+
{ value: 'one_time', label: t('billing.items.type.one_time', 'One-time') },
|
|
106
|
+
{ value: 'recurring', label: t('billing.items.type.recurring', 'Recurring') },
|
|
107
|
+
{ value: 'usage', label: t('billing.items.type.usage', 'Usage') },
|
|
108
|
+
],
|
|
109
|
+
},
|
|
110
|
+
{
|
|
111
|
+
id: 'billAccountId',
|
|
112
|
+
label: t('billing.items.filters.account', 'Billing Account'),
|
|
113
|
+
type: 'text',
|
|
114
|
+
formatValue: (id: string) => accountNames[id] ?? id,
|
|
115
|
+
},
|
|
116
|
+
{
|
|
117
|
+
id: 'subscriptionId',
|
|
118
|
+
label: t('billing.items.filters.subscription', 'Subscription ID'),
|
|
119
|
+
type: 'text',
|
|
120
|
+
},
|
|
121
|
+
{
|
|
122
|
+
id: 'isActive',
|
|
123
|
+
label: t('billing.items.filters.active', 'Active'),
|
|
124
|
+
type: 'select',
|
|
125
|
+
options: [
|
|
126
|
+
{ value: 'true', label: t('billing.common.active', 'Active') },
|
|
127
|
+
{ value: 'false', label: t('billing.common.inactive', 'Inactive') },
|
|
128
|
+
],
|
|
129
|
+
},
|
|
130
|
+
],
|
|
131
|
+
[t, accountNames],
|
|
132
|
+
)
|
|
133
|
+
|
|
134
|
+
const loadRows = React.useCallback(async () => {
|
|
135
|
+
setLoading(true)
|
|
136
|
+
try {
|
|
137
|
+
const params = new URLSearchParams({
|
|
138
|
+
page: String(page),
|
|
139
|
+
pageSize: String(pageSize),
|
|
140
|
+
sortField: 'createdAt',
|
|
141
|
+
sortDir: 'desc',
|
|
142
|
+
})
|
|
143
|
+
if (search.trim()) params.set('search', search.trim())
|
|
144
|
+
if (typeof filters.type === 'string' && filters.type) {
|
|
145
|
+
params.set('type', filters.type)
|
|
146
|
+
}
|
|
147
|
+
if (typeof filters.billAccountId === 'string' && filters.billAccountId.trim()) {
|
|
148
|
+
params.set('billAccountId', filters.billAccountId.trim())
|
|
149
|
+
}
|
|
150
|
+
if (typeof filters.subscriptionId === 'string' && filters.subscriptionId.trim()) {
|
|
151
|
+
params.set('subscriptionId', filters.subscriptionId.trim())
|
|
152
|
+
}
|
|
153
|
+
if (filters.isActive === 'true' || filters.isActive === 'false') {
|
|
154
|
+
params.set('isActive', String(filters.isActive))
|
|
155
|
+
}
|
|
156
|
+
const result = await readApiResultOrThrow<ListResponse>(
|
|
157
|
+
`/api/billing/items?${params.toString()}`,
|
|
158
|
+
)
|
|
159
|
+
setRows(result.items ?? [])
|
|
160
|
+
setTotal(result.total ?? 0)
|
|
161
|
+
setTotalPages(result.totalPages ?? 1)
|
|
162
|
+
} finally {
|
|
163
|
+
setLoading(false)
|
|
164
|
+
}
|
|
165
|
+
}, [filters.billAccountId, filters.isActive, filters.subscriptionId, filters.type, page, pageSize, search])
|
|
166
|
+
|
|
167
|
+
React.useEffect(() => {
|
|
168
|
+
void loadRows()
|
|
169
|
+
}, [loadRows])
|
|
170
|
+
|
|
171
|
+
const columns = React.useMemo<ColumnDef<BillingItemRow>[]>(
|
|
172
|
+
() => [
|
|
173
|
+
{
|
|
174
|
+
accessorKey: 'description',
|
|
175
|
+
header: t('billing.items.columns.description', 'Description'),
|
|
176
|
+
cell: ({ row }) => (
|
|
177
|
+
<span className="text-sm font-medium text-primary">
|
|
178
|
+
{row.original.description}
|
|
179
|
+
</span>
|
|
180
|
+
),
|
|
181
|
+
},
|
|
182
|
+
{
|
|
183
|
+
accessorKey: 'type',
|
|
184
|
+
header: t('billing.items.columns.type', 'Type'),
|
|
185
|
+
cell: ({ row }) => (
|
|
186
|
+
<Tag variant={typeVariant(row.original.type)}>{row.original.type}</Tag>
|
|
187
|
+
),
|
|
188
|
+
},
|
|
189
|
+
{
|
|
190
|
+
accessorKey: 'bill_account_id',
|
|
191
|
+
header: t('billing.items.columns.account', 'Account'),
|
|
192
|
+
cell: ({ row }) => (
|
|
193
|
+
<Link
|
|
194
|
+
href={`/backend/billing/accounts/${row.original.bill_account_id}`}
|
|
195
|
+
className="text-sm text-primary hover:underline"
|
|
196
|
+
onClick={(e) => e.stopPropagation()}
|
|
197
|
+
>
|
|
198
|
+
{row.original.bill_account_name ?? row.original.bill_account_id}
|
|
199
|
+
</Link>
|
|
200
|
+
),
|
|
201
|
+
},
|
|
202
|
+
{
|
|
203
|
+
accessorKey: 'bill_start_date',
|
|
204
|
+
header: t('billing.items.columns.start', 'Start'),
|
|
205
|
+
cell: ({ row }) => formatDate(row.original.bill_start_date),
|
|
206
|
+
},
|
|
207
|
+
{
|
|
208
|
+
accessorKey: 'bill_end_date',
|
|
209
|
+
header: t('billing.items.columns.end', 'End'),
|
|
210
|
+
cell: ({ row }) => formatDate(row.original.bill_end_date),
|
|
211
|
+
},
|
|
212
|
+
{
|
|
213
|
+
accessorKey: 'uom_code',
|
|
214
|
+
header: t('billing.items.columns.uom', 'UoM'),
|
|
215
|
+
cell: ({ row }) => row.original.uom_code ?? '—',
|
|
216
|
+
},
|
|
217
|
+
{
|
|
218
|
+
accessorKey: 'is_active',
|
|
219
|
+
header: t('billing.items.columns.status', 'Status'),
|
|
220
|
+
cell: ({ row }) =>
|
|
221
|
+
row.original.is_active ? (
|
|
222
|
+
<Tag variant="success">{t('billing.common.active', 'Active')}</Tag>
|
|
223
|
+
) : (
|
|
224
|
+
<Tag variant="default">{t('billing.common.inactive', 'Inactive')}</Tag>
|
|
225
|
+
),
|
|
226
|
+
},
|
|
227
|
+
],
|
|
228
|
+
[t],
|
|
229
|
+
)
|
|
230
|
+
|
|
231
|
+
return (
|
|
232
|
+
<Page>
|
|
233
|
+
<PageBody>
|
|
234
|
+
<DataTable
|
|
235
|
+
title={t('billing.items.title', 'Billing Items')}
|
|
236
|
+
actions={
|
|
237
|
+
<Button asChild>
|
|
238
|
+
<Link href="/backend/billing/items/create">
|
|
239
|
+
<Plus size={16} />
|
|
240
|
+
{t('billing.items.create.action', 'New item')}
|
|
241
|
+
</Link>
|
|
242
|
+
</Button>
|
|
243
|
+
}
|
|
244
|
+
refreshButton={{
|
|
245
|
+
label: t('billing.common.refresh', 'Refresh'),
|
|
246
|
+
onRefresh: loadRows,
|
|
247
|
+
isRefreshing: loading,
|
|
248
|
+
}}
|
|
249
|
+
columnChooser={{ auto: true }}
|
|
250
|
+
perspective={{ tableId: 'billing-items' }}
|
|
251
|
+
columns={columns}
|
|
252
|
+
data={rows}
|
|
253
|
+
onRowClick={(row) => router.push(`/backend/billing/items/${row.id}`)}
|
|
254
|
+
isLoading={loading}
|
|
255
|
+
pagination={{
|
|
256
|
+
page,
|
|
257
|
+
pageSize,
|
|
258
|
+
total,
|
|
259
|
+
totalPages,
|
|
260
|
+
onPageChange: setPage,
|
|
261
|
+
}}
|
|
262
|
+
filters={filterDefs}
|
|
263
|
+
filterValues={filters}
|
|
264
|
+
onFiltersApply={setFilters}
|
|
265
|
+
onFiltersClear={() => setFilters({})}
|
|
266
|
+
searchValue={search}
|
|
267
|
+
onSearchChange={setSearch}
|
|
268
|
+
/>
|
|
269
|
+
</PageBody>
|
|
270
|
+
</Page>
|
|
271
|
+
)
|
|
272
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import React from 'react'
|
|
2
|
+
import { PlayCircle } from 'lucide-react'
|
|
3
|
+
|
|
4
|
+
const runIcon = React.createElement(PlayCircle, { size: 16 })
|
|
5
|
+
|
|
6
|
+
export const metadata = {
|
|
7
|
+
requireAuth: true,
|
|
8
|
+
requireFeatures: ['billing.run.view'],
|
|
9
|
+
pageTitle: 'Bill Run',
|
|
10
|
+
pageTitleKey: 'billing.runs.detail.title',
|
|
11
|
+
pageGroup: 'Billing',
|
|
12
|
+
pageGroupKey: 'billing.nav.group',
|
|
13
|
+
navHidden: true,
|
|
14
|
+
icon: runIcon,
|
|
15
|
+
breadcrumb: [
|
|
16
|
+
{ label: 'Bill Runs', labelKey: 'billing.runs.title', href: '/backend/billing/runs' },
|
|
17
|
+
{ label: 'Run', labelKey: 'billing.runs.detail.title' },
|
|
18
|
+
],
|
|
19
|
+
} as const
|