Skip to main content

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