From 3fc2ab244cc33e1512b4eb757f29ed1ef60dc678 Mon Sep 17 00:00:00 2001
From: Stephanie Freitag <stephanie.freitag@holi.team>
Date: Thu, 27 Feb 2025 21:10:10 +0100
Subject: [PATCH] HOLI-9793, HOLI-10970: use default topics as fallback for
 recos V2

---
 app/volunteering_db.ts      | 36 ++++++++++++++++++++++++++++++++----
 app/volunteering_db_test.ts | 30 ++++++++++++++++++++++++++++++
 smoketest/main.js           |  2 +-
 3 files changed, 63 insertions(+), 5 deletions(-)

diff --git a/app/volunteering_db.ts b/app/volunteering_db.ts
index 1d3db4b..359bded 100644
--- a/app/volunteering_db.ts
+++ b/app/volunteering_db.ts
@@ -233,6 +233,15 @@ const MATCH_SKILL_CONFIDENCE_THRESHOLD = 0.8
 const MAX_LIMIT = 100
 const DEFAULT_LIMIT = 10
 
+const DEFAULT_RECO_TOPICS_V2 = [
+  'human-rights',
+  'animal-welfare',
+  'arts-culture',
+  'literacy-education',
+  'mental-physical-health',
+  'democracy-politics',
+]
+
 const queryEmbeddingVector = (topics: string[], skills: string[]): number[] => {
   const queryVector: number[] = Array(EMBEDDING_DIMENSIONS).fill(0.0)
   for (const topic of topics) {
@@ -532,16 +541,35 @@ export class PostgresVolunteeringDB implements VolunteeringDB {
         version,
       )
     } else if (geolocationCoordinates) {
-      return this.queryRecosBasedOnLocation(
+      if (version === 'v2') {
+        return this.queryRecosBasedOnTopicsSkillsAndLocation(
+          offset,
+          limit,
+          DEFAULT_RECO_TOPICS_V2,
+          skills,
+          geolocationCoordinates,
+          version,
+        )
+      } else {
+        return this.queryRecosBasedOnLocation(
+          offset,
+          limit,
+          geolocationCoordinates,
+        )
+      }
+    } else if (topics.length > 0 || skills.length > 0) {
+      return this.queryRecosBasedOnTopicsAndSkills(
         offset,
         limit,
-        geolocationCoordinates,
+        topics,
+        skills,
+        version,
       )
-    } else if (topics.length > 0 || skills.length > 0) {
+    } else if (version === 'v2') {
       return this.queryRecosBasedOnTopicsAndSkills(
         offset,
         limit,
-        topics,
+        DEFAULT_RECO_TOPICS_V2,
         skills,
         version,
       )
diff --git a/app/volunteering_db_test.ts b/app/volunteering_db_test.ts
index 1ac76b9..357c9d5 100644
--- a/app/volunteering_db_test.ts
+++ b/app/volunteering_db_test.ts
@@ -606,6 +606,36 @@ describe('VolunteeringDB', () => {
         },
       )
     })
+    it('falls back to default topics if only a geolocation is given in V2', () => {
+      withMockedDependencies([dbFixtures.row1])(
+        async (volunteeringDB, _geoAPIClient) => {
+          const result = await volunteeringDB.engagementRecos('v2', {
+            limit: 10,
+            offset: 0,
+            topics: [],
+            skills: [],
+            geolocationId: 'mock-geolocation-id',
+          })
+          assertEquals(result.totalResults, 1)
+          assertEquals(result.data.length, 1)
+        },
+      )
+    })
+    it('falls back to default topics if no topics, skills or geolocation is given in V2', () => {
+      withMockedDependencies([dbFixtures.row1])(
+        async (volunteeringDB, _geoAPIClient) => {
+          const result = await volunteeringDB.engagementRecos('v2', {
+            limit: 10,
+            offset: 0,
+            topics: [],
+            skills: [],
+            geolocationId: 'mock-geolocation-id',
+          })
+          assertEquals(result.totalResults, 1)
+          assertEquals(result.data.length, 1)
+        },
+      )
+    })
   })
 
   describe('filterEngagements', () => {
diff --git a/smoketest/main.js b/smoketest/main.js
index 99a6d49..1ecded4 100644
--- a/smoketest/main.js
+++ b/smoketest/main.js
@@ -29,7 +29,7 @@ function forQuery(query, checkFunction) {
 export default () => {
   forQuery(
     `{
-      engagementRecosV2(offset:0, limit:10, topics:["animal-welfare"]) {
+      engagementRecosV2(offset:0, limit:10) {
         data {
           engagement {
             id
-- 
GitLab