@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,89 @@
|
|
|
1
|
+
import type { ModuleCli } from '@open-mercato/shared/modules/registry'
|
|
2
|
+
import { createRequestContainer } from '@open-mercato/shared/lib/di/container'
|
|
3
|
+
import type { EntityManager } from '@mikro-orm/postgresql'
|
|
4
|
+
import { seedBillingDemo } from './lib/seeds/demo'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* `mercato billing seed [--reset] [--tenant <id>] [--org <id>]`
|
|
8
|
+
*
|
|
9
|
+
* Loads self-contained billing test data (demo accounts + recurring/usage
|
|
10
|
+
* items + an unrated usage record) into a single tenant. With no `--tenant`/
|
|
11
|
+
* `--org`, it resolves the first (primary) organization created by
|
|
12
|
+
* `mercato init`. `--reset` clears the seed's own rows first.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
function parseArgs(rest: string[]): Record<string, string | boolean> {
|
|
16
|
+
const out: Record<string, string | boolean> = {}
|
|
17
|
+
for (let i = 0; i < rest.length; i++) {
|
|
18
|
+
const token = rest[i]
|
|
19
|
+
if (!token || !token.startsWith('--')) continue
|
|
20
|
+
const key = token.slice(2)
|
|
21
|
+
const next = rest[i + 1]
|
|
22
|
+
if (next === undefined || next.startsWith('--')) out[key] = true
|
|
23
|
+
else {
|
|
24
|
+
out[key] = next
|
|
25
|
+
i++
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return out
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
async function resolveScope(
|
|
32
|
+
em: EntityManager,
|
|
33
|
+
args: Record<string, string | boolean>,
|
|
34
|
+
): Promise<{ tenantId: string; organizationId: string } | null> {
|
|
35
|
+
const orgArg = typeof args.org === 'string' ? args.org : undefined
|
|
36
|
+
const tenantArg = typeof args.tenant === 'string' ? args.tenant : undefined
|
|
37
|
+
|
|
38
|
+
const { Organization } = await import('@open-mercato/core/modules/directory/data/entities')
|
|
39
|
+
|
|
40
|
+
if (orgArg) {
|
|
41
|
+
const org = await em.findOne(Organization, { id: orgArg } as never)
|
|
42
|
+
if (!org) return null
|
|
43
|
+
return { organizationId: org.id, tenantId: tenantArg ?? org.tenant.id }
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const orgs = await em.find(
|
|
47
|
+
Organization,
|
|
48
|
+
{ isActive: true } as never,
|
|
49
|
+
{ orderBy: { createdAt: 'asc' } as never, limit: 5 },
|
|
50
|
+
)
|
|
51
|
+
if (orgs.length === 0) return null
|
|
52
|
+
const org = orgs[0]
|
|
53
|
+
if (orgs.length > 1) {
|
|
54
|
+
console.warn(
|
|
55
|
+
`[billing seed] Multiple organizations found — defaulting to "${org.name}" (${org.id}). ` +
|
|
56
|
+
`Pass --org <id> to target another.`,
|
|
57
|
+
)
|
|
58
|
+
}
|
|
59
|
+
return { organizationId: org.id, tenantId: tenantArg ?? org.tenant.id }
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const seed: ModuleCli = {
|
|
63
|
+
command: 'seed',
|
|
64
|
+
async run(rest) {
|
|
65
|
+
const args = parseArgs(rest)
|
|
66
|
+
const reset = Boolean(args.reset)
|
|
67
|
+
|
|
68
|
+
const container = await createRequestContainer()
|
|
69
|
+
const em = container.resolve<EntityManager>('em')
|
|
70
|
+
|
|
71
|
+
const scope = await resolveScope(em, args)
|
|
72
|
+
if (!scope) {
|
|
73
|
+
console.error(
|
|
74
|
+
'[billing seed] No organization found. Run "yarn initialize" first, or pass --org <id>.',
|
|
75
|
+
)
|
|
76
|
+
return
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const res = await seedBillingDemo(em, scope, { reset })
|
|
80
|
+
console.log(
|
|
81
|
+
`[billing seed] tenant=${scope.tenantId} org=${scope.organizationId}${reset ? ' (reset)' : ''}\n` +
|
|
82
|
+
` ✓ accounts +${res.accountsCreated}, items +${res.itemsCreated}, usage +${res.usageCreated} ` +
|
|
83
|
+
`(existing rows left untouched)\n` +
|
|
84
|
+
` → Bill Runs · New run · As-of = today · Real → invoice for the previous month.`,
|
|
85
|
+
)
|
|
86
|
+
},
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export default [seed]
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
import type { EntityManager } from '@mikro-orm/postgresql'
|
|
2
|
+
import type { CommandHandler, CommandRuntimeContext } from '@open-mercato/shared/lib/commands/types'
|
|
3
|
+
import { registerCommand } from '@open-mercato/shared/lib/commands'
|
|
4
|
+
import {
|
|
5
|
+
ensureOrganizationScope,
|
|
6
|
+
ensureTenantScope,
|
|
7
|
+
} from '@open-mercato/shared/lib/commands/scope'
|
|
8
|
+
import {
|
|
9
|
+
emitCrudSideEffects,
|
|
10
|
+
requireId,
|
|
11
|
+
} from '@open-mercato/shared/lib/commands/helpers'
|
|
12
|
+
import { CrudHttpError } from '@open-mercato/shared/lib/crud/errors'
|
|
13
|
+
import type { DataEngine } from '@open-mercato/shared/lib/data/engine'
|
|
14
|
+
import { BillingAccount } from '../data/entities'
|
|
15
|
+
import { billingEntityIds } from '../data/entityIds'
|
|
16
|
+
import {
|
|
17
|
+
billingAccountCreateSchema,
|
|
18
|
+
billingAccountDeleteSchema,
|
|
19
|
+
billingAccountUpdateSchema,
|
|
20
|
+
type BillingAccountCreateInput,
|
|
21
|
+
type BillingAccountDeleteInput,
|
|
22
|
+
type BillingAccountUpdateInput,
|
|
23
|
+
} from '../data/validators'
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* BillingAccount writes — create / update / soft-delete.
|
|
27
|
+
*
|
|
28
|
+
* Phase 1 keeps the command surface lean — no undo / snapshot / audit
|
|
29
|
+
* log. Phase 4 (admin UI + posting flow) revisits to add operator-driven
|
|
30
|
+
* undo. The patterns to add:
|
|
31
|
+
* - `prepare`: capture BEFORE snapshot for update / delete
|
|
32
|
+
* - `captureAfter`: capture AFTER snapshot
|
|
33
|
+
* - `buildLog`: emit a structured audit log entry
|
|
34
|
+
* - `undo`: restore from snapshots + reverse side effects
|
|
35
|
+
* See `node_modules/@open-mercato/core/src/modules/customers/commands/people.ts`
|
|
36
|
+
* for the canonical full-feature shape.
|
|
37
|
+
*/
|
|
38
|
+
|
|
39
|
+
const accountIndexer = { entityType: billingEntityIds.account } as const
|
|
40
|
+
const accountEvents = { module: 'billing', entity: 'account', persistent: true } as const
|
|
41
|
+
|
|
42
|
+
function getEm(ctx: CommandRuntimeContext): EntityManager {
|
|
43
|
+
return (ctx.container.resolve('em') as EntityManager).fork()
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function getDataEngine(ctx: CommandRuntimeContext): DataEngine {
|
|
47
|
+
return ctx.container.resolve('dataEngine') as DataEngine
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// ─── Create ──────────────────────────────────────────────────────
|
|
51
|
+
|
|
52
|
+
const createAccountCommand: CommandHandler<BillingAccountCreateInput, { id: string }> = {
|
|
53
|
+
id: 'billing.accounts.create',
|
|
54
|
+
|
|
55
|
+
async execute(rawInput, ctx) {
|
|
56
|
+
const parsed = billingAccountCreateSchema.parse(rawInput)
|
|
57
|
+
ensureTenantScope(ctx, parsed.tenantId)
|
|
58
|
+
ensureOrganizationScope(ctx, parsed.organizationId)
|
|
59
|
+
|
|
60
|
+
const em = getEm(ctx)
|
|
61
|
+
const now = new Date()
|
|
62
|
+
const entity = em.create(BillingAccount, {
|
|
63
|
+
organizationId: parsed.organizationId,
|
|
64
|
+
tenantId: parsed.tenantId,
|
|
65
|
+
customerId: parsed.customerId,
|
|
66
|
+
name: parsed.name,
|
|
67
|
+
currencyCode: parsed.currencyCode,
|
|
68
|
+
billCycle: parsed.billCycle,
|
|
69
|
+
billCycleAnchor: parsed.billCycleAnchor,
|
|
70
|
+
invoiceEmail: parsed.invoiceEmail,
|
|
71
|
+
invoiceLanguage: parsed.invoiceLanguage,
|
|
72
|
+
taxId: parsed.taxId ?? null,
|
|
73
|
+
invoiceAddress: parsed.invoiceAddress as Record<string, unknown>,
|
|
74
|
+
nextBillDate: parsed.nextBillDate,
|
|
75
|
+
lastBillDate: parsed.lastBillDate ?? null,
|
|
76
|
+
isActive: parsed.isActive ?? true,
|
|
77
|
+
createdAt: now,
|
|
78
|
+
updatedAt: now,
|
|
79
|
+
})
|
|
80
|
+
em.persist(entity)
|
|
81
|
+
await em.flush()
|
|
82
|
+
|
|
83
|
+
await emitCrudSideEffects({
|
|
84
|
+
dataEngine: getDataEngine(ctx),
|
|
85
|
+
action: 'created',
|
|
86
|
+
entity,
|
|
87
|
+
identifiers: {
|
|
88
|
+
id: entity.id,
|
|
89
|
+
tenantId: entity.tenantId,
|
|
90
|
+
organizationId: entity.organizationId,
|
|
91
|
+
},
|
|
92
|
+
indexer: accountIndexer,
|
|
93
|
+
events: accountEvents,
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
return { id: entity.id }
|
|
97
|
+
},
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// ─── Update ──────────────────────────────────────────────────────
|
|
101
|
+
|
|
102
|
+
const updateAccountCommand: CommandHandler<BillingAccountUpdateInput, { id: string }> = {
|
|
103
|
+
id: 'billing.accounts.update',
|
|
104
|
+
|
|
105
|
+
async execute(rawInput, ctx) {
|
|
106
|
+
const parsed = billingAccountUpdateSchema.parse(rawInput)
|
|
107
|
+
ensureTenantScope(ctx, parsed.tenantId)
|
|
108
|
+
ensureOrganizationScope(ctx, parsed.organizationId)
|
|
109
|
+
|
|
110
|
+
const em = getEm(ctx)
|
|
111
|
+
const record = await em.findOne(BillingAccount, {
|
|
112
|
+
id: parsed.id,
|
|
113
|
+
tenantId: parsed.tenantId,
|
|
114
|
+
organizationId: parsed.organizationId,
|
|
115
|
+
deletedAt: null,
|
|
116
|
+
})
|
|
117
|
+
if (!record) {
|
|
118
|
+
throw new CrudHttpError(404, { error: 'Billing account not found' })
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (parsed.name !== undefined) record.name = parsed.name
|
|
122
|
+
if (parsed.billCycle !== undefined) record.billCycle = parsed.billCycle
|
|
123
|
+
if (parsed.billCycleAnchor !== undefined) record.billCycleAnchor = parsed.billCycleAnchor
|
|
124
|
+
if (parsed.invoiceEmail !== undefined) record.invoiceEmail = parsed.invoiceEmail
|
|
125
|
+
if (parsed.invoiceLanguage !== undefined) record.invoiceLanguage = parsed.invoiceLanguage
|
|
126
|
+
if (parsed.taxId !== undefined) record.taxId = parsed.taxId
|
|
127
|
+
if (parsed.invoiceAddress !== undefined) {
|
|
128
|
+
record.invoiceAddress = parsed.invoiceAddress as Record<string, unknown>
|
|
129
|
+
}
|
|
130
|
+
if (parsed.nextBillDate !== undefined) record.nextBillDate = parsed.nextBillDate
|
|
131
|
+
if (parsed.lastBillDate !== undefined) record.lastBillDate = parsed.lastBillDate
|
|
132
|
+
if (parsed.isActive !== undefined) record.isActive = parsed.isActive
|
|
133
|
+
|
|
134
|
+
record.updatedAt = new Date()
|
|
135
|
+
await em.flush()
|
|
136
|
+
|
|
137
|
+
await emitCrudSideEffects({
|
|
138
|
+
dataEngine: getDataEngine(ctx),
|
|
139
|
+
action: 'updated',
|
|
140
|
+
entity: record,
|
|
141
|
+
identifiers: {
|
|
142
|
+
id: record.id,
|
|
143
|
+
tenantId: record.tenantId,
|
|
144
|
+
organizationId: record.organizationId,
|
|
145
|
+
},
|
|
146
|
+
indexer: accountIndexer,
|
|
147
|
+
events: accountEvents,
|
|
148
|
+
})
|
|
149
|
+
|
|
150
|
+
return { id: record.id }
|
|
151
|
+
},
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// ─── Delete (soft) ───────────────────────────────────────────────
|
|
155
|
+
|
|
156
|
+
const deleteAccountCommand: CommandHandler<BillingAccountDeleteInput, { id: string }> = {
|
|
157
|
+
id: 'billing.accounts.delete',
|
|
158
|
+
|
|
159
|
+
async execute(rawInput, ctx) {
|
|
160
|
+
const parsed = billingAccountDeleteSchema.parse(rawInput)
|
|
161
|
+
ensureTenantScope(ctx, parsed.tenantId)
|
|
162
|
+
ensureOrganizationScope(ctx, parsed.organizationId)
|
|
163
|
+
const id = requireId(parsed)
|
|
164
|
+
|
|
165
|
+
const em = getEm(ctx)
|
|
166
|
+
const record = await em.findOne(BillingAccount, {
|
|
167
|
+
id,
|
|
168
|
+
tenantId: parsed.tenantId,
|
|
169
|
+
organizationId: parsed.organizationId,
|
|
170
|
+
deletedAt: null,
|
|
171
|
+
})
|
|
172
|
+
if (!record) {
|
|
173
|
+
throw new CrudHttpError(404, { error: 'Billing account not found' })
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
record.deletedAt = new Date()
|
|
177
|
+
record.updatedAt = record.deletedAt
|
|
178
|
+
await em.flush()
|
|
179
|
+
|
|
180
|
+
await emitCrudSideEffects({
|
|
181
|
+
dataEngine: getDataEngine(ctx),
|
|
182
|
+
action: 'deleted',
|
|
183
|
+
entity: record,
|
|
184
|
+
identifiers: {
|
|
185
|
+
id: record.id,
|
|
186
|
+
tenantId: record.tenantId,
|
|
187
|
+
organizationId: record.organizationId,
|
|
188
|
+
},
|
|
189
|
+
indexer: accountIndexer,
|
|
190
|
+
events: accountEvents,
|
|
191
|
+
})
|
|
192
|
+
|
|
193
|
+
return { id: record.id }
|
|
194
|
+
},
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
registerCommand(createAccountCommand)
|
|
198
|
+
registerCommand(updateAccountCommand)
|
|
199
|
+
registerCommand(deleteAccountCommand)
|
|
200
|
+
|
|
201
|
+
export {
|
|
202
|
+
createAccountCommand,
|
|
203
|
+
updateAccountCommand,
|
|
204
|
+
deleteAccountCommand,
|
|
205
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Billing module command registry.
|
|
3
|
+
*
|
|
4
|
+
* Each file below side-effect-registers its commands via
|
|
5
|
+
* `registerCommand(...)` from `@open-mercato/shared/lib/commands`.
|
|
6
|
+
* Importing this barrel forces the registrations to run. The CRUD
|
|
7
|
+
* factory's `commandId` lookups then resolve at request time.
|
|
8
|
+
*/
|
|
9
|
+
import './accounts'
|
|
10
|
+
import './items'
|
|
11
|
+
import './usage'
|
|
12
|
+
import './runs'
|
|
13
|
+
import './invoices'
|