feat: init merchant
This commit is contained in:
parent
2832ee928c
commit
99492b36c8
6 changed files with 164 additions and 8 deletions
42
crud.py
Normal file
42
crud.py
Normal file
|
|
@ -0,0 +1,42 @@
|
||||||
|
import json
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
from lnbits.helpers import urlsafe_short_hash
|
||||||
|
|
||||||
|
from . import db
|
||||||
|
from .models import Merchant, PartialMerchant
|
||||||
|
|
||||||
|
|
||||||
|
async def create_merchant(user_id: str, m: PartialMerchant) -> Merchant:
|
||||||
|
merchant_id = urlsafe_short_hash()
|
||||||
|
await db.execute(
|
||||||
|
"""
|
||||||
|
INSERT INTO nostrmarket.merchants (user_id, id, private_key, public_key, meta)
|
||||||
|
VALUES (?, ?, ?, ?, ?)
|
||||||
|
""",
|
||||||
|
(user_id, merchant_id, m.private_key, m.public_key, json.dumps(dict(m.config))),
|
||||||
|
)
|
||||||
|
merchant = await get_merchant(user_id, merchant_id)
|
||||||
|
assert merchant, "Created merchant cannot be retrieved"
|
||||||
|
return merchant
|
||||||
|
|
||||||
|
|
||||||
|
async def get_merchant(user_id: str, merchant_id: str) -> Optional[Merchant]:
|
||||||
|
row = await db.fetchone(
|
||||||
|
"""SELECT * FROM nostrmarket.merchants WHERE user_id = ? AND id = ?""",
|
||||||
|
(
|
||||||
|
user_id,
|
||||||
|
merchant_id,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
return Merchant.from_row(row) if row else None
|
||||||
|
|
||||||
|
|
||||||
|
async def get_merchant_for_user(user_id: str) -> Optional[Merchant]:
|
||||||
|
row = await db.fetchone(
|
||||||
|
"""SELECT * FROM nostrmarket.merchants WHERE user_id = ? """,
|
||||||
|
(user_id,),
|
||||||
|
)
|
||||||
|
|
||||||
|
return Merchant.from_row(row) if row else None
|
||||||
|
|
@ -7,9 +7,10 @@ async def m001_initial(db):
|
||||||
"""
|
"""
|
||||||
CREATE TABLE nostrmarket.merchants (
|
CREATE TABLE nostrmarket.merchants (
|
||||||
user_id TEXT NOT NULL,
|
user_id TEXT NOT NULL,
|
||||||
|
id TEXT PRIMARY KEY,
|
||||||
private_key TEXT NOT NULL,
|
private_key TEXT NOT NULL,
|
||||||
public_key TEXT NOT NULL,
|
public_key TEXT NOT NULL,
|
||||||
config TEXT NOT NULL
|
meta TEXT NOT NULL DEFAULT '{}'
|
||||||
);
|
);
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
|
|
|
||||||
25
models.py
Normal file
25
models.py
Normal file
|
|
@ -0,0 +1,25 @@
|
||||||
|
import json
|
||||||
|
from sqlite3 import Row
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
|
||||||
|
class MerchantConfig(BaseModel):
|
||||||
|
name: Optional[str]
|
||||||
|
|
||||||
|
|
||||||
|
class PartialMerchant(BaseModel):
|
||||||
|
private_key: str
|
||||||
|
public_key: str
|
||||||
|
config: MerchantConfig = MerchantConfig()
|
||||||
|
|
||||||
|
|
||||||
|
class Merchant(PartialMerchant):
|
||||||
|
id: str
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_row(cls, row: Row) -> "Merchant":
|
||||||
|
merchant = cls(**dict(row))
|
||||||
|
merchant.config = MerchantConfig(**json.loads(row["meta"]))
|
||||||
|
return merchant
|
||||||
|
|
@ -1,15 +1,52 @@
|
||||||
const stalls = async () => {
|
const merchant = async () => {
|
||||||
Vue.component(VueQrcode.name, VueQrcode)
|
Vue.component(VueQrcode.name, VueQrcode)
|
||||||
|
|
||||||
await stallDetails('static/components/stall-details/stall-details.html')
|
await stallDetails('static/components/stall-details/stall-details.html')
|
||||||
|
|
||||||
|
const nostr = window.NostrTools
|
||||||
|
|
||||||
new Vue({
|
new Vue({
|
||||||
el: '#vue',
|
el: '#vue',
|
||||||
mixins: [windowMixin],
|
mixins: [windowMixin],
|
||||||
data: function () {
|
data: function () {
|
||||||
return {}
|
return {
|
||||||
|
merchant: null
|
||||||
|
}
|
||||||
|
},
|
||||||
|
methods: {
|
||||||
|
generateKeys: async function () {
|
||||||
|
const privkey = nostr.generatePrivateKey()
|
||||||
|
const pubkey = nostr.getPublicKey(privkey)
|
||||||
|
|
||||||
|
const data = {private_key: privkey, public_key: pubkey, config: {}}
|
||||||
|
try {
|
||||||
|
const resp = await LNbits.api.request(
|
||||||
|
'POST',
|
||||||
|
'/nostrmarket/api/v1/merchant',
|
||||||
|
this.g.user.wallets[0].adminkey,
|
||||||
|
data
|
||||||
|
)
|
||||||
|
} catch (error) {
|
||||||
|
LNbits.utils.notifyApiError(error)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
getMerchant: async function () {
|
||||||
|
try {
|
||||||
|
const {data} = await LNbits.api.request(
|
||||||
|
'get',
|
||||||
|
'/nostrmarket/api/v1/merchant',
|
||||||
|
this.g.user.wallets[0].adminkey
|
||||||
|
)
|
||||||
|
this.merchant = data
|
||||||
|
} catch (error) {
|
||||||
|
LNbits.utils.notifyApiError(error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
created: async function () {
|
||||||
|
await this.getMerchant()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
stalls()
|
merchant()
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
%} {% block page %}
|
%} {% block page %}
|
||||||
<div class="row q-col-gutter-md">
|
<div class="row q-col-gutter-md">
|
||||||
<div class="col-12 col-md-7 q-gutter-y-md">
|
<div class="col-12 col-md-7 q-gutter-y-md">
|
||||||
<q-card>
|
<q-card v-if="!merchant">
|
||||||
<q-card-section>
|
<q-card-section>
|
||||||
<span class="text-h4">Wellcome to Nostr Market!</span><br />
|
<span class="text-h4">Wellcome to Nostr Market!</span><br />
|
||||||
In Nostr Market, merchant and customer communicate via NOSTR relays, so
|
In Nostr Market, merchant and customer communicate via NOSTR relays, so
|
||||||
|
|
@ -41,15 +41,14 @@
|
||||||
disabled
|
disabled
|
||||||
label="Import Key"
|
label="Import Key"
|
||||||
color="primary"
|
color="primary"
|
||||||
@click="importKey"
|
|
||||||
class="float-left"
|
class="float-left"
|
||||||
>
|
>
|
||||||
<q-tooltip> Use an existing private key (hex or npub) </q-tooltip>
|
<q-tooltip> Use an existing private key (hex or npub) </q-tooltip>
|
||||||
</q-btn>
|
</q-btn>
|
||||||
<q-btn
|
<q-btn
|
||||||
label="Create New Key"
|
label="Generate New Key"
|
||||||
color="green"
|
color="green"
|
||||||
@click="generateKey"
|
@click="generateKeys"
|
||||||
class="float-right"
|
class="float-right"
|
||||||
>
|
>
|
||||||
<q-tooltip> A new key pair will be generated for you </q-tooltip>
|
<q-tooltip> A new key pair will be generated for you </q-tooltip>
|
||||||
|
|
@ -58,6 +57,11 @@
|
||||||
</div>
|
</div>
|
||||||
</q-card-section>
|
</q-card-section>
|
||||||
</q-card>
|
</q-card>
|
||||||
|
<div v-else>
|
||||||
|
<q-card>
|
||||||
|
<q-card-section> Merchant Exists </q-card-section>
|
||||||
|
</q-card>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="col-12 col-md-5 q-gutter-y-md">
|
<div class="col-12 col-md-5 q-gutter-y-md">
|
||||||
|
|
@ -75,6 +79,8 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{% endblock%}{% block scripts %} {{ window_vars(user) }}
|
{% endblock%}{% block scripts %} {{ window_vars(user) }}
|
||||||
|
<script src="https://unpkg.com/nostr-tools/lib/nostr.bundle.js"></script>
|
||||||
|
|
||||||
<script src="{{ url_for('nostrmarket_static', path='js/utils.js') }}"></script>
|
<script src="{{ url_for('nostrmarket_static', path='js/utils.js') }}"></script>
|
||||||
<script src="{{ url_for('nostrmarket_static', path='components/stall-details/stall-details.js') }}"></script>
|
<script src="{{ url_for('nostrmarket_static', path='components/stall-details/stall-details.js') }}"></script>
|
||||||
<script src="{{ url_for('nostrmarket_static', path='js/index.js') }}"></script>
|
<script src="{{ url_for('nostrmarket_static', path='js/index.js') }}"></script>
|
||||||
|
|
|
||||||
45
views_api.py
45
views_api.py
|
|
@ -0,0 +1,45 @@
|
||||||
|
from http import HTTPStatus
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
from fastapi import Depends
|
||||||
|
from fastapi.exceptions import HTTPException
|
||||||
|
from loguru import logger
|
||||||
|
|
||||||
|
from lnbits.decorators import WalletTypeInfo, require_admin_key, require_invoice_key
|
||||||
|
|
||||||
|
from . import nostrmarket_ext
|
||||||
|
from .crud import create_merchant, get_merchant_for_user
|
||||||
|
from .models import Merchant, PartialMerchant
|
||||||
|
|
||||||
|
|
||||||
|
@nostrmarket_ext.post("/api/v1/merchant")
|
||||||
|
async def api_create_merchant(
|
||||||
|
data: PartialMerchant,
|
||||||
|
wallet: WalletTypeInfo = Depends(require_admin_key),
|
||||||
|
) -> Merchant:
|
||||||
|
|
||||||
|
try:
|
||||||
|
merchant = await create_merchant(wallet.wallet.user, data)
|
||||||
|
return merchant
|
||||||
|
except Exception as ex:
|
||||||
|
logger.warning(ex)
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=HTTPStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
detail="Cannot create merchant",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@nostrmarket_ext.get("/api/v1/merchant")
|
||||||
|
async def api_get_merchant(
|
||||||
|
wallet: WalletTypeInfo = Depends(require_invoice_key),
|
||||||
|
) -> Optional[Merchant]:
|
||||||
|
|
||||||
|
try:
|
||||||
|
merchant = await get_merchant_for_user(wallet.wallet.user)
|
||||||
|
return merchant
|
||||||
|
except Exception as ex:
|
||||||
|
logger.warning(ex)
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=HTTPStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
detail="Cannot create merchant",
|
||||||
|
)
|
||||||
Loading…
Add table
Add a link
Reference in a new issue