Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.

0 downloads

Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-object-server.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-object-server.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-object-server.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 signup: the person chooses the password and name on the confirm page 83c7aa6 2 weeks, 5 days ago History
wallets.py · 3.4 KB · 87 lines
1"""Pre-write hook for wallets: one code, one card.
2
3A gift card is a BEARER instrument. Whoever holds the code can spend the
4balance, and nothing else identifies it -- there is no account, no email
5and usually no name. That makes the code the only key, and a key that is
6not unique is not a key: two cards sharing one code means one customer
7spending another's money, with no evidence afterwards about which balance
8was meant. So this gate exists for exactly one reason, and refuses a
9create or an update that would give a second wallet a code somebody else
10already has.
11
12Schemas in this repo carry no uniqueness constraint, which is why the
13rule lives here rather than in wallets.json. That is the same division
14hook_wallet_entries draws: the schema describes shape, a hook decides
15what may be written.
16
17**Compared upper-cased and trimmed**, because the code is compared that
18way at redemption (object_promotions.normalize_code takes the identical
19view of promotion codes, for the identical reason). A gate that matched
20case-sensitively while the redeemer matched case-insensitively would let
21`gc-abc` and `GC-ABC` both be created and then let either be spent as the
22other -- the worst kind of near-miss, since both look fine in a list.
23
24**A blank code is not a collision.** Ordinary wallets have no code and
25there are many of them; uniqueness is a property of the codes that exist,
26not a requirement that every wallet have one.
27
28**Fails CLOSED when the collection cannot be read.** An unreadable wallet
29list means unknown codes, and issuing a card that might duplicate a live
30one is worse than refusing to issue it: a refused card is reissued in ten
31seconds, a duplicated one is discovered when somebody's balance is gone.
32Same direction hook_wallet_entries takes about an unreadable ledger.
33
34Nothing else is gated here. The balance is the entries' business
35(hook_wallet_entries), the kind is a label, and this hook deliberately
36does not decide who may open a wallet -- that is permissions'.
37"""
38
39import os
40
41import object_records
42
43
44def _base_dir():
45 return os.environ.get("DBBASIC_DATA_DIR", "data")
46
47
48def _text(value):
49 return str(value if value is not None else "").strip()
50
51
52def _code(value):
53 return _text(value).upper()
54
55
56def BEFORE_WRITE(request):
57 action = _text(request.get("action"))
58 if action not in ("create", "update"):
59 return None
60 record = request.get("record") or {}
61
62 code = _code(record.get("code"))
63 if not code:
64 return None # most wallets have none; that is fine
65
66 mine = _text(record.get("id"))
67 base = _base_dir()
68 try:
69 rows = object_records.read_collection_records("wallets", base_dir=base)
70 except Exception:
71 return {"error": ("The wallet list is unreadable, so this code cannot "
72 "be checked for a duplicate. Refusing rather than "
73 "issuing a card that might already exist."),
74 "status": 409}
75
76 for row in rows:
77 if _text(row.get("id")) == mine:
78 continue
79 if _code(row.get("code")) == code:
80 return {
81 "error": (f"Another wallet already carries the code {code}. A "
82 f"gift card is spent by whoever holds its code, so "
83 f"two cards with one code is one customer spending "
84 f"another's money -- issue a different code."),
85 "status": 409,
86 }
87 return None