package storage import ( "context" "time" "github.com/tech/sendico/ledger/storage/model" pkm "github.com/tech/sendico/pkg/model" "go.mongodb.org/mongo-driver/bson/primitive" ) type storageError string func (e storageError) Error() string { return string(e) } var ( ErrAccountNotFound = storageError("ledger.storage: account not found") ErrJournalEntryNotFound = storageError("ledger.storage: journal entry not found") ErrBalanceNotFound = storageError("ledger.storage: balance not found") ErrDuplicateIdempotency = storageError("ledger.storage: duplicate idempotency key") ErrInsufficientBalance = storageError("ledger.storage: insufficient balance") ErrAccountFrozen = storageError("ledger.storage: account is frozen") ErrNegativeBalancePolicy = storageError("ledger.storage: negative balance not allowed") ) // AccountsFilter describes optional filter parameters for listing accounts. type AccountsFilter struct { // OwnerRefFilter is a 3-state filter: // - nil: no filter on owner_ref (return all) // - pointer to zero ObjectID: filter for accounts where owner_ref is nil // - pointer to a value: filter for accounts where owner_ref matches OwnerRefFilter *primitive.ObjectID } type AccountsStore interface { Create(ctx context.Context, account *pkm.LedgerAccount) error Get(ctx context.Context, accountRef primitive.ObjectID) (*pkm.LedgerAccount, error) GetByAccountCode(ctx context.Context, orgRef primitive.ObjectID, accountCode, currency string) (*pkm.LedgerAccount, error) GetByRole(ctx context.Context, orgRef primitive.ObjectID, currency string, role pkm.AccountRole) (*pkm.LedgerAccount, error) GetSystemAccount(ctx context.Context, purpose pkm.SystemAccountPurpose, currency string) (*pkm.LedgerAccount, error) GetDefaultSettlement(ctx context.Context, orgRef primitive.ObjectID, currency string) (*pkm.LedgerAccount, error) ListByOrganization(ctx context.Context, orgRef primitive.ObjectID, filter *AccountsFilter, limit int, offset int) ([]*pkm.LedgerAccount, error) UpdateStatus(ctx context.Context, accountRef primitive.ObjectID, status pkm.LedgerAccountStatus) error } type JournalEntriesStore interface { Create(ctx context.Context, entry *model.JournalEntry) error Get(ctx context.Context, entryRef primitive.ObjectID) (*model.JournalEntry, error) GetByIdempotencyKey(ctx context.Context, orgRef primitive.ObjectID, idempotencyKey string) (*model.JournalEntry, error) ListByOrganization(ctx context.Context, orgRef primitive.ObjectID, limit int, offset int) ([]*model.JournalEntry, error) } type PostingLinesStore interface { CreateMany(ctx context.Context, lines []*model.PostingLine) error ListByJournalEntry(ctx context.Context, entryRef primitive.ObjectID) ([]*model.PostingLine, error) ListByAccount(ctx context.Context, accountRef primitive.ObjectID, limit int, offset int) ([]*model.PostingLine, error) } type BalancesStore interface { Get(ctx context.Context, accountRef primitive.ObjectID) (*model.AccountBalance, error) Upsert(ctx context.Context, balance *model.AccountBalance) error IncrementBalance(ctx context.Context, accountRef primitive.ObjectID, amount string) error } type OutboxStore interface { Create(ctx context.Context, event *model.OutboxEvent) error ListPending(ctx context.Context, limit int) ([]*model.OutboxEvent, error) MarkSent(ctx context.Context, eventRef primitive.ObjectID, sentAt time.Time) error MarkFailed(ctx context.Context, eventRef primitive.ObjectID) error IncrementAttempts(ctx context.Context, eventRef primitive.ObjectID) error }