From dd4a9f10cf4c965f8efcf7285503bdbf57d04727 Mon Sep 17 00:00:00 2001 From: Vlad Stan Date: Wed, 21 Dec 2022 14:57:00 +0200 Subject: [PATCH] feat: add function to update the `extra` JSON values --- lnbits/core/crud.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/lnbits/core/crud.py b/lnbits/core/crud.py index 1c8c71ad..771ff397 100644 --- a/lnbits/core/crud.py +++ b/lnbits/core/crud.py @@ -451,6 +451,36 @@ async def update_payment_details( return +async def update_payment_extra( + payment_hash: str, + extra: dict, + conn: Optional[Connection] = None, +) -> None: + """ + Only update the `extra` field for the payment. + Old values in the `extra` JSON object will be kept unless the new `extra` overwrites them. + """ + + row = await (conn or db).fetchone( + "SELECT hash, extra from apipayments WHERE hash = ?", + (payment_hash), + ) + if not row: + return + existing_extra = json.loads(row["extra"] if row["extra"] else "{}") + new_extra = { + **existing_extra, + **extra, + } + await (conn or db).execute( + """ + UPDATE apipayments SET extra = ? + WHERE hash = ? + """, + (json.dumps(new_extra), payment_hash), + ) + + async def delete_payment(checking_id: str, conn: Optional[Connection] = None) -> None: await (conn or db).execute( "DELETE FROM apipayments WHERE checking_id = ?", (checking_id,)