account

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2025 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateAccount

func CreateAccount(
	accountSvc *accountsvc.Service,
	authSvc *authsvc.Service,
) fiber.Handler

CreateAccount returns a Fiber handler for creating a new account for the current user. It extracts the user ID from the request context, initializes the account service using the provided UnitOfWork factory, and attempts to create a new account. On success, it returns the created account as JSON. On failure, it logs the error and returns an appropriate error response. @Summary Create a new account @Description Creates a new account for the authenticated user.

You can specify the currency for the account.
Returns the created account details.

@Tags accounts @Accept json @Produce json @Success 201 {object} common.Response "Account created successfully" @Failure 400 {object} common.ProblemDetails "Invalid request" @Failure 401 {object} common.ProblemDetails "Unauthorized" @Failure 429 {object} common.ProblemDetails "Too many requests" @Failure 500 {object} common.ProblemDetails "Internal server error" @Router /account [post] @Security Bearer

func Deposit

func Deposit(
	accountSvc *accountsvc.Service,
	authSvc *authsvc.Service,
) fiber.Handler

Deposit returns a Fiber handler for depositing an amount into a user's account. It expects a UnitOfWork factory function as a dependency for transactional operations. The handler parses the current user ID from the request context, validates the account ID from the URL, and parses the deposit amount from the request body. If successful, it performs the deposit operation using the AccountService and returns the transaction as JSON. On error, it logs the issue and returns an appropriate JSON error response. @Summary Deposit funds into an account @Description Adds funds to the specified account. Specify the amount, currency, and optional money source. Returns the transaction details. @Tags accounts @Accept json @Produce json @Param id path string true "Account ID" @Param request body DepositRequest true "Deposit details" @Success 200 {object} common.Response "Deposit successful" @Failure 400 {object} common.ProblemDetails "Invalid request" @Failure 401 {object} common.ProblemDetails "Unauthorized" @Failure 429 {object} common.ProblemDetails "Too many requests" @Failure 500 {object} common.ProblemDetails "Internal server error" @Router /account/{id}/deposit [post] @Security Bearer

func GetBalance

func GetBalance(
	accountSvc *accountsvc.Service,
	authSvc *authsvc.Service,
) fiber.Handler

GetBalance returns a Fiber handler for retrieving the balance of a specific account. It expects a UnitOfWork factory function as a dependency for service instantiation. The handler extracts the current user ID from the request context and parses the account ID from the URL parameters. On success, it returns the account balance as a JSON response. On error, it logs the error and returns an appropriate JSON error response. @Summary Get account balance @Description Retrieves the current balance for the specified account. Returns the balance amount and currency. @Tags accounts @Accept json @Produce json @Param id path string true "Account ID" @Success 200 {object} common.Response "Balance fetched" @Failure 400 {object} common.ProblemDetails "Invalid request" @Failure 401 {object} common.ProblemDetails "Unauthorized" @Failure 429 {object} common.ProblemDetails "Too many requests" @Failure 500 {object} common.ProblemDetails "Internal server error" @Router /account/{id}/balance [get] @Security Bearer

func GetTransactions

func GetTransactions(
	accountSvc *accountsvc.Service,
	authSvc *authsvc.Service,
) fiber.Handler

GetTransactions returns a Fiber handler that retrieves the list of transactions

for a specific account.

It expects a UnitOfWork factory function as a dependency for service instantiation. The handler extracts the current user ID from the request context and parses the account ID from the URL parameters. On success, it returns the transactions as a JSON response. On error, it logs the error and returns an appropriate JSON error response. @Summary Get account transactions @Description Retrieves a list of transactions for the specified account. Returns an array of transaction details. @Tags accounts @Accept json @Produce json @Param id path string true "Account ID" @Success 200 {object} common.Response "Transactions fetched" @Failure 400 {object} common.ProblemDetails "Invalid request" @Failure 401 {object} common.ProblemDetails "Unauthorized" @Failure 429 {object} common.ProblemDetails "Too many requests" @Failure 500 {object} common.ProblemDetails "Internal server error" @Router /account/{id}/transactions [get] @Security Bearer

func Routes

func Routes(
	app *fiber.App,
	accountSvc *accountsvc.Service,
	authSvc *authsvc.Service,
	cfg *config.App,
)

Routes registers HTTP routes for account-related operations using the Fiber web framework. It sets up endpoints for creating accounts, depositing and withdrawing funds, retrieving account balances, and listing account transactions. All routes are protected by authentication middleware and require a valid user context.

Routes:

  • POST /account : Create a new account for the authenticated user.
  • POST /account/:id/deposit : Deposit funds into the specified account.
  • POST /account/:id/withdraw : Withdraw funds from the specified account.
  • GET /account/:id/balance : Retrieve the balance of the specified account.
  • GET /account/:id/transactions : List transactions for the specified account.

func Transfer

func Transfer(
	accountSvc *accountsvc.Service,
	authSvc *authsvc.Service,
) fiber.Handler

Transfer returns a Fiber handler for transferring funds between accounts. @Summary Transfer funds between accounts @Description Transfers a specified amount from one account to another. Specify the source and destination account IDs, amount, and currency. Returns the transaction details. @Tags accounts @Accept json @Produce json @Param id path string true "Source Account ID" @Param request body TransferRequest true "Transfer details" @Success 200 {object} common.Response "Transfer successful" @Failure 400 {object} common.ProblemDetails "Invalid request" @Failure 401 {object} common.ProblemDetails "Unauthorized" @Failure 422 {object} common.ProblemDetails "Unprocessable entity" @Failure 429 {object} common.ProblemDetails "Too many requests" @Failure 500 {object} common.ProblemDetails "Internal server error" @Router /account/{id}/transfer [post] @Security Bearer

func Withdraw

func Withdraw(
	accountSvc *accountsvc.Service,
	authSvc *authsvc.Service,
) fiber.Handler

Withdraw returns a Fiber handler for processing account withdrawal requests. It expects a UnitOfWork factory function as a dependency for transactional operations.

The handler performs the following steps:

  1. Retrieves the current user ID from the request context.
  2. Parses the account ID from the route parameters.
  3. Parses the withdrawal amount from the request body.
  4. Calls the AccountService.Withdraw method to process the withdrawal.
  5. Returns the transaction details as a JSON response on success.

Error responses are returned in JSON format with appropriate status codes if any step fails (e.g., invalid user ID, invalid account ID,

parsing errors, or withdrawal errors).

@Summary Withdraw funds from an account @Description Withdraws a specified amount from the user's account. Specify the amount and currency. Returns the transaction details.

@Tags accounts @Accept json @Produce json @Param id path string true "Account ID" @Param request body WithdrawRequest true "Withdrawal details" @Success 200 {object} common.Response "Withdrawal successful" @Failure 400 {object} common.ProblemDetails "Invalid request" @Failure 401 {object} common.ProblemDetails "Unauthorized" @Failure 429 {object} common.ProblemDetails "Too many requests" @Failure 500 {object} common.ProblemDetails "Internal server error" @Router /account/{id}/withdraw [post] @Security Bearer

Types

type ConversionInfoDTO

type ConversionInfoDTO struct {
	OriginalAmount    float64 `json:"original_amount"`
	OriginalCurrency  string  `json:"original_currency"`
	ConvertedAmount   float64 `json:"converted_amount"`
	ConvertedCurrency string  `json:"converted_currency"`
	ConversionRate    float64 `json:"conversion_rate"`
}

ConversionInfoDTO holds conversion details for API responses.

func ToConversionInfoDTO

func ToConversionInfoDTO(convInfo *provider.ExchangeInfo) *ConversionInfoDTO

ToConversionInfoDTO maps provider.ExchangeRate to ConversionInfoDTO.

type CreateAccountRequest

type CreateAccountRequest struct {
	Currency string `json:"currency" validate:"omitempty,len=3,uppercase,alpha"`
}

CreateAccountRequest represents the request body for creating a new account.

type DepositRequest

type DepositRequest struct {
	Amount      float64 `json:"amount" xml:"amount" form:"amount" validate:"required,gt=0"`
	Currency    string  `json:"currency" validate:"omitempty,len=3,uppercase"`
	MoneySource string  `json:"money_source" validate:"required,min=2,max=64"`
}

DepositRequest represents the request body for depositing funds into an account.

type ExternalTarget

type ExternalTarget struct {
	BankAccountNumber     string `json:"bank_account_number,omitempty" validate:"omitempty,min=6,max=34"`
	RoutingNumber         string `json:"routing_number,omitempty" validate:"omitempty,min=6,max=12"`
	ExternalWalletAddress string `json:"external_wallet_address,omitempty" validate:"omitempty,min=6,max=128"`
}

ExternalTarget represents the destination for an external withdrawal, such as a bank account or wallet.

type TransactionDTO

type TransactionDTO struct {
	ID          string  `json:"id"`
	UserID      string  `json:"user_id"`
	AccountID   string  `json:"account_id"`
	Amount      float64 `json:"amount"`
	Balance     float64 `json:"balance"`
	CreatedAt   string  `json:"created_at"`
	Currency    string  `json:"currency"`
	MoneySource string  `json:"money_source"`
}

TransactionDTO is the API response representation of a transaction.

func ToTransactionDTO

func ToTransactionDTO(tx *dto.TransactionRead) *TransactionDTO

ToTransactionDTO maps a dto.TransactionRead to a TransactionDTO.

type TransferRequest

type TransferRequest struct {
	Amount               float64 `json:"amount" validate:"required,gt=0"`
	Currency             string  `json:"currency" validate:"omitempty,len=3,uppercase,alpha"`
	DestinationAccountID string  `json:"destination_account_id" validate:"required,uuid4"`
}

TransferRequest represents the request body for transferring funds between accounts.

type TransferResponseDTO

type TransferResponseDTO struct {
	Outgoing       *TransactionDTO    `json:"outgoing_transaction"`
	Incoming       *TransactionDTO    `json:"incoming_transaction"`
	ConversionInfo *ConversionInfoDTO `json:"conversion_info"`
}

TransferResponseDTO is the API response for a transfer operation, containing both transactions and a single conversion_info field (like deposit/withdraw).

func ToTransferResponseDTO

func ToTransferResponseDTO(txOut, txIn *dto.TransactionRead, convInfo *provider.ExchangeInfo) *TransferResponseDTO

ToTransferResponseDTO maps domain transactions and conversion info to a TransferResponseDTO with a single conversion_info field.

type WithdrawRequest

type WithdrawRequest struct {
	Amount         float64        `json:"amount" xml:"amount" form:"amount" validate:"required,gt=0"`
	Currency       string         `json:"currency" validate:"omitempty,len=3,uppercase"`
	ExternalTarget ExternalTarget `json:"external_target" validate:"required"`
}

WithdrawRequest represents the request body for withdrawing funds from an account.

Jump to

Keyboard shortcuts

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