recording card tapping
This commit is contained in:
parent
4fab2d3101
commit
3cb62d1899
5 changed files with 102 additions and 7 deletions
|
|
@ -3,7 +3,7 @@ from typing import List, Optional, Union
|
||||||
from lnbits.helpers import urlsafe_short_hash
|
from lnbits.helpers import urlsafe_short_hash
|
||||||
|
|
||||||
from . import db
|
from . import db
|
||||||
from .models import Card, CreateCardData
|
from .models import Card, CreateCardData, Hit
|
||||||
|
|
||||||
async def create_card(
|
async def create_card(
|
||||||
data: CreateCardData, wallet_id: str
|
data: CreateCardData, wallet_id: str
|
||||||
|
|
@ -34,9 +34,9 @@ async def create_card(
|
||||||
data.meta_key,
|
data.meta_key,
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
link = await get_card(card_id, 0)
|
card = await get_card(card_id, 0)
|
||||||
assert link, "Newly created card couldn't be retrieved"
|
assert card, "Newly created card couldn't be retrieved"
|
||||||
return link
|
return card
|
||||||
|
|
||||||
async def update_card(card_id: str, **kwargs) -> Optional[Card]:
|
async def update_card(card_id: str, **kwargs) -> Optional[Card]:
|
||||||
if "is_unique" in kwargs:
|
if "is_unique" in kwargs:
|
||||||
|
|
@ -89,3 +89,54 @@ async def update_card_counter(counter: int, id: str):
|
||||||
"UPDATE boltcards.cards SET counter = ? WHERE id = ?",
|
"UPDATE boltcards.cards SET counter = ? WHERE id = ?",
|
||||||
(counter, id),
|
(counter, id),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
async def get_hit(hit_id: str) -> Optional[Hit]:
|
||||||
|
row = await db.fetchone(
|
||||||
|
f"SELECT * FROM boltcards.hits WHERE id = ?", (hit_id)
|
||||||
|
)
|
||||||
|
if not row:
|
||||||
|
return None
|
||||||
|
|
||||||
|
hit = dict(**row)
|
||||||
|
|
||||||
|
return Hit.parse_obj(hit)
|
||||||
|
|
||||||
|
async def get_hits(wallet_ids: Union[str, List[str]]) -> List[Hit]:
|
||||||
|
|
||||||
|
cards = get_cards(wallet_ids)
|
||||||
|
|
||||||
|
q = ",".join(["?"] * len(cards))
|
||||||
|
rows = await db.fetchall(
|
||||||
|
f"SELECT * FROM boltcards.hits WHERE wallet IN ({q})", (*(card.card_id for card in cards),)
|
||||||
|
)
|
||||||
|
|
||||||
|
return [Card(**row) for row in rows]
|
||||||
|
|
||||||
|
async def create_hit(
|
||||||
|
card_id, ip, useragent, old_ctr, new_ctr
|
||||||
|
) -> Hit:
|
||||||
|
hit_id = urlsafe_short_hash()
|
||||||
|
await db.execute(
|
||||||
|
"""
|
||||||
|
INSERT INTO boltcards.hits (
|
||||||
|
id,
|
||||||
|
card_id,
|
||||||
|
ip,
|
||||||
|
useragent,
|
||||||
|
old_ctr,
|
||||||
|
new_ctr
|
||||||
|
)
|
||||||
|
VALUES (?, ?, ?, ?, ?, ?)
|
||||||
|
""",
|
||||||
|
(
|
||||||
|
hit_id,
|
||||||
|
card_id,
|
||||||
|
ip,
|
||||||
|
useragent,
|
||||||
|
old_ctr,
|
||||||
|
new_ctr,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
hit = await get_hit(hit_id)
|
||||||
|
assert hit, "Newly recorded hit couldn't be retrieved"
|
||||||
|
return hit
|
||||||
|
|
|
||||||
|
|
@ -18,3 +18,19 @@ async def m001_initial(db):
|
||||||
);
|
);
|
||||||
"""
|
"""
|
||||||
)
|
)
|
||||||
|
|
||||||
|
await db.execute(
|
||||||
|
"""
|
||||||
|
CREATE TABLE boltcards.hits (
|
||||||
|
id TEXT PRIMARY KEY,
|
||||||
|
card_id TEXT NOT NULL,
|
||||||
|
ip TEXT NOT NULL,
|
||||||
|
useragent TEXT,
|
||||||
|
old_ctr INT NOT NULL DEFAULT 0,
|
||||||
|
new_ctr INT NOT NULL DEFAULT 0,
|
||||||
|
time TIMESTAMP NOT NULL DEFAULT """
|
||||||
|
+ db.timestamp_now
|
||||||
|
+ """
|
||||||
|
);
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
|
|
||||||
|
|
@ -19,3 +19,21 @@ class CreateCardData(BaseModel):
|
||||||
withdraw: str = Query(...)
|
withdraw: str = Query(...)
|
||||||
file_key: str = Query(...)
|
file_key: str = Query(...)
|
||||||
meta_key: str = Query(...)
|
meta_key: str = Query(...)
|
||||||
|
|
||||||
|
class Hit(BaseModel):
|
||||||
|
id: str
|
||||||
|
card_id: str
|
||||||
|
ip: str
|
||||||
|
useragent: str
|
||||||
|
old_ctr: int
|
||||||
|
new_ctr: int
|
||||||
|
time: int
|
||||||
|
|
||||||
|
'''
|
||||||
|
class CreateHitData(BaseModel):
|
||||||
|
card_id: str = Query(...)
|
||||||
|
ip: str = Query(...)
|
||||||
|
useragent: str = Query(...)
|
||||||
|
old_ctr: int = Query(...)
|
||||||
|
new_ctr: int = Query(...)
|
||||||
|
'''
|
||||||
|
|
|
||||||
|
|
@ -368,6 +368,7 @@
|
||||||
if (this.g.user.wallets.length) {
|
if (this.g.user.wallets.length) {
|
||||||
this.getCards()
|
this.getCards()
|
||||||
this.getWithdraws()
|
this.getWithdraws()
|
||||||
|
this.getHits()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,7 @@ from lnbits.extensions.withdraw import get_withdraw_link
|
||||||
from . import boltcards_ext
|
from . import boltcards_ext
|
||||||
from .nxp424 import decryptSUN, getSunMAC
|
from .nxp424 import decryptSUN, getSunMAC
|
||||||
from .crud import (
|
from .crud import (
|
||||||
|
create_hit,
|
||||||
get_all_cards,
|
get_all_cards,
|
||||||
get_cards,
|
get_cards,
|
||||||
get_card,
|
get_card,
|
||||||
|
|
@ -43,7 +44,7 @@ async def api_cards(
|
||||||
@boltcards_ext.post("/api/v1/cards", status_code=HTTPStatus.CREATED)
|
@boltcards_ext.post("/api/v1/cards", status_code=HTTPStatus.CREATED)
|
||||||
@boltcards_ext.put("/api/v1/cards/{card_id}", status_code=HTTPStatus.OK)
|
@boltcards_ext.put("/api/v1/cards/{card_id}", status_code=HTTPStatus.OK)
|
||||||
async def api_link_create_or_update(
|
async def api_link_create_or_update(
|
||||||
req: Request,
|
# req: Request,
|
||||||
data: CreateCardData,
|
data: CreateCardData,
|
||||||
card_id: str = None,
|
card_id: str = None,
|
||||||
wallet: WalletTypeInfo = Depends(require_admin_key),
|
wallet: WalletTypeInfo = Depends(require_admin_key),
|
||||||
|
|
@ -157,5 +158,13 @@ async def api_scane(
|
||||||
|
|
||||||
await update_card_counter(counter_int, card.id)
|
await update_card_counter(counter_int, card.id)
|
||||||
|
|
||||||
|
ip = request.client.host
|
||||||
|
if request.headers['x-real-ip']:
|
||||||
|
ip = request.headers['x-real-ip']
|
||||||
|
elif request.headers['x-forwarded-for']:
|
||||||
|
ip = request.headers['x-forwarded-for']
|
||||||
|
|
||||||
|
await create_hit(card.id, ip, request.headers['user-agent'], card.counter, counter_int)
|
||||||
|
|
||||||
link = await get_withdraw_link(card.withdraw, 0)
|
link = await get_withdraw_link(card.withdraw, 0)
|
||||||
return link.lnurl_response(request)
|
return link.lnurl_response(request)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue