Skip to content

Commit

Permalink
feat: add created_at time to links
Browse files Browse the repository at this point in the history
closes #16

format
  • Loading branch information
dni committed Aug 14, 2024
1 parent 4e6d61f commit b419fb0
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 31 deletions.
7 changes: 5 additions & 2 deletions crud.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from datetime import datetime
from time import time
from typing import List, Optional, Tuple

import shortuuid
Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand Down
17 changes: 17 additions & 0 deletions migrations.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from time import time


async def m001_initial(db):
"""
Creates an improved withdraw table and migrates the existing data.
Expand Down Expand Up @@ -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()),),
)
3 changes: 3 additions & 0 deletions models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import datetime

import shortuuid
from fastapi import Query, Request
from lnurl import Lnurl, LnurlWithdrawResponse
Expand All @@ -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)
Expand Down
41 changes: 21 additions & 20 deletions static/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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',
Expand Down Expand Up @@ -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)
})
Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand Down
18 changes: 9 additions & 9 deletions templates/withdraw/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ <h5 class="text-subtitle1 q-my-none">Withdraw links</h5>
{% raw %}
<template v-slot:header="props">
<q-tr :props="props">
<q-th auto-width></q-th>
<q-th auto-width></q-th>
<q-th v-for="col in props.cols" :key="col.name" :props="props">
{{ col.label }}
</q-th>
<q-th auto-width></q-th>
<q-th auto-width></q-th>
</q-tr>
</template>
<template v-slot:body="props">
Expand Down Expand Up @@ -92,14 +92,6 @@ <h5 class="text-subtitle1 q-my-none">Withdraw links</h5>
><q-tooltip> view LNURL </q-tooltip></q-btn
>
</q-td>
<q-td v-for="col in props.cols" :key="col.name" :props="props">
{{ col.value }}
</q-td>
<q-td>
<q-icon v-if="props.row.webhook_url" size="14px" name="http">
<q-tooltip>Webhook to {{ props.row.webhook_url}}</q-tooltip>
</q-icon>
</q-td>
<q-td auto-width>
<q-btn
flat
Expand All @@ -118,6 +110,14 @@ <h5 class="text-subtitle1 q-my-none">Withdraw links</h5>
color="pink"
></q-btn>
</q-td>
<q-td v-for="col in props.cols" :key="col.name" :props="props">
{{ col.value }}
</q-td>
<q-td>
<q-icon v-if="props.row.webhook_url" size="14px" name="http">
<q-tooltip>Webhook to {{ props.row.webhook_url}}</q-tooltip>
</q-icon>
</q-td>
</q-tr>
</template>
{% endraw %}
Expand Down

0 comments on commit b419fb0

Please sign in to comment.