From bf9a768b886e318638ee0e2904ce3dfda8641781 Mon Sep 17 00:00:00 2001 From: Jon Staab Date: Thu, 21 May 2026 15:11:43 -0700 Subject: [PATCH] Inline some invoie route logic --- backend/src/billing.rs | 35 ---------------------------- backend/src/routes/invoices.rs | 42 ++++++++++++++++++++++++++-------- 2 files changed, 33 insertions(+), 44 deletions(-) diff --git a/backend/src/billing.rs b/backend/src/billing.rs index e244209..dec8d15 100644 --- a/backend/src/billing.rs +++ b/backend/src/billing.rs @@ -231,41 +231,6 @@ impl Billing { // --- Public API helpers --- - pub async fn get_or_create_manual_lightning_bolt11( - &self, - invoice_id: &str, - tenant_pubkey: &str, - amount_due_minor: i64, - currency: &str, - ) -> Result { - if let Some(existing_bolt11) = self - .query - .get_invoice_manual_lightning_bolt11(invoice_id) - .await? - { - return Ok(existing_bolt11); - } - - let bolt11 = self.create_bolt11(amount_due_minor, currency).await?; - - if self - .command - .insert_manual_lightning_invoice_payment(invoice_id, tenant_pubkey, &bolt11) - .await? - { - return Ok(bolt11); - } - - self.query - .get_invoice_manual_lightning_bolt11(invoice_id) - .await? - .ok_or_else(|| { - anyhow!( - "manual lightning payment row missing after insert race for invoice {invoice_id}" - ) - }) - } - pub async fn create_bolt11(&self, amount_due_minor: i64, currency: &str) -> Result { let amount_msats = bitcoin::fiat_to_msats(amount_due_minor, currency).await?; self.wallet diff --git a/backend/src/routes/invoices.rs b/backend/src/routes/invoices.rs index ed6353d..f7ba71f 100644 --- a/backend/src/routes/invoices.rs +++ b/backend/src/routes/invoices.rs @@ -74,15 +74,39 @@ pub async fn get_invoice_bolt11( return Err(bad_request("invoice-not-open", "invoice is not open")); } - let bolt11 = api - .billing - .get_or_create_manual_lightning_bolt11( - &id, - &tenant.pubkey, - invoice.amount_due, - &invoice.currency, - ) + let bolt11 = if let Some(existing_bolt11) = api + .query + .get_invoice_manual_lightning_bolt11(&id) .await - .map_err(internal)?; + .map_err(internal)? + { + existing_bolt11 + } else { + let bolt11 = api + .billing + .create_bolt11(invoice.amount_due, &invoice.currency) + .await + .map_err(internal)?; + + if api + .command + .insert_manual_lightning_invoice_payment(&id, &tenant.pubkey, &bolt11) + .await + .map_err(internal)? + { + bolt11 + } else { + api.query + .get_invoice_manual_lightning_bolt11(&id) + .await + .map_err(internal)? + .ok_or_else(|| { + internal(format!( + "manual lightning payment row missing after insert race for invoice {id}" + )) + })? + } + }; + ok(serde_json::json!({ "bolt11": bolt11 })) }