package ledger

import (
	"context"

	"github.com/tech/sendico/pkg/db/transaction"
	"github.com/tech/sendico/pkg/merrors"
)

type transactionProvider interface {
	TransactionFactory() transaction.Factory
}

func (s *Service) executeTransaction(ctx context.Context, cb transaction.Callback) (any, error) {
	provider, ok := s.storage.(transactionProvider)
	if !ok || provider == nil {
		return nil, merrors.Internal("storage does not support transactions")
	}
	return provider.TransactionFactory().CreateTransaction().Execute(ctx, cb)
}
