🔌 sync with the latest backend contract
Browse files at this commit2c19dff2e78f0c31ff658b5525d64c404fb78f51Parent: 211d704- feat
- 🔌 sync with the latest backend contract
11 changed files +96 -7
- src/api/__tests__/chatbot/chatbot.test.ts +4 -1
- src/api/__tests__/users/users.test.ts +14 -0
- src/api/chatbot/chatbotStreamUrl.ts +7 -0
- src/api/chatbot/index.ts +1 -0
- src/api/client/types.ts +23 -1
- src/api/users/index.ts +1 -0
- src/api/users/postUser.ts +6 -0
- src/components/layout/celebi-panel.tsx +2 -2
- src/components/users/profile-form.tsx +25 -2
- src/i18n/messages.ts +6 -0
- src/pages/profile-page.tsx +7 -1
src/api/__tests__/chatbot/chatbot.test.tsmodified+4 -1
| @@ -1,5 +1,5 @@ | ||
| 1 | 1 | import { afterEach, describe, expect, it, vi } from "vitest"; |
| 2 | -import { deleteChatbotThreadById, getChatbotMessageById, getChatbotThreadMessages, getChatbotThreads, patchChatbotThreadById, postChatbotMessage, postChatbotThread } from "../../chatbot"; | |
| 2 | +import { chatbotStreamUrl, deleteChatbotThreadById, getChatbotMessageById, getChatbotThreadMessages, getChatbotThreads, patchChatbotThreadById, postChatbotMessage, postChatbotThread } from "../../chatbot"; | |
| 3 | 3 | import { lastFetchCall, mockFetch204, mockFetchSuccess } from "../helpers/mock-fetch"; |
| 4 | 4 | |
| 5 | 5 | describe("chatbot API", () => { |
| @@ -38,4 +38,7 @@ describe("chatbot API", () => { | ||
| 38 | 38 | expect(url).toBe("/api/chatbot/threads/thread-1/messages/answer-1"); |
| 39 | 39 | expect(init?.method).toBe("GET"); |
| 40 | 40 | }); |
| 41 | + it("chatbotStreamUrl builds the SSE path for an EventSource", () => { | |
| 42 | + expect(chatbotStreamUrl("t 1", "m1")).toBe("/api/chatbot/threads/t%201/messages/m1/stream"); | |
| 43 | + }); | |
| 41 | 44 | }); |
src/api/__tests__/users/users.test.tsmodified+14 -0
| @@ -14,6 +14,7 @@ import { postMyAvatar } from "../../users"; | ||
| 14 | 14 | import { deleteMyAvatar } from "../../users"; |
| 15 | 15 | import { deleteUserAvatar } from "../../users"; |
| 16 | 16 | import { getUserAvatarUrl } from "../../users"; |
| 17 | +import { postUser } from "../../users"; | |
| 17 | 18 | import { lastFetchCall, mockFetch204, mockFetchSuccess } from "../helpers/mock-fetch"; |
| 18 | 19 | |
| 19 | 20 | describe("users API", () => { |
| @@ -214,4 +215,17 @@ describe("users API", () => { | ||
| 214 | 215 | expect(getUserAvatarUrl("u1", 3)).toBe("/api/users/u1/avatar?v=3"); |
| 215 | 216 | expect(getUserAvatarUrl("u/1")).toBe("/api/users/u%2F1/avatar"); |
| 216 | 217 | }); |
| 218 | + it("postUser posts the new account to /users", async () => { | |
| 219 | + const mockUser = { id: "u9", username: "ayse", role: "teacher" }; | |
| 220 | + mockFetchSuccess(mockUser); | |
| 221 | + | |
| 222 | + const body = { username: "ayse", password: "secret123", role: "teacher" as const }; | |
| 223 | + const result = await postUser(body); | |
| 224 | + expect(result).toEqual(mockUser); | |
| 225 | + | |
| 226 | + const [url, init] = lastFetchCall(); | |
| 227 | + expect(url).toBe("/api/users"); | |
| 228 | + expect(init?.method).toBe("POST"); | |
| 229 | + expect(JSON.parse(init?.body as string)).toEqual(body); | |
| 230 | + }); | |
| 217 | 231 | }); |
src/api/chatbot/chatbotStreamUrl.tsadded+7 -0
| @@ -0,0 +1,7 @@ | ||
| 1 | +/** | |
| 2 | + * The SSE URL for one pending turn: `delta` chunks, then one `done` or | |
| 3 | + * `error`. Not a request — hand it to an `EventSource` (`withCredentials: true`). | |
| 4 | + */ | |
| 5 | +export function chatbotStreamUrl(threadId: string, messageId: string): string { | |
| 6 | + return `/api/chatbot/threads/${encodeURIComponent(threadId)}/messages/${encodeURIComponent(messageId)}/stream`; | |
| 7 | +} |
src/api/chatbot/index.tsmodified+1 -0
| @@ -5,4 +5,5 @@ export * from "./patchChatbotThreadById"; | ||
| 5 | 5 | export * from "./deleteChatbotThreadById"; |
| 6 | 6 | export * from "./postChatbotMessage"; |
| 7 | 7 | export * from "./postChatbotThread"; |
| 8 | +export * from "./chatbotStreamUrl"; | |
| 8 | 9 | export * from "./types"; |
src/api/client/types.tsmodified+23 -1
| @@ -38,6 +38,17 @@ export type ProfileUpdate = { | ||
| 38 | 38 | birth_date?: string | null; |
| 39 | 39 | display_name?: string | null; |
| 40 | 40 | bio?: string | null; |
| 41 | + // The teacher's branş, by name from settings.branches. | |
| 42 | + branch?: string | null; | |
| 43 | +}; | |
| 44 | + | |
| 45 | +// POST /users (admin): the school office opens an account directly. `role` | |
| 46 | +// defaults to student on the server; an existing person joins this school only | |
| 47 | +// when the password matches their credential (409 otherwise). | |
| 48 | +export type CreateUserInput = { | |
| 49 | + username: string; | |
| 50 | + password: string; | |
| 51 | + role?: Role; | |
| 41 | 52 | }; |
| 42 | 53 | |
| 43 | 54 | export type PersonRef = { |
| @@ -105,6 +116,8 @@ export type Profile = { | ||
| 105 | 116 | display_name: string | null; |
| 106 | 117 | role: Role; |
| 107 | 118 | bio: string | null; |
| 119 | + // The teacher's branş, one of settings.branches; null when unset. | |
| 120 | + branch: string | null; | |
| 108 | 121 | avatar: AvatarMeta | null; |
| 109 | 122 | // Capped at limits.user.max_profile_classes; stats.classes holds the true total. |
| 110 | 123 | classes: ProfileClassRef[]; |
| @@ -395,6 +408,12 @@ export type ClassMember = { | ||
| 395 | 408 | class: string; |
| 396 | 409 | user: PersonRef; |
| 397 | 410 | added_by: PersonRef; |
| 411 | + // One stint: a leave-and-rejoin is a fresh row with a fresh id. | |
| 412 | + joined_at: number; | |
| 413 | + // Null while the stint is live; the roster routes only list live rows. | |
| 414 | + left_at: number | null; | |
| 415 | + // The şube a year rollover copied this member out of; null when placed by hand. | |
| 416 | + source_class_group: string | null; | |
| 398 | 417 | }; |
| 399 | 418 | |
| 400 | 419 | // The instance as the class routes return it: an Instance plus who attached |
| @@ -833,6 +852,9 @@ export type Limits = { | ||
| 833 | 852 | max_class_grade_len: number; |
| 834 | 853 | max_class_members: number; |
| 835 | 854 | max_class_courses: number; |
| 855 | + max_academic_year_name_len: number; | |
| 856 | + min_ders_saati: number; | |
| 857 | + max_ders_saati: number; | |
| 836 | 858 | }; |
| 837 | 859 | exam: { |
| 838 | 860 | max_title_len: number; |
| @@ -934,7 +956,7 @@ export type Limits = { | ||
| 934 | 956 | required_attendance_statuses: string[]; |
| 935 | 957 | }; |
| 936 | 958 | request: { max_page_limit: number; max_request_id_len: number; schedule_past_grace_ms: number; request_timeout_secs: number }; |
| 937 | - rate: { window_secs: number; auth_per_minute: number; api_per_minute: number; chatbot_per_minute: number }; | |
| 959 | + rate: { window_secs: number; auth_per_minute: number; api_per_minute: number; chatbot_per_minute: number; rag_per_minute: number }; | |
| 938 | 960 | // The RAG nest. Message length, thread titles, history depth and the thread |
| 939 | 961 | // cap are the chatbot's own knobs, republished so a RAG client need not read |
| 940 | 962 | // the chatbot group to bound its input. |
src/api/users/index.tsmodified+1 -0
| @@ -13,3 +13,4 @@ export * from "./postMyAvatar"; | ||
| 13 | 13 | export * from "./deleteMyAvatar"; |
| 14 | 14 | export * from "./deleteUserAvatar"; |
| 15 | 15 | export * from "./getUserAvatarUrl"; |
| 16 | +export * from "./postUser"; |
src/api/users/postUser.tsadded+6 -0
| @@ -0,0 +1,6 @@ | ||
| 1 | +import { client } from "../client"; | |
| 2 | +import type { CreateUserInput, User } from "../client"; | |
| 3 | + | |
| 4 | +export function postUser(body: CreateUserInput): Promise<User> { | |
| 5 | + return client<User>("/users", { method: "POST", body }); | |
| 6 | +} |
src/components/layout/celebi-panel.tsxmodified+2 -2
| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | import { For, Match, Show, Switch, createEffect, createSignal, on, onCleanup, untrack } from "solid-js"; |
| 2 | 2 | import { useNavigate } from "@tanstack/solid-router"; |
| 3 | -import { deleteChatbotThreadById, getChatbotMessageById, getChatbotThreadMessages, getChatbotThreads, patchChatbotThreadById, postChatbotMessage, postChatbotThread, type ChatbotMessage, type ChatbotThread } from "@/api/chatbot"; | |
| 3 | +import { chatbotStreamUrl, deleteChatbotThreadById, getChatbotMessageById, getChatbotThreadMessages, getChatbotThreads, patchChatbotThreadById, postChatbotMessage, postChatbotThread, type ChatbotMessage, type ChatbotThread } from "@/api/chatbot"; | |
| 4 | 4 | import { formatApiError } from "@/api/client"; |
| 5 | 5 | import { CelebiComposer } from "@/components/layout/celebi-composer"; |
| 6 | 6 | import { CelebiMarkdown } from "@/components/layout/celebi-markdown"; |
| @@ -217,7 +217,7 @@ export function CelebiPanel(props: { open: boolean; onOpenChange: (open: boolean | ||
| 217 | 217 | |
| 218 | 218 | const streamAnswer = (activeThreadId: string, messageId: string) => { |
| 219 | 219 | stopStream(); |
| 220 | - const source = new EventSource(`/api/chatbot/threads/${encodeURIComponent(activeThreadId)}/messages/${encodeURIComponent(messageId)}/stream`, { withCredentials: true }); | |
| 220 | + const source = new EventSource(chatbotStreamUrl(activeThreadId, messageId), { withCredentials: true }); | |
| 221 | 221 | stream = source; |
| 222 | 222 | source.addEventListener("delta", (event) => { try { const data = JSON.parse((event as MessageEvent).data) as { text?: unknown }; if (typeof data.text === "string") setMessages((items) => items.map((item) => item.id === messageId ? { ...item, content: item.content + data.text } : item)); } catch { /* ignore malformed chunk */ } }); |
| 223 | 223 | source.addEventListener("done", (event) => { try { updateAssistant((JSON.parse((event as MessageEvent).data) as { message: ChatbotMessage }).message); } catch { poll(activeThreadId, messageId); } finally { stopStream(); void loadThreads(); } }); |
src/components/users/profile-form.tsxmodified+25 -2
| @@ -1,4 +1,4 @@ | ||
| 1 | -import { Show, createSignal } from "solid-js"; | |
| 1 | +import { For, Show, createSignal } from "solid-js"; | |
| 2 | 2 | import { patchMe } from "@/api/users"; |
| 3 | 3 | import type { ProfileUpdate, User } from "@/api/client"; |
| 4 | 4 | import { formatApiError } from "@/api/client"; |
| @@ -7,8 +7,10 @@ import { Button } from "@/components/ui/button"; | ||
| 7 | 7 | import { DatePicker } from "@/components/ui/date-picker"; |
| 8 | 8 | import { Input } from "@/components/ui/input"; |
| 9 | 9 | import { Label } from "@/components/ui/label"; |
| 10 | +import { Select } from "@/components/ui/select"; | |
| 10 | 11 | import { Textarea } from "@/components/ui/textarea"; |
| 11 | 12 | import { createFlash } from "@/lib/flash"; |
| 13 | +import { hasMinRole } from "@/lib/roles"; | |
| 12 | 14 | import { useT } from "@/stores/preferences-context"; |
| 13 | 15 | |
| 14 | 16 | const EMAIL_RE = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; |
| @@ -47,7 +49,9 @@ export function ProfileForm(props: { | ||
| 47 | 49 | * them where a profile is in hand; omit them and the fields stay hidden |
| 48 | 50 | * rather than silently offering to clear values they cannot see. |
| 49 | 51 | */ |
| 50 | - profile?: { display_name: string | null; bio: string | null }; | |
| 52 | + profile?: { display_name: string | null; bio: string | null; branch?: string | null }; | |
| 53 | + /** The school's branş vocabulary (settings.branches); the picker hides when empty. */ | |
| 54 | + branches?: string[]; | |
| 51 | 55 | maxDisplayNameLen?: number; |
| 52 | 56 | maxBioLen?: number; |
| 53 | 57 | onSaved: () => void; |
| @@ -61,6 +65,15 @@ export function ProfileForm(props: { | ||
| 61 | 65 | const [birthDate, setBirthDate] = createSignal(dateInputFromIso(props.user.birth_date)); |
| 62 | 66 | const [displayName, setDisplayName] = createSignal(props.profile?.display_name ?? ""); |
| 63 | 67 | const [bio, setBio] = createSignal(props.profile?.bio ?? ""); |
| 68 | + const [branch, setBranch] = createSignal(props.profile?.branch ?? ""); | |
| 69 | + // Branş is a teacher's field; a stored value outside the list still shows so | |
| 70 | + // saving the form never silently drops it. | |
| 71 | + const branchOptions = () => { | |
| 72 | + const list = props.branches ?? []; | |
| 73 | + const stored = props.profile?.branch; | |
| 74 | + return stored && !list.includes(stored) ? [stored, ...list] : list; | |
| 75 | + }; | |
| 76 | + const showBranch = () => !!props.profile && hasMinRole(props.user.role, "teacher") && branchOptions().length > 0; | |
| 64 | 77 | const [error, setError] = createSignal(""); |
| 65 | 78 | const [flash, setFlash] = createFlash(); |
| 66 | 79 | const [pending, setPending] = createSignal(false); |
| @@ -93,6 +106,7 @@ export function ProfileForm(props: { | ||
| 93 | 106 | const bi = bio().trim(); |
| 94 | 107 | if (dn !== (props.profile.display_name ?? "")) b.display_name = dn; |
| 95 | 108 | if (bi !== (props.profile.bio ?? "")) b.bio = bi; |
| 109 | + if (showBranch() && branch() !== (props.profile.branch ?? "")) b.branch = branch(); | |
| 96 | 110 | } |
| 97 | 111 | return b; |
| 98 | 112 | }; |
| @@ -160,6 +174,15 @@ export function ProfileForm(props: { | ||
| 160 | 174 | </p> |
| 161 | 175 | </div> |
| 162 | 176 | </Show> |
| 177 | + <Show when={showBranch()}> | |
| 178 | + <div class="space-y-1.5"> | |
| 179 | + <Label for="pf-branch">{t("profile.branch")}</Label> | |
| 180 | + <Select id="pf-branch" class="h-10" value={branch()} onChange={(e) => setBranch(e.currentTarget.value)}> | |
| 181 | + <option value="">{t("profile.branchNone")}</option> | |
| 182 | + <For each={branchOptions()}>{(name) => <option value={name}>{name}</option>}</For> | |
| 183 | + </Select> | |
| 184 | + </div> | |
| 185 | + </Show> | |
| 163 | 186 | <div class="space-y-1.5"> |
| 164 | 187 | <Label for="pf-email">{t("profile.email")}</Label> |
| 165 | 188 | <Input id="pf-email" class="h-10" type="email" value={email()} onInput={(e) => setEmail(e.currentTarget.value)} /> |
src/i18n/messages.tsmodified+6 -0
| @@ -126,6 +126,8 @@ export type MessageKey = | ||
| 126 | 126 | | "profile.displayNameHint" |
| 127 | 127 | | "profile.bio" |
| 128 | 128 | | "profile.bioHint" |
| 129 | + | "profile.branch" | |
| 130 | + | "profile.branchNone" | |
| 129 | 131 | | "profile.bioEmpty" |
| 130 | 132 | | "profile.classes" |
| 131 | 133 | | "profile.noClasses" |
| @@ -2121,6 +2123,8 @@ const en: Dict = { | ||
| 2121 | 2123 | "profile.displayNameHint": "Shown to other people instead of your username.", |
| 2122 | 2124 | "profile.bio": "About", |
| 2123 | 2125 | "profile.bioHint": "A short line about yourself.", |
| 2126 | + "profile.branch": "Teaching subject (branş)", | |
| 2127 | + "profile.branchNone": "Not set", | |
| 2124 | 2128 | "profile.bioEmpty": "No bio yet.", |
| 2125 | 2129 | "profile.classes": "Classes", |
| 2126 | 2130 | "profile.noClasses": "Not in any class.", |
| @@ -4124,6 +4128,8 @@ const tr: Dict = { | ||
| 4124 | 4128 | "profile.displayNameHint": "Diğer kişilere kullanıcı adın yerine bu gösterilir.", |
| 4125 | 4129 | "profile.bio": "Hakkında", |
| 4126 | 4130 | "profile.bioHint": "Kendinle ilgili kısa bir not.", |
| 4131 | + "profile.branch": "Branş", | |
| 4132 | + "profile.branchNone": "Belirtilmedi", | |
| 4127 | 4133 | "profile.bioEmpty": "Henüz bir not yok.", |
| 4128 | 4134 | "profile.classes": "Şubeler", |
| 4129 | 4135 | "profile.noClasses": "Hiçbir şubede değil.", |
src/pages/profile-page.tsxmodified+7 -1
| @@ -3,6 +3,7 @@ import { createResource } from "@/lib/create-resource"; | ||
| 3 | 3 | import { useLocation, useParams } from "@tanstack/solid-router"; |
| 4 | 4 | import { getClassesByUserId, getMyClasses } from "@/api/classes"; |
| 5 | 5 | import { getLimits } from "@/api/limits"; |
| 6 | +import { getSettings } from "@/api/settings"; | |
| 6 | 7 | import { getMyProfile, getUserProfile } from "@/api/users"; |
| 7 | 8 | import { ApiError, formatApiError, type Profile } from "@/api/client"; |
| 8 | 9 | import { RouteGuard } from "@/components/layout/route-guard"; |
| @@ -50,6 +51,7 @@ function ProfileContent() { | ||
| 50 | 51 | id === "me" ? getMyProfile() : getUserProfile(id), |
| 51 | 52 | ); |
| 52 | 53 | const [limits] = createResource(() => getLimits()); |
| 54 | + const [settings] = createResource(() => getSettings().catch(() => null)); | |
| 53 | 55 | const [editing, setEditing] = createSignal(false); |
| 54 | 56 | |
| 55 | 57 | // The five counters that fit the profile owner's role: a student's own |
| @@ -160,6 +162,9 @@ function ProfileContent() { | ||
| 160 | 162 | <div class="flex flex-wrap items-center gap-2"> |
| 161 | 163 | <span class="text-sm text-muted-foreground">@{p().username}</span> |
| 162 | 164 | <RoleBadge role={p().role} /> |
| 165 | + <Show when={p().branch}> | |
| 166 | + {(b) => <Badge variant="secondary" class="rounded-md">{b()}</Badge>} | |
| 167 | + </Show> | |
| 163 | 168 | </div> |
| 164 | 169 | <p class="max-w-prose whitespace-pre-wrap text-sm text-muted-foreground"> |
| 165 | 170 | {p().bio || t("profile.bioEmpty")} |
| @@ -220,7 +225,8 @@ function ProfileContent() { | ||
| 220 | 225 | {(u) => ( |
| 221 | 226 | <ProfileForm |
| 222 | 227 | user={u()} |
| 223 | - profile={{ display_name: p().display_name, bio: p().bio }} | |
| 228 | + profile={{ display_name: p().display_name, bio: p().bio, branch: p().branch }} | |
| 229 | + branches={settings.latest?.branches} | |
| 224 | 230 | maxDisplayNameLen={limits.latest?.user.max_display_name_len} |
| 225 | 231 | maxBioLen={limits.latest?.user.max_bio_len} |
| 226 | 232 | onSaved={() => { |