File size: 1,759 Bytes
cee2387
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package resolvers

import (
	"errors"
	"fmt"
	"net/http"

	"github.com/openmeterio/openmeter/openmeter/customer"
	"github.com/openmeterio/openmeter/openmeter/ledger"
	"github.com/openmeterio/openmeter/pkg/framework/commonhttp"
	"github.com/openmeterio/openmeter/pkg/models"
)

type validationErrors interface {
	ValidationErrors() (models.ValidationIssues, error)
}

const ErrCodeCustomerAccountConflict models.ErrorCode = "customer_account_conflict"

var ErrCustomerAccountConflict = models.NewValidationIssue(
	ErrCodeCustomerAccountConflict,
	"customer account mapping conflict, a mapping with the same customer and account type already exists",
	commonhttp.WithHTTPStatusCodeAttribute(http.StatusConflict),
)

type CustomerAccountAlreadyExistsError struct {
	CustomerID  customer.CustomerID
	AccountType ledger.AccountType
	AccountID   string
}

var _ validationErrors = (*CustomerAccountAlreadyExistsError)(nil)

func (e *CustomerAccountAlreadyExistsError) Error() string {
	return fmt.Sprintf(
		"customer account mapping already exists: namespace=%s customer_id=%s account_type=%s account_id=%s",
		e.CustomerID.Namespace,
		e.CustomerID.ID,
		e.AccountType,
		e.AccountID,
	)
}

func (e *CustomerAccountAlreadyExistsError) ValidationErrors() (models.ValidationIssues, error) {
	return models.ValidationIssues{
		ErrCustomerAccountConflict.WithAttrs(models.Attributes{
			"namespace":    e.CustomerID.Namespace,
			"customer_id":  e.CustomerID.ID,
			"account_type": e.AccountType,
			"account_id":   e.AccountID,
		}),
	}, nil
}

func AsCustomerAccountAlreadyExistsError(err error) (*CustomerAccountAlreadyExistsError, bool) {
	var target *CustomerAccountAlreadyExistsError
	if errors.As(err, &target) {
		return target, true
	}

	return nil, false
}