@pwngh/economy-lab
    Preparing search index...

    Interface SubscriptionStore

    Tracks recurring subscriptions and when each is next due to bill.

    interface SubscriptionStore {
        activeFor(
            userId: string,
            sku: string,
            sellerId: string,
            options?: CallOptions,
        ): Promise<Subscription | null>;
        cancel(id: string, options?: CallOptions): Promise<void>;
        claimDue(
            now: number,
            limit: number,
            options?: CallOptions,
        ): Promise<readonly Subscription[]>;
        load(id: string, options?: CallOptions): Promise<Subscription | null>;
        markBilled(
            id: string,
            nextDueAt: number,
            expectedDueAt: number,
            options?: CallOptions,
        ): Promise<boolean>;
        markLapsed(id: string, options?: CallOptions): Promise<void>;
        open(sub: Subscription, options?: CallOptions): Promise<void>;
    }
    Index
    • The one ACTIVE subscription for this (userId, sku, sellerId), or null; subscribe uses it to refuse a second active subscription, which would double-bill.

      Parameters

      • userId: string
      • sku: string
      • sellerId: string
      • Optionaloptions: CallOptions

      Returns Promise<Subscription | null>

    • Sets the row CANCELED whatever state it held; an unknown id changes nothing. Terminal: a canceled subscription never claims due again.

      Parameters

      Returns Promise<void>

    • Finds up to limit subscriptions whose next charge is due, for the renewal sweep.

      Parameters

      • now: number
      • limit: number
      • Optionaloptions: CallOptions

      Returns Promise<readonly Subscription[]>

    • Records a successful renewal as a compare-and-set against the period the sweeper claimed (next_due_at = expectedDueAt); returns false and changes nothing when another sweeper already billed the period, so the loser never double-charges.

      Parameters

      • id: string
      • nextDueAt: number
      • expectedDueAt: number
      • Optionaloptions: CallOptions

      Returns Promise<boolean>

    • Marks a subscription LAPSED because a renewal couldn't be paid — distinct from a user-requested cancel; either way the sweep stops re-billing it.

      Parameters

      Returns Promise<void>