transfers

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 21, 2020 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Deposit  = "DEPOSIT"
	Withdraw = "WITHDRAW"
	Internal = "INTERNAL"
)

Transfer type enums.

View Source
const (
	Incoming = "INCOMING"
	Outgoing = "OUTGOING"
)

Direction enums.

Variables

View Source
var (
	ErrAccountsMustBeDifferent = errors.New("accounts_must_be_different")
	ErrAmountMustBePositive    = errors.New("transfer_amount_must_be_positive")
	ErrUnsupportedCurrency     = errors.New("currency_not_supported")
	ErrInsufficientFunds       = errors.New("insufficient_funds")
	ErrSenderNotExists         = errors.New("sender_account_not_exist")
	ErrReceiverNotExists       = errors.New("receiver_account_not_exist")
	ErrSenderWrongCurrency     = errors.New("sender_account_wrong_currency")
	ErrReceiverWrongCurrency   = errors.New("receiver_account_wrong_currency")
	ErrEmptyTransferID         = errors.New("transfer_id_is_empty")
	ErrEmptySenderAccountID    = errors.New("sender_account_id_is_empty")
	ErrEmptyReceiverAccountID  = errors.New("receiver_account_id_is_empty")
)

Business logic level errors that provide enough information about what went wrong.

View Source
var ErrNoRowsAffected = errors.New("db_no_rows_affected")

Functions

func DecodeCreateTransferRequest

func DecodeCreateTransferRequest(_ context.Context, r *http.Request) (interface{}, error)

func DecodeGetAccountsRequest

func DecodeGetAccountsRequest(_ context.Context, r *http.Request) (interface{}, error)

func DecodeGetTransfersForAccountRequest

func DecodeGetTransfersForAccountRequest(_ context.Context, r *http.Request) (interface{}, error)

func EncodeCreateTransferResponse

func EncodeCreateTransferResponse(_ context.Context, w http.ResponseWriter, res interface{}) error

func EncodeGetAccountsResponse

func EncodeGetAccountsResponse(_ context.Context, w http.ResponseWriter, res interface{}) error

func EncodeGetTransfersForAccountResponse

func EncodeGetTransfersForAccountResponse(_ context.Context, w http.ResponseWriter, res interface{}) error

func ErrorEncoder

func ErrorEncoder(_ context.Context, err error, w http.ResponseWriter)

func MakeCreateTransferEndpoint

func MakeCreateTransferEndpoint(s Service) endpoint.Endpoint

func MakeGetAccountsEndpoint

func MakeGetAccountsEndpoint(s Service) endpoint.Endpoint

func MakeGetTransfersForAccountEndpoint

func MakeGetTransfersForAccountEndpoint(s Service) endpoint.Endpoint

func NewHTTPHandler

func NewHTTPHandler(endpoints Endpoints, logger log.Logger) http.Handler

Types

type Account

type Account struct {
	ID           uuid.UUID       `json:"id"`
	CurrencyCode string          `json:"currency_code"`
	Balance      decimal.Decimal `json:"balance"`
	CreatedAt    time.Time       `json:"created_at"`
	UpdatedAt    time.Time       `json:"updated_at"`
}

Account representation with time fields that are updated accordingly.

type CommonResponse

type CommonResponse struct {
	Error   string      `json:"error,omitempty"`
	Result  string      `json:"result"`
	Payload interface{} `json:"payload,omitempty"`
}

func NewCommonResponse

func NewCommonResponse(payload interface{}, err error) CommonResponse

type CreateTransferRequest

type CreateTransferRequest struct {
	InnerTransferOrder
}

type CreateTransferResponse

type CreateTransferResponse struct {
	Err error
}

type Currency

type Currency struct {
	Code      string
	Precision uint
}

Currency model for multiple currencies each one with different precision.

type Endpoints

type Endpoints struct {
	CreateTransfer         endpoint.Endpoint
	GetTransfersForAccount endpoint.Endpoint
	GetAccounts            endpoint.Endpoint
}

func NewEndpoints

func NewEndpoints(s Service) Endpoints

type GetAccountsRequest

type GetAccountsRequest struct{}

type GetAccountsResponse

type GetAccountsResponse struct {
	Accounts []Account
	Err      error
}

type GetTransfersForAccountRequest

type GetTransfersForAccountRequest struct {
	AccountID uuid.UUID
}

type GetTransfersForAccountResponse

type GetTransfersForAccountResponse struct {
	Transfers []TransferInfo
	Err       error
}

type InnerTransferActions

type InnerTransferActions interface {
	CreateTransfer(transfer Transfer) error
	UpdateBalance(accountID uuid.UUID, diff decimal.Decimal) error
	CreateTransferPart(tp TransferPart) error
}

type InnerTransferCallback

type InnerTransferCallback func(sender, receiver Account, a InnerTransferActions) error

type InnerTransferOrder

type InnerTransferOrder struct {
	ID                uuid.UUID       `json:"id"`
	SenderAccountID   uuid.UUID       `json:"sender_account_id"`
	ReceiverAccountID uuid.UUID       `json:"receiver_account_id"`
	Amount            decimal.Decimal `json:"amount"`
	CurrencyCode      string          `json:"currency_code"`
}

Transfer order for system to process.

type Repository

type Repository interface {
	GetPrecision(ctx context.Context, currency string) (precision uint, exists bool, err error)
	// For separation business logic errors from database errors
	IsTransferIDUsedError(err error) bool
	IsEntityNotFoundError(uuid uuid.UUID, err error) bool
	// locks sender and receiver and manipulates data inside db transaction,
	// return entity not found error if sender or receiver don't exist
	CreateInnerTransferTransactionWithLock(ctx context.Context, sender, receiver uuid.UUID, c InnerTransferCallback) error
	GetAccounts(ctx context.Context, limit uint) ([]Account, error)
	GetTransferInfos(ctx context.Context, accountID uuid.UUID, limit uint) ([]TransferInfo, error)
}

func NewRepository

func NewRepository(db *sql.DB) Repository

type Service

type Service interface {
	CreateTransfer(ctx context.Context, order InnerTransferOrder) error
	GetTransfersForAccount(ctx context.Context, accountID uuid.UUID) ([]TransferInfo, error)
	GetAccounts(ctx context.Context) ([]Account, error)
}

Business actions.

func NewService

func NewService(repo Repository) Service

type Transfer

type Transfer struct {
	ID           uuid.UUID
	Type         string // Deposit, Withdraw, Internal
	Amount       decimal.Decimal
	CurrencyCode string
	CreatedAt    time.Time
}

type TransferInfo

type TransferInfo struct {
	ID                     uuid.UUID       `json:"id"`
	AccountID              uuid.UUID       `json:"account_id"`
	CorrespondingAccountID *uuid.UUID      `json:"corresponding_account_id"`
	Type                   string          `json:"type"`
	Direction              string          `json:"direction"`
	Amount                 decimal.Decimal `json:"amount"`
	CurrencyCode           string          `json:"currency_code"`
	CreatedAt              time.Time       `json:"created_at"`
}

type TransferPart

type TransferPart struct {
	TransferID             uuid.UUID
	AccountID              uuid.UUID
	CorrespondingAccountID *uuid.UUID
	Direction              string
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL