moov-io / customers

Customer registry supporting Know Your Customer (KYC), Customer Identification Program (CIP), and OFAC checks
https://moov.io
Apache License 2.0
69 stars 19 forks source link

Creating customer with multiple addresses #280

Closed artemderkach closed 3 years ago

artemderkach commented 3 years ago

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

adamdecaf commented 3 years ago

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

adamdecaf commented 3 years ago

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!