78 lines
2.1 KiB
Go
78 lines
2.1 KiB
Go
package model
|
|
|
|
import (
|
|
"sort"
|
|
"strings"
|
|
|
|
"git.sendico.io/tech/sxchnge/bff/pkg/merrors"
|
|
"git.sendico.io/tech/sxchnge/bff/pkg/moneybase"
|
|
)
|
|
|
|
type Currency string
|
|
|
|
const (
|
|
CurrencyEUR Currency = "EUR" // Euro
|
|
CurrencyUSD Currency = "USD" // US Dollar
|
|
CurrencyRUB Currency = "RUB" // Russian Ruble
|
|
CurrencyUAH Currency = "UAH" // Ukrainian Hryvnia
|
|
CurrencyPLN Currency = "PLN" // Polish Złoty
|
|
CurrencyCZK Currency = "CZK" // Czech Koruna
|
|
CurrencyUSDT Currency = "USDT" // Tether USD
|
|
)
|
|
|
|
const MaxCurrencyScale uint32 = moneybase.MaxScale
|
|
|
|
func NormalizeCurrency(value string) Currency {
|
|
return Currency(strings.ToUpper(strings.TrimSpace(value)))
|
|
}
|
|
|
|
func NormalizeExplicitCurrencyScales(values map[Currency]uint32) (map[Currency]uint32, error) {
|
|
if len(values) == 0 {
|
|
return nil, merrors.InvalidArgument("currency scales are required")
|
|
}
|
|
out := make(map[Currency]uint32, len(values))
|
|
for rawCurrency, scale := range values {
|
|
currency := NormalizeCurrency(string(rawCurrency))
|
|
if currency == "" {
|
|
return nil, merrors.InvalidArgument("currency scale key is required")
|
|
}
|
|
if scale > MaxCurrencyScale {
|
|
return nil, merrors.InvalidArgumentf("currency scale for %s exceeds %d", currency, MaxCurrencyScale)
|
|
}
|
|
out[currency] = scale
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
func CurrencyScale(scales map[Currency]uint32, currency string) (uint32, error) {
|
|
normalized := NormalizeCurrency(currency)
|
|
if normalized == "" {
|
|
return 0, merrors.InvalidArgument("currency is required")
|
|
}
|
|
if scales == nil {
|
|
return 0, merrors.InvalidArgument("currency scale map is required")
|
|
}
|
|
scale, ok := scales[normalized]
|
|
if !ok {
|
|
return 0, merrors.InvalidArgument("currency scale for " + string(normalized) + " is not configured")
|
|
}
|
|
return scale, nil
|
|
}
|
|
|
|
func CurrencyScaleKeys(scales map[Currency]uint32) []string {
|
|
if scales == nil {
|
|
return nil
|
|
}
|
|
keys := make([]string, 0, len(scales))
|
|
for currency := range scales {
|
|
keys = append(keys, string(currency))
|
|
}
|
|
sort.Strings(keys)
|
|
return keys
|
|
}
|
|
|
|
type Amount struct {
|
|
Total float64 `bson:"total" json:"total"` // Total amount billed
|
|
Currency Currency `bson:"currency" json:"currency"` // Currency for the invoice
|
|
}
|