hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits
| 1 | /** |
| 2 | * Which appointment actions the backend will actually accept, per viewer. |
| 3 | * |
| 4 | * Mirrors the guards in `hezarfen_backend/src/web/appointments.rs` and |
| 5 | * `src/domain/appointment.rs`. This app serves children: an action must never |
| 6 | * be offered when the server is going to answer 403/409. |
| 7 | */ |
| 8 | import type { Appointment } from "@/api/client"; |
| 9 | |
| 10 | export type AppointmentAction = |
| 11 | // staff = the slot's teacher, or any manager+ (`can_manage`) |
| 12 | | "approve" |
| 13 | | "reject" |
| 14 | | "reschedule" |
| 15 | // requester = the person who asked for the meeting (`ensure_requester`) |
| 16 | | "cancel" |
| 17 | | "acceptReschedule" |
| 18 | | "declineReschedule"; |
| 19 | |
| 20 | export type AppointmentViewer = "staff" | "requester"; |
| 21 | |
| 22 | /** |
| 23 | * A counter-proposal still waiting for the requester's answer. |
| 24 | * |
| 25 | * The backend NEVER clears `proposed_*` — `accept_proposal` delegates to |
| 26 | * `approve`, which writes `status`/`decided_by` only. So `proposed_* != null` |
| 27 | * on a settled or approved row is an *answered* proposal, not a standing one, |
| 28 | * and gating on it would strand the teacher without a reschedule action forever. |
| 29 | */ |
| 30 | export function hasStandingProposal(a: Appointment): boolean { |
| 31 | return a.status === "pending" && a.proposed_starts_at != null && a.proposed_ends_at != null; |
| 32 | } |
| 33 | |
| 34 | const isLive = (a: Appointment) => a.status === "pending" || a.status === "approved"; |
| 35 | |
| 36 | /** |
| 37 | * Has the *effective* window opened? `starts_at` is already the effective one |
| 38 | * (`Appointment::window` prefers a proposal whenever the row carries one), which |
| 39 | * is exactly the value every `<= now` 409 guard compares. `null` means the slot |
| 40 | * is gone, so every action would 404 — treat it as closed. |
| 41 | */ |
| 42 | const hasStarted = (a: Appointment, now: number) => a.starts_at == null || a.starts_at <= now; |
| 43 | |
| 44 | export function appointmentActions(a: Appointment, viewer: AppointmentViewer, now: number): AppointmentAction[] { |
| 45 | const standing = hasStandingProposal(a); |
| 46 | const actions: AppointmentAction[] = []; |
| 47 | if (viewer === "staff") { |
| 48 | // `approve` needs pending + a window that has not opened. While the |
| 49 | // teacher's own counter-proposal stands the backend would still take it, |
| 50 | // but accepting it is the requester's call — we do not offer a way to move |
| 51 | // someone else's meeting without their answer. |
| 52 | if (a.status === "pending" && !standing && !hasStarted(a, now)) actions.push("approve"); |
| 53 | // `reject` is guarded by `read_pending` alone — no time check — so a passed |
| 54 | // pending request (or a standing proposal the teacher wants back) can |
| 55 | // always be turned down. |
| 56 | if (a.status === "pending") actions.push("reject"); |
| 57 | // `propose` needs a live row only; the *new* time must be in the future, |
| 58 | // which the reschedule form validates. Re-proposing over an answered or a |
| 59 | // standing proposal is legal. |
| 60 | if (isLive(a)) actions.push("reschedule"); |
| 61 | return actions; |
| 62 | } |
| 63 | // Requester side. Accepting is `approve` at the proposed time and declining |
| 64 | // is `cancel`, so both close once that window opens. |
| 65 | if (standing) { |
| 66 | if (!hasStarted(a, now)) actions.push("acceptReschedule", "declineReschedule"); |
| 67 | return actions; |
| 68 | } |
| 69 | if (isLive(a) && !hasStarted(a, now)) actions.push("cancel"); |
| 70 | return actions; |
| 71 | } |