0.1.3Updated 6 months ago
import type { Ledger } from "@infinity-ledger/ledger.ts";
import type { LedgerEntry } from "@infinity-ledger/ledger.types/ledger.entry.ts";
import type { ConsumptionResponseMapped } from "@infinity-ledger/ledger.db.queries/consume.ledger.query.ts";
import type { UnenteredLedgerConsumption } from "@infinity-ledger/ledger.types/ledger.consumption.ts";

export type LedgerEvents = {
  /**
   * Event for adding an entry to the ledger.
   * 
   * @param entry_values All values passed to the creation of the new entry
   */
  'ledger created': [Ledger<any>]

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

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

  /**
   * Runs before a consumption is added to the ledger. Cancelable.
   * 
   * @param params The information that will be used to create the consumption entry
   * @param fail Call this with `true` to cancel the consumption event.
   */
  'before-consumption': [params: UnenteredLedgerConsumption, fail: FailMethod]

  /**
   * Runs after a consumption has been added to the ledger.
   * 
   * @param consumption_data The ID of the new entry, and data about the altered entries
   * @param params The information that was used to create the consumption entry
   */
  'consumption': [consumption_data: ConsumptionResponseMapped, params: UnenteredLedgerConsumption]
}

type FailMethod = (are_you_sure: boolean, reason?: string) => void