0.1.1Updated 7 months ago
import { EventMap } from "@infinity-beyond/modules/entity.ts";

export type LedgerEvents = EventMap<{
  /**
   * Event for adding an entry to the ledger.
   * 
   * @param entry_values All values passed to the creation of the new entry
   */
  entry_created: [Omit<LedgerEntry, 'id' | 'timestamp'>]

  /**
   * Event for adding an entry to the ledger.
   * 
   * @param reason Why this entry could not be created
   */
  entry_creation_failed: [string, Omit<LedgerEntry, 'id' | 'timestamp'>]
}>

export interface LedgerOptions {
  /**
   * Should this ledger allow users to go into negative balances?
   * 
   * If this is false, `.AddEntry()` will *fail* when trying to subtract below a zero balance.
   * 
   * **Default**: `false`
   * */
  allow_negative_balances?: boolean

  /**
   * What is the maximum length a key value could be?
   * 
   * A *key* is used to identify ledger data based on a user. Usually the `msisdn`.
   * 
   * This is used when creating the ledger tables to improve performance.
   * 
   * **Default**: `11`
   */
  key_max_length?: number
}


export type LedgerEntry = {
  id?: number
  key: string
  amount: number
  timestamp: Date
  reason?: string
  description?: string
  meta?: string
  request_id?: string
}

export type LedgerBalance = {
  id?: number
  key: string
  balance: number
}