From 3f8780c1c398415008d371f1d3711050b19d03f9 Mon Sep 17 00:00:00 2001 From: Michael-u21546551 Date: Mon, 30 Sep 2024 15:20:45 +0200 Subject: [PATCH] Refactor GetLocationsCount function in analytics.go --- occupi-backend/pkg/analytics/analytics.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/occupi-backend/pkg/analytics/analytics.go b/occupi-backend/pkg/analytics/analytics.go index d5781f7a..961d29c7 100644 --- a/occupi-backend/pkg/analytics/analytics.go +++ b/occupi-backend/pkg/analytics/analytics.go @@ -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