diff --git a/crud.py b/crud.py index aa45ecb..7829338 100644 --- a/crud.py +++ b/crud.py @@ -1,4 +1,5 @@ from datetime import datetime +from time import time from typing import List, Optional, Tuple import shortuuid @@ -33,9 +34,10 @@ async def create_withdraw_link( webhook_url, webhook_headers, webhook_body, - custom_url + custom_url, + created_at ) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) """, ( link_id, @@ -54,6 +56,7 @@ async def create_withdraw_link( data.webhook_headers, data.webhook_body, data.custom_url, + int(time()), ), ) link = await get_withdraw_link(link_id, 0) diff --git a/migrations.py b/migrations.py index 95805ae..ad515f5 100644 --- a/migrations.py +++ b/migrations.py @@ -1,3 +1,6 @@ +from time import time + + async def m001_initial(db): """ Creates an improved withdraw table and migrates the existing data. @@ -132,3 +135,17 @@ async def m006_webhook_headers_and_body(db): "ALTER TABLE withdraw.withdraw_link ADD COLUMN webhook_headers TEXT;" ) await db.execute("ALTER TABLE withdraw.withdraw_link ADD COLUMN webhook_body TEXT;") + + +async def m007_add_created_at_timestamp(db): + await db.execute( + "ALTER TABLE withdraw.withdraw_link " + f"ADD COLUMN created_at TIMESTAMP DEFAULT {db.timestamp_column_default}" + ) + # Set created_at to current time for all existing rows + await db.execute( + f""" + UPDATE withdraw.withdraw_link SET created_at = {db.timestamp_placeholder} + """, + (int(time()),), + ) diff --git a/models.py b/models.py index 6e9c362..4a1937c 100644 --- a/models.py +++ b/models.py @@ -1,3 +1,5 @@ +import datetime + import shortuuid from fastapi import Query, Request from lnurl import Lnurl, LnurlWithdrawResponse @@ -21,6 +23,7 @@ class CreateWithdrawData(BaseModel): class WithdrawLink(BaseModel): id: str + created_at: datetime.datetime wallet: str = Query(None) title: str = Query(None) min_withdrawable: int = Query(0) diff --git a/static/js/index.js b/static/js/index.js index 46eea7c..79d9a43 100644 --- a/static/js/index.js +++ b/static/js/index.js @@ -11,10 +11,6 @@ var locationPath = [ var mapWithdrawLink = function (obj) { obj._data = _.clone(obj) - obj.date = Quasar.utils.date.formatDate( - new Date(obj.time * 1000), - 'YYYY-MM-DD HH:mm' - ) obj.min_fsat = new Intl.NumberFormat(LOCALE).format(obj.min_withdrawable) obj.max_fsat = new Intl.NumberFormat(LOCALE).format(obj.max_withdrawable) obj.uses_left = obj.uses - obj.used @@ -35,8 +31,17 @@ new Vue({ withdrawLinks: [], withdrawLinksTable: { columns: [ - {name: 'id', align: 'left', label: 'ID', field: 'id'}, {name: 'title', align: 'left', label: 'Title', field: 'title'}, + { + name: 'created_at', + align: 'left', + label: 'Created At', + field: 'created_at', + sortable: true, + format: function (val, row) { + return new Date(val).toLocaleString() + } + }, { name: 'wait_time', align: 'right', @@ -118,13 +123,13 @@ new Vue({ `/withdraw/api/v1/links?all_wallets=true&limit=${query.limit}&offset=${query.offset}`, this.g.user.wallets[0].inkey ) - .then(function (response) { - self.withdrawLinks = response.data.data.map(function (obj) { + .then(response => { + this.withdrawLinks = response.data.data.map(function (obj) { return mapWithdrawLink(obj) }) - self.withdrawLinksTable.pagination.rowsNumber = response.data.total + this.withdrawLinksTable.pagination.rowsNumber = response.data.total }) - .catch(function (error) { + .catch(error => { clearInterval(self.checker) LNbits.utils.notifyApiError(error) }) @@ -210,8 +215,6 @@ new Vue({ } }, updateWithdrawLink: function (wallet, data) { - var self = this - // Remove webhook info if toggle is set to false if (!data.has_webhook) { data.webhook_url = null @@ -227,26 +230,24 @@ new Vue({ data ) .then(response => { - self.withdrawLinks = _.reject(self.withdrawLinks, function (obj) { + this.withdrawLinks = _.reject(this.withdrawLinks, function (obj) { return obj.id === data.id }) - self.withdrawLinks.push(mapWithdrawLink(response.data)) - self.formDialog.show = false + this.withdrawLinks.push(mapWithdrawLink(response.data)) + this.formDialog.show = false this.closeFormDialog() }) - .catch(function (error) { + .catch(error => { LNbits.utils.notifyApiError(error) }) }, createWithdrawLink: function (wallet, data) { - var self = this - LNbits.api .request('POST', '/withdraw/api/v1/links', wallet.adminkey, data) .then(response => { - self.withdrawLinks.push(mapWithdrawLink(response.data)) - self.formDialog.show = false - self.simpleformDialog.show = false + this.withdrawLinks.push(mapWithdrawLink(response.data)) + this.formDialog.show = false + this.simpleformDialog.show = false this.closeFormDialog() }) .catch(function (error) { diff --git a/templates/withdraw/index.html b/templates/withdraw/index.html index dc165a0..b4dbc08 100644 --- a/templates/withdraw/index.html +++ b/templates/withdraw/index.html @@ -37,12 +37,12 @@ {% raw %} {% endraw %}