Skip to content

Commit

Permalink
Refactor GetLocationsCount function in analytics.go
Browse files Browse the repository at this point in the history
  • Loading branch information
waveyboym committed Sep 30, 2024
1 parent 582cc12 commit 3f8780c
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions occupi-backend/pkg/analytics/analytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -1161,14 +1161,17 @@ func GetUsersLocationsPipeLine(limit int64, skip int64, order string, email stri
}

func GetLocationsCount(email string) bson.A {
var mongoFilter bson.M
// Create a match filter
matchFilter := bson.D{}

// Conditionally add the email filter if email is not empty
if email != "" {
mongoFilter = bson.M{"email": email}
matchFilter = append(matchFilter, bson.E{Key: "email", Value: bson.D{{Key: "$eq", Value: email}}})
}

return bson.A{
// Step 1: Match users by email
bson.D{{Key: "$match", Value: mongoFilter}},
bson.D{{Key: "$match", Value: matchFilter}},
// Step 2: Project only the size of the knownLocations array
bson.D{{Key: "$project", Value: bson.M{"locationCount": bson.M{"$size": "$knownLocations"}}}},
// Step 3: Group to sum up the total size of all knownLocations arrays
Expand Down

0 comments on commit 3f8780c

Please sign in to comment.