breez_sdk_spark/sdk/
contacts.rs1use 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 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 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 pub async fn delete_contact(&self, id: String) -> Result<(), SdkError> {
84 self.storage.get_contact(id.clone()).await?;
86
87 self.storage.delete_contact(id).await?;
88 Ok(())
89 }
90
91 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}