moov-io/customers

Creating customer with multiple addresses

artemderkach opened this issue · 2 comments

Customers Version: 0.5.0

What were you trying to do?
add customer with multiple addresses

What did you expect to see?
customer added successfully

What did you see?
Error customer already has an address with type 'primary'

How can we reproduce the problem?
So, my question is about this function

func validateAddresses(addrs []address) error {
	hasPrimaryAddr := false
	for _, addr := range addrs {
		if hasPrimaryAddr {
			return ErrAddressTypeDuplicate
		}

		if err := addr.validate(); err != nil {
			return fmt.Errorf("validating address: %v", err)
		}

		if addr.Type == "primary" {
			hasPrimaryAddr = true
		}
	}

	return nil
}

in case we pass multiple addresses while creating customer, for example:
[secondary, secondary, primary] - customer will be created successfully
[primary, secondary, secondary] - this will fail with error customer already has an address with type 'primary'

function will not check if second and third addresses in array is secondary

My guess, for this function, it should look like this

func validateAddresses(addrs []address) error {
	hasPrimaryAddr := false
	for _, addr := range addrs {
		if hasPrimaryAddr && addr.Type == "primary" {
			return ErrAddressTypeDuplicate
		}

		if err := addr.validate(); err != nil {
			return fmt.Errorf("validating address: %v", err)
		}

		if addr.Type == "primary" {
			hasPrimaryAddr = true
		}
	}

	return nil
}

But maybe i'm getting a wrong vibes on what to expect from this function/functionality

Yep. That's totally a bug. I'll release v0.5.1 with a fix.

https://github.com/moov-io/customers/releases/tag/v0.5.1 has been released. Can you verify @Mind-Rot? Please re-open or reply here if you run into issues.

Thanks for the report!