From 37a47bfe85fd920418bfa88ddd11773ee0d588d9 Mon Sep 17 00:00:00 2001 From: Michael-u21546551 Date: Mon, 30 Sep 2024 15:50:02 +0200 Subject: [PATCH] Refactor GetLocationsCount function in analytics.go to handle empty knownLocations array --- occupi-backend/pkg/analytics/analytics.go | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/occupi-backend/pkg/analytics/analytics.go b/occupi-backend/pkg/analytics/analytics.go index 961d29c7..2315538c 100644 --- a/occupi-backend/pkg/analytics/analytics.go +++ b/occupi-backend/pkg/analytics/analytics.go @@ -1173,7 +1173,26 @@ func GetLocationsCount(email string) bson.A { // Step 1: Match users by email 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"}}}}, + bson.D{ + {Key: "$project", + Value: bson.D{ + {Key: "locationCount", + Value: bson.D{ + {Key: "$size", + Value: bson.D{ + {Key: "$ifNull", + Value: bson.A{ + "$knownLocations", + bson.A{}, + }, + }, + }, + }, + }, + }, + }, + }, + }, // Step 3: Group to sum up the total size of all knownLocations arrays bson.D{{Key: "$group", Value: bson.M{ "_id": nil, // We don't care about grouping by email here