Skip to content

Commit

Permalink
remove date-of-brith from name search with non communities
Browse files Browse the repository at this point in the history
  • Loading branch information
Linesmerrill committed Oct 8, 2023
1 parent fe1051d commit 174f202
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 31 deletions.
37 changes: 10 additions & 27 deletions api/handlers/civilian.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ func (c Civilian) CiviliansByUserIDHandler(w http.ResponseWriter, r *http.Reques
func (c Civilian) CiviliansByNameSearchHandler(w http.ResponseWriter, r *http.Request) {
firstName := r.URL.Query().Get("first_name")
lastName := r.URL.Query().Get("last_name")
dateOfBirth := r.URL.Query().Get("date_of_birth") // optional
activeCommunityID := r.URL.Query().Get("active_community_id") // optional
Limit, err := strconv.Atoi(r.URL.Query().Get("limit"))
if err != nil {
Expand All @@ -158,7 +157,7 @@ func (c Civilian) CiviliansByNameSearchHandler(w http.ResponseWriter, r *http.Re
Page = getPage(Page, r)
skip64 := int64(Page * Limit)

zap.S().Debugf("first_name: '%v', last_name: '%v', date_of_birth: '%v'", firstName, lastName, dateOfBirth)
zap.S().Debugf("first_name: '%v', last_name: '%v'", firstName, lastName)
zap.S().Debugf("active_community: '%v'", activeCommunityID)

var dbResp []models.Civilian
Expand All @@ -170,31 +169,15 @@ func (c Civilian) CiviliansByNameSearchHandler(w http.ResponseWriter, r *http.Re
// Likewise, if the user is not in a community, then we will display only the civilians
// that are not in a community
err = nil
if activeCommunityID != "" && activeCommunityID != "null" && activeCommunityID != "undefined" {
dbResp, err = c.DB.Find(context.TODO(), bson.M{
"$text": bson.M{
"$search": fmt.Sprintf("%s %s", firstName, lastName),
},
"civilian.activeCommunityID": activeCommunityID,
}, &options.FindOptions{Limit: &limit64, Skip: &skip64})
if err != nil {
config.ErrorStatus("failed to get civilian name search with active community id", http.StatusNotFound, w, err)
return
}
} else {
dbResp, err = c.DB.Find(context.TODO(), bson.M{
"civilian.firstName": firstName,
"civilian.lastName": lastName,
"civilian.birthday": dateOfBirth,
"$or": []bson.M{
{"civilian.activeCommunityID": nil},
{"civilian.activeCommunityID": ""},
},
}, &options.FindOptions{Limit: &limit64, Skip: &skip64})
if err != nil {
config.ErrorStatus("failed to get civilian name search with empty active community id", http.StatusNotFound, w, err)
return
}
dbResp, err = c.DB.Find(context.TODO(), bson.M{
"$text": bson.M{
"$search": fmt.Sprintf("%s %s", firstName, lastName),
},
"civilian.activeCommunityID": activeCommunityID,
}, &options.FindOptions{Limit: &limit64, Skip: &skip64})
if err != nil {
config.ErrorStatus("failed to get civilian name search", http.StatusNotFound, w, err)
return
}

// Because the frontend requires that the data elements inside models.Civilians exist, if
Expand Down
8 changes: 4 additions & 4 deletions api/handlers/civilian_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -968,7 +968,7 @@ func TestCivilian_CiviliansByNameSearchHandlerFailedToFindOne(t *testing.T) {
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusNotFound)
}

expected := models.ErrorMessageResponse{Response: models.MessageError{Message: "failed to get civilian name search with active community id", Error: "mongo: no documents in result"}}
expected := models.ErrorMessageResponse{Response: models.MessageError{Message: "failed to get civilian name search", Error: "mongo: no documents in result"}}
b, _ := json.Marshal(expected)
if rr.Body.String() != string(b) {
t.Errorf("handler returned unexpected body: got %v want %v", rr.Body.String(), expected)
Expand Down Expand Up @@ -1013,7 +1013,7 @@ func TestCivilian_CiviliansByNameSearchHandlerActiveCommunityIDFailedToFindOne(t
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusNotFound)
}

expected := models.ErrorMessageResponse{Response: models.MessageError{Message: "failed to get civilian name search with active community id", Error: "mongo: no documents in result"}}
expected := models.ErrorMessageResponse{Response: models.MessageError{Message: "failed to get civilian name search", Error: "mongo: no documents in result"}}
b, _ := json.Marshal(expected)
if rr.Body.String() != string(b) {
t.Errorf("handler returned unexpected body: got %v want %v", rr.Body.String(), expected)
Expand Down Expand Up @@ -1165,7 +1165,7 @@ func TestCivilian_CiviliansByNameSearchHandlerSuccessWithNullCommunityID(t *test
}

func TestCivilian_CiviliansByNameSearchHandlerFailedToFindOneWithEmptyCommunityID(t *testing.T) {
req, err := http.NewRequest("GET", "/api/v1/civilians/search?active_community_id=null&first_name=alessandro&last_name=mills&date_of_birth=1987-03-20", nil)
req, err := http.NewRequest("GET", "/api/v1/civilians/search?active_community_id=null&first_name=alessandro&last_name=mills", nil)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -1202,7 +1202,7 @@ func TestCivilian_CiviliansByNameSearchHandlerFailedToFindOneWithEmptyCommunityI
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusNotFound)
}

expected := models.ErrorMessageResponse{Response: models.MessageError{Message: "failed to get civilian name search with empty active community id", Error: "mongo: no documents in result"}}
expected := models.ErrorMessageResponse{Response: models.MessageError{Message: "failed to get civilian name search", Error: "mongo: no documents in result"}}
b, _ := json.Marshal(expected)
if rr.Body.String() != string(b) {
t.Errorf("handler returned unexpected body: got %v want %v", rr.Body.String(), expected)
Expand Down

0 comments on commit 174f202

Please sign in to comment.