import { b as PortableUserCredits, g as CreditCheckResult, a as CreditOperationType, c as PortableReservation, d as PortableUsageLog, S as SubscriptionTier, j as UsageHistoryResponse, f as PortableJournalEntry } from '../types-DgQtamEe.js'; import { I as ICreditRepository, J as JournalEntryQuery } from '../types-fW2JNmmr.js'; /** * Notification callback type for low balance notifications */ type LowBalanceNotificationCallback = (userId: string, balance: number) => Promise; /** * Notification callback type for subscription expired notifications */ type SubscriptionExpiredNotificationCallback = (userId: string, wasDowngraded: boolean) => Promise; /** * Options for reserving credits. */ interface ReserveCreditsOptions { /** * Time-to-live for the reservation, in milliseconds, before the cleanup * sweep may expire it. Defaults to `config.reservationExpiryMs` (suitable * for synchronous, in-request operations). Long-running async jobs * (e.g. background media generation) should pass a TTL matching their * own lifecycle so the reservation is not expired while the job is still * legitimately in flight. */ ttlMs?: number; } /** * Credits service with dependency injection for repository * * Provides business logic for credit operations, delegating * database operations to the injected repository. */ declare class CreditsService { private readonly repository; private lowBalanceCallback?; private subscriptionExpiredCallback?; constructor(repository: ICreditRepository); /** * Set callback for low balance notifications */ setLowBalanceCallback(callback: LowBalanceNotificationCallback): void; /** * Set callback for subscription expired notifications */ setSubscriptionExpiredCallback(callback: SubscriptionExpiredNotificationCallback): void; /** * Get user credits, performing monthly reset and subscription expiry checks if needed * * This method uses atomic operations to prevent race conditions: * 1. Checks subscription expiry with grace period * 2. Atomically performs monthly reset if needed (with optimistic locking) * * @param userId - User ID * @returns User credits or null if not found */ getUserCredits(userId: string): Promise; /** * Initialize credits for a new user with free tier * @param userId - User ID * @returns Initialized user credits */ initializeUserCredits(userId: string): Promise; /** * Get or create user credits * Initializes with free tier if not exists * @param userId - User ID * @returns User credits */ getOrCreateUserCredits(userId: string): Promise; /** * Check if user has sufficient credits for an operation * @param userId - User ID * @param requiredCredits - Credits required * @returns Credit check result */ checkCredits(userId: string, requiredCredits: number): Promise; /** * Reserve credits for an operation (phase 1 of two-phase commit) * Creates a reservation and locks the credits * @param userId - User ID * @param amount - Credits to reserve * @param operationType - Operation type for tracking * @param options - Optional settings (e.g. a custom `ttlMs` for long-running async jobs) * @returns Reservation object * @throws Error if insufficient credits */ reserveCredits(userId: string, amount: number, operationType: CreditOperationType, options?: ReserveCreditsOptions): Promise; /** * Commit a reservation (phase 2 of two-phase commit - success) * Deducts credits and marks reservation as committed * Also triggers low balance notifications if balance drops below threshold */ commitCredits(userId: string, reservationId: string): Promise; /** * Release a reservation (phase 2 of two-phase commit - failure) * Returns reserved credits and marks reservation as released */ releaseCredits(userId: string, reservationId: string): Promise; /** * Log usage for audit trail * @param log - Usage log data */ logUsage(log: Omit): Promise; /** * Add credits to user account (for purchases, bonuses, etc.) * @param userId - User ID * @param amount - Credits to add * @param description - Transaction description * @param paymentRef - Optional payment reference */ addCredits(userId: string, amount: number, description: string, paymentRef?: string): Promise; /** * Update user subscription tier * @param userId - User ID * @param tier - New subscription tier * @param expiresAt - Subscription expiry date (optional) */ updateTier(userId: string, tier: SubscriptionTier, expiresAt?: Date): Promise; /** * Get usage logs with optional filtering * @param userId - Optional user ID filter * @param limit - Max results * @param offset - Skip results * @returns List of usage logs */ getUsageLogs(userId?: string, limit?: number, offset?: number): Promise; /** * Get user-friendly usage history * Combines journal entries into a user-facing format * * @param userId - User ID * @param limit - Max results per page * @param offset - Skip results for pagination * @returns Paginated usage history response */ getUsageHistory(userId: string, limit?: number, offset?: number): Promise; /** * Map journal entry source to user-friendly history type */ private mapSourceToHistoryType; /** * Get journal entries directly (for admin or debugging) * @param query - Journal entry query parameters * @returns List of journal entries */ getJournalEntries(query: JournalEntryQuery): Promise; /** * Get the underlying repository (for advanced use cases) */ getRepository(): ICreditRepository; } /** * Create a credits service with a repository */ declare function createCreditsService(repository: ICreditRepository): CreditsService; export { CreditsService, type LowBalanceNotificationCallback, type SubscriptionExpiredNotificationCallback, createCreditsService };