mirror of
https://github.com/hanzo-fi/payments.git
synced 2026-10-30 20:31:20 +00:00
* Add more validation constraints to connector config base fields * Add engine tests for connector config update * Ensure that name field is also updated alongside connector config update * Remove unusued connectorID state variable from Adyen plugin * Provide plugin wrapper with PSP name and ConnectorID * Create unit tests for wrapper to improve test coverage
77 lines
1.6 KiB
Go
77 lines
1.6 KiB
Go
package models
|
|
|
|
import (
|
|
"encoding/json"
|
|
"time"
|
|
|
|
"github.com/go-playground/validator/v10"
|
|
)
|
|
|
|
const (
|
|
defaultPollingPeriod = 2 * time.Minute
|
|
defaultPageSize = 25
|
|
)
|
|
|
|
type Config struct {
|
|
Name string `json:"name" validate:"required,gte=3,lte=500"`
|
|
PollingPeriod time.Duration `json:"pollingPeriod" validate:"required,gte=30000000000,lte=86400000000000"` // gte=30s lte=1d in ns
|
|
PageSize int `json:"pageSize" validate:"lte=150"`
|
|
}
|
|
|
|
func (c Config) MarshalJSON() ([]byte, error) {
|
|
return json.Marshal(struct {
|
|
Name string `json:"name"`
|
|
PollingPeriod string `json:"pollingPeriod"`
|
|
PageSize int `json:"pageSize"`
|
|
}{
|
|
Name: c.Name,
|
|
PollingPeriod: c.PollingPeriod.String(),
|
|
PageSize: c.PageSize,
|
|
})
|
|
}
|
|
|
|
func (c *Config) UnmarshalJSON(data []byte) error {
|
|
var raw struct {
|
|
Name string `json:"name"`
|
|
PollingPeriod string `json:"pollingPeriod"`
|
|
PageSize int `json:"pageSize"`
|
|
}
|
|
|
|
if err := json.Unmarshal(data, &raw); err != nil {
|
|
return err
|
|
}
|
|
|
|
pollingPeriod := defaultPollingPeriod
|
|
if raw.PollingPeriod != "" {
|
|
p, err := time.ParseDuration(raw.PollingPeriod)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
pollingPeriod = p
|
|
}
|
|
|
|
c.Name = raw.Name
|
|
|
|
if pollingPeriod > 0 {
|
|
c.PollingPeriod = pollingPeriod
|
|
}
|
|
|
|
if raw.PageSize > 0 {
|
|
c.PageSize = raw.PageSize
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (c Config) Validate() error {
|
|
validate := validator.New(validator.WithRequiredStructEnabled())
|
|
return validate.Struct(c)
|
|
}
|
|
|
|
func DefaultConfig() Config {
|
|
return Config{
|
|
PollingPeriod: defaultPollingPeriod,
|
|
PageSize: defaultPageSize,
|
|
}
|
|
}
|