diff options
author | Santo Cariotti <santo@dcariotti.me> | 2024-09-03 21:06:20 +0200 |
---|---|---|
committer | Santo Cariotti <santo@dcariotti.me> | 2024-09-03 21:06:20 +0200 |
commit | a0e7fe51936351ee9b8ae66c18a6dc32e64078ed (patch) | |
tree | 77fc134227ec41f88e51736b1107b76ead4759b6 /src/graphql | |
parent | 4a85154f72e89f5114b9b89fa82914972518bde8 (diff) |
Notification must be filtered by seen field
Diffstat (limited to 'src/graphql')
-rw-r--r-- | src/graphql/query.rs | 5 | ||||
-rw-r--r-- | src/graphql/types/notification.rs | 25 |
2 files changed, 17 insertions, 13 deletions
diff --git a/src/graphql/query.rs b/src/graphql/query.rs index 2e2466f..58685ed 100644 --- a/src/graphql/query.rs +++ b/src/graphql/query.rs @@ -111,17 +111,18 @@ impl Query { /// curl http://localhost:8000/graphql /// -H 'authorization: Bearer ***' /// -H 'content-type: application/json' - /// -d '{"query":"{notifications(alertId: 1) { + /// -d '{"query":"{notifications(seen: false alertId: 1) { /// id, alert { id, userId, createdAt, area, extendedArea, level, reachedUsers }, position {id, userId, createdAt, latitude, longitude, movingActivity}, seen, createdAt /// }}"}' /// ``` async fn notifications<'ctx>( &self, ctx: &Context<'ctx>, + #[graphql(desc = "Show only seen or not notifications")] seen: bool, #[graphql(desc = "Filter by alert ID")] alert_id: Option<i32>, #[graphql(desc = "Limit results")] limit: Option<i64>, #[graphql(desc = "Offset results")] offset: Option<i64>, ) -> Result<Option<Vec<notification::Notification>>, String> { - notification::get_notifications(ctx, alert_id, limit, offset).await + notification::get_notifications(ctx, seen, alert_id, limit, offset).await } } diff --git a/src/graphql/types/notification.rs b/src/graphql/types/notification.rs index c73fe66..67fb42f 100644 --- a/src/graphql/types/notification.rs +++ b/src/graphql/types/notification.rs @@ -18,7 +18,10 @@ pub struct Notification { pub async fn get_notifications<'ctx>( ctx: &Context<'ctx>, - // Optional filter by id. + // Filter for `seen` field + seen: bool, + + // Optional filter by alert id alert_id: Option<i32>, // Optional limit results @@ -40,7 +43,7 @@ pub async fn get_notifications<'ctx>( let limit = limit.unwrap_or(20); let offset = offset.unwrap_or(0); - let mut base_query = "SELECT n.id, + let base_query = "SELECT n.id, n.alert_id, n.position_id, n.seen, @@ -73,31 +76,31 @@ pub async fn get_notifications<'ctx>( JOIN positions p ON n.position_id = p.id".to_string(); let rows = match alert_id { - Some(id) if claim_user.is_admin => + Some(id) if claim_user.is_admin => client .query(&format!( - "{base_query} WHERE n.alert_id = $1 ORDER BY n.id DESC LIMIT $2 OFFSET $3", - ), &[&id, &limit, &offset]) + "{base_query} WHERE seen = $1 AND n.alert_id = $2 ORDER BY n.id DESC LIMIT $3 OFFSET $4", + ), &[&seen, &id, &limit, &offset]) .await .unwrap(), Some (id) => client .query(&format!( - "{base_query} WHERE p.user_id = $1 AND n.alert_id = $2 ORDER BY n.id DESC LIMIT $3 OFFSET $4", - ), &[&claim_user.id, &id, &limit, &offset]) + "{base_query} WHERE seen = $1 AND p.user_id = $2 AND n.alert_id = $3 ORDER BY n.id DESC LIMIT $4 OFFSET $5", + ), &[&seen, &claim_user.id, &id, &limit, &offset]) .await .unwrap(), None if claim_user.is_admin => client .query( - &format!("{base_query} ORDER BY n.id DESC LIMIT $1 OFFSET $2"), - &[&limit, &offset], + &format!("{base_query} WHERE seen = $1 ORDER BY n.id DESC LIMIT $2 OFFSET $3"), + &[&seen, &limit, &offset], ) .await .unwrap(), None => client.query( - &format!("{base_query} WHERE p.user_id = $1 ORDER BY n.id DESC LIMIT $2 OFFSET $3"), - &[&claim_user.id, &limit, &offset], + &format!("{base_query} WHERE seen = $1 AND p.user_id = $2 ORDER BY n.id DESC LIMIT $3 OFFSET $4"), + &[&seen, &claim_user.id, &limit, &offset], ) .await .unwrap(), |