import { SmrtClassOptions } from '@happyvertical/smrt-core'; import { Commission, CommissionCalculationService, CommissionCollection, CommissionComponentSkip, EarningEvent } from '../../commissions/index.js'; import { ReferralCollection } from '../collections/ReferralCollection.js'; import { ReferralTermSnapshotCollection } from '../collections/ReferralTermSnapshotCollection.js'; import { ReferrerCollection } from '../collections/ReferrerCollection.js'; /** * The `termsSnapshotKind` every referral-bridged Commission carries — * points `termsSnapshotId` at a ReferralTermSnapshot row. */ export declare const REFERRAL_TERMS_SNAPSHOT_KIND = "referral_term_snapshot"; /** Collaborators for {@link ReferralCommissionService}. */ export interface ReferralCommissionServiceDeps { referrals: ReferralCollection; snapshots: ReferralTermSnapshotCollection; referrers: ReferrerCollection; /** The commissions module's calculation service (same package). */ calculation: CommissionCalculationService; /** The commissions module's commission collection (occurrence counting). */ commissions: CommissionCollection; } /** Input for {@link ReferralCommissionService.processEarningEvent}. */ export interface ProcessEarningEventInput { /** The (persisted) earning event to pay referrals from. */ event: EarningEvent; /** Resolve qualified referrals by target … */ targetKind?: string; targetId?: string; /** … or name them explicitly (takes precedence over the target pair). */ referralIds?: string[]; /** Reserved clock override for symmetry with the sibling services. */ now?: Date; } /** Why a referral produced no calculation. */ export type ReferralProcessingSkipReason = 'referral_not_found' | 'not_qualified' | 'missing_snapshot' | 'referrer_missing_earner' | 'tenant_mismatch'; /** One referral the bridge skipped, and why. */ export interface ReferralProcessingSkip { referralId: string; reason: ReferralProcessingSkipReason; } /** Per-referral calculation outcome. */ export interface ReferralCommissionOutcome { referralId: string; /** Commissions newly created for this referral by THIS call. */ created: Commission[]; /** Idempotent replays — commissions that already existed. */ existing: Commission[]; /** Components the calculation declined, with reasons. */ skipped: CommissionComponentSkip[]; } /** Result of {@link ReferralCommissionService.processEarningEvent}. */ export interface ProcessEarningEventResult { /** One entry per qualified referral that reached calculation. */ results: ReferralCommissionOutcome[]; /** Referrals that never reached calculation, with reasons. */ skippedReferrals: ReferralProcessingSkip[]; } export declare class ReferralCommissionService { private readonly deps; constructor(deps: ReferralCommissionServiceDeps); static create(classOptions?: SmrtClassOptions): Promise; /** * Turn one earning event into Commissions for every qualified referral it * concerns. * * Referral resolution: explicit `referralIds` when given (rows that are * missing / not qualified are reported in `skippedReferrals`), else * `findQualifiedByTarget(targetKind, targetId)`. One of the two forms is * required. * * Per referral: * 1. Load its ReferralTermSnapshot (`'missing_snapshot'` skip when the * pointer is empty/dangling). * 2. Resolve the referrer's `earnerId` * (`'referrer_missing_earner'` skip when absent — roles without a * payout account cannot earn). * 3. Delegate to `CommissionCalculationService.calculateForEvent` with * the snapshot's plan pin, FROZEN components, currency (mismatches * surface as the calc service's `currency_mismatch` skips), * clearingDays, the referral's `creditFraction` as the share, its * `splitGroupId`, the * `('referral_term_snapshot', snapshot.id)` terms reference, and an * occurrence resolver counting existing Commissions for * `(termsSnapshotId, componentKey)` from PRIOR events. * * Fully idempotent on replay: the calculation service's dedupe keys * return prior rows in each outcome's `existing`. */ processEarningEvent(input: ProcessEarningEventInput): Promise; } export default ReferralCommissionService; //# sourceMappingURL=ReferralCommissionService.d.ts.map