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#[cfg_attr(feature = "uniffi", uniffi::export(async_runtime = "tokio"))]
9#[allow(clippy::needless_pass_by_value)]
10impl BreezSdk {
11 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 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 pub async fn delete_contact(&self, id: String) -> Result<(), SdkError> {
83 self.storage.get_contact(id.clone()).await?;
85
86 self.storage.delete_contact(id).await?;
87 Ok(())
88 }
89
90 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}