Skip to content

Commit

Permalink
refactor: finish refactor (#2)
Browse files Browse the repository at this point in the history
* finish refactor
* offline payments
* lnpos needs fiat currency
  • Loading branch information
dni authored Nov 27, 2024
1 parent 84763cd commit 7a7b69c
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 383 deletions.
14 changes: 5 additions & 9 deletions crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,15 @@

async def create_lnpos(data: CreateLnpos) -> Lnpos:

if data.device == "pos" or data.device == "atm":
lnpos_id = shortuuid.uuid()[:5]
else:
lnpos_id = urlsafe_short_hash()

lnpos_id = shortuuid.uuid()[:5]
lnpos_key = urlsafe_short_hash()

device = Lnpos(
id=lnpos_id,
key=lnpos_key,
title=data.title,
wallet=data.wallet,
profit=data.profit,
currency=data.currency,
device=data.device,
profit=data.profit or 0.0,
currency=data.currency or "sat",
)

await db.insert("lnpos.lnpos", device)
Expand Down Expand Up @@ -80,6 +74,7 @@ async def get_lnpos_payment(
return await db.fetchone(
"SELECT * FROM lnpos.lnpos_payment WHERE id = :id",
{"id": lnpos_payment_id},
LnposPayment,
)


Expand All @@ -104,6 +99,7 @@ async def get_lnpos_payment_by_payhash(
return await db.fetchone(
"SELECT * FROM lnpos.lnpos_payment WHERE payhash = :payhash",
{"payhash": payhash},
LnposPayment,
)


Expand Down
1 change: 0 additions & 1 deletion migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ async def m001_initial(db):
title TEXT NOT NULL,
wallet TEXT NOT NULL,
currency TEXT NOT NULL,
device TEXT NOT NULL,
profit FLOAT NOT NULL,
timestamp TIMESTAMP NOT NULL DEFAULT {db.timestamp_now}
);
Expand Down
11 changes: 3 additions & 8 deletions models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
from typing import Optional

from lnurl.types import LnurlPayMetadata
from pydantic import BaseModel
Expand All @@ -7,9 +8,8 @@
class CreateLnpos(BaseModel):
title: str
wallet: str
currency: str
device: str
profit: float
currency: Optional[str] = "sat"
profit: Optional[float] = None


class Lnpos(BaseModel):
Expand All @@ -19,7 +19,6 @@ class Lnpos(BaseModel):
wallet: str
profit: float
currency: str
device: str

@property
def lnurlpay_metadata(self) -> LnurlPayMetadata:
Expand All @@ -33,7 +32,3 @@ class LnposPayment(BaseModel):
payload: str
pin: int
sats: int


class Lnurlencode(BaseModel):
url: str
154 changes: 56 additions & 98 deletions static/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ window.app = Vue.createApp({
location: window.location.hostname,
filter: '',
currency: 'USD',
lnurlValue: '',
deviceString: '',
lnposs: [],
atmLinks: [],
lnpossObj: [],
lnposTable: {
columns: [
{
Expand Down Expand Up @@ -48,34 +46,20 @@ window.app = Vue.createApp({
rowsPerPage: 10
}
},
passedlnurldevice: {},
settingsDialog: {
show: false,
data: {}
},
formDialog: {
show: false,
data: {}
},
formDialog: {
show: false,
data: {
lnurl_toggle: false,
show_message: false,
show_ack: false,
show_price: 'None',
device: 'pos',
profit: 1,
amount: 1,
title: ''
}
}
}
},
methods: {
cancelLnpos() {
self.formDialog.show = false
self.clearFormDialog()
this.formDialog.show = false
this.clearFormDialog()
},
closeFormDialog() {
this.clearFormDialog()
Expand All @@ -84,106 +68,89 @@ window.app = Vue.createApp({
}
},
sendFormData() {
if (!self.formDialog.data.profit) {
self.formDialog.data.profit = 0
if (!this.formDialog.data.profit) {
this.formDialog.data.profit = 0
}
if (self.formDialog.data.id) {
this.updateLnpos(self.g.user.wallets[0].adminkey, self.formDialog.data)
if (this.formDialog.data.id) {
this.updateLnpos(this.g.user.wallets[0].adminkey, this.formDialog.data)
} else {
this.createLnpos(self.g.user.wallets[0].adminkey, self.formDialog.data)
this.createLnpos(this.g.user.wallets[0].adminkey, this.formDialog.data)
}
},

createLnpos: function (wallet, data) {
createLnpos(wallet, data) {
const updatedData = {}
for (const property in data) {
if (data[property]) {
updatedData[property] = data[property]
}
}
LNbits.api
.request('POST', '/lnurldevice/api/v1/lnurlpos', wallet, updatedData)
.then(function (response) {
self.lnposs.push(response.data)
self.formDialog.show = false
self.clearFormDialog()
})
.catch(function (error) {
LNbits.utils.notifyApiError(error)
.request('POST', '/lnpos/api/v1', wallet, updatedData)
.then(response => {
this.lnposs.push(response.data)
this.formDialog.show = false
this.clearFormDialog()
})
.catch(LNbits.utils.notifyApiError)
},
getLnposs() {
LNbits.api
.request(
'GET',
'/lnurldevice/api/v1/lnurlpos',
self.g.user.wallets[0].adminkey
)
.then(function (response) {
.request('GET', '/lnpos/api/v1', this.g.user.wallets[0].adminkey)
.then(response => {
if (response.data) {
self.lnposs = response.data.map(maplnurldevice)
this.lnposs = response.data
}
})
.catch(function (error) {
LNbits.utils.notifyApiError(error)
})
.catch(LNbits.utils.notifyApiError)
},
getLnpos: function (lnurldevice_id) {
getLnpos: lnpos_id => {
LNbits.api
.request(
'GET',
'/lnurldevice/api/v1/lnurlpos/' + lnurldevice_id,
self.g.user.wallets[0].adminkey
'/lnpos/api/v1/' + lnpos_id,
this.g.user.wallets[0].adminkey
)
.then(function (response) {
localStorage.setItem('lnurldevice', JSON.stringify(response.data))
localStorage.setItem('inkey', self.g.user.wallets[0].inkey)
})
.catch(function (error) {
LNbits.utils.notifyApiError(error)
.then(response => {
localStorage.setItem('lnpos', JSON.stringify(response.data))
localStorage.setItem('inkey', this.g.user.wallets[0].inkey)
})
.catch(LNbits.utils.notifyApiError)
},
deleteLnpos: function (lnurldeviceId) {
const link = _.findWhere(this.lnposs, {id: lnurldeviceId})
deleteLnpos(lnposId) {
LNbits.utils
.confirmDialog('Are you sure you want to delete this pay link?')
.onOk(function () {
.onOk(() => {
LNbits.api
.request(
'DELETE',
'/lnurldevice/api/v1/lnurlpos/' + lnurldeviceId,
self.g.user.wallets[0].adminkey
'/lnpos/api/v1/' + lnposId,
this.g.user.wallets[0].adminkey
)
.then(function (response) {
self.lnposs = _.reject(self.lnposs, function (obj) {
return obj.id === lnurldeviceId
.then(response => {
this.lnposs = _.reject(this.lnposs, obj => {
return obj.id === lnposId
})
})
.catch(function (error) {
LNbits.utils.notifyApiError(error)
})
.catch(LNbits.utils.notifyApiError)
})
},
openUpdateLnpos: function (lnurldeviceId) {
const lnurldevice = _.findWhere(this.lnposs, {
id: lnurldeviceId
openUpdateLnpos(lnposId) {
const lnpos = _.findWhere(this.lnposs, {
id: lnposId
})
self.formDialog.data = _.clone(lnurldevice._data)
if (lnurldevice.device == 'atm' && lnurldevice.extra == 'boltz') {
self.boltzToggleState = true
} else {
self.boltzToggleState = false
}
self.formDialog.show = true
this.formDialog.data = _.clone(lnpos)
this.formDialog.show = true
},
openSettings: function (lnurldeviceId) {
const lnurldevice = _.findWhere(this.lnposs, {
id: lnurldeviceId
openSettings(lnposId) {
const lnpos = _.findWhere(this.lnposs, {
id: lnposId
})
self.settingsDialog.data = _.clone(lnurldevice._data)
self.settingsDialog.show = true
this.deviceString = `${this.protocol}//${this.location}/lnpos/api/v1/lnurl/${lnpos.id},${lnpos.key},${lnpos.currency}`
this.settingsDialog.data = _.clone(lnpos)
this.settingsDialog.show = true
},
updateLnpos: function (wallet, data) {
updateLnpos(wallet, data) {
const updatedData = {}
for (const property in data) {
if (data[property]) {
Expand All @@ -192,23 +159,16 @@ window.app = Vue.createApp({
}

LNbits.api
.request(
'PUT',
'/lnurldevice/api/v1/lnurlpos/' + updatedData.id,
wallet,
updatedData
)
.then(function (response) {
self.lnposs = _.reject(self.lnposs, function (obj) {
.request('PUT', '/lnpos/api/v1/' + updatedData.id, wallet, updatedData)
.then(response => {
this.lnposs = _.reject(this.lnposs, obj => {
return obj.id === updatedData.id
})
self.lnposs.push(response.data)
self.formDialog.show = false
self.clearFormDialog()
})
.catch(function (error) {
LNbits.utils.notifyApiError(error)
this.lnposs.push(response.data)
this.formDialog.show = false
this.clearFormDialog()
})
.catch(LNbits.utils.notifyApiError)
},
clearFormDialog() {
this.formDialog.data = {
Expand All @@ -220,18 +180,16 @@ window.app = Vue.createApp({
}
},
exportCSV() {
LNbits.utils.exportCSV(self.lnposTable.columns, this.lnposs)
LNbits.utils.exportCSV(this.lnposTable.columns, this.lnposs)
}
},
created() {
this.getLnposs()
LNbits.api
.request('GET', '/api/v1/currencies')
.then(response => {
this.currency = ['sat', 'USD', ...response.data]
})
.catch(err => {
LNbits.utils.notifyApiError(err)
this.currency = response.data
})
.catch(LNbits.utils.notifyApiError)
}
})
19 changes: 8 additions & 11 deletions templates/lnpos/error.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,12 @@ <h3 class="q-my-none">LNURL-pay not paid</h3>
</q-card-section>
</q-card>
</div>

{% endblock %} {% block scripts %}

<script>
window.app = Vue.createApp({
el: '#vue',
mixins: [windowMixin]
})
</script>

{% endblock %}
</div>
{% endblock %} {% block scripts %}
<script>
window.app = Vue.createApp({
el: '#vue',
mixins: [windowMixin]
})
</script>
{% endblock %}
21 changes: 21 additions & 0 deletions templates/lnpos/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,27 @@ <h6 class="text-subtitle1 q-my-none">{{SITE_TITLE}} LNPoS Extension</h6>
</q-card>
</div>

<q-dialog
v-model="settingsDialog.show"
deviceition="top"
@hide="closeFormDialog"
>
<q-card class="q-pa-lg q-pt-xl lnbits__dialog-card">
<div class="text-h6">Device string</div>
<q-btn
dense
outline
unelevated
color="primary"
size="md"
@click="copyText(deviceString, 'Link copied to clipboard!')"
>
<span v-text="deviceString"></span>
<q-tooltip>Click to copy URL</q-tooltip>
</q-btn>
</q-card>
</q-dialog>

<q-dialog v-model="formDialog.show" deviceition="top" @hide="closeFormDialog">
<q-card class="q-pa-lg q-pt-xl lnbits__dialog-card">
<q-form @submit="sendFormData" class="q-gutter-md">
Expand Down
Loading

0 comments on commit 7a7b69c

Please sign in to comment.