Skip to content

Commit

Permalink
feat: Set invoice state to canceled instead of failed
Browse files Browse the repository at this point in the history
We can't really know if the invoice failed or got canceled. I think canceled is the better state name though.
  • Loading branch information
holzeis committed May 31, 2024
1 parent 73164d9 commit f72e1f0
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- Postgres does not allow removing enum type values. One can only re-create an enum type with fewer values and replace the references.
-- However, there is no proper way to replace the values to be removed where they are used (i.e. referenced in `hodl_invoice` table)
-- We opt to NOT remove enum values that were added at a later point.

select 1;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TYPE "InvoiceState_Type" ADD VALUE IF NOT EXISTS 'Canceled';
2 changes: 2 additions & 0 deletions coordinator/src/db/custom_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ impl ToSql<InvoiceStateType, Pg> for InvoiceState {
InvoiceState::Accepted => out.write_all(b"Accepted")?,
InvoiceState::Settled => out.write_all(b"Settled")?,
InvoiceState::Failed => out.write_all(b"Failed")?,
InvoiceState::Canceled => out.write_all(b"Canceled")?,
}
Ok(IsNull::No)
}
Expand All @@ -287,6 +288,7 @@ impl FromSql<InvoiceStateType, Pg> for InvoiceState {
b"Accepted" => Ok(InvoiceState::Accepted),
b"Settled" => Ok(InvoiceState::Settled),
b"Failed" => Ok(InvoiceState::Failed),
b"Canceled" => Ok(InvoiceState::Canceled),
_ => Err("Unrecognized enum variant".into()),
}
}
Expand Down
5 changes: 3 additions & 2 deletions coordinator/src/db/hodl_invoice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub enum InvoiceState {
Accepted,
Settled,
Failed,
Canceled,
}

impl QueryId for InvoiceStateType {
Expand Down Expand Up @@ -95,15 +96,15 @@ pub fn update_hodl_invoice_to_settled(
.get_result(conn)
}

pub fn update_hodl_invoice_to_failed_by_r_hash(
pub fn update_hodl_invoice_to_canceled(
conn: &mut PgConnection,
r_hash: String,
) -> QueryResult<usize> {
diesel::update(hodl_invoices::table)
.filter(hodl_invoices::r_hash.eq(r_hash))
.set((
hodl_invoices::updated_at.eq(OffsetDateTime::now_utc()),
hodl_invoices::invoice_state.eq(InvoiceState::Failed),
hodl_invoices::invoice_state.eq(InvoiceState::Canceled),
))
.execute(conn)
}
2 changes: 1 addition & 1 deletion coordinator/src/node/invoice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub fn spawn_invoice_watch(
let r_hash = r_hash.clone();
move || {
let mut conn = pool.get()?;
db::hodl_invoice::update_hodl_invoice_to_failed_by_r_hash(
db::hodl_invoice::update_hodl_invoice_to_canceled(
&mut conn, r_hash,
)?;
anyhow::Ok(())
Expand Down

0 comments on commit f72e1f0

Please sign in to comment.