@@ -331,6 +331,7 @@ impl DBM {
331331 pub ( crate ) fn load_appointments (
332332 & self ,
333333 locator : Option < Locator > ,
334+ user_id : Option < UserId > ,
334335 ) -> HashMap < UUID , ExtendedAppointment > {
335336 let mut appointments = HashMap :: new ( ) ;
336337
@@ -339,12 +340,20 @@ impl DBM {
339340 FROM appointments as a LEFT JOIN trackers as t ON a.UUID=t.UUID WHERE t.UUID IS NULL" . to_string ( ) ;
340341 // If a locator was passed, filter based on it.
341342 if locator. is_some ( ) {
342- sql. push_str ( " AND a.locator=(?)" ) ;
343+ sql. push_str ( " AND a.locator=(?1)" ) ;
344+ if user_id. is_some ( ) {
345+ sql. push_str ( " AND a.user_id=(?2)" ) ;
346+ }
343347 }
344348 let mut stmt = self . connection . prepare ( & sql) . unwrap ( ) ;
345349
346350 let mut rows = if let Some ( locator) = locator {
347- stmt. query ( [ locator. to_vec ( ) ] ) . unwrap ( )
351+ if let Some ( user_id) = user_id {
352+ stmt. query ( [ locator. to_vec ( ) , user_id. to_vec ( ) ] ) . unwrap ( )
353+ } else {
354+ stmt. query ( [ locator. to_vec ( ) ] ) . unwrap ( )
355+
356+ }
348357 } else {
349358 stmt. query ( [ ] ) . unwrap ( )
350359 } ;
@@ -1106,7 +1115,7 @@ mod tests {
11061115 appointments. insert ( uuid, appointment) ;
11071116 }
11081117
1109- assert_eq ! ( dbm. load_appointments( None ) , appointments) ;
1118+ assert_eq ! ( dbm. load_appointments( None , None ) , appointments) ;
11101119
11111120 // If an appointment has an associated tracker, it should not be loaded since it is seen
11121121 // as a triggered appointment
@@ -1122,7 +1131,7 @@ mod tests {
11221131 dbm. store_tracker ( uuid, & tracker) . unwrap ( ) ;
11231132
11241133 // We should get all the appointments back except from the triggered one
1125- assert_eq ! ( dbm. load_appointments( None ) , appointments) ;
1134+ assert_eq ! ( dbm. load_appointments( None , None ) , appointments) ;
11261135 }
11271136
11281137 #[ test]
@@ -1157,7 +1166,7 @@ mod tests {
11571166 }
11581167
11591168 // Validate that no other appointments than the ones with our locator are returned.
1160- assert_eq ! ( dbm. load_appointments( Some ( locator) ) , appointments) ;
1169+ assert_eq ! ( dbm. load_appointments( Some ( locator) , None ) , appointments) ;
11611170
11621171 // If an appointment has an associated tracker, it should not be loaded since it is seen
11631172 // as a triggered appointment
@@ -1175,7 +1184,49 @@ mod tests {
11751184 dbm. store_tracker ( uuid, & tracker) . unwrap ( ) ;
11761185
11771186 // We should get all the appointments matching our locator back except from the triggered one
1178- assert_eq ! ( dbm. load_appointments( Some ( locator) ) , appointments) ;
1187+ assert_eq ! ( dbm. load_appointments( Some ( locator) , None ) , appointments) ;
1188+ }
1189+
1190+ #[ test]
1191+ fn test_load_appointments_with_locator_and_user_id ( ) {
1192+ let dbm = DBM :: in_memory ( ) . unwrap ( ) ;
1193+
1194+ // create two appointment maps for two userId
1195+ let mut user_id1_appointments = HashMap :: new ( ) ;
1196+ let mut user_id2_appointments = HashMap :: new ( ) ;
1197+
1198+ let dispute_tx = get_random_tx ( ) ;
1199+ let dispute_txid = dispute_tx. txid ( ) ;
1200+ let locator = Locator :: new ( dispute_txid) ;
1201+
1202+ // generate two user ids
1203+ let user_id1 = get_random_user_id ( ) ;
1204+ let user_id2 = get_random_user_id ( ) ;
1205+
1206+ let user = UserInfo :: new (
1207+ AVAILABLE_SLOTS ,
1208+ SUBSCRIPTION_START ,
1209+ SUBSCRIPTION_EXPIRY ,
1210+ ) ;
1211+ dbm. store_user ( user_id1, & user) . unwrap ( ) ;
1212+ dbm. store_user ( user_id2, & user) . unwrap ( ) ;
1213+
1214+ let ( uuid, appointment) = generate_dummy_appointment_with_user ( user_id1, Some ( & dispute_txid) ) ;
1215+ dbm. store_appointment ( uuid, & appointment) . unwrap ( ) ;
1216+ // Store the appointment for the first user_id made using our dispute tx.
1217+ user_id1_appointments. insert ( uuid, appointment) ;
1218+
1219+ let ( uuid, appointment) = generate_dummy_appointment_with_user ( user_id2, Some ( & dispute_txid) ) ;
1220+ dbm. store_appointment ( uuid, & appointment) . unwrap ( ) ;
1221+ // Store the appointment for the second user_id made using our dispute tx.
1222+ user_id2_appointments. insert ( uuid, appointment) ;
1223+
1224+ // Validate that the first user_id appointment map matches the fetched appointments.
1225+ assert_eq ! ( dbm. load_appointments( Some ( locator) , Some ( user_id1) ) , user_id1_appointments) ;
1226+
1227+ // Validate that the second user_id appointment map matches the fetched appointments.
1228+ assert_eq ! ( dbm. load_appointments( Some ( locator) , Some ( user_id2) ) , user_id2_appointments) ;
1229+
11791230 }
11801231
11811232 #[ test]
@@ -1259,7 +1310,7 @@ mod tests {
12591310 i as usize
12601311 ) ;
12611312 // Check appointment data was deleted and users properly updated
1262- assert_eq ! ( rest, dbm. load_appointments( None ) . keys( ) . cloned( ) . collect( ) ) ;
1313+ assert_eq ! ( rest, dbm. load_appointments( None , None ) . keys( ) . cloned( ) . collect( ) ) ;
12631314 assert_eq ! (
12641315 dbm. load_user( user_id) . unwrap( ) . available_slots,
12651316 user. available_slots
0 commit comments