@@ -5,7 +5,7 @@ use serde_json::Value;
5
5
use surrealdb:: { engine:: any:: Any , sql:: Thing , Surreal } ;
6
6
7
7
use crate :: {
8
- persistence:: notification:: NotificationStoreApi ,
8
+ persistence:: notification:: { NotificationFilter , NotificationStoreApi } ,
9
9
service:: notification_service:: { ActionType , Notification , NotificationType } ,
10
10
util:: date:: { now, DateTimeUtc } ,
11
11
} ;
@@ -46,14 +46,28 @@ impl NotificationStoreApi for SurrealNotificationStore {
46
46
}
47
47
}
48
48
/// Returns all currently active notifications from the database
49
- async fn list ( & self ) -> Result < Vec < Notification > > {
50
- let result: Vec < NotificationDb > = self
49
+ async fn list ( & self , filter : NotificationFilter ) -> Result < Vec < Notification > > {
50
+ let filters = filter. filters ( ) ;
51
+ let mut query = self
51
52
. db
52
- . query ( "SELECT * FROM type::table($table) WHERE active = true ORDER BY datetime DESC" )
53
+ . query ( format ! (
54
+ "SELECT * FROM type::table($table) {} ORDER BY datetime DESC LIMIT $limit START $offset" ,
55
+ filters
56
+ ) )
53
57
. bind ( ( "table" , Self :: TABLE ) )
54
- . await ?
55
- . take ( 0 ) ? ;
58
+ . bind ( ( "limit" , filter . get_limit ( ) ) )
59
+ . bind ( ( "offset" , filter . get_offset ( ) ) ) ;
56
60
61
+ if let Some ( active) = filter. get_active ( ) {
62
+ query = query. bind ( active. to_owned ( ) ) ;
63
+ }
64
+ if let Some ( reference_id) = filter. get_reference_id ( ) {
65
+ query = query. bind ( reference_id. to_owned ( ) ) ;
66
+ }
67
+ if let Some ( notification_type) = filter. get_notification_type ( ) {
68
+ query = query. bind ( notification_type. to_owned ( ) ) ;
69
+ }
70
+ let result: Vec < NotificationDb > = query. await ?. take ( 0 ) ?;
57
71
Ok ( result. into_iter ( ) . map ( |n| n. into ( ) ) . collect ( ) )
58
72
}
59
73
/// Returns the latest active notification for the given reference and notification type
@@ -62,24 +76,27 @@ impl NotificationStoreApi for SurrealNotificationStore {
62
76
reference : & str ,
63
77
notification_type : NotificationType ,
64
78
) -> Result < Option < Notification > > {
65
- let result: Vec < NotificationDb > = self . db . query ( "SELECT * FROM type::table($table) WHERE active = true AND reference_id = $reference_id AND notification_type = $notification_type ORDER BY datetime desc" )
66
- . bind ( ( "table" , Self :: TABLE ) )
67
- . bind ( ( "reference_id" , reference. to_owned ( ) ) )
68
- . bind ( ( "notification_type" , notification_type) )
69
- . await ?
70
- . take ( 0 ) ?;
71
-
72
- Ok ( result. first ( ) . map ( |n| n. clone ( ) . into ( ) ) )
79
+ let result = self
80
+ . list ( NotificationFilter {
81
+ active : Some ( true ) ,
82
+ reference_id : Some ( reference. to_owned ( ) ) ,
83
+ notification_type : Some ( notification_type. to_string ( ) ) ,
84
+ limit : Some ( 1 ) ,
85
+ ..Default :: default ( )
86
+ } )
87
+ . await ?;
88
+ Ok ( result. first ( ) . cloned ( ) )
73
89
}
74
90
/// Returns all notifications for the given reference and notification type that are active
75
91
async fn list_by_type ( & self , notification_type : NotificationType ) -> Result < Vec < Notification > > {
76
- let result: Vec < NotificationDb > = self . db . query ( "SELECT * FROM type::table($table) WHERE active = true AND notification_type = $notification_type ORDER BY datetime desc" )
77
- . bind ( ( "table" , Self :: TABLE ) )
78
- . bind ( ( "notification_type" , notification_type) )
79
- . await ?
80
- . take ( 0 ) ?;
81
-
82
- Ok ( result. into_iter ( ) . map ( |n| n. into ( ) ) . collect ( ) )
92
+ let result = self
93
+ . list ( NotificationFilter {
94
+ active : Some ( true ) ,
95
+ notification_type : Some ( notification_type. to_string ( ) ) ,
96
+ ..Default :: default ( )
97
+ } )
98
+ . await ?;
99
+ Ok ( result)
83
100
}
84
101
/// Marks an active notification as done
85
102
async fn mark_as_done ( & self , notification_id : & str ) -> Result < ( ) > {
@@ -135,6 +152,7 @@ impl NotificationStoreApi for SurrealNotificationStore {
135
152
#[ derive( Debug , Clone , Serialize , Deserialize ) ]
136
153
struct NotificationDb {
137
154
pub id : Thing ,
155
+ pub node_id : Option < String > ,
138
156
pub notification_type : NotificationType ,
139
157
pub reference_id : Option < String > ,
140
158
pub description : String ,
@@ -147,6 +165,7 @@ impl From<NotificationDb> for Notification {
147
165
fn from ( value : NotificationDb ) -> Self {
148
166
Self {
149
167
id : value. id . id . to_raw ( ) ,
168
+ node_id : value. node_id ,
150
169
notification_type : value. notification_type ,
151
170
reference_id : value. reference_id ,
152
171
description : value. description ,
@@ -165,6 +184,7 @@ impl From<Notification> for NotificationDb {
165
184
value. id . to_owned ( ) ,
166
185
)
167
186
. into ( ) ,
187
+ node_id : value. node_id ,
168
188
notification_type : value. notification_type ,
169
189
reference_id : value. reference_id ,
170
190
description : value. description ,
@@ -243,36 +263,45 @@ mod tests {
243
263
}
244
264
245
265
#[ tokio:: test]
246
- async fn test_inserts_and_queries_notifiction ( ) {
266
+ async fn test_inserts_and_queries_notification ( ) {
247
267
let store = get_store ( ) . await ;
248
268
let notification = test_notification ( "bill_id" , Some ( test_payload ( ) ) ) ;
249
269
let r = store
250
270
. add ( notification. clone ( ) )
251
271
. await
252
272
. expect ( "could not create notification" ) ;
253
273
254
- let all = store. list ( ) . await . expect ( "could not list notifications" ) ;
274
+ let all = store
275
+ . list ( NotificationFilter :: default ( ) )
276
+ . await
277
+ . expect ( "could not list notifications" ) ;
255
278
assert ! ( !all. is_empty( ) ) ;
256
279
assert_eq ! ( notification. id, r. id) ;
257
280
}
258
281
259
282
#[ tokio:: test]
260
- async fn test_deletes_existing_notifiction ( ) {
283
+ async fn test_deletes_existing_notification ( ) {
261
284
let store = get_store ( ) . await ;
262
285
let notification = test_notification ( "bill_id" , Some ( test_payload ( ) ) ) ;
263
286
let r = store
264
287
. add ( notification. clone ( ) )
265
288
. await
266
289
. expect ( "could not create notification" ) ;
267
290
268
- let all = store. list ( ) . await . expect ( "could not list notifications" ) ;
291
+ let all = store
292
+ . list ( NotificationFilter :: default ( ) )
293
+ . await
294
+ . expect ( "could not list notifications" ) ;
269
295
assert ! ( !all. is_empty( ) ) ;
270
296
271
297
store
272
298
. delete ( & r. id )
273
299
. await
274
300
. expect ( "could not delete notification" ) ;
275
- let all = store. list ( ) . await . expect ( "could not list notifications" ) ;
301
+ let all = store
302
+ . list ( NotificationFilter :: default ( ) )
303
+ . await
304
+ . expect ( "could not list notifications" ) ;
276
305
assert ! ( all. is_empty( ) ) ;
277
306
}
278
307
@@ -285,15 +314,23 @@ mod tests {
285
314
. await
286
315
. expect ( "could not create notification" ) ;
287
316
288
- let all = store. list ( ) . await . expect ( "could not list notifications" ) ;
317
+ let mut filter = NotificationFilter :: default ( ) ;
318
+ filter. active = Some ( true ) ;
319
+ let all = store
320
+ . list ( filter. clone ( ) )
321
+ . await
322
+ . expect ( "could not list notifications" ) ;
289
323
assert ! ( !all. is_empty( ) ) ;
290
324
291
325
store
292
326
. mark_as_done ( & r. id )
293
327
. await
294
328
. expect ( "could not mark notification as done" ) ;
295
329
296
- let all = store. list ( ) . await . expect ( "could not list notifications" ) ;
330
+ let all = store
331
+ . list ( filter)
332
+ . await
333
+ . expect ( "could not list notifications" ) ;
297
334
assert ! ( all. is_empty( ) ) ;
298
335
}
299
336
@@ -369,7 +406,7 @@ mod tests {
369
406
}
370
407
371
408
fn test_notification ( bill_id : & str , payload : Option < Value > ) -> Notification {
372
- Notification :: new_bill_notification ( bill_id, "test_notification" , payload)
409
+ Notification :: new_bill_notification ( bill_id, "node_id" , " test_notification", payload)
373
410
}
374
411
375
412
fn test_payload ( ) -> Value {
@@ -379,6 +416,7 @@ mod tests {
379
416
fn test_general_notification ( ) -> Notification {
380
417
Notification {
381
418
id : Uuid :: new_v4 ( ) . to_string ( ) ,
419
+ node_id : Some ( "node_id" . to_string ( ) ) ,
382
420
notification_type : NotificationType :: General ,
383
421
reference_id : Some ( "general" . to_string ( ) ) ,
384
422
description : "general desc" . to_string ( ) ,
0 commit comments