feat: prepare for merchant active/restoring state
This commit is contained in:
parent
16e15939d3
commit
b89d761b04
5 changed files with 63 additions and 7 deletions
47
crud.py
47
crud.py
|
|
@ -1,4 +1,5 @@
|
|||
import json
|
||||
import time
|
||||
from typing import List, Optional
|
||||
|
||||
from lnbits.helpers import urlsafe_short_hash
|
||||
|
|
@ -43,13 +44,25 @@ async def update_merchant(
|
|||
) -> Optional[Merchant]:
|
||||
await db.execute(
|
||||
f"""
|
||||
UPDATE nostrmarket.merchants SET meta = ?
|
||||
UPDATE nostrmarket.merchants SET meta = ?, time = {db.timestamp_now}
|
||||
WHERE id = ? AND user_id = ?
|
||||
""",
|
||||
(json.dumps(config.dict()), merchant_id, user_id),
|
||||
)
|
||||
return await get_merchant(user_id, merchant_id)
|
||||
|
||||
async def touch_merchant(
|
||||
user_id: str, merchant_id: str
|
||||
) -> Optional[Merchant]:
|
||||
await db.execute(
|
||||
f"""
|
||||
UPDATE nostrmarket.merchants SET time = {db.timestamp_now}
|
||||
WHERE id = ? AND user_id = ?
|
||||
""",
|
||||
(merchant_id, user_id),
|
||||
)
|
||||
return await get_merchant(user_id, merchant_id)
|
||||
|
||||
|
||||
async def get_merchant(user_id: str, merchant_id: str) -> Optional[Merchant]:
|
||||
row = await db.fetchone(
|
||||
|
|
@ -89,6 +102,22 @@ async def get_merchant_for_user(user_id: str) -> Optional[Merchant]:
|
|||
return Merchant.from_row(row) if row else None
|
||||
|
||||
|
||||
async def get_merchant_activity(merchant_id):
|
||||
|
||||
rows = await db.fetchall(
|
||||
"""
|
||||
SELECT time from nostrmarket.direct_messages WHERE merchant_id = ?
|
||||
ORDER by time DESC
|
||||
""",
|
||||
(merchant_id,),
|
||||
)
|
||||
|
||||
# row = await db.fetchone("SELECT now()")
|
||||
|
||||
print("### time", time.time())
|
||||
print("### rows", rows)
|
||||
return rows
|
||||
|
||||
async def delete_merchant(merchant_id: str) -> None:
|
||||
await db.execute(
|
||||
"DELETE FROM nostrmarket.merchants WHERE id = ?",
|
||||
|
|
@ -645,10 +674,19 @@ async def get_orders_from_direct_messages(merchant_id: str) -> List[DirectMessag
|
|||
return [DirectMessage.from_row(row) for row in rows]
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
async def get_last_direct_messages_time(merchant_id: str) -> int:
|
||||
row = await db.fetchone(
|
||||
"""
|
||||
SELECT time FROM nostrmarket.direct_messages
|
||||
WHERE merchant_id = ? ORDER BY time DESC LIMIT 1
|
||||
""",
|
||||
(merchant_id,),
|
||||
)
|
||||
return row[0] if row else 0
|
||||
|
||||
|
||||
|
||||
async def get_last_direct_messages_created_at(merchant_id: str) -> int:
|
||||
row = await db.fetchone(
|
||||
"""
|
||||
SELECT event_created_at FROM nostrmarket.direct_messages
|
||||
|
|
@ -659,6 +697,7 @@ async def get_last_direct_messages_time(merchant_id: str) -> int:
|
|||
return row[0] if row else 0
|
||||
|
||||
|
||||
|
||||
async def delete_merchant_direct_messages(merchant_id: str) -> None:
|
||||
await db.execute(
|
||||
"DELETE FROM nostrmarket.direct_messages WHERE merchant_id = ?",
|
||||
|
|
|
|||
|
|
@ -168,3 +168,8 @@ async def m003_update_direct_message_type(db):
|
|||
await db.execute(
|
||||
"ALTER TABLE nostrmarket.direct_messages ADD COLUMN type INTEGER NOT NULL DEFAULT -1;"
|
||||
)
|
||||
|
||||
async def m004_add_merchant_timestamp(db):
|
||||
await db.execute(
|
||||
f"ALTER TABLE nostrmarket.merchants ADD COLUMN time TIMESTAMP NOT NULL DEFAULT {db.timestamp_now};"
|
||||
)
|
||||
|
|
@ -42,7 +42,8 @@ class MerchantProfile(BaseModel):
|
|||
class MerchantConfig(MerchantProfile):
|
||||
event_id: Optional[str]
|
||||
sync_from_nostr = False
|
||||
|
||||
active: bool = False
|
||||
restore_in_progress: Optional[bool] = False
|
||||
|
||||
class PartialMerchant(BaseModel):
|
||||
private_key: str
|
||||
|
|
@ -52,6 +53,7 @@ class PartialMerchant(BaseModel):
|
|||
|
||||
class Merchant(PartialMerchant, Nostrable):
|
||||
id: str
|
||||
time: Optional[int]
|
||||
|
||||
def sign_hash(self, hash: bytes) -> str:
|
||||
return sign_message_hash(self.private_key, hash)
|
||||
|
|
|
|||
4
tasks.py
4
tasks.py
|
|
@ -5,7 +5,7 @@ from lnbits.tasks import register_invoice_listener
|
|||
|
||||
from .crud import (
|
||||
get_all_unique_customers,
|
||||
get_last_direct_messages_time,
|
||||
get_last_direct_messages_created_at,
|
||||
get_last_order_time,
|
||||
get_last_product_update_time,
|
||||
get_last_stall_update_time,
|
||||
|
|
@ -40,7 +40,7 @@ async def wait_for_nostr_events(nostr_client: NostrClient):
|
|||
merchant_ids = await get_merchants_ids_with_pubkeys()
|
||||
for id, pk in merchant_ids:
|
||||
last_order_time = await get_last_order_time(id)
|
||||
last_dm_time = await get_last_direct_messages_time(id)
|
||||
last_dm_time = await get_last_direct_messages_created_at(id)
|
||||
since = max(last_order_time, last_dm_time)
|
||||
|
||||
await nostr_client.subscribe_to_direct_messages(pk, since)
|
||||
|
|
|
|||
10
views_api.py
10
views_api.py
|
|
@ -35,6 +35,7 @@ from .crud import (
|
|||
get_customer,
|
||||
get_customers,
|
||||
get_direct_messages,
|
||||
get_last_direct_messages_time,
|
||||
get_merchant_by_pubkey,
|
||||
get_merchant_for_user,
|
||||
get_merchants_ids_with_pubkeys,
|
||||
|
|
@ -48,6 +49,7 @@ from .crud import (
|
|||
get_stalls,
|
||||
get_zone,
|
||||
get_zones,
|
||||
touch_merchant,
|
||||
update_customer_no_unread_messages,
|
||||
update_merchant,
|
||||
update_order_shipped_status,
|
||||
|
|
@ -132,6 +134,14 @@ async def api_get_merchant(
|
|||
|
||||
try:
|
||||
merchant = await get_merchant_for_user(wallet.wallet.user)
|
||||
if not merchant:
|
||||
return
|
||||
|
||||
merchant = await touch_merchant(wallet.wallet.user, merchant.id)
|
||||
last_dm_time = await get_last_direct_messages_time(merchant.id)
|
||||
|
||||
merchant.config.restore_in_progress = (merchant.time - last_dm_time) < 60
|
||||
|
||||
return merchant
|
||||
except Exception as ex:
|
||||
logger.warning(ex)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue