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.
wallets.json · 9.8 KB · 212 lines
| 1 | { |
| 2 | "name": "wallets", |
| 3 | "title": "Wallets", |
| 4 | "version": 6, |
| 5 | "unique": [ |
| 6 | { |
| 7 | "fields": [ |
| 8 | "code" |
| 9 | ], |
| 10 | "ignore_case": true, |
| 11 | "message": "Another wallet already carries that gift-card code. A gift card is spent by whoever holds its code, so two cards with one code is one customer spending another's money -- issue a different code." |
| 12 | }, |
| 13 | { |
| 14 | "fields": [ |
| 15 | "generated_from" |
| 16 | ], |
| 17 | "message": "A wallet has already been opened for this event." |
| 18 | } |
| 19 | ], |
| 20 | "description": "Prepaid balance for pay-as-you-go usage. The design decision that matters is in one field: balance_minor is a ROLLUP over wallet_entries, never a stored number anyone writes. The predecessor kept a mutable Wallet.balance column beside a Transaction table -- two truths that drift, and when they drift the customer is right and you cannot prove otherwise. Here the entries ARE the balance; the number on this record is a materialized view of them that the engine recomputes on every entry write (docs/logic-decisions.md #3: money moves, it does not mutate). Amounts are integers of the denomination's smallest unit (object_money.py), so a wallet can hold dollars, satoshis, or a gift-card unit without special cases. Card rails charge a fixed fee per transaction, which is why prepaid exists at all: sub-dollar charges are individually nonviable, so the provider sees occasional top-ups and the micro-debits stay internal. Auto-replenishment carries a MONTHLY CAP for a reason worth naming -- autopay plus one runaway metering bug is how a customer wakes up to a four-figure surprise; the cap turns that into a bounded annoyance someone notices. v2 says out loud what this container already was: A GIFT CARD IS A WALLET, and so is store credit. Both are prepaid money the customer can spend here, both need a movement log rather than a mutable number, and both need a gate that refuses to let somebody spend what is not there -- which is precisely what this collection and hook_wallet_entries already are. Inventing a gift_cards table with its own balance column and its own spend check would be building the predecessor's drift bug a second time, in a corner nobody watches, for money that arrives as a bearer instrument. So v2 adds exactly two fields and no new machinery: `kind` says which of the three this wallet is, and `code` is the bearer token a gift card is redeemed by. The distinction between them is WHO CAN SPEND IT: a balance and store credit belong to owner_id and are spent by that person, while a gift card belongs to whoever holds the code -- which is the whole point of a gift card and the reason redemption keys on the code rather than on an account the recipient does not have. v3 declares two keys `unique`, enforced by object_records inside the write lock for every writer: `code` (compared without case, blank exempt), so two cards can never share a bearer code whichever door wrote them; and `generated_from`, the provenance of a wallet a handler opened (system_gift_card_issue stamps gift_card/{order_line_id}), so two overlapping deliveries of one payment open one card rather than two -- the second card used to be created before the ledger's marker check could stop its top-up. v5: relation.requires: update on the entity, read on the denomination. v6: what a wallet holds and whose card fills it are not the holder's to set. The registered update grant (kept: the holder sets their own auto top-up threshold, amount, monthly cap and is_active) let the owner PUT denomination_id -- 500 cents of USD restated as 500 of whatever else is configured -- turn a balance or store-credit wallet into a bearer gift card by setting kind and code, or point stripe_customer_id and payment_method_ref at another customer's saved card, which system_wallet_replenish then charges off-session into this wallet. kind, code, denomination_id, stripe_customer_id, payment_method_ref and entity_id are field permissions {edit: role:manager, role:admin; default: read}; the handlers that open and fill wallets write in process and are untouched.", |
| 21 | "fields": [ |
| 22 | { |
| 23 | "name": "id" |
| 24 | }, |
| 25 | { |
| 26 | "name": "owner_id", |
| 27 | "type": "text", |
| 28 | "required": true, |
| 29 | "label": "Owner", |
| 30 | "help": "One wallet per owner per denomination. For a GIFT CARD this is the shop, not the recipient: a gift card is a bearer instrument redeemed by its code, and a guest who was handed one has no account to own it with." |
| 31 | }, |
| 32 | { |
| 33 | "name": "kind", |
| 34 | "type": "enum", |
| 35 | "default": "balance", |
| 36 | "label": "Kind", |
| 37 | "enum": [ |
| 38 | "balance", |
| 39 | "gift_card", |
| 40 | "store_credit" |
| 41 | ], |
| 42 | "help": "balance: the ordinary prepaid wallet a customer tops up to pay for usage. gift_card: money somebody bought for somebody else, spent by whoever holds `code`. store_credit: money the shop gave back -- a refund taken as credit, or a dispute resolved that way (action_resolve_dispute writes into exactly this collection). All three are the same container with the same ledger and the same gate; the field says which conversation you are in, not which rules apply.", |
| 43 | "permissions": { |
| 44 | "edit": [ |
| 45 | "role:manager", |
| 46 | "role:admin" |
| 47 | ], |
| 48 | "default": "read" |
| 49 | } |
| 50 | }, |
| 51 | { |
| 52 | "name": "code", |
| 53 | "type": "text", |
| 54 | "label": "Code", |
| 55 | "validation": { |
| 56 | "max_length": 40 |
| 57 | }, |
| 58 | "help": "The bearer token a gift card is redeemed by, blank for an ordinary wallet. UNIQUE WHEN PRESENT and compared upper-cased -- the schema's `unique` declaration enforces it inside the write lock for every writer (hook_wallets still gives the friendly refusal), because two cards sharing a code would let one person spend the other's money with no way to tell which balance was meant. Given to a store_credit wallet as well when the shop wants it redeemable at a guest checkout, which is the only way credit reaches somebody who has no account.", |
| 59 | "permissions": { |
| 60 | "edit": [ |
| 61 | "role:manager", |
| 62 | "role:admin" |
| 63 | ], |
| 64 | "default": "read" |
| 65 | } |
| 66 | }, |
| 67 | { |
| 68 | "name": "generated_from", |
| 69 | "type": "text", |
| 70 | "label": "Generated From", |
| 71 | "read_only": true, |
| 72 | "help": "Provenance for a wallet a handler opened (gift_card/{order_line_id} for a sold gift card). Unique when present, so a replayed or overlapping event opens nothing twice. Blank for a wallet a person opened." |
| 73 | }, |
| 74 | { |
| 75 | "name": "denomination_id", |
| 76 | "label": "Denomination", |
| 77 | "relation": { |
| 78 | "collection": "denominations", |
| 79 | "display_field": "code", |
| 80 | "requires": "read" |
| 81 | }, |
| 82 | "help": "What this wallet holds. Blank means the server default (USD).", |
| 83 | "permissions": { |
| 84 | "edit": [ |
| 85 | "role:manager", |
| 86 | "role:admin" |
| 87 | ], |
| 88 | "default": "read" |
| 89 | } |
| 90 | }, |
| 91 | { |
| 92 | "name": "balance_minor", |
| 93 | "type": "computed", |
| 94 | "label": "Balance", |
| 95 | "rollup": { |
| 96 | "collection": "wallet_entries", |
| 97 | "fk_field": "wallet_id", |
| 98 | "op": "sum", |
| 99 | "field": "amount_minor" |
| 100 | }, |
| 101 | "help": "Derived, never written: the sum of every entry, credits positive and debits negative. If this and the entries ever disagreed, the entries would be right -- which is why nothing may set it." |
| 102 | }, |
| 103 | { |
| 104 | "name": "auto_replenish_enabled", |
| 105 | "type": "boolean", |
| 106 | "default": "false", |
| 107 | "label": "Auto Top-up" |
| 108 | }, |
| 109 | { |
| 110 | "name": "auto_replenish_threshold_minor", |
| 111 | "type": "integer", |
| 112 | "default": "0", |
| 113 | "label": "Top up below", |
| 114 | "help": "When the balance falls below this, charge the saved card." |
| 115 | }, |
| 116 | { |
| 117 | "name": "auto_replenish_amount_minor", |
| 118 | "type": "integer", |
| 119 | "default": "0", |
| 120 | "label": "Top-up amount" |
| 121 | }, |
| 122 | { |
| 123 | "name": "auto_replenish_monthly_cap_minor", |
| 124 | "type": "integer", |
| 125 | "default": "0", |
| 126 | "label": "Monthly cap", |
| 127 | "help": "The most that may be auto-charged in a calendar month. Zero means auto top-up is effectively off: a cap of nothing is the safe default, because an uncapped autopay plus a metering bug is a four-figure surprise." |
| 128 | }, |
| 129 | { |
| 130 | "name": "stripe_customer_id", |
| 131 | "type": "text", |
| 132 | "label": "Stripe Customer", |
| 133 | "help": "Set when a card is saved. The card itself never touches this server -- only the provider's reference to it.", |
| 134 | "permissions": { |
| 135 | "edit": [ |
| 136 | "role:manager", |
| 137 | "role:admin" |
| 138 | ], |
| 139 | "default": "read" |
| 140 | } |
| 141 | }, |
| 142 | { |
| 143 | "name": "payment_method_ref", |
| 144 | "type": "text", |
| 145 | "label": "Saved Card", |
| 146 | "help": "The provider's payment-method id used for off-session top-ups. An identifier, not an instrument.", |
| 147 | "permissions": { |
| 148 | "edit": [ |
| 149 | "role:manager", |
| 150 | "role:admin" |
| 151 | ], |
| 152 | "default": "read" |
| 153 | } |
| 154 | }, |
| 155 | { |
| 156 | "name": "is_active", |
| 157 | "type": "boolean", |
| 158 | "default": "true", |
| 159 | "label": "Active" |
| 160 | }, |
| 161 | { |
| 162 | "name": "entity_id", |
| 163 | "type": "text", |
| 164 | "label": "Entity", |
| 165 | "relation": { |
| 166 | "collection": "entities", |
| 167 | "display_field": "name", |
| 168 | "requires": "update" |
| 169 | }, |
| 170 | "permissions": { |
| 171 | "edit": [ |
| 172 | "role:manager", |
| 173 | "role:admin" |
| 174 | ], |
| 175 | "default": "read" |
| 176 | } |
| 177 | }, |
| 178 | { |
| 179 | "name": "created_at", |
| 180 | "type": "datetime", |
| 181 | "read_only": true, |
| 182 | "label": "Created" |
| 183 | } |
| 184 | ], |
| 185 | "forms": { |
| 186 | "default": { |
| 187 | "fields": [ |
| 188 | "kind", |
| 189 | "code", |
| 190 | "denomination_id", |
| 191 | "auto_replenish_enabled", |
| 192 | "auto_replenish_threshold_minor", |
| 193 | "auto_replenish_amount_minor", |
| 194 | "auto_replenish_monthly_cap_minor", |
| 195 | "is_active" |
| 196 | ] |
| 197 | } |
| 198 | }, |
| 199 | "views": { |
| 200 | "list_mode": "table", |
| 201 | "list_fields": [ |
| 202 | "owner_id", |
| 203 | "kind", |
| 204 | "balance_minor", |
| 205 | "auto_replenish_enabled", |
| 206 | "is_active" |
| 207 | ], |
| 208 | "filter_fields": [ |
| 209 | "kind" |
| 210 | ] |
| 211 | } |
| 212 | } |