Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ bin/

# OS
.DS_Store

# jqwik failure database
.jqwik-database
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ Le projet avance par jalons ; un jalon n'est terminé que lorsque ses tests pass
| Jalon | Contenu | État |
| ----- | -------------------------------------------------------- | ------- |
| M0 | Socle du monorepo : Gradle, CI, Docker Compose, ArchUnit | Terminé |
| M1 | Domaine du ledger en Java pur, tests de propriété | À faire |
| M1 | Domaine du ledger en Java pur, tests de propriété | Terminé |
| M2 | Persistance PostgreSQL, algorithme de posting, API REST | À faire |
| M3 | Outbox, RabbitMQ, contrats d'événements | À faire |
| M4 | Contexte wallet : KYC, plafonds, frais, P2P | À faire |
Expand Down
4 changes: 3 additions & 1 deletion contexts/ledger/ledger-domain/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
plugins {
id("senwallet.java-library")
`java-test-fixtures`
}

dependencies {
implementation(project(":platform:shared-kernel"))
api(project(":platform:shared-kernel"))
testImplementation(libs.jqwik)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package sn.senwallet.ledger.domain.event;

import sn.senwallet.ledger.domain.model.transaction.ExternalId;
import sn.senwallet.shared.event.DomainEvent;
import sn.senwallet.shared.id.TransactionId;

public sealed interface LedgerTransactionEvent extends DomainEvent
permits TransactionPending, TransactionPosted, TransactionArchived {

TransactionId transactionId();

ExternalId externalId();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package sn.senwallet.ledger.domain.event;

import java.time.Instant;
import java.util.Objects;
import java.util.UUID;
import sn.senwallet.ledger.domain.model.transaction.ExternalId;
import sn.senwallet.shared.id.TransactionId;

public record TransactionArchived(UUID eventId, Instant occurredAt, TransactionId transactionId, ExternalId externalId)
implements LedgerTransactionEvent {

public TransactionArchived {
Objects.requireNonNull(eventId, "eventId");
Objects.requireNonNull(occurredAt, "occurredAt");
Objects.requireNonNull(transactionId, "transactionId");
Objects.requireNonNull(externalId, "externalId");
}

public static TransactionArchived of(TransactionId transactionId, ExternalId externalId, Instant occurredAt) {
return new TransactionArchived(UUID.randomUUID(), occurredAt, transactionId, externalId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package sn.senwallet.ledger.domain.event;

import java.time.Instant;
import java.util.Objects;
import java.util.UUID;
import sn.senwallet.ledger.domain.model.transaction.ExternalId;
import sn.senwallet.shared.id.TransactionId;

public record TransactionPending(UUID eventId, Instant occurredAt, TransactionId transactionId, ExternalId externalId)
implements LedgerTransactionEvent {

public TransactionPending {
Objects.requireNonNull(eventId, "eventId");
Objects.requireNonNull(occurredAt, "occurredAt");
Objects.requireNonNull(transactionId, "transactionId");
Objects.requireNonNull(externalId, "externalId");
}

public static TransactionPending of(TransactionId transactionId, ExternalId externalId, Instant occurredAt) {
return new TransactionPending(UUID.randomUUID(), occurredAt, transactionId, externalId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package sn.senwallet.ledger.domain.event;

import java.time.Instant;
import java.util.Objects;
import java.util.UUID;
import sn.senwallet.ledger.domain.model.transaction.ExternalId;
import sn.senwallet.shared.id.TransactionId;

public record TransactionPosted(UUID eventId, Instant occurredAt, TransactionId transactionId, ExternalId externalId)
implements LedgerTransactionEvent {

public TransactionPosted {
Objects.requireNonNull(eventId, "eventId");
Objects.requireNonNull(occurredAt, "occurredAt");
Objects.requireNonNull(transactionId, "transactionId");
Objects.requireNonNull(externalId, "externalId");
}

public static TransactionPosted of(TransactionId transactionId, ExternalId externalId, Instant occurredAt) {
return new TransactionPosted(UUID.randomUUID(), occurredAt, transactionId, externalId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package sn.senwallet.ledger.domain.exception;

import sn.senwallet.shared.id.AccountId;

/** An entry or condition targets an account that is missing or inactive. */
public final class AccountNotPostableException extends RuntimeException {

private static final long serialVersionUID = 1L;

private final transient AccountId accountId;

public AccountNotPostableException(AccountId accountId, String reason) {
super("account " + accountId + " cannot be posted to: " + reason);
this.accountId = accountId;
}

public AccountId accountId() {
return accountId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package sn.senwallet.ledger.domain.exception;

import sn.senwallet.ledger.domain.model.condition.BalanceCondition;
import sn.senwallet.shared.money.Money;

/**
* A balance condition is false once the transaction is applied; nothing was
* changed.
*/
public final class BalanceConditionViolatedException extends RuntimeException {

private static final long serialVersionUID = 1L;

private final transient BalanceCondition condition;
private final transient Money actual;

public BalanceConditionViolatedException(BalanceCondition condition, Money actual) {
super("balance condition violated: " + condition + " (would be " + actual + ")");
this.condition = condition;
this.actual = actual;
}

public BalanceCondition condition() {
return condition;
}

public Money actual() {
return actual;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package sn.senwallet.ledger.domain.exception;

import sn.senwallet.ledger.domain.model.transaction.TransactionStatus;

public final class IllegalTransitionException extends RuntimeException {

private static final long serialVersionUID = 1L;

private final TransactionStatus from;
private final TransactionStatus to;

public IllegalTransitionException(TransactionStatus from, TransactionStatus to) {
super("illegal transition " + from + " -> " + to);
this.from = from;
this.to = to;
}

public TransactionStatus from() {
return from;
}

public TransactionStatus to() {
return to;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package sn.senwallet.ledger.domain.exception;

import sn.senwallet.shared.money.Money;

/**
* A transaction with fewer than two entries, or whose debits and credits
* differ.
*/
public final class UnbalancedTransactionException extends RuntimeException {

private static final long serialVersionUID = 1L;

public UnbalancedTransactionException(String message) {
super(message);
}

public UnbalancedTransactionException(Money debits, Money credits) {
super("debits " + debits + " != credits " + credits);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package sn.senwallet.ledger.domain.model.account;

public enum AccountStatus {
ACTIVE,
INACTIVE
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package sn.senwallet.ledger.domain.model.account;

/**
* {@code SYNC} accounts are locked and updated in the posting transaction;
* {@code ASYNC} accounts (shared hot accounts such as revenue) take no lock and
* therefore cannot carry a balance condition (ADR-05).
*/
public enum BalanceMode {
SYNC,
ASYNC
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package sn.senwallet.ledger.domain.model.account;

import java.util.Objects;
import sn.senwallet.ledger.domain.model.transaction.Amount;
import sn.senwallet.ledger.domain.model.transaction.Direction;
import sn.senwallet.shared.money.Currency;
import sn.senwallet.shared.money.CurrencyMismatchException;
import sn.senwallet.shared.money.Money;

/**
* The four counters of an account ({@code account_balances}). Each bucket only
* ever accumulates entry amounts, so it is never negative; balances are derived
* from them according to the account's {@link NormalBalance}.
*/
public record Balances(
Money postedDebits,
Money postedCredits,
Money pendingDebits,
Money pendingCredits,
long lockVersion) {

public Balances {
Objects.requireNonNull(postedDebits, "postedDebits");
Objects.requireNonNull(postedCredits, "postedCredits");
Objects.requireNonNull(pendingDebits, "pendingDebits");
Objects.requireNonNull(pendingCredits, "pendingCredits");
var currency = postedDebits.currency();
for (var bucket : new Money[] { postedCredits, pendingDebits, pendingCredits }) {
if (bucket.currency() != currency) {
throw new CurrencyMismatchException(currency, bucket.currency());
}
}
for (var bucket : new Money[] { postedDebits, postedCredits, pendingDebits, pendingCredits }) {
if (bucket.isNegative()) {
throw new IllegalArgumentException("balance bucket must be >= 0, got " + bucket);
}
}
if (lockVersion < 0) {
throw new IllegalArgumentException("lockVersion must be >= 0, got " + lockVersion);
}
}

public static Balances zero(Currency currency) {
var zero = Money.zero(currency);
return new Balances(zero, zero, zero, zero, 0);
}

public Currency currency() {
return postedDebits.currency();
}

/** Settled entries only. */
public Money posted(NormalBalance normal) {
return normal == NormalBalance.CREDIT
? postedCredits.minus(postedDebits)
: postedDebits.minus(postedCredits);
}

/** Settled plus in-flight entries, in both directions. */
public Money pending(NormalBalance normal) {
var debits = postedDebits.plus(pendingDebits);
var credits = postedCredits.plus(pendingCredits);
return normal == NormalBalance.CREDIT ? credits.minus(debits) : debits.minus(credits);
}

/**
* Settled minus in-flight outflows; in-flight inflows are not spendable yet.
*/
public Money available(NormalBalance normal) {
return normal == NormalBalance.CREDIT
? postedCredits.minus(postedDebits.plus(pendingDebits))
: postedDebits.minus(postedCredits.plus(pendingCredits));
}

public Balances withPosted(Direction direction, Amount amount) {
var value = amount.value();
return direction == Direction.DEBIT
? new Balances(postedDebits.plus(value), postedCredits, pendingDebits, pendingCredits, lockVersion)
: new Balances(postedDebits, postedCredits.plus(value), pendingDebits, pendingCredits, lockVersion);
}

public Balances withPending(Direction direction, Amount amount) {
var value = amount.value();
return direction == Direction.DEBIT
? new Balances(postedDebits, postedCredits, pendingDebits.plus(value), pendingCredits, lockVersion)
: new Balances(postedDebits, postedCredits, pendingDebits, pendingCredits.plus(value), lockVersion);
}

/**
* Moves an amount from the pending bucket to the posted bucket of the same
* direction.
*/
public Balances settled(Direction direction, Amount amount) {
return released(direction, amount).withPosted(direction, amount);
}

/**
* Removes an amount from the pending bucket; throws if the bucket does not hold
* it.
*/
public Balances released(Direction direction, Amount amount) {
var value = amount.value();
return direction == Direction.DEBIT
? new Balances(postedDebits, postedCredits, pendingDebits.minus(value), pendingCredits, lockVersion)
: new Balances(postedDebits, postedCredits, pendingDebits, pendingCredits.minus(value), lockVersion);
}

public Balances nextVersion() {
return new Balances(postedDebits, postedCredits, pendingDebits, pendingCredits, lockVersion + 1);
}
}
Loading
Loading