CI (SIAX Cloud) / sonar (pull_request) Skipped
masterplan-lock / check (pull_request) Successful in 4s
masterplan-lock / check (push) Successful in 5s
CI (SIAX Cloud) / security (pull_request) Successful in 16s
Security Scan (SIAX Cloud) / security (pull_request) Successful in 16s
CI (SIAX Cloud) / integration (pull_request) Successful in 9m58s
CI (SIAX Cloud) / quality (pull_request) Successful in 10m25s
- Ledger accepts kind='grant' (a server-owned balance grant, not a payment). - OPT_TRIAL_CREDITS=N grants N credits once per tenant (idempotent key trial:<tenant>) on balance/structure, so reserve -> settle is exercised before any customer purchase. - sql/020 extends the kind constraint (re-runnable). - Tests for the grant helper; full suite 1345 passed.
41 lines
1.2 KiB
TypeScript
41 lines
1.2 KiB
TypeScript
/**
|
|
* One-time trial credits.
|
|
*
|
|
* A new tenant can be granted a fixed, server-owned amount so the credit
|
|
* ledger (reserve → settle) works in production without a card charge. It is
|
|
* idempotent per tenant: the ledger's unique (tenant, idempotency_key) makes
|
|
* every repeat a replay, never a second grant. The amount is never read from a
|
|
* client; `OPT_TRIAL_CREDITS=0` (default) disables it entirely.
|
|
*/
|
|
export interface CreditCommandSink {
|
|
apply(command: {
|
|
tenantId: string;
|
|
idempotencyKey: string;
|
|
kind: 'grant';
|
|
reference: string;
|
|
amount: number;
|
|
}): Promise<unknown>;
|
|
}
|
|
|
|
export function readTrialCredits(env: NodeJS.ProcessEnv = process.env): number {
|
|
const parsed = Number(env.OPT_TRIAL_CREDITS ?? '0');
|
|
if (!Number.isSafeInteger(parsed) || parsed < 0 || parsed > 1_000_000) return 0;
|
|
return parsed;
|
|
}
|
|
|
|
export async function ensureTrialCredits(
|
|
ledger: CreditCommandSink,
|
|
tenantId: string,
|
|
amount: number,
|
|
): Promise<void> {
|
|
if (!Number.isSafeInteger(amount) || amount <= 0) return;
|
|
if (typeof tenantId !== 'string' || !tenantId.trim()) return;
|
|
await ledger.apply({
|
|
tenantId,
|
|
idempotencyKey: `trial:${tenantId}`,
|
|
kind: 'grant',
|
|
reference: `trial:${tenantId}`,
|
|
amount,
|
|
});
|
|
}
|