1use std::str::FromStr;
4
5use async_trait::async_trait;
6use cdk_common::database::mint::{CompletedOperationsDatabase, CompletedOperationsTransaction};
7use cdk_common::database::Error;
8use cdk_common::util::unix_time;
9use cdk_common::{mint, Amount, PaymentMethod};
10
11use super::{SQLMintDatabase, SQLTransaction};
12use crate::pool::DatabasePool;
13use crate::stmt::{query, Column};
14use crate::{column_as_nullable_string, column_as_number, column_as_string, unpack_into};
15
16fn sql_row_to_completed_operation(row: Vec<Column>) -> Result<mint::Operation, Error> {
17 unpack_into!(
18 let (
19 operation_id,
20 operation_kind,
21 completed_at,
22 total_issued,
23 total_redeemed,
24 fee_collected,
25 payment_method
26 ) = row
27 );
28
29 let operation_id_str = column_as_string!(&operation_id);
30 let operation_id = uuid::Uuid::parse_str(&operation_id_str)
31 .map_err(|e| Error::Internal(format!("Invalid operation_id UUID: {e}")))?;
32
33 let operation_kind_str = column_as_string!(&operation_kind);
34 let operation_kind = mint::OperationKind::from_str(&operation_kind_str)
35 .map_err(|e| Error::Internal(format!("Invalid operation kind: {e}")))?;
36
37 let completed_at: u64 = column_as_number!(completed_at);
38 let total_issued_u64: u64 = column_as_number!(total_issued);
39 let total_redeemed_u64: u64 = column_as_number!(total_redeemed);
40 let fee_collected_u64: u64 = column_as_number!(fee_collected);
41
42 let total_issued = Amount::from(total_issued_u64);
43 let total_redeemed = Amount::from(total_redeemed_u64);
44 let fee_collected = Amount::from(fee_collected_u64);
45
46 let payment_method = column_as_nullable_string!(payment_method)
47 .map(|s| PaymentMethod::from_str(&s))
48 .transpose()
49 .map_err(|e| Error::Internal(format!("Invalid payment method: {e}")))?;
50
51 Ok(mint::Operation::new(
52 operation_id,
53 operation_kind,
54 total_issued,
55 total_redeemed,
56 fee_collected,
57 Some(completed_at),
58 payment_method,
59 ))
60}
61
62#[async_trait]
63impl<RM> CompletedOperationsTransaction for SQLTransaction<RM>
64where
65 RM: DatabasePool + 'static,
66{
67 type Err = Error;
68
69 async fn add_completed_operation(
70 &mut self,
71 operation: &mint::Operation,
72 fee_by_keyset: &std::collections::HashMap<cdk_common::nuts::Id, cdk_common::Amount>,
73 ) -> Result<(), Self::Err> {
74 query(
75 r#"
76 INSERT INTO completed_operations
77 (operation_id, operation_kind, completed_at, total_issued, total_redeemed, fee_collected, payment_amount, payment_fee, payment_method)
78 VALUES
79 (:operation_id, :operation_kind, :completed_at, :total_issued, :total_redeemed, :fee_collected, :payment_amount, :payment_fee, :payment_method)
80 "#,
81 )?
82 .bind("operation_id", operation.id().to_string())
83 .bind("operation_kind", operation.kind().to_string())
84 .bind("completed_at", operation.completed_at().unwrap_or(unix_time()) as i64)
85 .bind("total_issued", operation.total_issued().to_u64() as i64)
86 .bind("total_redeemed", operation.total_redeemed().to_u64() as i64)
87 .bind("fee_collected", operation.fee_collected().to_u64() as i64)
88 .bind("payment_amount", operation.payment_amount().map(|a| a.to_u64() as i64))
89 .bind("payment_fee", operation.payment_fee().map(|a| a.to_u64() as i64))
90 .bind("payment_method", operation.payment_method().map(|m| m.to_string()))
91 .execute(&self.inner)
92 .await?;
93
94 for (keyset_id, fee) in fee_by_keyset {
96 if fee.to_u64() > 0 {
97 query(
98 r#"
99 INSERT INTO keyset_amounts (keyset_id, total_issued, total_redeemed, fee_collected)
100 VALUES (:keyset_id, 0, 0, :fee)
101 ON CONFLICT (keyset_id)
102 DO UPDATE SET fee_collected = keyset_amounts.fee_collected + EXCLUDED.fee_collected
103 "#,
104 )?
105 .bind("keyset_id", keyset_id.to_string())
106 .bind("fee", fee.to_u64() as i64)
107 .execute(&self.inner)
108 .await?;
109 }
110 }
111
112 Ok(())
113 }
114}
115
116#[async_trait]
117impl<RM> CompletedOperationsDatabase for SQLMintDatabase<RM>
118where
119 RM: DatabasePool + 'static,
120{
121 type Err = Error;
122
123 async fn get_completed_operation(
124 &self,
125 operation_id: &uuid::Uuid,
126 ) -> Result<Option<mint::Operation>, Self::Err> {
127 let conn = self.pool.get().map_err(|e| Error::Database(Box::new(e)))?;
128 Ok(query(
129 r#"
130 SELECT
131 operation_id,
132 operation_kind,
133 completed_at,
134 total_issued,
135 total_redeemed,
136 fee_collected,
137 payment_method
138 FROM
139 completed_operations
140 WHERE
141 operation_id = :operation_id
142 "#,
143 )?
144 .bind("operation_id", operation_id.to_string())
145 .fetch_one(&*conn)
146 .await?
147 .map(sql_row_to_completed_operation)
148 .transpose()?)
149 }
150
151 async fn get_completed_operations_by_kind(
152 &self,
153 operation_kind: mint::OperationKind,
154 ) -> Result<Vec<mint::Operation>, Self::Err> {
155 let conn = self.pool.get().map_err(|e| Error::Database(Box::new(e)))?;
156 Ok(query(
157 r#"
158 SELECT
159 operation_id,
160 operation_kind,
161 completed_at,
162 total_issued,
163 total_redeemed,
164 fee_collected,
165 payment_method
166 FROM
167 completed_operations
168 WHERE
169 operation_kind = :operation_kind
170 ORDER BY completed_at DESC
171 "#,
172 )?
173 .bind("operation_kind", operation_kind.to_string())
174 .fetch_all(&*conn)
175 .await?
176 .into_iter()
177 .map(sql_row_to_completed_operation)
178 .collect::<Result<Vec<_>, _>>()?)
179 }
180
181 async fn get_completed_operations(&self) -> Result<Vec<mint::Operation>, Self::Err> {
182 let conn = self.pool.get().map_err(|e| Error::Database(Box::new(e)))?;
183 Ok(query(
184 r#"
185 SELECT
186 operation_id,
187 operation_kind,
188 completed_at,
189 total_issued,
190 total_redeemed,
191 fee_collected,
192 payment_method
193 FROM
194 completed_operations
195 ORDER BY completed_at DESC
196 "#,
197 )?
198 .fetch_all(&*conn)
199 .await?
200 .into_iter()
201 .map(sql_row_to_completed_operation)
202 .collect::<Result<Vec<_>, _>>()?)
203 }
204}