Add filtering for mint quote states in database (#586)

* filter for mint quote states in db

---------

Co-authored-by: thesimplekid <tsk@thesimplekid.com>
Co-authored-by: ok300 <106775972+ok300@users.noreply.github.com>
This commit is contained in:
callebtc
2025-02-08 10:28:54 +01:00
committed by GitHub
parent 8a8b697d9a
commit b818054f9a
5 changed files with 89 additions and 10 deletions

View File

@@ -49,6 +49,11 @@ pub trait Database {
) -> Result<Option<MintMintQuote>, Self::Err>;
/// Get Mint Quotes
async fn get_mint_quotes(&self) -> Result<Vec<MintMintQuote>, Self::Err>;
/// Get Mint Quotes with state
async fn get_mint_quotes_with_state(
&self,
state: MintQuoteState,
) -> Result<Vec<MintMintQuote>, Self::Err>;
/// Remove [`MintMintQuote`]
async fn remove_mint_quote(&self, quote_id: &Uuid) -> Result<(), Self::Err>;

View File

@@ -381,6 +381,28 @@ impl MintDatabase for MintRedbDatabase {
Ok(quotes)
}
async fn get_mint_quotes_with_state(
&self,
state: MintQuoteState,
) -> Result<Vec<MintQuote>, Self::Err> {
let read_txn = self.db.begin_read().map_err(Error::from)?;
let table = read_txn
.open_table(MINT_QUOTES_TABLE)
.map_err(Error::from)?;
let mut quotes = Vec::new();
for (_id, quote) in (table.iter().map_err(Error::from)?).flatten() {
let quote: MintQuote = serde_json::from_str(quote.value()).map_err(Error::from)?;
if quote.state == state {
quotes.push(quote)
}
}
Ok(quotes)
}
async fn remove_mint_quote(&self, quote_id: &Uuid) -> Result<(), Self::Err> {
let write_txn = self.db.begin_write().map_err(Error::from)?;

View File

@@ -431,6 +431,43 @@ FROM mint_quote
}
}
async fn get_mint_quotes_with_state(
&self,
state: MintQuoteState,
) -> Result<Vec<MintQuote>, Self::Err> {
let mut transaction = self.pool.begin().await.map_err(Error::from)?;
let rec = sqlx::query(
r#"
SELECT *
FROM mint_quote
WHERE state = ?
"#,
)
.bind(state.to_string())
.fetch_all(&mut transaction)
.await;
match rec {
Ok(rows) => {
transaction.commit().await.map_err(Error::from)?;
let mint_quotes = rows
.into_iter()
.map(sqlite_row_to_mint_quote)
.collect::<Result<Vec<MintQuote>, _>>()?;
Ok(mint_quotes)
}
Err(err) => {
tracing::error!("SQLite get mint quotes with state");
if let Err(err) = transaction.rollback().await {
tracing::error!("Could not rollback sql transaction: {}", err);
}
return Err(Error::from(err).into());
}
}
}
async fn remove_mint_quote(&self, quote_id: &Uuid) -> Result<(), Self::Err> {
let mut transaction = self.pool.begin().await.map_err(Error::from)?;

View File

@@ -194,6 +194,21 @@ impl MintDatabase for MintMemoryDatabase {
Ok(self.mint_quotes.read().await.values().cloned().collect())
}
async fn get_mint_quotes_with_state(
&self,
state: MintQuoteState,
) -> Result<Vec<MintQuote>, Self::Err> {
let mint_quotes = self.mint_quotes.read().await;
let pending_quotes = mint_quotes
.values()
.filter(|q| q.state == state)
.cloned()
.collect();
Ok(pending_quotes)
}
async fn remove_mint_quote(&self, quote_id: &Uuid) -> Result<(), Self::Err> {
self.mint_quotes.write().await.remove(quote_id);

View File

@@ -178,23 +178,23 @@ impl Mint {
/// Get pending mint quotes
#[instrument(skip_all)]
pub async fn get_pending_mint_quotes(&self) -> Result<Vec<MintQuote>, Error> {
let mint_quotes = self.localstore.get_mint_quotes().await?;
let mint_quotes = self
.localstore
.get_mint_quotes_with_state(MintQuoteState::Pending)
.await?;
Ok(mint_quotes
.into_iter()
.filter(|p| p.state == MintQuoteState::Pending)
.collect())
Ok(mint_quotes)
}
/// Get pending mint quotes
#[instrument(skip_all)]
pub async fn get_unpaid_mint_quotes(&self) -> Result<Vec<MintQuote>, Error> {
let mint_quotes = self.localstore.get_mint_quotes().await?;
let mint_quotes = self
.localstore
.get_mint_quotes_with_state(MintQuoteState::Unpaid)
.await?;
Ok(mint_quotes
.into_iter()
.filter(|p| p.state == MintQuoteState::Unpaid)
.collect())
Ok(mint_quotes)
}
/// Remove mint quote