|
| 1 | +use super::super::Result; |
| 2 | +use async_trait::async_trait; |
| 3 | +use bcr_ebill_core::{NodeId, ServiceTraitBounds}; |
| 4 | +use serde::{Deserialize, Serialize}; |
| 5 | + |
| 6 | +use crate::{db::surreal::SurrealWrapper, notification::EmailNotificationStoreApi}; |
| 7 | + |
| 8 | +#[derive(Clone)] |
| 9 | +pub struct SurrealEmailNotificationStore { |
| 10 | + db: SurrealWrapper, |
| 11 | +} |
| 12 | + |
| 13 | +impl SurrealEmailNotificationStore { |
| 14 | + const TABLE: &'static str = "email_notifications"; |
| 15 | + |
| 16 | + pub fn new(db: SurrealWrapper) -> Self { |
| 17 | + Self { db } |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +impl ServiceTraitBounds for SurrealEmailNotificationStore {} |
| 22 | + |
| 23 | +#[cfg_attr(target_arch = "wasm32", async_trait(?Send))] |
| 24 | +#[cfg_attr(not(target_arch = "wasm32"), async_trait)] |
| 25 | +impl EmailNotificationStoreApi for SurrealEmailNotificationStore { |
| 26 | + async fn add_email_preferences_link_for_node_id( |
| 27 | + &self, |
| 28 | + email_preferences_link: &url::Url, |
| 29 | + node_id: &NodeId, |
| 30 | + ) -> Result<()> { |
| 31 | + let db = EmailNotificationPreferencesDb { |
| 32 | + node_id: node_id.to_owned(), |
| 33 | + email_preferences_link: email_preferences_link.to_string(), |
| 34 | + }; |
| 35 | + let _: Option<EmailNotificationPreferencesDb> = self |
| 36 | + .db |
| 37 | + .create(Self::TABLE, Some(node_id.to_string()), db) |
| 38 | + .await?; |
| 39 | + Ok(()) |
| 40 | + } |
| 41 | + |
| 42 | + async fn get_email_preferences_link_for_node_id( |
| 43 | + &self, |
| 44 | + node_id: &NodeId, |
| 45 | + ) -> Result<Option<url::Url>> { |
| 46 | + let result: Option<EmailNotificationPreferencesDb> = |
| 47 | + self.db.select_one(Self::TABLE, node_id.to_string()).await?; |
| 48 | + |
| 49 | + Ok(match result { |
| 50 | + Some(r) => url::Url::parse(&r.email_preferences_link).ok(), |
| 51 | + None => None, |
| 52 | + }) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 57 | +struct EmailNotificationPreferencesDb { |
| 58 | + pub node_id: NodeId, |
| 59 | + pub email_preferences_link: String, |
| 60 | +} |
| 61 | + |
| 62 | +#[cfg(test)] |
| 63 | +mod tests { |
| 64 | + use crate::{db::get_memory_db, tests::tests::node_id_test}; |
| 65 | + |
| 66 | + use super::*; |
| 67 | + |
| 68 | + async fn get_store() -> SurrealEmailNotificationStore { |
| 69 | + let db = get_memory_db("test", "email_notification") |
| 70 | + .await |
| 71 | + .expect("could not create memory db"); |
| 72 | + SurrealEmailNotificationStore::new(SurrealWrapper { db, files: false }) |
| 73 | + } |
| 74 | + |
| 75 | + #[tokio::test] |
| 76 | + async fn test_email_preferences_link_for_node_id() { |
| 77 | + let store = get_store().await; |
| 78 | + let link_before = store |
| 79 | + .get_email_preferences_link_for_node_id(&node_id_test()) |
| 80 | + .await |
| 81 | + .expect("can fetch empty link if it's not set"); |
| 82 | + assert!(link_before.is_none()); |
| 83 | + |
| 84 | + store |
| 85 | + .add_email_preferences_link_for_node_id( |
| 86 | + &url::Url::parse("https://www.bit.cr/").unwrap(), |
| 87 | + &node_id_test(), |
| 88 | + ) |
| 89 | + .await |
| 90 | + .unwrap(); |
| 91 | + |
| 92 | + let link = store |
| 93 | + .get_email_preferences_link_for_node_id(&node_id_test()) |
| 94 | + .await |
| 95 | + .expect("can fetch link after it was set"); |
| 96 | + assert_eq!(link, Some(url::Url::parse("https://www.bit.cr/").unwrap())); |
| 97 | + } |
| 98 | +} |
0 commit comments