From 0b883d02b3f2cd6a302d60ea09c60641306b1911 Mon Sep 17 00:00:00 2001 From: Tiago Vasconcelos Date: Tue, 8 Nov 2022 13:16:08 +0000 Subject: [PATCH 01/13] get payment from db --- lnbits/extensions/tpos/crud.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lnbits/extensions/tpos/crud.py b/lnbits/extensions/tpos/crud.py index 94e2c0068..04b5eaa9d 100644 --- a/lnbits/extensions/tpos/crud.py +++ b/lnbits/extensions/tpos/crud.py @@ -1,5 +1,6 @@ from typing import List, Optional, Union +from lnbits.core.models import Payment from lnbits.helpers import urlsafe_short_hash from . import db @@ -47,3 +48,12 @@ async def get_tposs(wallet_ids: Union[str, List[str]]) -> List[TPoS]: async def delete_tpos(tpos_id: str) -> None: await db.execute("DELETE FROM tpos.tposs WHERE id = ?", (tpos_id,)) + + +async def get_tpos_payments(tpos_id: str, limit: int = 5): + + rows = await db.fetchall( + f"SELECT * FROM apipayments WHERE extra LIKE '%tposId%' AND extra LIKE '%{tpos_id}%' ORDER BY time DESC LIMIT {limit}" + ) + + return [Payment.from_row(row) for row in rows] From 8d8a309258dcd560d2277a8d0e7c4d1cf1394515 Mon Sep 17 00:00:00 2001 From: Tiago Vasconcelos Date: Tue, 8 Nov 2022 13:16:27 +0000 Subject: [PATCH 02/13] api endpoint to get last payments --- lnbits/extensions/tpos/views_api.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/lnbits/extensions/tpos/views_api.py b/lnbits/extensions/tpos/views_api.py index 805014917..9167b5a11 100644 --- a/lnbits/extensions/tpos/views_api.py +++ b/lnbits/extensions/tpos/views_api.py @@ -13,7 +13,7 @@ from lnbits.core.views.api import api_payment from lnbits.decorators import WalletTypeInfo, get_key_type, require_admin_key from . import tpos_ext -from .crud import create_tpos, delete_tpos, get_tpos, get_tposs +from .crud import create_tpos, delete_tpos, get_tpos, get_tpos_payments, get_tposs from .models import CreateTposData, PayLnurlWData @@ -81,6 +81,22 @@ async def api_tpos_create_invoice( return {"payment_hash": payment_hash, "payment_request": payment_request} +@tpos_ext.get("/api/v1/tposs/{tpos_id}/invoices") +async def api_tpos_get_latest_invoices(tpos_id: str = None): + payments = await get_tpos_payments(tpos_id) + + return [ + { + "checking_id": payment.checking_id, + "amount": payment.amount, + "time": payment.time, + "bolt11": payment.bolt11, + "pending": payment.pending, + } + for payment in payments + ] + + @tpos_ext.post( "/api/v1/tposs/{tpos_id}/invoices/{payment_request}/pay", status_code=HTTPStatus.OK ) From 9f7b4c1370a93eb51a419951b2db0d570f572eb2 Mon Sep 17 00:00:00 2001 From: Tiago Vasconcelos Date: Tue, 8 Nov 2022 13:16:47 +0000 Subject: [PATCH 03/13] add button and list --- .../extensions/tpos/templates/tpos/tpos.html | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/lnbits/extensions/tpos/templates/tpos/tpos.html b/lnbits/extensions/tpos/templates/tpos/tpos.html index ad04b3bea..36d76cf78 100644 --- a/lnbits/extensions/tpos/templates/tpos/tpos.html +++ b/lnbits/extensions/tpos/templates/tpos/tpos.html @@ -148,6 +148,9 @@ + + + + + + + + + + + + + {%raw%} + + {{payment.amount / 1000}} sats + {{payment.checking_id}} + + + {{payment.dateFrom}} + + + {%endraw%} + + + + {% endblock %} {% block styles %} @@ -296,6 +322,10 @@ tipAmount: 0.0, hasNFC: false, nfcTagReading: false, + lastPaymentsDialog: { + show: false, + data: [] + }, invoiceDialog: { show: false, data: null, @@ -520,6 +550,25 @@ self.exchangeRate = response.data.data['BTC' + self.currency][self.currency] }) + }, + getLastPayments(){ + return axios + .get(`/tpos/api/v1/tposs/${this.tposId}/invoices`) + .then(res => { + if(res.data && res.data.length){ + let last = [...res.data] + //last.length = Math.min(last.length, 5) + this.lastPaymentsDialog.data = last.map(obj => { + obj.dateFrom = moment(obj.time * 1000).fromNow() + return obj + }) + } + }) + .catch(e => console.error(e)) + }, + showLastPayments(){ + this.getLastPayments() + this.lastPaymentsDialog.show = true } }, created: function () { From 8e85651c4fbefdec0c389af029a419c7ad3966c5 Mon Sep 17 00:00:00 2001 From: Tiago Vasconcelos Date: Tue, 8 Nov 2022 13:18:10 +0000 Subject: [PATCH 04/13] make format --- .../extensions/tpos/templates/tpos/tpos.html | 24 +++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/lnbits/extensions/tpos/templates/tpos/tpos.html b/lnbits/extensions/tpos/templates/tpos/tpos.html index 36d76cf78..32ed0b884 100644 --- a/lnbits/extensions/tpos/templates/tpos/tpos.html +++ b/lnbits/extensions/tpos/templates/tpos/tpos.html @@ -149,7 +149,12 @@ - + {{payment.amount / 1000}} sats - {{payment.checking_id}} + {{payment.checking_id}} {{payment.dateFrom}} - + {%endraw%} - + @@ -551,11 +561,11 @@ response.data.data['BTC' + self.currency][self.currency] }) }, - getLastPayments(){ + getLastPayments() { return axios .get(`/tpos/api/v1/tposs/${this.tposId}/invoices`) .then(res => { - if(res.data && res.data.length){ + if (res.data && res.data.length) { let last = [...res.data] //last.length = Math.min(last.length, 5) this.lastPaymentsDialog.data = last.map(obj => { @@ -566,7 +576,7 @@ }) .catch(e => console.error(e)) }, - showLastPayments(){ + showLastPayments() { this.getLastPayments() this.lastPaymentsDialog.show = true } From a2092675cdceaf974bce48fcee1257107c51361b Mon Sep 17 00:00:00 2001 From: Tiago Vasconcelos Date: Tue, 8 Nov 2022 15:18:07 +0000 Subject: [PATCH 05/13] added some UI sugar, removed pending tx --- lnbits/extensions/tpos/crud.py | 8 +++++++- lnbits/extensions/tpos/templates/tpos/tpos.html | 11 +++++------ lnbits/extensions/tpos/views_api.py | 1 - 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/lnbits/extensions/tpos/crud.py b/lnbits/extensions/tpos/crud.py index 04b5eaa9d..c403eb5a0 100644 --- a/lnbits/extensions/tpos/crud.py +++ b/lnbits/extensions/tpos/crud.py @@ -53,7 +53,13 @@ async def delete_tpos(tpos_id: str) -> None: async def get_tpos_payments(tpos_id: str, limit: int = 5): rows = await db.fetchall( - f"SELECT * FROM apipayments WHERE extra LIKE '%tposId%' AND extra LIKE '%{tpos_id}%' ORDER BY time DESC LIMIT {limit}" + f""" + SELECT * FROM apipayments + WHERE pending = 'false' + AND extra LIKE '%tposId%' + AND extra LIKE '%{tpos_id}%' + ORDER BY time DESC LIMIT {limit} + """ ) return [Payment.from_row(row) for row in rows] diff --git a/lnbits/extensions/tpos/templates/tpos/tpos.html b/lnbits/extensions/tpos/templates/tpos/tpos.html index 32ed0b884..8fe61452e 100644 --- a/lnbits/extensions/tpos/templates/tpos/tpos.html +++ b/lnbits/extensions/tpos/templates/tpos/tpos.html @@ -275,17 +275,16 @@ {%raw%} - {{payment.amount / 1000}} sats + {{payment.amount / 1000}} sats {{payment.checking_id}}Hash: {{payment.checking_id.slice(0, 30)}}... {{payment.dateFrom}} - + {%endraw%} diff --git a/lnbits/extensions/tpos/views_api.py b/lnbits/extensions/tpos/views_api.py index 9167b5a11..206c19561 100644 --- a/lnbits/extensions/tpos/views_api.py +++ b/lnbits/extensions/tpos/views_api.py @@ -90,7 +90,6 @@ async def api_tpos_get_latest_invoices(tpos_id: str = None): "checking_id": payment.checking_id, "amount": payment.amount, "time": payment.time, - "bolt11": payment.bolt11, "pending": payment.pending, } for payment in payments From 4549190e8688a58f874706c41ee2ddf223586d79 Mon Sep 17 00:00:00 2001 From: Tiago Vasconcelos Date: Thu, 17 Nov 2022 12:59:40 +0000 Subject: [PATCH 06/13] abstract get latest payments for extensions --- lnbits/core/crud.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/lnbits/core/crud.py b/lnbits/core/crud.py index bb1ca0c1c..881d10014 100644 --- a/lnbits/core/crud.py +++ b/lnbits/core/crud.py @@ -229,6 +229,24 @@ async def get_wallet_payment( return Payment.from_row(row) if row else None +async def get_latest_payments_by_extension(ext_name: str, ext_id: str, limit: int = 5): + rows = await db.fetchall( + f""" + SELECT * FROM apipayments + WHERE pending = 'false' + AND extra LIKE ? + AND extra LIKE ? + ORDER BY time DESC LIMIT {limit} + """, + ( + f"%{ext_name}%", + f"%{ext_id}%", + ), + ) + + return rows + + async def get_payments( *, wallet_id: Optional[str] = None, From facc7bbf5e3e7ffb70da6490f695dafeb5428d9f Mon Sep 17 00:00:00 2001 From: Tiago Vasconcelos Date: Thu, 17 Nov 2022 13:00:17 +0000 Subject: [PATCH 07/13] remove core db action from extension --- lnbits/extensions/tpos/crud.py | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/lnbits/extensions/tpos/crud.py b/lnbits/extensions/tpos/crud.py index c403eb5a0..94e2c0068 100644 --- a/lnbits/extensions/tpos/crud.py +++ b/lnbits/extensions/tpos/crud.py @@ -1,6 +1,5 @@ from typing import List, Optional, Union -from lnbits.core.models import Payment from lnbits.helpers import urlsafe_short_hash from . import db @@ -48,18 +47,3 @@ async def get_tposs(wallet_ids: Union[str, List[str]]) -> List[TPoS]: async def delete_tpos(tpos_id: str) -> None: await db.execute("DELETE FROM tpos.tposs WHERE id = ?", (tpos_id,)) - - -async def get_tpos_payments(tpos_id: str, limit: int = 5): - - rows = await db.fetchall( - f""" - SELECT * FROM apipayments - WHERE pending = 'false' - AND extra LIKE '%tposId%' - AND extra LIKE '%{tpos_id}%' - ORDER BY time DESC LIMIT {limit} - """ - ) - - return [Payment.from_row(row) for row in rows] From 121331fa0b0d884b705eb89ce8160b530c3375c6 Mon Sep 17 00:00:00 2001 From: Tiago Vasconcelos Date: Thu, 17 Nov 2022 13:00:53 +0000 Subject: [PATCH 08/13] refactor get latest payments --- lnbits/extensions/tpos/views_api.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/lnbits/extensions/tpos/views_api.py b/lnbits/extensions/tpos/views_api.py index 206c19561..e8dec2b42 100644 --- a/lnbits/extensions/tpos/views_api.py +++ b/lnbits/extensions/tpos/views_api.py @@ -7,13 +7,14 @@ from lnurl import decode as decode_lnurl from loguru import logger from starlette.exceptions import HTTPException -from lnbits.core.crud import get_user +from lnbits.core.crud import get_user, get_latest_payments_by_extension from lnbits.core.services import create_invoice from lnbits.core.views.api import api_payment from lnbits.decorators import WalletTypeInfo, get_key_type, require_admin_key +from lnbits.core.models import Payment from . import tpos_ext -from .crud import create_tpos, delete_tpos, get_tpos, get_tpos_payments, get_tposs +from .crud import create_tpos, delete_tpos, get_tpos, get_tposs from .models import CreateTposData, PayLnurlWData @@ -83,7 +84,12 @@ async def api_tpos_create_invoice( @tpos_ext.get("/api/v1/tposs/{tpos_id}/invoices") async def api_tpos_get_latest_invoices(tpos_id: str = None): - payments = await get_tpos_payments(tpos_id) + payments = [ + Payment.from_row(row) + for row in await get_latest_payments_by_extension( + ext_name="tpos", ext_id=tpos_id + ) + ] return [ { From e93d1b322be5b71567a8e6ca4ed8cd5eadbea74a Mon Sep 17 00:00:00 2001 From: Tiago Vasconcelos Date: Thu, 17 Nov 2022 13:02:32 +0000 Subject: [PATCH 09/13] make format --- lnbits/extensions/tpos/views_api.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lnbits/extensions/tpos/views_api.py b/lnbits/extensions/tpos/views_api.py index e8dec2b42..204548b1b 100644 --- a/lnbits/extensions/tpos/views_api.py +++ b/lnbits/extensions/tpos/views_api.py @@ -7,11 +7,11 @@ from lnurl import decode as decode_lnurl from loguru import logger from starlette.exceptions import HTTPException -from lnbits.core.crud import get_user, get_latest_payments_by_extension +from lnbits.core.crud import get_latest_payments_by_extension, get_user +from lnbits.core.models import Payment from lnbits.core.services import create_invoice from lnbits.core.views.api import api_payment from lnbits.decorators import WalletTypeInfo, get_key_type, require_admin_key -from lnbits.core.models import Payment from . import tpos_ext from .crud import create_tpos, delete_tpos, get_tpos, get_tposs From 2eaf5e5668501c2bfb348d9af34c2b03f3e1a12f Mon Sep 17 00:00:00 2001 From: Tiago Vasconcelos Date: Thu, 17 Nov 2022 15:33:20 +0000 Subject: [PATCH 10/13] remove commented out code --- lnbits/extensions/tpos/templates/tpos/tpos.html | 1 - 1 file changed, 1 deletion(-) diff --git a/lnbits/extensions/tpos/templates/tpos/tpos.html b/lnbits/extensions/tpos/templates/tpos/tpos.html index 8fe61452e..12e18bec9 100644 --- a/lnbits/extensions/tpos/templates/tpos/tpos.html +++ b/lnbits/extensions/tpos/templates/tpos/tpos.html @@ -566,7 +566,6 @@ .then(res => { if (res.data && res.data.length) { let last = [...res.data] - //last.length = Math.min(last.length, 5) this.lastPaymentsDialog.data = last.map(obj => { obj.dateFrom = moment(obj.time * 1000).fromNow() return obj From ebeecdecca4bd15fff506e6fd383b90bfdf879d9 Mon Sep 17 00:00:00 2001 From: Tiago Vasconcelos Date: Fri, 18 Nov 2022 10:22:11 +0000 Subject: [PATCH 11/13] add "No paid invoices" if... no paid invoices --- lnbits/extensions/tpos/templates/tpos/tpos.html | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lnbits/extensions/tpos/templates/tpos/tpos.html b/lnbits/extensions/tpos/templates/tpos/tpos.html index 12e18bec9..6bc6a6268 100644 --- a/lnbits/extensions/tpos/templates/tpos/tpos.html +++ b/lnbits/extensions/tpos/templates/tpos/tpos.html @@ -272,6 +272,13 @@ + + + No paid invoices + + {%raw%} From 28e62f3333f5ea651bae4d52923d59e4f793399d Mon Sep 17 00:00:00 2001 From: Tiago Vasconcelos Date: Fri, 18 Nov 2022 10:27:23 +0000 Subject: [PATCH 12/13] make format --- lnbits/extensions/tpos/templates/tpos/tpos.html | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lnbits/extensions/tpos/templates/tpos/tpos.html b/lnbits/extensions/tpos/templates/tpos/tpos.html index 6bc6a6268..c66238f76 100644 --- a/lnbits/extensions/tpos/templates/tpos/tpos.html +++ b/lnbits/extensions/tpos/templates/tpos/tpos.html @@ -274,9 +274,7 @@ - No paid invoices + No paid invoices From 79fc0b9be4785f7a8a3f403c785e9029338a4544 Mon Sep 17 00:00:00 2001 From: Tiago Vasconcelos Date: Fri, 18 Nov 2022 10:56:14 +0000 Subject: [PATCH 13/13] wrap db call in try except --- lnbits/extensions/tpos/views_api.py | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/lnbits/extensions/tpos/views_api.py b/lnbits/extensions/tpos/views_api.py index 204548b1b..fe63a2471 100644 --- a/lnbits/extensions/tpos/views_api.py +++ b/lnbits/extensions/tpos/views_api.py @@ -84,12 +84,16 @@ async def api_tpos_create_invoice( @tpos_ext.get("/api/v1/tposs/{tpos_id}/invoices") async def api_tpos_get_latest_invoices(tpos_id: str = None): - payments = [ - Payment.from_row(row) - for row in await get_latest_payments_by_extension( - ext_name="tpos", ext_id=tpos_id - ) - ] + try: + payments = [ + Payment.from_row(row) + for row in await get_latest_payments_by_extension( + ext_name="tpos", ext_id=tpos_id + ) + ] + + except Exception as e: + raise HTTPException(status_code=HTTPStatus.INTERNAL_SERVER_ERROR, detail=str(e)) return [ {