Skip to content

Commit

Permalink
post func validate before sending
Browse files Browse the repository at this point in the history
  • Loading branch information
ungerik committed Sep 3, 2024
1 parent 52cb1ed commit 77523c8
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 0 deletions.
28 changes: 28 additions & 0 deletions golang/domonda/bankaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package domonda

import (
"context"
"errors"
"fmt"
"net/url"

Expand All @@ -26,6 +27,23 @@ type BankAccount struct {
Description nullable.TrimmedString
}

func (a *BankAccount) Validate() error {
var err error
if e := a.IBAN.Validate(); e != nil {
err = errors.Join(err, fmt.Errorf("invalid BankAccount.IBAN %q: %w", a.IBAN, e))
}
if e := a.BIC.Validate(); e != nil {
err = errors.Join(err, fmt.Errorf("invalid BankAccount.BIC %q: %w", a.BIC, e))
}
if !a.Currency.Valid() {
err = errors.Join(err, fmt.Errorf("invalid BankAccount.Currency %q", a.Currency))
}
if a.Holder.IsEmpty() {
err = errors.Join(err, errors.New("empty BankAccount.Holder"))
}
return err
}

// PostBankAccounts posts the given bankAccounts to the domonda API.
//
// Usage example:
Expand All @@ -37,6 +55,16 @@ type BankAccount struct {
// --include \
// https://domonda.app/api/public/masterdata/bank-accounts
func PostBankAccounts(ctx context.Context, apiKey string, bankAccounts []*BankAccount, source string) error {
var err error
for i, acc := range bankAccounts {
if e := acc.Validate(); e != nil {
err = errors.Join(err, fmt.Errorf("BankAccount at index %d has error: %w", i, e))
}
}
if err != nil {
return err
}

vals := make(url.Values)
if source != "" {
vals.Set("source", source)
Expand Down
10 changes: 10 additions & 0 deletions golang/domonda/glaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ func (a *GLAccount) Validate() error {

// nil for objectSpecificAccountNos means we do not care about this
func PostGLAccounts(ctx context.Context, apiKey string, accounts []*GLAccount, objectSpecificAccountNos *bool, source string) error {
var err error
for i, acc := range accounts {
if e := acc.Validate(); e != nil {
err = errors.Join(err, fmt.Errorf("GLAccount at index %d has error: %w", i, e))
}
}
if err != nil {
return err
}

vals := make(url.Values)
if objectSpecificAccountNos != nil {
vals.Set("objectSpecificAccountNos", fmt.Sprint(*objectSpecificAccountNos))
Expand Down
10 changes: 10 additions & 0 deletions golang/domonda/partner.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ func (p *Partner) ClientAccountNumberUint() uint64 {
}

func PostPartners(ctx context.Context, apiKey string, partners []*Partner, source string) error {
var err error
for i, partner := range partners {
if e := partner.Validate(); e != nil {
err = errors.Join(err, fmt.Errorf("Partner at index %d has error: %w", i, e))
}
}
if err != nil {
return err
}

vals := make(url.Values)
if source != "" {
vals.Set("source", source)
Expand Down
10 changes: 10 additions & 0 deletions golang/domonda/realestateobject.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,16 @@ func (r RealEstateObjectType) String() string {
}

func PostRealEstateObjects(ctx context.Context, apiKey string, objects []*RealEstateObject, source string) error {
var err error
for i, obj := range objects {
if e := obj.Validate(); e != nil {
err = errors.Join(err, fmt.Errorf("RealEstateObject at index %d has error: %w", i, e))
}
}
if err != nil {
return err
}

vals := make(url.Values)
if source != "" {
vals.Set("source", source)
Expand Down

0 comments on commit 77523c8

Please sign in to comment.