-
Notifications
You must be signed in to change notification settings - Fork 22
/
crud.py
937 lines (794 loc) · 27.3 KB
/
crud.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
import json
from typing import List, Optional, Tuple
from lnbits.helpers import urlsafe_short_hash
from . import db
from .models import (
Customer,
CustomerProfile,
DirectMessage,
Merchant,
MerchantConfig,
Order,
PartialDirectMessage,
PartialMerchant,
Product,
Stall,
Zone,
)
######################################## MERCHANT ######################################
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, :id, :private_key, :public_key, :meta)
""",
{
"user_id": user_id,
"id": merchant_id,
"private_key": m.private_key,
"public_key": m.public_key,
"meta": json.dumps(dict(m.config)),
},
)
merchant = await get_merchant(user_id, merchant_id)
assert merchant, "Created merchant cannot be retrieved"
return merchant
async def update_merchant(
user_id: str, merchant_id: str, config: MerchantConfig
) -> Optional[Merchant]:
await db.execute(
f"""
UPDATE nostrmarket.merchants SET meta = :meta, time = {db.timestamp_now}
WHERE id = :id AND user_id = :user_id
""",
{"meta": json.dumps(config.dict()), "id": merchant_id, "user_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 = :id AND user_id = :user_id
""",
{"id": merchant_id, "user_id": user_id},
)
return await get_merchant(user_id, merchant_id)
async def get_merchant(user_id: str, merchant_id: str) -> Optional[Merchant]:
row: dict = await db.fetchone(
"""SELECT * FROM nostrmarket.merchants WHERE user_id = :user_id AND id = :id""",
{
"user_id": user_id,
"id": merchant_id,
},
)
return Merchant.from_row(row) if row else None
async def get_merchant_by_pubkey(public_key: str) -> Optional[Merchant]:
row: dict = await db.fetchone(
"""SELECT * FROM nostrmarket.merchants WHERE public_key = :public_key""",
{"public_key": public_key},
)
return Merchant.from_row(row) if row else None
async def get_merchants_ids_with_pubkeys() -> List[Tuple[str, str]]:
rows: list[dict] = await db.fetchall(
"""SELECT id, public_key FROM nostrmarket.merchants""",
)
return [(row["id"], row["public_key"]) for row in rows]
async def get_merchant_for_user(user_id: str) -> Optional[Merchant]:
row: dict = await db.fetchone(
"""SELECT * FROM nostrmarket.merchants WHERE user_id = :user_id """,
{"user_id": user_id},
)
return Merchant.from_row(row) if row else None
async def delete_merchant(merchant_id: str) -> None:
await db.execute(
"DELETE FROM nostrmarket.merchants WHERE id = :id",
{
"id": merchant_id,
},
)
######################################## ZONES ########################################
async def create_zone(merchant_id: str, data: Zone) -> Zone:
zone_id = data.id or urlsafe_short_hash()
await db.execute(
"""
INSERT INTO nostrmarket.zones (id, merchant_id, name, currency, cost, regions)
VALUES (:id, :merchant_id, :name, :currency, :cost, :regions)
""",
{
"id": zone_id,
"merchant_id": merchant_id,
"name": data.name,
"currency": data.currency,
"cost": data.cost,
"regions": json.dumps(data.countries),
},
)
zone = await get_zone(merchant_id, zone_id)
assert zone, "Newly created zone couldn't be retrieved"
return zone
async def update_zone(merchant_id: str, z: Zone) -> Optional[Zone]:
await db.execute(
"""
UPDATE nostrmarket.zones
SET name = :name, cost = :cost, regions = :regions
WHERE id = :id AND merchant_id = :merchant_id
""",
{
"name": z.name,
"cost": z.cost,
"regions": json.dumps(z.countries),
"id": z.id,
"merchant_id": merchant_id,
},
)
assert z.id
return await get_zone(merchant_id, z.id)
async def get_zone(merchant_id: str, zone_id: str) -> Optional[Zone]:
row: dict = await db.fetchone(
"SELECT * FROM nostrmarket.zones WHERE merchant_id = :merchant_id AND id = :id",
{
"merchant_id": merchant_id,
"id": zone_id,
},
)
return Zone.from_row(row) if row else None
async def get_zones(merchant_id: str) -> List[Zone]:
rows: list[dict] = await db.fetchall(
"SELECT * FROM nostrmarket.zones WHERE merchant_id = :merchant_id",
{"merchant_id": merchant_id},
)
return [Zone.from_row(row) for row in rows]
async def delete_zone(merchant_id: str, zone_id: str) -> None:
await db.execute(
"DELETE FROM nostrmarket.zones WHERE merchant_id = :merchant_id AND id = :id",
{
"merchant_id": merchant_id,
"id": zone_id,
},
)
async def delete_merchant_zones(merchant_id: str) -> None:
await db.execute(
"DELETE FROM nostrmarket.zones WHERE merchant_id = ?",
{"merchant_id": merchant_id},
)
######################################## STALL ########################################
async def create_stall(merchant_id: str, data: Stall) -> Stall:
stall_id = data.id or urlsafe_short_hash()
await db.execute(
"""
INSERT INTO nostrmarket.stalls
(
merchant_id, id, wallet, name, currency,
pending, event_id, event_created_at, zones, meta
)
VALUES
(
:merchant_id, :id, :wallet, :name, :currency,
:pending, :event_id, :event_created_at, :zones, :meta
)
ON CONFLICT(id) DO NOTHING
""",
{
"merchant_id": merchant_id,
"id": stall_id,
"wallet": data.wallet,
"name": data.name,
"currency": data.currency,
"pending": data.pending,
"event_id": data.event_id,
"event_created_at": data.event_created_at,
"zones": json.dumps(
[z.dict() for z in data.shipping_zones]
), # todo: cost is float. should be int for sats
"meta": json.dumps(data.config.dict()),
},
)
stall = await get_stall(merchant_id, stall_id)
assert stall, f"Newly created stall couldn't be retrieved. Id: {stall_id}"
return stall
async def get_stall(merchant_id: str, stall_id: str) -> Optional[Stall]:
row: dict = await db.fetchone(
"""
SELECT * FROM nostrmarket.stalls
WHERE merchant_id = :merchant_id AND id = :id
""",
{
"merchant_id": merchant_id,
"id": stall_id,
},
)
return Stall.from_row(row) if row else None
async def get_stalls(merchant_id: str, pending: Optional[bool] = False) -> List[Stall]:
rows: list[dict] = await db.fetchall(
"""
SELECT * FROM nostrmarket.stalls
WHERE merchant_id = :merchant_id AND pending = :pending
""",
{
"merchant_id": merchant_id,
"pending": pending,
},
)
return [Stall.from_row(row) for row in rows]
async def get_last_stall_update_time() -> int:
row: dict = await db.fetchone(
"""
SELECT event_created_at FROM nostrmarket.stalls
ORDER BY event_created_at DESC LIMIT 1
""",
{},
)
return row["event_created_at"] or 0 if row else 0
async def update_stall(merchant_id: str, stall: Stall) -> Optional[Stall]:
await db.execute(
"""
UPDATE nostrmarket.stalls
SET wallet = :wallet, name = :name, currency = :currency,
pending = :pending, event_id = :event_id,
event_created_at = :event_created_at,
zones = :zones, meta = :meta
WHERE merchant_id = :merchant_id AND id = :id
""",
{
"wallet": stall.wallet,
"name": stall.name,
"currency": stall.currency,
"pending": stall.pending,
"event_id": stall.event_id,
"event_created_at": stall.event_created_at,
"zones": json.dumps(
[z.dict() for z in stall.shipping_zones]
), # todo: cost is float. should be int for sats
"meta": json.dumps(stall.config.dict()),
"merchant_id": merchant_id,
"id": stall.id,
},
)
assert stall.id
return await get_stall(merchant_id, stall.id)
async def delete_stall(merchant_id: str, stall_id: str) -> None:
await db.execute(
"""
DELETE FROM nostrmarket.stalls
WHERE merchant_id = :merchant_id AND id = :id
""",
{
"merchant_id": merchant_id,
"id": stall_id,
},
)
async def delete_merchant_stalls(merchant_id: str) -> None:
await db.execute(
"DELETE FROM nostrmarket.stalls WHERE merchant_id = :merchant_id",
{"merchant_id": merchant_id},
)
######################################## PRODUCTS ######################################
async def create_product(merchant_id: str, data: Product) -> Product:
product_id = data.id or urlsafe_short_hash()
await db.execute(
"""
INSERT INTO nostrmarket.products
(
merchant_id, id, stall_id, name, price, quantity,
active, pending, event_id, event_created_at,
image_urls, category_list, meta
)
VALUES (
:merchant_id, :id, :stall_id, :name, :price, :quantity,
:active, :pending, :event_id, :event_created_at,
:image_urls, :category_list, :meta
)
ON CONFLICT(id) DO NOTHING
""",
{
"merchant_id": merchant_id,
"id": product_id,
"stall_id": data.stall_id,
"name": data.name,
"price": data.price,
"quantity": data.quantity,
"active": data.active,
"pending": data.pending,
"event_id": data.event_id,
"event_created_at": data.event_created_at,
"image_urls": json.dumps(data.images),
"category_list": json.dumps(data.categories),
"meta": json.dumps(data.config.dict()),
},
)
product = await get_product(merchant_id, product_id)
assert product, "Newly created product couldn't be retrieved"
return product
async def update_product(merchant_id: str, product: Product) -> Product:
assert product.id
await db.execute(
"""
UPDATE nostrmarket.products
SET name = :name, price = :price, quantity = :quantity,
active = :active, pending = :pending, event_id =:event_id,
event_created_at = :event_created_at, image_urls = :image_urls,
category_list = :category_list, meta = :meta
WHERE merchant_id = :merchant_id AND id = :id
""",
{
"name": product.name,
"price": product.price,
"quantity": product.quantity,
"active": product.active,
"pending": product.pending,
"event_id": product.event_id,
"event_created_at": product.event_created_at,
"image_urls": json.dumps(product.images),
"category_list": json.dumps(product.categories),
"meta": json.dumps(product.config.dict()),
"merchant_id": merchant_id,
"id": product.id,
},
)
updated_product = await get_product(merchant_id, product.id)
assert updated_product, "Updated product couldn't be retrieved"
return updated_product
async def update_product_quantity(
product_id: str, new_quantity: int
) -> Optional[Product]:
await db.execute(
"""
UPDATE nostrmarket.products SET quantity = :quantity
WHERE id = :id
""",
{"quantity": new_quantity, "id": product_id},
)
row: dict = await db.fetchone(
"SELECT * FROM nostrmarket.products WHERE id = :id",
{"id": product_id},
)
return Product.from_row(row) if row else None
async def get_product(merchant_id: str, product_id: str) -> Optional[Product]:
row: dict = await db.fetchone(
"""
SELECT * FROM nostrmarket.products
WHERE merchant_id = :merchant_id AND id = :id
""",
{
"merchant_id": merchant_id,
"id": product_id,
},
)
# TODO: remove from_row
return Product.from_row(row) if row else None
async def get_products(
merchant_id: str, stall_id: str, pending: Optional[bool] = False
) -> List[Product]:
rows: list[dict] = await db.fetchall(
"""
SELECT * FROM nostrmarket.products
WHERE merchant_id = :merchant_id
AND stall_id = :stall_id AND pending = :pending
""",
{"merchant_id": merchant_id, "stall_id": stall_id, "pending": pending},
)
return [Product.from_row(row) for row in rows]
async def get_products_by_ids(
merchant_id: str, product_ids: List[str]
) -> List[Product]:
# todo: revisit
keys = []
values = {"merchant_id": merchant_id}
for i, v in enumerate(product_ids):
key = f"p_{i}"
values[key] = v
keys.append(f":{key}")
rows: list[dict] = await db.fetchall(
f"""
SELECT id, stall_id, name, price, quantity, active, category_list, meta
FROM nostrmarket.products
WHERE merchant_id = :merchant_id
AND pending = false AND id IN ({", ".join(keys)})
""",
values,
)
return [Product.from_row(row) for row in rows]
async def get_wallet_for_product(product_id: str) -> Optional[str]:
row: dict = await db.fetchone(
"""
SELECT s.wallet as wallet FROM nostrmarket.products p
INNER JOIN nostrmarket.stalls s
ON p.stall_id = s.id
WHERE p.id = :product_id AND p.pending = false AND s.pending = false
""",
{"product_id": product_id},
)
return row["wallet"] if row else None
async def get_last_product_update_time() -> int:
row: dict = await db.fetchone(
"""
SELECT event_created_at FROM nostrmarket.products
ORDER BY event_created_at DESC LIMIT 1
""",
{},
)
return row["event_created_at"] or 0 if row else 0
async def delete_product(merchant_id: str, product_id: str) -> None:
await db.execute(
"""
DELETE FROM nostrmarket.products
WHERE merchant_id = :merchant_id AND id = :id
""",
{
"merchant_id": merchant_id,
"id": product_id,
},
)
async def delete_merchant_products(merchant_id: str) -> None:
await db.execute(
"DELETE FROM nostrmarket.products WHERE merchant_id = :merchant_id",
{"merchant_id": merchant_id},
)
######################################## ORDERS ########################################
async def create_order(merchant_id: str, o: Order) -> Order:
await db.execute(
"""
INSERT INTO nostrmarket.orders (
merchant_id,
id,
event_id,
event_created_at,
merchant_public_key,
public_key,
address,
contact_data,
extra_data,
order_items,
shipping_id,
stall_id,
invoice_id,
total
)
VALUES (
:merchant_id,
:id,
:event_id,
:event_created_at,
:merchant_public_key,
:public_key,
:address,
:contact_data,
:extra_data,
:order_items,
:shipping_id,
:stall_id,
:invoice_id,
:total
)
ON CONFLICT(event_id) DO NOTHING
""",
{
"merchant_id": merchant_id,
"id": o.id,
"event_id": o.event_id,
"event_created_at": o.event_created_at,
"merchant_public_key": o.merchant_public_key,
"public_key": o.public_key,
"address": o.address,
"contact_data": json.dumps(o.contact.dict() if o.contact else {}),
"extra_data": json.dumps(o.extra.dict()),
"order_items": json.dumps([i.dict() for i in o.items]),
"shipping_id": o.shipping_id,
"stall_id": o.stall_id,
"invoice_id": o.invoice_id,
"total": o.total,
},
)
order = await get_order(merchant_id, o.id)
assert order, "Newly created order couldn't be retrieved"
return order
async def get_order(merchant_id: str, order_id: str) -> Optional[Order]:
row: dict = await db.fetchone(
"""
SELECT * FROM nostrmarket.orders
WHERE merchant_id = :merchant_id AND id = :id
""",
{
"merchant_id": merchant_id,
"id": order_id,
},
)
return Order.from_row(row) if row else None
async def get_order_by_event_id(merchant_id: str, event_id: str) -> Optional[Order]:
row: dict = await db.fetchone(
"""
SELECT * FROM nostrmarket.orders
WHERE merchant_id = :merchant_id AND event_id = :event_id
""",
{
"merchant_id": merchant_id,
"event_id": event_id,
},
)
return Order.from_row(row) if row else None
async def get_orders(merchant_id: str, **kwargs) -> List[Order]:
q = " AND ".join(
[
f"{field[0]} = :{field[0]}"
for field in kwargs.items()
if field[1] is not None
]
)
values = {"merchant_id": merchant_id}
for field in kwargs.items():
if field[1] is None:
continue
values[field[0]] = field[1]
rows: list[dict] = await db.fetchall(
f"""
SELECT * FROM nostrmarket.orders
WHERE merchant_id = :merchant_id {q}
ORDER BY event_created_at DESC
""",
values,
)
return [Order.from_row(row) for row in rows]
async def get_orders_for_stall(
merchant_id: str, stall_id: str, **kwargs
) -> List[Order]:
q = " AND ".join(
[
f"{field[0]} = :{field[0]}"
for field in kwargs.items()
if field[1] is not None
]
)
values = {"merchant_id": merchant_id, "stall_id": stall_id}
for field in kwargs.items():
if field[1] is None:
continue
values[field[0]] = field[1]
rows: list[dict] = await db.fetchall(
f"""
SELECT * FROM nostrmarket.orders
WHERE merchant_id = :merchant_id AND stall_id = :stall_id {q}
ORDER BY time DESC
""",
values,
)
return [Order.from_row(row) for row in rows]
async def update_order(merchant_id: str, order_id: str, **kwargs) -> Optional[Order]:
q = ", ".join(
[
f"{field[0]} = :{field[0]}"
for field in kwargs.items()
if field[1] is not None
]
)
values = {"merchant_id": merchant_id, "id": order_id}
for field in kwargs.items():
if field[1] is None:
continue
values[field[0]] = field[1]
await db.execute(
f"""
UPDATE nostrmarket.orders
SET {q} WHERE merchant_id = :merchant_id and id = :id
""",
values,
)
return await get_order(merchant_id, order_id)
async def update_order_paid_status(order_id: str, paid: bool) -> Optional[Order]:
await db.execute(
"UPDATE nostrmarket.orders SET paid = :paid WHERE id = :id",
{"paid": paid, "id": order_id},
)
row: dict = await db.fetchone(
"SELECT * FROM nostrmarket.orders WHERE id = :id",
{"id": order_id},
)
return Order.from_row(row) if row else None
async def update_order_shipped_status(
merchant_id: str, order_id: str, shipped: bool
) -> Optional[Order]:
await db.execute(
"""
UPDATE nostrmarket.orders
SET shipped = :shipped
WHERE merchant_id = :merchant_id AND id = :id
""",
{"shipped": shipped, "merchant_id": merchant_id, "id": order_id},
)
row: dict = await db.fetchone(
"SELECT * FROM nostrmarket.orders WHERE id = :id",
{"id": order_id},
)
return Order.from_row(row) if row else None
async def delete_merchant_orders(merchant_id: str) -> None:
await db.execute(
"DELETE FROM nostrmarket.orders WHERE merchant_id = :merchant_id",
{"merchant_id": merchant_id},
)
######################################## MESSAGES ######################################
async def create_direct_message(
merchant_id: str, dm: PartialDirectMessage
) -> DirectMessage:
dm_id = urlsafe_short_hash()
await db.execute(
"""
INSERT INTO nostrmarket.direct_messages
(
merchant_id, id, event_id, event_created_at,
message, public_key, type, incoming
)
VALUES
(
:merchant_id, :id, :event_id, :event_created_at,
:message, :public_key, :type, :incoming
)
ON CONFLICT(event_id) DO NOTHING
""",
{
"merchant_id": merchant_id,
"id": dm_id,
"event_id": dm.event_id,
"event_created_at": dm.event_created_at,
"message": dm.message,
"public_key": dm.public_key,
"type": dm.type,
"incoming": dm.incoming,
},
)
if dm.event_id:
msg = await get_direct_message_by_event_id(merchant_id, dm.event_id)
else:
msg = await get_direct_message(merchant_id, dm_id)
assert msg, "Newly created dm couldn't be retrieved"
return msg
async def get_direct_message(merchant_id: str, dm_id: str) -> Optional[DirectMessage]:
row: dict = await db.fetchone(
"""
SELECT * FROM nostrmarket.direct_messages
WHERE merchant_id = :merchant_id AND id = :id
""",
{
"merchant_id": merchant_id,
"id": dm_id,
},
)
return DirectMessage.from_row(row) if row else None
async def get_direct_message_by_event_id(
merchant_id: str, event_id: str
) -> Optional[DirectMessage]:
row: dict = await db.fetchone(
"""
SELECT * FROM nostrmarket.direct_messages
WHERE merchant_id = :merchant_id AND event_id = :event_id
""",
{
"merchant_id": merchant_id,
"event_id": event_id,
},
)
return DirectMessage.from_row(row) if row else None
async def get_direct_messages(merchant_id: str, public_key: str) -> List[DirectMessage]:
rows: list[dict] = await db.fetchall(
"""
SELECT * FROM nostrmarket.direct_messages
WHERE merchant_id = :merchant_id AND public_key = :public_key
ORDER BY event_created_at
""",
{"merchant_id": merchant_id, "public_key": public_key},
)
return [DirectMessage.from_row(row) for row in rows]
async def get_orders_from_direct_messages(merchant_id: str) -> List[DirectMessage]:
rows: list[dict] = await db.fetchall(
"""
SELECT * FROM nostrmarket.direct_messages
WHERE merchant_id = :merchant_id AND type >= 0 ORDER BY event_created_at, type
""",
{"merchant_id": merchant_id},
)
return [DirectMessage.from_row(row) for row in rows]
async def get_last_direct_messages_time(merchant_id: str) -> int:
row: dict = await db.fetchone(
"""
SELECT time FROM nostrmarket.direct_messages
WHERE merchant_id = :merchant_id ORDER BY time DESC LIMIT 1
""",
{"merchant_id": merchant_id},
)
return row["time"] if row else 0
async def get_last_direct_messages_created_at() -> int:
row: dict = await db.fetchone(
"""
SELECT event_created_at FROM nostrmarket.direct_messages
ORDER BY event_created_at DESC LIMIT 1
""",
{},
)
return row["event_created_at"] 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 = :merchant_id",
{"merchant_id": merchant_id},
)
######################################## CUSTOMERS #####################################
async def create_customer(merchant_id: str, data: Customer) -> Customer:
await db.execute(
"""
INSERT INTO nostrmarket.customers (merchant_id, public_key, meta)
VALUES (:merchant_id, :public_key, :meta)
""",
{
"merchant_id": merchant_id,
"public_key": data.public_key,
"meta": json.dumps(data.profile) if data.profile else "{}",
},
)
customer = await get_customer(merchant_id, data.public_key)
assert customer, "Newly created customer couldn't be retrieved"
return customer
async def get_customer(merchant_id: str, public_key: str) -> Optional[Customer]:
row: dict = await db.fetchone(
"""
SELECT * FROM nostrmarket.customers
WHERE merchant_id = :merchant_id AND public_key = :public_key
""",
{
"merchant_id": merchant_id,
"public_key": public_key,
},
)
return Customer.from_row(row) if row else None
async def get_customers(merchant_id: str) -> List[Customer]:
rows: list[dict] = await db.fetchall(
"SELECT * FROM nostrmarket.customers WHERE merchant_id = :merchant_id",
{"merchant_id": merchant_id},
)
return [Customer.from_row(row) for row in rows]
async def get_all_unique_customers() -> List[Customer]:
q = """
SELECT public_key, MAX(merchant_id) as merchant_id, MAX(event_created_at)
FROM nostrmarket.customers
GROUP BY public_key
"""
rows: list[dict] = await db.fetchall(q)
return [Customer.from_row(row) for row in rows]
async def update_customer_profile(
public_key: str, event_created_at: int, profile: CustomerProfile
):
await db.execute(
"""
UPDATE nostrmarket.customers
SET event_created_at = :event_created_at, meta = :meta
WHERE public_key = :public_key
""",
{
"event_created_at": event_created_at,
"meta": json.dumps(profile.dict()),
"public_key": public_key,
},
)
async def increment_customer_unread_messages(merchant_id: str, public_key: str):
await db.execute(
"""
UPDATE nostrmarket.customers
SET unread_messages = unread_messages + 1
WHERE merchant_id = :merchant_id AND public_key = :public_key
""",
{
"merchant_id": merchant_id,
"public_key": public_key,
},
)
# ??? two merchants
async def update_customer_no_unread_messages(merchant_id: str, public_key: str):
await db.execute(
"""
UPDATE nostrmarket.customers
SET unread_messages = 0
WHERE merchant_id = :merchant_id AND public_key = :public_key
""",
{
"merchant_id": merchant_id,
"public_key": public_key,
},
)