askrobots / dbbasic-object-server
Public
Runs live, versioned Python application objects on one VM, with a suite of apps: projects, notes, tasks, files and more.
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 | |
| 3 | A gift card is a BEARER instrument. Whoever holds the code can spend the |
| 4 | balance, and nothing else identifies it -- there is no account, no email |
| 5 | and usually no name. That makes the code the only key, and a key that is |
| 6 | not unique is not a key: two cards sharing one code means one customer |
| 7 | spending another's money, with no evidence afterwards about which balance |
| 8 | was meant. So this gate exists for exactly one reason, and refuses a |
| 9 | create or an update that would give a second wallet a code somebody else |
| 10 | already has. |
| 11 | |
| 12 | Schemas in this repo carry no uniqueness constraint, which is why the |
| 13 | rule lives here rather than in wallets.json. That is the same division |
| 14 | hook_wallet_entries draws: the schema describes shape, a hook decides |
| 15 | what may be written. |
| 16 | |
| 17 | **Compared upper-cased and trimmed**, because the code is compared that |
| 18 | way at redemption (object_promotions.normalize_code takes the identical |
| 19 | view of promotion codes, for the identical reason). A gate that matched |
| 20 | case-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 |
| 22 | other -- 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 |
| 25 | there are many of them; uniqueness is a property of the codes that exist, |
| 26 | not a requirement that every wallet have one. |
| 27 | |
| 28 | **Fails CLOSED when the collection cannot be read.** An unreadable wallet |
| 29 | list means unknown codes, and issuing a card that might duplicate a live |
| 30 | one is worse than refusing to issue it: a refused card is reissued in ten |
| 31 | seconds, a duplicated one is discovered when somebody's balance is gone. |
| 32 | Same direction hook_wallet_entries takes about an unreadable ledger. |
| 33 | |
| 34 | Nothing else is gated here. The balance is the entries' business |
| 35 | (hook_wallet_entries), the kind is a label, and this hook deliberately |
| 36 | does not decide who may open a wallet -- that is permissions'. |
| 37 | """ |
| 38 | |
| 39 | import os |
| 40 | |
| 41 | import object_records |
| 42 | |
| 43 | |
| 44 | def _base_dir(): |
| 45 | return os.environ.get("DBBASIC_DATA_DIR", "data") |
| 46 | |
| 47 | |
| 48 | def _text(value): |
| 49 | return str(value if value is not None else "").strip() |
| 50 | |
| 51 | |
| 52 | def _code(value): |
| 53 | return _text(value).upper() |
| 54 | |
| 55 | |
| 56 | def 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 |