Fix: Update apipayments updated_at field when a payment is updated. (#3699)
Co-authored-by: Vlad Stan <stan.v.vlad@gmail.com>
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
from datetime import datetime, timezone
|
||||||
from time import time
|
from time import time
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
@@ -304,8 +305,16 @@ async def update_payment_checking_id(
|
|||||||
checking_id: str, new_checking_id: str, conn: Connection | None = None
|
checking_id: str, new_checking_id: str, conn: Connection | None = None
|
||||||
) -> None:
|
) -> None:
|
||||||
await (conn or db).execute(
|
await (conn or db).execute(
|
||||||
"UPDATE apipayments SET checking_id = :new_id WHERE checking_id = :old_id",
|
f"""
|
||||||
{"new_id": new_checking_id, "old_id": checking_id},
|
UPDATE apipayments
|
||||||
|
SET checking_id = :new_id, updated_at = {db.timestamp_placeholder('now')}
|
||||||
|
WHERE checking_id = :old_id
|
||||||
|
""", # noqa: S608
|
||||||
|
{
|
||||||
|
"new_id": new_checking_id,
|
||||||
|
"old_id": checking_id,
|
||||||
|
"now": int(time()),
|
||||||
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -314,6 +323,7 @@ async def update_payment(
|
|||||||
new_checking_id: str | None = None,
|
new_checking_id: str | None = None,
|
||||||
conn: Connection | None = None,
|
conn: Connection | None = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
|
payment.updated_at = datetime.now(timezone.utc)
|
||||||
await (conn or db).update(
|
await (conn or db).update(
|
||||||
"apipayments", payment, "WHERE checking_id = :checking_id"
|
"apipayments", payment, "WHERE checking_id = :checking_id"
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import asyncio
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from lnbits.core.crud import (
|
from lnbits.core.crud import (
|
||||||
@@ -6,6 +8,7 @@ from lnbits.core.crud import (
|
|||||||
get_payments_paginated,
|
get_payments_paginated,
|
||||||
update_payment,
|
update_payment,
|
||||||
)
|
)
|
||||||
|
from lnbits.core.crud.payments import get_standalone_payment
|
||||||
from lnbits.core.models import PaymentFilters, PaymentState
|
from lnbits.core.models import PaymentFilters, PaymentState
|
||||||
from lnbits.core.services import (
|
from lnbits.core.services import (
|
||||||
create_invoice,
|
create_invoice,
|
||||||
@@ -39,6 +42,15 @@ async def test_crud_get_payments(app):
|
|||||||
filters = Filters(limit=100)
|
filters = Filters(limit=100)
|
||||||
payments = await get_payments(wallet_id=wallet.id, filters=filters)
|
payments = await get_payments(wallet_id=wallet.id, filters=filters)
|
||||||
assert len(payments) == 22, "should return 22 successful payments"
|
assert len(payments) == 22, "should return 22 successful payments"
|
||||||
|
first_payment = payments[0]
|
||||||
|
first_payment_updated_at = first_payment.updated_at.replace()
|
||||||
|
await update_payment(first_payment)
|
||||||
|
await asyncio.sleep(0.1) # ensure updated_at will be different
|
||||||
|
first_payment_updated = await get_standalone_payment(first_payment.checking_id)
|
||||||
|
assert first_payment_updated, "Updated payment not found"
|
||||||
|
assert (
|
||||||
|
first_payment_updated.updated_at > first_payment_updated_at
|
||||||
|
), "Updated payment timestamp is not newer"
|
||||||
|
|
||||||
payments = await get_payments(wallet_id=wallet.id, incoming=True, filters=filters)
|
payments = await get_payments(wallet_id=wallet.id, incoming=True, filters=filters)
|
||||||
assert len(payments) == 11, "should return 11 successful incoming payments"
|
assert len(payments) == 11, "should return 11 successful incoming payments"
|
||||||
|
|||||||
Reference in New Issue
Block a user