breez_sdk_spark/sdk/
contacts.rs

1use crate::{
2    AddContactRequest, Contact, ListContactsRequest, UpdateContactRequest, error::SdkError,
3    utils::contacts_validation::validate_contact_input,
4};
5
6use super::BreezSdk;
7
8#[cfg_attr(feature = "uniffi", uniffi::export(async_runtime = "tokio"))]
9#[allow(clippy::needless_pass_by_value)]
10impl BreezSdk {
11    /// Adds a new contact.
12    ///
13    /// # Arguments
14    ///
15    /// * `request` - The request containing the contact details
16    ///
17    /// # Returns
18    ///
19    /// The created contact or an error
20    pub async fn add_contact(&self, request: AddContactRequest) -> Result<Contact, SdkError> {
21        let name = validate_contact_input(&request.name, &request.payment_identifier)?;
22        let payment_identifier = request.payment_identifier.trim().to_string();
23
24        let now = web_time::SystemTime::now()
25            .duration_since(web_time::UNIX_EPOCH)
26            .map(|d| d.as_secs())
27            .map_err(|_| SdkError::Generic("Failed to get current time".to_string()))?;
28
29        let contact = Contact {
30            id: uuid::Uuid::now_v7().to_string(),
31            name,
32            payment_identifier,
33            created_at: now,
34            updated_at: now,
35        };
36
37        self.storage.insert_contact(contact.clone()).await?;
38        Ok(contact)
39    }
40
41    /// Updates an existing contact.
42    ///
43    /// # Arguments
44    ///
45    /// * `request` - The request containing the updated contact details
46    ///
47    /// # Returns
48    ///
49    /// The updated contact or an error
50    pub async fn update_contact(&self, request: UpdateContactRequest) -> Result<Contact, SdkError> {
51        let name = validate_contact_input(&request.name, &request.payment_identifier)?;
52        let payment_identifier = request.payment_identifier.trim().to_string();
53
54        let existing = self.storage.get_contact(request.id.clone()).await?;
55
56        let now = web_time::SystemTime::now()
57            .duration_since(web_time::UNIX_EPOCH)
58            .map(|d| d.as_secs())
59            .map_err(|_| SdkError::Generic("Failed to get current time".to_string()))?;
60
61        let contact = Contact {
62            id: request.id,
63            name,
64            payment_identifier,
65            created_at: existing.created_at,
66            updated_at: now,
67        };
68
69        self.storage.insert_contact(contact.clone()).await?;
70        Ok(contact)
71    }
72
73    /// Deletes a contact by its ID.
74    ///
75    /// # Arguments
76    ///
77    /// * `id` - The ID of the contact to delete
78    ///
79    /// # Returns
80    ///
81    /// Success or an error
82    pub async fn delete_contact(&self, id: String) -> Result<(), SdkError> {
83        // Validate contact exists
84        self.storage.get_contact(id.clone()).await?;
85
86        self.storage.delete_contact(id).await?;
87        Ok(())
88    }
89
90    /// Lists contacts with optional pagination.
91    ///
92    /// # Arguments
93    ///
94    /// * `request` - The request containing optional pagination parameters
95    ///
96    /// # Returns
97    ///
98    /// A list of contacts or an error
99    pub async fn list_contacts(
100        &self,
101        request: ListContactsRequest,
102    ) -> Result<Vec<Contact>, SdkError> {
103        let contacts = self.storage.list_contacts(request).await?;
104        Ok(contacts)
105    }
106}