nodeless_rs/
transaction.rs1use std::collections::HashMap;
3
4use serde::{Deserialize, Deserializer, Serialize, Serializer};
5
6use crate::error::NodelessError;
7use crate::serde_utils::{opt_serde_timestamp, serde_timestamp};
8use crate::Nodeless;
9
10#[derive(Clone, Debug, PartialEq, Eq)]
12pub enum TransactableType {
13 Donation,
14 Other(String),
15}
16
17impl<'de> Deserialize<'de> for TransactableType {
18 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
19 where
20 D: Deserializer<'de>,
21 {
22 let status_str = String::deserialize(deserializer)?;
23 match status_str.as_str() {
24 "Donation" => Ok(TransactableType::Donation),
25 _ => Ok(TransactableType::Other(status_str)),
26 }
27 }
28}
29
30impl Serialize for TransactableType {
31 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
32 where
33 S: Serializer,
34 {
35 match self {
36 TransactableType::Donation => serializer.serialize_str("Donation"),
37 TransactableType::Other(status_str) => serializer.serialize_str(status_str),
38 }
39 }
40}
41
42#[derive(Clone, Debug, PartialEq, Eq)]
44pub enum TransactionStatus {
45 Settled,
46 Other(String),
47}
48
49impl<'de> Deserialize<'de> for TransactionStatus {
50 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
51 where
52 D: Deserializer<'de>,
53 {
54 let status_str = String::deserialize(deserializer)?;
55 match status_str.as_str() {
56 "settled" => Ok(TransactionStatus::Settled),
57 _ => Ok(TransactionStatus::Other(status_str)),
58 }
59 }
60}
61
62impl Serialize for TransactionStatus {
63 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
64 where
65 S: Serializer,
66 {
67 match self {
68 TransactionStatus::Settled => serializer.serialize_str("settled"),
69 TransactionStatus::Other(status_str) => serializer.serialize_str(status_str),
70 }
71 }
72}
73
74#[derive(Clone, Debug, Deserialize, Serialize)]
76pub struct Transactable {
77 id: u64,
78 uuid: String,
79 donation_page_id: Option<u64>,
80 amount: u64,
81 amount_paid: u64,
82 name: Option<String>,
83 message: Option<String>,
84 status: String,
85 #[serde(rename = "type")]
86 type_: String,
87 metadata: Option<HashMap<String, String>>,
88 #[serde(with = "serde_timestamp")]
89 created_at: i64,
90 #[serde(with = "opt_serde_timestamp")]
91 updated_at: Option<i64>,
92 #[serde(with = "opt_serde_timestamp")]
93 paid_at: Option<i64>,
94}
95
96#[derive(Clone, Debug, Deserialize, Serialize)]
98pub struct Transaction {
99 pub id: String,
100 pub transactable_type: TransactableType,
101 pub transactable: Transactable,
102 pub amount: f64,
103 #[serde(rename = "type")]
104 pub type_: String,
105 pub status: TransactionStatus,
106 #[serde(with = "serde_timestamp")]
107 pub created_at: i64,
108 #[serde(with = "serde_timestamp")]
109 pub updated_at: i64,
110 pub is_fee: bool,
111}
112impl Nodeless {
113 pub async fn get_transaction(&self, id: &str) -> Result<Transaction, NodelessError> {
115 let url = self.base_url.join(&format!("api/v1/transaction/{}", id))?;
116 let res = self.make_get(url).await?;
117 Ok(serde_json::from_value(res["data"].to_owned())?)
118 }
119
120 pub async fn get_transactions(&self, is_fee: bool) -> Result<Vec<Transaction>, NodelessError> {
122 let url = match is_fee {
123 false => self.base_url.join("api/v1/transaction")?,
124 true => self.base_url.join("api/v1/transaction?isFee=1")?,
125 };
126 let res = self.make_get(url).await?;
127 Ok(serde_json::from_value(res["data"].to_owned())?)
128 }
129}