Repository: moov-io/customers (go)
Issue: Saving metadata only saves/returns the last{key: value} pair
Customers Version: `0.5.0-dev25`

**What were you trying to do?**
Save customer metadata


**What did you expect to see?**
All properties passed to be saved.


**What did you see?**
Only the last property in the request body is being saved


**How can we reproduce the problem?**
`PUT /customers/{customerID}/metadata`
Request
```json
{
   "metadata":{
      "keyone":"value",
      "keytwo":"valuetwo",
      "keythree":"valuethree"
   }
}
```
Response: 
```json
{
...
   "metadata":{
      "keytwo":"valuetwo"
   },
...
}
```

Pull request description: fix: bug when fetching customer metadata
Fixes #259 

Patch:
diff --git a/pkg/customers/customer_search.go b/pkg/customers/customer_search.go
index 2b41b5a..f1e92de 100644
--- a/pkg/customers/customer_search.go
+++ b/pkg/customers/customer_search.go
@@ -286,20 +286,22 @@ func (r *sqlCustomerRepository) getMetadata(customerIDs []string) (map[string]cl
 	}
 	defer rows.Close()
 
-	ret := make(map[string]client.CustomerMetadata)
+	result := make(map[string]client.CustomerMetadata)
+	m := client.CustomerMetadata{
+		Metadata: make(map[string]string),
+	}
+
 	for rows.Next() {
-		m := client.CustomerMetadata{
-			Metadata: make(map[string]string),
-		}
 		var customerID string
 		var k, v string
 		if err := rows.Scan(&customerID, &k, &v); err != nil {
 			return nil, fmt.Errorf("scanning row: %v", err)
 		}
 		m.Metadata[k] = v
-		ret[customerID] = m
+		result[customerID] = m
 	}
-	return ret, nil
+
+	return result, nil
 }
 
 func (r *sqlCustomerRepository) queryRowsByCustomerIDs(query string, customerIDs []string) (*sql.Rows, error) {
diff --git a/pkg/customers/customers.go b/pkg/customers/customers.go
index ee0da13..ac09807 100644
--- a/pkg/customers/customers.go
+++ b/pkg/customers/customers.go
@@ -708,7 +708,6 @@ func (r *sqlCustomerRepository) replaceCustomerMetadata(customerID string, metad
 	}
 	stmt.Close()
 
-	// Insert each k/v pair
 	query = `insert into customer_metadata (customer_id, meta_key, meta_value) values (?, ?, ?);`
 	stmt, err = tx.Prepare(query)
 	if err != nil {


Test patch:
diff --git a/pkg/customers/customers_test.go b/pkg/customers/customers_test.go
index 4f4e515..98ff57c 100644
--- a/pkg/customers/customers_test.go
+++ b/pkg/customers/customers_test.go
@@ -785,10 +785,20 @@ func TestCustomers__replaceCustomerMetadata(t *testing.T) {
 		t.Fatal(err)
 	}
 
-	body := strings.NewReader(`{ "metadata": { "key": "bar"} }`)
+	payload := struct {
+		Metadata map[string]string `json:"metadata"`
+	}{
+		Metadata: map[string]string{
+			"key-1": "val-1",
+			"key-2": "val-2",
+			"key-3": "val-3",
+		},
+	}
+	b, err := json.Marshal(payload)
+	require.NoError(t, err)
 
 	w := httptest.NewRecorder()
-	req := httptest.NewRequest("PUT", fmt.Sprintf("/customers/%s/metadata", cust.CustomerID), body)
+	req := httptest.NewRequest("PUT", fmt.Sprintf("/customers/%s/metadata", cust.CustomerID), bytes.NewReader(b))
 	req.Header.Set("x-organization", organization)
 	req.Header.Set("x-request-id", "test")
 
@@ -805,9 +815,7 @@ func TestCustomers__replaceCustomerMetadata(t *testing.T) {
 	if err := json.NewDecoder(w.Body).Decode(&customer); err != nil {
 		t.Fatal(err)
 	}
-	if customer.Metadata["key"] != "bar" {
-		t.Errorf("unknown Customer metadata: %#v", customer.Metadata)
-	}
+	require.Equal(t, payload.Metadata, customer.Metadata)
 
 	// sad path
 	repo2 := &testCustomerRepository{err: errors.New("bad error")}