Skip to main content

cdk_sql_common/mint/
completed_operations.rs

1//! Completed operations database implementation
2
3use 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        // Update keyset_amounts with fee_collected from the breakdown
95        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
128            .pool
129            .get()
130            .await
131            .map_err(|e| Error::Database(Box::new(e)))?;
132        Ok(query(
133            r#"
134            SELECT
135                operation_id,
136                operation_kind,
137                completed_at,
138                total_issued,
139                total_redeemed,
140                fee_collected,
141                payment_method
142            FROM
143                completed_operations
144            WHERE
145                operation_id = :operation_id
146            "#,
147        )?
148        .bind("operation_id", operation_id.to_string())
149        .fetch_one(&*conn)
150        .await?
151        .map(sql_row_to_completed_operation)
152        .transpose()?)
153    }
154
155    async fn get_completed_operations_by_kind(
156        &self,
157        operation_kind: mint::OperationKind,
158    ) -> Result<Vec<mint::Operation>, Self::Err> {
159        let conn = self
160            .pool
161            .get()
162            .await
163            .map_err(|e| Error::Database(Box::new(e)))?;
164        Ok(query(
165            r#"
166            SELECT
167                operation_id,
168                operation_kind,
169                completed_at,
170                total_issued,
171                total_redeemed,
172                fee_collected,
173                payment_method
174            FROM
175                completed_operations
176            WHERE
177                operation_kind = :operation_kind
178            ORDER BY completed_at DESC
179            "#,
180        )?
181        .bind("operation_kind", operation_kind.to_string())
182        .fetch_all(&*conn)
183        .await?
184        .into_iter()
185        .map(sql_row_to_completed_operation)
186        .collect::<Result<Vec<_>, _>>()?)
187    }
188
189    async fn get_completed_operations(&self) -> Result<Vec<mint::Operation>, Self::Err> {
190        let conn = self
191            .pool
192            .get()
193            .await
194            .map_err(|e| Error::Database(Box::new(e)))?;
195        Ok(query(
196            r#"
197            SELECT
198                operation_id,
199                operation_kind,
200                completed_at,
201                total_issued,
202                total_redeemed,
203                fee_collected,
204                payment_method
205            FROM
206                completed_operations
207            ORDER BY completed_at DESC
208            "#,
209        )?
210        .fetch_all(&*conn)
211        .await?
212        .into_iter()
213        .map(sql_row_to_completed_operation)
214        .collect::<Result<Vec<_>, _>>()?)
215    }
216}