Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a created at field #77

Merged
merged 8 commits into from
Mar 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion crud.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import datetime, timezone
from typing import List, Optional, Union

from lnbits.db import Database
Expand Down Expand Up @@ -39,10 +40,10 @@ async def get_pay_link_by_username(username: str) -> Optional[PayLink]:


async def create_pay_link(data: CreatePayLinkData) -> PayLink:

link_id = urlsafe_short_hash()[:6]

assert data.wallet, "Wallet is required"
now = datetime.now(timezone.utc)

link = PayLink(
id=link_id,
Expand All @@ -63,6 +64,8 @@ async def create_pay_link(data: CreatePayLinkData) -> PayLink:
currency=data.currency,
comment_chars=data.comment_chars,
fiat_base_multiplier=data.fiat_base_multiplier,
created_at=now,
updated_at=now,
)

await db.insert("lnurlp.pay_links", link)
Expand Down Expand Up @@ -96,6 +99,7 @@ async def get_pay_links(wallet_ids: Union[str, List[str]]) -> List[PayLink]:


async def update_pay_link(link: PayLink) -> PayLink:
link.updated_at = datetime.now(timezone.utc)
await db.update("lnurlp.pay_links", link)
return link

Expand Down
31 changes: 31 additions & 0 deletions migrations.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
from time import time

from lnbits.db import Connection


async def m001_initial(db):
"""
Initial pay table.
Expand Down Expand Up @@ -181,3 +186,29 @@ async def m010_add_pay_link_domain(db):
Add domain to pay links
"""
await db.execute("ALTER TABLE lnurlp.pay_links ADD COLUMN domain TEXT;")


async def m011_add_created_at(db: Connection):
"""
Add created_at to pay links
"""

await db.execute(
f"""ALTER TABLE lnurlp.pay_links ADD COLUMN
created_at TIMESTAMP DEFAULT {db.timestamp_column_default}"""
)
await db.execute(
f"""ALTER TABLE lnurlp.pay_links ADD COLUMN
updated_at TIMESTAMP DEFAULT {db.timestamp_column_default}"""
)

now = int(time())
await db.execute(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this required?
the new columns are NOT NULL and have default value.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i removed not null, so yes

f"""
UPDATE lnurlp.pay_links
SET created_at = {db.timestamp_placeholder('now')},
updated_at = {db.timestamp_placeholder('now')}
WHERE created_at IS NULL AND updated_at IS NULL
""",
{"now": now},
)
3 changes: 3 additions & 0 deletions models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
from datetime import datetime
from typing import Optional

from fastapi import Query, Request
Expand Down Expand Up @@ -58,6 +59,8 @@ class PayLink(BaseModel):
comment_chars: int
max: float
fiat_base_multiplier: int
created_at: datetime
updated_at: datetime

def lnurl(self, req: Request) -> str:
url = req.url_for("lnurlp.api_lnurl_response", link_id=self.id)
Expand Down
46 changes: 44 additions & 2 deletions static/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ const locationPath = [

const mapPayLink = obj => {
obj._data = _.clone(obj)
obj.date = LNbits.utils.formatDate(obj.time)
obj.created_at = LNbits.utils.formatDateString(obj.created_at)
obj.updated_at = LNbits.utils.formatDateString(obj.updated_at)

obj.amount = new Intl.NumberFormat(LOCALE).format(obj.amount)
obj.print_url = [locationPath, 'print/', obj.id].join('')
obj.pay_url = [locationPath, 'link/', obj.id].join('')
return obj
Expand Down Expand Up @@ -39,6 +39,48 @@ window.app = Vue.createApp({
fiatRates: {},
payLinks: [],
payLinksTable: {
columns: [
{
name: 'created_at',
label: 'Created',
align: 'left',
field: 'created_at',
sortable: true
},
{
name: 'description',
label: 'Description',
align: 'left',
field: 'description'
},
{
name: 'amount',
label: 'Amount',
align: 'left',
format: (_, row) => {
const min = row.min
const max = row.max
if (min === max) return `${min}`
return `${min} - ${max}`
}
},
{
name: 'currency',
label: 'Currency',
align: 'left',
field: 'currency',
format: val => val ?? 'sat'
},
{
name: 'username',
label: 'Username',
align: 'left',
field: 'username',
sortable: true,
format: val => val ?? 'None',
classes: val => (val ? 'text-normal' : 'text-grey')
}
],
pagination: {
rowsPerPage: 10
}
Expand Down
27 changes: 8 additions & 19 deletions templates/lnurlp/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ <h5 class="text-subtitle1 q-my-none">Pay links</h5>
dense
flat
:rows="payLinks"
:columns="payLinksTable.columns"
row-key="id"
v-model:pagination="payLinksTable.pagination"
>
<template v-slot:header="props">
<q-tr class="text-left" :props="props">
<q-th auto-width></q-th>
<q-th auto-width>Description</q-th>
<q-th auto-width>Amount</q-th>
<q-th auto-width>Currency</q-th>
<q-th auto-width>Username</q-th>
<q-th v-for="col in props.cols" :key="col.name" :props="props">
<span v-text="col.label"></span>
</q-th>
<q-th auto-width></q-th>
<q-th auto-width></q-th>
</q-tr>
Expand Down Expand Up @@ -66,22 +66,11 @@ <h5 class="text-subtitle1 q-my-none">Pay links</h5>
><q-tooltip>View Link</q-tooltip></q-btn
>
</q-td>
<q-td auto-width v-text="props.row.description"></q-td>
<q-td auto-width>
<span
v-if="props.row.min == props.row.max"
v-text="props.row.min"
></span>
<span
v-else
v-text="props.row.min + ' - ' + props.row.max"
></span>
</q-td>
<q-td v-text="props.row.currency || 'sat'"></q-td>
<q-td
auto-width
:class="(props.row.username) ? 'text-normal' : 'text-grey'"
v-text="props.row.username || 'None'"
v-for="col in props.cols"
:key="col.name"
:props="props"
v-text="col.value"
></q-td>
<q-td>
<q-icon v-if="props.row.webhook_url" size="14px" name="http">
Expand Down