hezarfen_backend
Clone
https://git.tahinli.com/archive/hezarfen_backend.git
0 watching0 forksRust8.3 MiB6d68754Ahmet Kaan Gümüş2 months ago566 commits
| 1 | //! The `payment_ledger` table: the append-only `INSERT`s and the reads the |
| 2 | //! balance and the over-payment cap fold. Nothing here ever `UPDATE`s or |
| 3 | //! `DELETE`s a row — a mistake is corrected by appending the opposing line. |
| 4 | //! The row locks that serialize the money arithmetic around these calls are |
| 5 | //! taken by the workflows in [`crate::service::payment_ledger`] |
| 6 | //! ([`lock_for_cap`]). |
| 7 | //! |
| 8 | //! The ledger row's `id` is a derived TEXT primary key, so Postgres itself is |
| 9 | //! the uniqueness check: a duplicate insert of the same key is the |
| 10 | //! "already billed" answer, surfaced here as |
| 11 | //! `INSERT … ON CONFLICT (id) DO NOTHING` plus a read-back of the row that |
| 12 | //! won. That is what makes every deterministic id (see |
| 13 | //! [`PaymentLedgerId::for_installment`] and friends) an idempotence key — |
| 14 | //! replaying the same append is a no-op, never a second line and never an |
| 15 | //! edit of the first, however the concurrent writers interleave. |
| 16 | |
| 17 | use PgExecutor; |
| 18 | |
| 19 | use crateMAX_LEDGER_APPLIED_LINES; |
| 20 | use crateDatabase; |
| 21 | use cratePagedList; |
| 22 | use crateInstallment; |
| 23 | use crateFeePlanAssignment; |
| 24 | use crate |
| 25 | |
| 26 | ; |
| 27 | use crateTimestamp; |
| 28 | use crateUserId; |
| 29 | use crateAppError; |
| 30 | |
| 31 | /// The only writer: one `INSERT`, no update path anywhere in this module. |
| 32 | /// |
| 33 | /// A line whose id already exists is left exactly as it is — the row wins, |
| 34 | /// the write is dropped, and the winner's line is read back in the *same |
| 35 | /// statement* (`ON CONFLICT (id) DO NOTHING`, and the `UNION ALL` arm answers |
| 36 | /// with the stored row when the insert was skipped). Unlike a scan, the point |
| 37 | /// read on the id cannot miss a row a concurrent writer just committed: an |
| 38 | /// insert that races an identical id waits on the unique index until the |
| 39 | /// other transaction settles, then takes the read-back arm. |
| 40 | /// |
| 41 | /// The read-back also checks the stored line is the *kind* that was being |
| 42 | /// appended. Handing a caller someone else's row is only safe while every id |
| 43 | /// shape is unambiguous (see [`PaymentLedgerId::for_request`]); if a future |
| 44 | /// marker or a widened key charset ever let two shapes meet, this is the |
| 45 | /// check that turns silently-wrong money into a 500 instead. It should be |
| 46 | /// unreachable, and it is cheap enough to keep it that way. |
| 47 | pub async |
| 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 | pub async |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | /// Bill installment `n` (1-based) of `assignment`, at the amount and due |
| 121 | /// date the plan carried when it was assigned — the charge is a frozen |
| 122 | /// copy, so editing the plan afterwards moves no existing line. |
| 123 | /// |
| 124 | /// Replaying it is free: the id is `(plan, student, n)`, so a duplicate |
| 125 | /// assign writes nothing and an assignment whose charges landed only in |
| 126 | /// part self-heals on the next assign of the same pair. |
| 127 | pub async |
| 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 | /// A student's whole statement, newest first. |
| 153 | pub async |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | /// One kind's fold, as [`balance_of`] and [`applied_to`] read it off the |
| 169 | /// `GROUP BY` — at most four rows, never the lines behind them. |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | /// The derived balance: `credits + reversals - charges - refunds`, in minor |
| 179 | /// units. Negative means the family owes the school. Never stored anywhere. |
| 180 | /// |
| 181 | /// **The sum is taken per kind by the database** and only the four totals |
| 182 | /// come back, so a balance read costs the same whether the ledger holds |
| 183 | /// four lines or forty thousand. |
| 184 | /// |
| 185 | /// **The signs stay here**, applied by the same |
| 186 | /// `PaymentLedgerKind::sign` the documented formula is spelled in — |
| 187 | /// [`PaymentLedger::fold_balance`](crate::domain::payment_ledger::PaymentLedger::fold_balance) |
| 188 | /// folds the grouped totals and the raw lines alike. Summing |
| 189 | /// `IF kind = 'charge' THEN -amount …` in SQL would have folded the whole |
| 190 | /// balance in one statement and forked the one rule that decides what money |
| 191 | /// means into a second language, where nothing fails the day the two |
| 192 | /// disagree. Grouping keeps the aggregate ignorant of signs. A stored |
| 193 | /// running total was the third option and is a counter that can drift — a |
| 194 | /// bug class this repo closes, not one it opens. |
| 195 | pub async |
| 196 | |
| 197 | |
| 198 | |
| 199 | |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | /// Lock the **ancestor chain** of `id` (`FOR UPDATE`, the line first, then |
| 212 | /// its target, and so on up to a charge). Every write that moves the money |
| 213 | /// arithmetic of a subtree takes this walk in its transaction, so writers to |
| 214 | /// one subtree all contend on its **root's** row lock: a payment folding a |
| 215 | /// charge's subtree cannot land between a refund's fold and its append |
| 216 | /// deeper down. That is the whole guarantee the over-payment cap rests on — |
| 217 | /// it replaces a process-wide mutex with the rows the money lives on. |
| 218 | /// |
| 219 | /// Lock order is *deepest first*: a writer always wants the next ancestor, |
| 220 | /// never the reverse, and an ancestor is never a descendant, so no two |
| 221 | /// walks can cycle — no deadlock, nothing for the retry loop to unwind. |
| 222 | /// The chain is at most three rows (charge ← credit ← refund; a reversal |
| 223 | /// points at a charge or a refund), and a charge's `source` names a |
| 224 | /// fee-plan assignment rather than a ledger row, which is where the walk |
| 225 | /// stops. |
| 226 | pub async |
| 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 | /// How much of `target` is already taken up — the balance fold restricted |
| 253 | /// to everything that points at it, however deep, and read the way `target` |
| 254 | /// is measured. Caller holds [`lock_for_cap`] on `target`'s chain in the |
| 255 | /// same transaction, so the fold and the append it authorizes cannot be |
| 256 | /// separated by a competing write. |
| 257 | /// |
| 258 | /// The whole subtree, not just the direct children, because money given |
| 259 | /// back frees the room it took: a charge paid in full and then *refunded* |
| 260 | /// is owed again, so it must be payable again (the fold nets to zero). The |
| 261 | /// same walk answers both sides, because the signs already say what each |
| 262 | /// line does — a credit adds under a charge, its refund takes that back, |
| 263 | /// and a reversal of that refund puts it back once more. `-target.sign()` |
| 264 | /// is what flips the reading for a credit, whose room is measured in the |
| 265 | /// refunds against it. The recursive CTE replaces the old |
| 266 | /// one-query-per-child walk; the signs still fold in Rust, by |
| 267 | /// [`PaymentLedger::fold_balance`](crate::domain::payment_ledger::PaymentLedger::fold_balance). |
| 268 | /// |
| 269 | /// **The ceiling stays.** Past [`MAX_LEDGER_APPLIED_LINES`] lines the fold |
| 270 | /// refuses — the count is what is refused, never the money already |
| 271 | /// recorded, so a line that is *already* over the ceiling still reads, |
| 272 | /// still refunds through its own children, and is still reversible. Only a |
| 273 | /// fresh line applied to *it* is turned away. |
| 274 | pub async |
| 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 |