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 `appointment` table: row reads and listings, the overlap probe behind |
| 2 | //! the booking guard, the seat claim the booking writes through, and the |
| 3 | //! compare-and-set every decision writes. The workflows live in |
| 4 | //! [`crate::service::appointment`] (no lock anymore: occupancy is a |
| 5 | //! conditional counter write, the decision CAS is one statement, and the |
| 6 | //! approval's overlap decision is a serializable transaction documented at |
| 7 | //! its own site). |
| 8 | |
| 9 | use crate; |
| 10 | use crateClaimed; |
| 11 | use cratePagedList; |
| 12 | use crate |
| 13 | |
| 14 | ; |
| 15 | use crateAppointmentSlotId; |
| 16 | use crateTimestamp; |
| 17 | use crateUserId; |
| 18 | use crateAppError; |
| 19 | use ; |
| 20 | |
| 21 | /// Is `[starts_at, ends_at)` already taken for either party? Both sides are |
| 22 | /// checked: a teacher can't be in two meetings at once, and neither can a |
| 23 | /// requester (a parent with two children's teachers, say). |
| 24 | /// |
| 25 | /// Only *approved* bookings block — a pending request is a wish, not a |
| 26 | /// commitment, so several may queue on overlapping times and the first |
| 27 | /// approval wins. `except` skips one row, so re-checking a booking against |
| 28 | /// the world never trips over itself. |
| 29 | /// |
| 30 | /// The effective window (the proposal when one stands, the slot's own |
| 31 | /// otherwise) is computed in the query and compared in Rust: one round |
| 32 | /// trip, and the comparison stays a plain, unit-tested predicate. A booking |
| 33 | /// whose slot was withdrawn since (no window left to stand on) is skipped. |
| 34 | pub async |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | /// [`conflicts`] against any executor — the approval decision re-asks this |
| 46 | /// inside its own transaction, so the verdict it acts on is the one its |
| 47 | /// snapshot holds. |
| 48 | pub async |
| 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 | pub async |
| 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 | pub async |
| 112 | |
| 113 | |
| 114 | |
| 115 | /// The row, insisting it is still open for a decision. |
| 116 | pub async |
| 117 | |
| 118 | |
| 119 | |
| 120 | /// [`read_pending`] against any executor (the approval's transaction). |
| 121 | pub async |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | /// The optional filters and the page of [`list_for_requester`], in one |
| 136 | /// struct — the fields outgrew a readable argument list. The default is |
| 137 | /// the unfiltered, unpaged read. |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | /// The caller's own bookings, newest first. The optional filters narrow the |
| 152 | /// read: `status` matches the stored state, `starts_after`/`starts_before` |
| 153 | /// bound the meeting's effective start — a standing proposal, the slot's |
| 154 | /// window otherwise — from below (inclusive) and above (exclusive, so a |
| 155 | /// month is `[start, next_start)`), and `teacher` is the booked slot's |
| 156 | /// teacher — which needs the slot row, so the read runs over a derived table |
| 157 | /// the window and its count share. A booking whose slot is gone has no |
| 158 | /// meeting time and no teacher, so any of those three filters drops it; with |
| 159 | /// no filter the read stays the plain single-table one, so today's rows |
| 160 | /// (dangling bookings included) are untouched. |
| 161 | pub async |
| 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 | /// Every booking aimed at `teacher`, across all their slots — the |
| 235 | /// teacher's request inbox. The teacher lives on the slot row now, so |
| 236 | /// this is a join; the page and its count share the one predicate, and |
| 237 | /// the columns are spelled out so the join's duplicate slot-column names |
| 238 | /// can never leak into the row decode. `status` narrows to one booking |
| 239 | /// state and `starts_after`/`starts_before` to meetings whose effective |
| 240 | /// start (a standing proposal, the slot's window otherwise) sits in |
| 241 | /// `[after, before)` — both bounds optional, both in the SQL, so the |
| 242 | /// count always describes exactly the rows the window pages over. |
| 243 | pub async |
| 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 | pub async |
| 293 | |
| 294 | |
| 295 | |
| 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | |
| 308 | |
| 309 | |
| 310 | |
| 311 | |
| 312 | |
| 313 | |
| 314 | |
| 315 | /// Request `slot`: take the slot's one seat and write the pending booking |
| 316 | /// in a single statement — two racing requests cannot both find the slot |
| 317 | /// free however they interleave, and a refused insert takes its own seat |
| 318 | /// bump back with it. Zero rows means "full, or the slot was deleted since |
| 319 | /// the caller read it": the marker is one, and the caller re-reads to pick |
| 320 | /// the 404/409. |
| 321 | pub async |
| 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 | /// Write the decision only while the stored row still carries the state it |
| 367 | /// was validated against. `None` means it does not: another decision landed |
| 368 | /// between the read and the write, so reload, re-validate, and try again. |
| 369 | /// |
| 370 | /// Four columns discriminate every write, and `status` alone would not: |
| 371 | /// `propose` leaves a pending booking pending, so an approval built from a |
| 372 | /// snapshot taken *before* that proposal would sail through a status-only |
| 373 | /// guard and confirm the meeting at the old, superseded time. With the |
| 374 | /// proposed window and `decided_by` alongside it, every mutator moves at |
| 375 | /// least one of the four. The fields only a terminal transition writes |
| 376 | /// (`cancelled_by`, `cancel_reason`, `reject_reason`) need no compare of |
| 377 | /// their own: they always travel with a `status` change into a state no |
| 378 | /// later decision is allowed from. Absent values compare truthfully under |
| 379 | /// `IS NOT DISTINCT FROM`. |
| 380 | /// |
| 381 | /// A transition out of a live status (reject, cancel) hands the slot's |
| 382 | /// `occupied` seat back in the same transaction — the counter is the |
| 383 | /// authority on whether the slot is taken, so it must move if and only if |
| 384 | /// the status did. The decrement is gated on the CAS having written |
| 385 | /// something, so a lost race frees nothing. |
| 386 | pub async |
| 387 | |
| 388 | |
| 389 | |
| 390 | |
| 391 | |
| 392 | |
| 393 | |
| 394 | |
| 395 | |
| 396 | |
| 397 | |
| 398 | |
| 399 | /// The decision's compare-and-set against any executor: one conditional |
| 400 | /// `UPDATE`, plus the seat release when the status left the live set. A |
| 401 | /// single statement decides under Postgres's row locking, so no retry |
| 402 | /// budget and no store-level "write conflict" class exists anymore — a miss |
| 403 | /// is simply `None`, the reload-and-re-decide answer. |
| 404 | pub async |
| 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 |