dodil/erp-brick-payments

DODIL ERP brick: payments

src/brick_payments/domain/readiness.py

"""A gate answered in the shared shape {gate, met, reason, fix} (shared.yaml conventions.readiness): `met` is
computed at read time, `reason` says why not in the customer's words with the figures, `fix` names the act."""
from dataclasses import asdict, dataclass

from brick_payments.domain.errors import RuleBroken


@dataclass(frozen=True)
class Gate:
    gate: str
    met: bool
    reason: str
    fix: str

    def as_dict(self) -> dict:
        return asdict(self)


def met(gate: str) -> Gate:
    return Gate(gate, True, "", "")


def unmet(gate: str, reason: str, fix: str) -> Gate:
    return Gate(gate, False, reason, fix)


def refuse(gate: Gate) -> None:
    """Raise the broken gate in the words a person reads: the reason, then the fix."""
    if not gate.met:
        raise RuleBroken(f"{gate.reason}. {gate.fix}")


def fmt(amount) -> str:
    """12345.5 -> 12,345.50"""
    return f"{amount:,.2f}"