Repository: moov-io/customers
Pull request #134 (2020-09-02 21:45:18)

Update customer search functionality to allow searching for Customer by Status.
This PR updates the search functionality for Customers to allow searching by Status.


Patch:
diff --git a/cmd/server/customer_search.go b/cmd/server/customer_search.go
index 29d2e94..164e24e 100644
--- a/cmd/server/customer_search.go
+++ b/cmd/server/customer_search.go
@@ -42,6 +42,7 @@ func searchCustomers(logger log.Logger, repo customerRepository) http.HandlerFun
 type searchParams struct {
 	Query string
 	Email string
+	Status string
 	Limit int64
 }
 
@@ -49,6 +50,7 @@ func readSearchParams(u *url.URL) searchParams {
 	params := searchParams{
 		Query: strings.ToLower(strings.TrimSpace(u.Query().Get("query"))),
 		Email: strings.ToLower(strings.TrimSpace(u.Query().Get("email"))),
+		Status: strings.ToLower(strings.TrimSpace(u.Query().Get("status"))),
 	}
 	if limit, err := strconv.ParseInt(util.Or(u.Query().Get("limit"), "20"), 10, 32); err == nil {
 		params.Limit = limit
@@ -95,11 +97,15 @@ func buildSearchQuery(params searchParams) (string, []interface{}) {
 	query := `select customer_id from customers where deleted_at is null`
 	if params.Query != "" {
 		query += " and lower(first_name) || \" \" || lower(last_name) LIKE ?"
-		args = append(args, "%"+strings.ToLower(params.Query)+"%")
+		args = append(args, "%"+params.Query+"%")
 	}
 	if params.Email != "" {
 		query += " and lower(email) like ?"
-		args = append(args, "%"+strings.ToLower(params.Email))
+		args = append(args, "%"+params.Email)
+	}
+	if params.Status != "" {
+		query += " and status like ?"
+		args = append(args, "%"+params.Status)
 	}
 	return query + " order by created_at asc limit ?;", append(args, fmt.Sprintf("%d", params.Limit))
 }


Test patch:
diff --git a/cmd/server/customer_search_test.go b/cmd/server/customer_search_test.go
index 9b57072..623fe6a 100644
--- a/cmd/server/customer_search_test.go
+++ b/cmd/server/customer_search_test.go
@@ -169,3 +169,33 @@ func TestGet100MostRecentlyCreatedCustomersWhenSpecifyingMoreThanAvailable(t *te
 	customers, _ := scope.GetCustomers("?limit=120")
 	scope.assert.Equal(100, len(customers))
 }
+
+func TestGetCustomersWithVerifiedStatus(t *testing.T) {
+	// Create two customers. 1 with Unknown STATUS and 1 with Verified
+	scope := Setup(t)
+	scope.CreateCustomers(2)
+	customers, _ := scope.GetCustomers("?limit=120")
+	scope.assert.Equal(2, len(customers))
+	for i := 0; i < len(customers); i++ {
+		if i % 2 == 0 {
+			// update status
+			if err := scope.customerRepo.updateCustomerStatus(customers[i].CustomerID, client.VERIFIED, "test comment"); err != nil {
+				print(err)
+			}
+		}
+	}
+
+	// Should have 1 Verified Status
+	customers, _ = scope.GetCustomers("?status=Verified&limit=20")
+	scope.assert.Equal(1, len(customers))
+	for i := 0; i < len(customers); i++ {
+		scope.assert.Equal("Verified", string(customers[i].Status))
+	}
+
+	// Should have 1 Unknown Status
+	customers, _ = scope.GetCustomers("?status=Unknown&limit=20")
+	scope.assert.Equal(1, len(customers))
+	for i := 0; i < len(customers); i++ {
+		scope.assert.Equal("Unknown", string(customers[i].Status))
+	}
+}
\ No newline at end of file