🪻 distributed transcription service thistle.dunkirk.sh

feat: add sections

dunkirk.sh 4d7be0af 8caa2a98

verified
+39
scripts/remove-from-classes.ts
···
+
#!/usr/bin/env bun
+
+
import db from "../src/db/schema";
+
+
const email = process.argv[2];
+
+
if (!email) {
+
console.error("Usage: bun scripts/remove-from-classes.ts <email>");
+
console.error(" Removes a user from all their enrolled classes");
+
process.exit(1);
+
}
+
+
const user = db
+
.query<{ id: number; email: string }, [string]>(
+
"SELECT id, email FROM users WHERE email = ?",
+
)
+
.get(email);
+
+
if (!user) {
+
console.error(`User with email ${email} not found`);
+
process.exit(1);
+
}
+
+
// Get current enrollments
+
const enrollments = db
+
.query<{ class_id: string }, [number]>(
+
"SELECT class_id FROM class_members WHERE user_id = ?",
+
)
+
.all(user.id);
+
+
if (enrollments.length === 0) {
+
console.log(`User ${email} is not enrolled in any classes`);
+
process.exit(0);
+
}
+
+
// Remove from all classes
+
db.run("DELETE FROM class_members WHERE user_id = ?", [user.id]);
+
+
console.log(`✅ Successfully removed ${email} from ${enrollments.length} class(es)`);
+215 -15
src/components/admin-classes.ts
···
@state() activeTab: "classes" | "waitlist" = "classes";
@state() approvingEntry: WaitlistEntry | null = null;
@state() showModal = false;
+
@state() showClassSettingsModal = false;
+
@state() editingClassId: string | null = null;
+
@state() editingClassInfo: Class | null = null;
+
@state() editingClassSections: { id: string; section_number: string }[] = [];
+
@state() newSectionNumber = "";
@state() meetingTimes: MeetingTime[] = [];
+
@state() sections: string[] = [];
@state() editingClass = {
courseCode: "",
courseName: "",
···
this.showModal = true;
}
+
private async handleEditSections(classId: string) {
+
try {
+
const response = await fetch(`/api/classes/${classId}`);
+
if (!response.ok) throw new Error("Failed to load class");
+
+
const data = await response.json();
+
this.editingClassId = classId;
+
this.editingClassInfo = data.class;
+
this.editingClassSections = data.sections || [];
+
this.newSectionNumber = "";
+
this.showClassSettingsModal = true;
+
} catch {
+
this.error = "Failed to load class details";
+
}
+
}
+
+
private async handleAddSection() {
+
if (!this.newSectionNumber.trim() || !this.editingClassId) return;
+
+
try {
+
const response = await fetch(`/api/classes/${this.editingClassId}/sections`, {
+
method: "POST",
+
headers: { "Content-Type": "application/json" },
+
body: JSON.stringify({ section_number: this.newSectionNumber.trim() }),
+
});
+
+
if (!response.ok) {
+
const data = await response.json();
+
this.error = data.error || "Failed to add section";
+
return;
+
}
+
+
const newSection = await response.json();
+
this.editingClassSections = [...this.editingClassSections, newSection];
+
this.newSectionNumber = "";
+
} catch {
+
this.error = "Failed to add section";
+
}
+
}
+
+
private async handleDeleteSection(sectionId: string) {
+
if (!this.editingClassId) return;
+
+
try {
+
const response = await fetch(`/api/classes/${this.editingClassId}/sections/${sectionId}`, {
+
method: "DELETE",
+
});
+
+
if (!response.ok) {
+
const data = await response.json();
+
this.error = data.error || "Failed to delete section";
+
return;
+
}
+
+
this.editingClassSections = this.editingClassSections.filter(s => s.id !== sectionId);
+
} catch {
+
this.error = "Failed to delete section";
+
}
+
}
+
+
private handleCloseSectionsModal() {
+
this.showClassSettingsModal = false;
+
this.editingClassId = null;
+
this.editingClassInfo = null;
+
this.editingClassSections = [];
+
this.newSectionNumber = "";
+
this.loadData();
+
}
+
+
private getFilteredClasses() {
if (!this.searchTerm) return this.classes;
···
}
${this.showModal ? this.renderApprovalModal() : ""}
+
${this.showClassSettingsModal ? this.renderClassSettingsModal() : ""}
`;
}
···
<div class="classes-grid">
${filteredClasses.map(
(cls) => html`
-
<div class="class-card ${cls.archived ? "archived" : ""}">
+
<div
+
class="class-card ${cls.archived ? "archived" : ""}"
+
@click=${() => this.handleEditSections(cls.id)}
+
style="cursor: pointer;"
+
>
<div class="class-header">
<div class="class-info">
<div class="course-code">${cls.course_code}</div>
···
${cls.archived ? html`<span class="badge archived">Archived</span>` : ""}
</div>
</div>
-
<div class="actions">
-
<button
-
class="btn-archive"
-
@click=${() => this.handleToggleArchive(cls.id)}
-
>
-
${cls.archived ? "Unarchive" : "Archive"}
-
</button>
-
<button
-
class="btn-delete"
-
@click=${() => this.handleDeleteClick(cls.id, "class")}
-
>
-
${this.getDeleteButtonText(cls.id, "class")}
-
</button>
-
</div>
</div>
</div>
`,
···
`;
}
+
private renderClassSettingsModal() {
+
if (!this.showClassSettingsModal || !this.editingClassInfo) return html``;
+
+
return html`
+
<div class="modal-overlay" @click=${this.handleCloseSectionsModal}>
+
<div class="modal" @click=${(e: Event) => e.stopPropagation()} style="max-width: 48rem;">
+
<div class="modal-header">
+
<h2 class="modal-title">${this.editingClassInfo.course_code} - ${this.editingClassInfo.name}</h2>
+
<button class="close-btn" @click=${this.handleCloseSectionsModal} type="button">×</button>
+
</div>
+
+
<div class="tabs" style="margin-bottom: 1.5rem;">
+
<div style="display: flex; gap: 0.5rem; border-bottom: 2px solid var(--secondary);">
+
<button
+
style="padding: 0.75rem 1.5rem; background: transparent; color: var(--primary); border: none; border-bottom: 2px solid var(--primary); font-weight: 600; cursor: pointer; margin-bottom: -2px;"
+
>
+
Sections
+
</button>
+
</div>
+
</div>
+
+
<!-- Sections Tab -->
+
<div style="margin-bottom: 1.5rem;">
+
<h3 style="margin-bottom: 1rem; color: var(--text);">Manage Sections</h3>
+
+
<div style="display: flex; gap: 0.75rem; margin-bottom: 1rem;">
+
<input
+
type="text"
+
placeholder="Section number (e.g., 01, 02, A, B)"
+
.value=${this.newSectionNumber}
+
@input=${(e: Event) => {
+
this.newSectionNumber = (e.target as HTMLInputElement).value;
+
}}
+
@keypress=${(e: KeyboardEvent) => {
+
if (e.key === "Enter") {
+
e.preventDefault();
+
this.handleAddSection();
+
}
+
}}
+
style="flex: 1; padding: 0.75rem; border: 2px solid var(--secondary); border-radius: 6px; font-size: 1rem; background: var(--background); color: var(--text);"
+
/>
+
<button
+
@click=${this.handleAddSection}
+
?disabled=${!this.newSectionNumber.trim()}
+
style="padding: 0.75rem 1.5rem; background: var(--primary); color: white; border: 2px solid var(--primary); border-radius: 6px; font-weight: 500; cursor: pointer; white-space: nowrap;"
+
>
+
Add Section
+
</button>
+
</div>
+
+
${
+
this.editingClassSections.length === 0
+
? html`<p style="color: var(--paynes-gray); text-align: center; padding: 2rem;">No sections yet. Add one above.</p>`
+
: html`
+
<div style="display: flex; flex-direction: column; gap: 0.5rem;">
+
${this.editingClassSections.map(
+
(section) => html`
+
<div style="display: flex; justify-content: space-between; align-items: center; padding: 0.75rem; background: color-mix(in srgb, var(--secondary) 30%, transparent); border-radius: 6px;">
+
<span style="font-weight: 500;">Section ${section.section_number}</span>
+
<button
+
@click=${(e: Event) => {
+
e.stopPropagation();
+
this.handleDeleteSection(section.id);
+
}}
+
style="padding: 0.5rem 1rem; background: transparent; color: red; border: 2px solid red; border-radius: 4px; font-size: 0.875rem; cursor: pointer;"
+
>
+
Delete
+
</button>
+
</div>
+
`,
+
)}
+
</div>
+
`
+
}
+
</div>
+
+
<!-- Actions -->
+
<div style="display: flex; gap: 0.75rem; justify-content: space-between; padding-top: 1.5rem; border-top: 2px solid var(--secondary);">
+
<div style="display: flex; gap: 0.75rem;">
+
<button
+
@click=${(e: Event) => {
+
e.stopPropagation();
+
this.handleToggleArchive(this.editingClassId!);
+
this.handleCloseSectionsModal();
+
}}
+
style="padding: 0.75rem 1.5rem; background: transparent; color: var(--primary); border: 2px solid var(--primary); border-radius: 6px; font-weight: 500; cursor: pointer;"
+
>
+
${this.editingClassInfo.archived ? "Unarchive" : "Archive"} Class
+
</button>
+
<button
+
@click=${(e: Event) => {
+
e.stopPropagation();
+
this.handleDeleteClick(this.editingClassId!, "class");
+
}}
+
style="padding: 0.75rem 1.5rem; background: transparent; color: red; border: 2px solid red; border-radius: 6px; font-weight: 500; cursor: pointer;"
+
>
+
${this.getDeleteButtonText(this.editingClassId!, "class")}
+
</button>
+
</div>
+
<button
+
@click=${this.handleCloseSectionsModal}
+
style="padding: 0.75rem 1.5rem; background: var(--primary); color: white; border: 2px solid var(--primary); border-radius: 6px; font-weight: 500; cursor: pointer;"
+
>
+
Done
+
</button>
+
</div>
+
+
${this.error ? html`<div style="color: red; margin-top: 1rem; padding: 0.75rem; background: color-mix(in srgb, red 10%, transparent); border-radius: 6px;">${this.error}</div>` : ""}
+
</div>
+
</div>
+
`;
+
}
+
private renderWaitlist() {
return html`
${
···
this.meetingTimes = e.detail;
}
+
private handleSectionsChange(e: Event) {
+
const value = (e.target as HTMLInputElement).value;
+
this.sections = value
+
.split(",")
+
.map((s) => s.trim())
+
.filter((s) => s);
+
}
+
private handleClassFieldInput(field: string, e: Event) {
const value = (e.target as HTMLInputElement | HTMLSelectElement).value;
this.editingClass = { ...this.editingClass, [field]: value };
···
this.showModal = false;
this.approvingEntry = null;
this.meetingTimes = [];
+
this.sections = [];
this.editingClass = {
courseCode: "",
courseName: "",
···
semester: this.editingClass.semester,
year: this.editingClass.year,
meeting_times: labels,
+
sections: this.sections.length > 0 ? this.sections : undefined,
}),
});
···
.value=${this.meetingTimes}
@change=${this.handleMeetingTimesChange}
></meeting-time-picker>
+
</div>
+
<div class="form-group form-group-full">
+
<label>Sections (optional)</label>
+
<input
+
type="text"
+
placeholder="e.g., 01, 02, 03 or A, B, C"
+
.value=${this.sections.join(", ")}
+
@input=${this.handleSectionsChange}
+
/>
+
<div class="help-text">Comma-separated list of section numbers. Leave blank if no sections.</div>
</div>
</div>
+60 -11
src/components/class-registration-modal.ts
···
professor: string;
semester: string;
year: number;
+
sections?: { id: string; section_number: string }[];
is_enrolled?: boolean;
}
···
@state() error = "";
@state() hasSearched = false;
@state() showWaitlistForm = false;
+
@state() selectedSections: Map<string, string> = new Map();
@state() waitlistData = {
courseCode: "",
courseName: "",
···
this.error = "";
this.hasSearched = false;
this.showWaitlistForm = false;
+
this.selectedSections = new Map();
this.waitlistData = {
courseCode: "",
courseName: "",
···
}
}
-
private async handleJoin(classId: string) {
+
private async handleJoin(
+
classId: string,
+
sections?: { id: string; section_number: string }[],
+
) {
+
// If class has sections, require section selection
+
const selectedSection = this.selectedSections.get(classId);
+
if (sections && sections.length > 0 && !selectedSection) {
+
this.error = "Please select a section";
+
this.requestUpdate();
+
return;
+
}
+
this.isJoining = true;
this.error = "";
···
const response = await fetch("/api/classes/join", {
method: "POST",
headers: { "Content-Type": "application/json" },
-
body: JSON.stringify({ class_id: classId }),
+
body: JSON.stringify({
+
class_id: classId,
+
section_id: selectedSection || null,
+
}),
});
if (!response.ok) {
const data = await response.json();
this.error = data.error || "Failed to join class";
+
this.isJoining = false;
+
this.requestUpdate();
return;
}
// Success - notify parent and close
this.dispatchEvent(new CustomEvent("class-joined"));
this.handleClose();
-
} catch {
+
} catch (error) {
+
console.error("Failed to join class:", error);
this.error = "Failed to join class. Please try again.";
-
} finally {
this.isJoining = false;
+
this.requestUpdate();
}
}
···
<div class="results-grid">
${this.results.map(
(cls) => html`
-
<button
-
class="class-card ${cls.is_enrolled ? "enrolled" : ""}"
-
@click=${() => !cls.is_enrolled && this.handleJoin(cls.id)}
-
?disabled=${this.isJoining || cls.is_enrolled}
-
>
+
<div class="class-card ${cls.is_enrolled ? "enrolled" : ""}">
<div class="class-header">
<div class="class-info">
<div class="course-code">
···
<span>👤 ${cls.professor}</span>
<span>📅 ${cls.semester} ${cls.year}</span>
</div>
+
${
+
!cls.is_enrolled &&
+
cls.sections &&
+
cls.sections.length > 0
+
? html`
+
<div style="margin-top: 0.75rem;">
+
<label style="font-size: 0.75rem; margin-bottom: 0.25rem;">Select Section *</label>
+
<select
+
style="width: 100%; padding: 0.5rem; border: 2px solid var(--secondary); border-radius: 4px; font-size: 0.875rem; background: var(--background); color: var(--text);"
+
@change=${(e: Event) => {
+
const sectionId = (
+
e.target as HTMLSelectElement
+
).value;
+
if (sectionId) {
+
this.selectedSections.set(cls.id, sectionId);
+
} else {
+
this.selectedSections.delete(cls.id);
+
}
+
this.error = "";
+
this.requestUpdate();
+
}}
+
>
+
<option value="">Choose a section...</option>
+
${cls.sections.map(
+
(s) =>
+
html`<option value="${s.id}" ?selected=${this.selectedSections.get(cls.id) === s.id}>${s.section_number}</option>`,
+
)}
+
</select>
+
</div>
+
`
+
: ""
+
}
</div>
${
!cls.is_enrolled
···
?disabled=${this.isJoining}
@click=${(e: Event) => {
e.stopPropagation();
-
this.handleJoin(cls.id);
+
console.log('Join button clicked for class:', cls.id, 'sections:', cls.sections);
+
this.handleJoin(cls.id, cls.sections);
}}
>
${this.isJoining ? "Joining..." : "Join"}
···
: ""
}
</div>
-
</button>
+
</div>
`,
)}
</div>
+71 -6
src/components/class-view.ts
···
id: string;
user_id: number;
meeting_time_id: string | null;
+
section_id: string | null;
filename: string;
original_filename: string;
status:
···
audioUrl?: string;
}
+
interface ClassSection {
+
id: string;
+
section_number: string;
+
}
+
@customElement("class-view")
export class ClassView extends LitElement {
@state() classId = "";
@state() classInfo: Class | null = null;
@state() meetingTimes: MeetingTime[] = [];
+
@state() sections: ClassSection[] = [];
+
@state() userSection: string | null = null;
+
@state() selectedSectionFilter: string | null = null;
@state() transcriptions: Transcription[] = [];
@state() isLoading = true;
@state() error: string | null = null;
···
const data = await response.json();
this.classInfo = data.class;
this.meetingTimes = data.meetingTimes || [];
+
this.sections = data.sections || [];
+
this.userSection = data.userSection || null;
this.transcriptions = data.transcriptions || [];
+
+
// Default to user's section for filtering
+
if (this.userSection && !this.selectedSectionFilter) {
+
this.selectedSectionFilter = this.userSection;
+
}
// Load VTT for completed transcriptions
await this.loadVTTForCompleted();
···
}
private get filteredTranscriptions() {
-
if (!this.searchQuery) return this.transcriptions;
+
let filtered = this.transcriptions;
+
+
// Filter by selected section (or user's section by default)
+
const sectionFilter = this.selectedSectionFilter || this.userSection;
+
+
// Only filter by section if:
+
// 1. There are sections in the class
+
// 2. User has a section OR has selected one
+
if (this.sections.length > 0 && sectionFilter) {
+
// For admins: show all transcriptions
+
// For users: show their section + transcriptions with no section (legacy/unassigned)
+
if (!this.isAdmin) {
+
filtered = filtered.filter(
+
(t) => t.section_id === sectionFilter || t.section_id === null,
+
);
+
}
+
}
+
+
// Filter by search query
+
if (this.searchQuery) {
+
const query = this.searchQuery.toLowerCase();
+
filtered = filtered.filter((t) =>
+
t.original_filename.toLowerCase().includes(query),
+
);
+
}
-
const query = this.searchQuery.toLowerCase();
-
return this.transcriptions.filter((t) =>
-
t.original_filename.toLowerCase().includes(query),
-
);
+
return filtered;
}
private formatDate(timestamp: number): string {
···
<div class="course-code">${this.classInfo.course_code}</div>
<h1>${this.classInfo.name}</h1>
<div class="professor">Professor: ${this.classInfo.professor}</div>
-
<div class="semester">${this.classInfo.semester} ${this.classInfo.year}</div>
+
<div class="semester">
+
${this.classInfo.semester} ${this.classInfo.year}
+
${
+
this.userSection
+
? ` • Section ${this.sections.find((s) => s.id === this.userSection)?.section_number || ""}`
+
: ""
+
}
+
</div>
</div>
</div>
···
`
: html`
<div class="search-upload">
+
${
+
this.sections.length > 1
+
? html`
+
<select
+
style="padding: 0.5rem 0.75rem; border: 1px solid var(--secondary); border-radius: 4px; font-size: 0.875rem; color: var(--text); background: var(--background);"
+
@change=${(e: Event) => {
+
this.selectedSectionFilter =
+
(e.target as HTMLSelectElement).value || null;
+
}}
+
.value=${this.selectedSectionFilter || ""}
+
>
+
${this.sections.map(
+
(s) =>
+
html`<option value=${s.id} ?selected=${s.id === this.selectedSectionFilter}>${s.section_number}</option>`,
+
)}
+
</select>
+
`
+
: ""
+
}
<input
type="text"
class="search-box"
···
?open=${this.uploadModalOpen}
.classId=${this.classId}
.meetingTimes=${this.meetingTimes.map((m) => ({ id: m.id, label: m.label }))}
+
.sections=${this.sections}
+
.userSection=${this.userSection}
@close=${this.handleModalClose}
@upload-success=${this.handleUploadSuccess}
></upload-recording-modal>
+1
src/components/classes-overview.ts
···
}
private async handleClassJoined() {
+
this.showRegistrationModal = false;
await this.loadClasses();
}
+45
src/components/upload-recording-modal.ts
···
label: string;
}
+
interface ClassSection {
+
id: string;
+
section_number: string;
+
}
+
@customElement("upload-recording-modal")
export class UploadRecordingModal extends LitElement {
@property({ type: Boolean }) open = false;
@property({ type: String }) classId = "";
@property({ type: Array }) meetingTimes: MeetingTime[] = [];
+
@property({ type: Array }) sections: ClassSection[] = [];
+
@property({ type: String }) userSection: string | null = null;
@state() private selectedFile: File | null = null;
@state() private selectedMeetingTimeId: string | null = null;
+
@state() private selectedSectionId: string | null = null;
@state() private uploading = false;
@state() private error: string | null = null;
···
this.selectedMeetingTimeId = select.value || null;
}
+
private handleSectionChange(e: Event) {
+
const select = e.target as HTMLSelectElement;
+
this.selectedSectionId = select.value || null;
+
}
+
private handleClose() {
if (this.uploading) return;
this.open = false;
this.selectedFile = null;
this.selectedMeetingTimeId = null;
+
this.selectedSectionId = null;
this.error = null;
this.dispatchEvent(new CustomEvent("close"));
}
···
formData.append("audio", this.selectedFile);
formData.append("class_id", this.classId);
formData.append("meeting_time_id", this.selectedMeetingTimeId);
+
+
// Use user's section by default, or allow override
+
const sectionToUse = this.selectedSectionId || this.userSection;
+
if (sectionToUse) {
+
formData.append("section_id", sectionToUse);
+
}
const response = await fetch("/api/transcriptions", {
method: "POST",
···
Select which meeting this recording is for
</div>
</div>
+
+
${
+
this.sections.length > 1
+
? html`
+
<div class="form-group">
+
<label for="section">Section (optional)</label>
+
<select
+
id="section"
+
@change=${this.handleSectionChange}
+
?disabled=${this.uploading}
+
>
+
<option value="">Use my section ${this.userSection ? `(${this.sections.find((s) => s.id === this.userSection)?.section_number})` : ""}</option>
+
${this.sections.map(
+
(section) => html`
+
<option value=${section.id}>${section.section_number}</option>
+
`,
+
)}
+
</select>
+
<div class="help-text">
+
Override which section this recording is for
+
</div>
+
</div>
+
`
+
: ""
+
}
</form>
<div class="modal-footer">
+28
src/db/schema.ts
···
VALUES (0, 'ghosty@thistle.internal', NULL, 'Ghosty', '👻', 'user', strftime('%s', 'now'));
`,
},
+
{
+
version: 2,
+
name: "Add sections support to classes and class members",
+
sql: `
+
-- Add section_number to classes (nullable for existing classes)
+
ALTER TABLE classes ADD COLUMN section_number TEXT;
+
+
-- Add section_id to class_members (nullable - NULL means default section)
+
ALTER TABLE class_members ADD COLUMN section_id TEXT;
+
+
-- Create sections table to track all available sections for a class
+
CREATE TABLE IF NOT EXISTS class_sections (
+
id TEXT PRIMARY KEY,
+
class_id TEXT NOT NULL,
+
section_number TEXT NOT NULL,
+
created_at INTEGER NOT NULL DEFAULT (strftime('%s', 'now')),
+
FOREIGN KEY (class_id) REFERENCES classes(id) ON DELETE CASCADE,
+
UNIQUE(class_id, section_number)
+
);
+
+
CREATE INDEX IF NOT EXISTS idx_class_sections_class_id ON class_sections(class_id);
+
+
-- Add section_id to transcriptions to track which section uploaded it
+
ALTER TABLE transcriptions ADD COLUMN section_id TEXT;
+
+
CREATE INDEX IF NOT EXISTS idx_transcriptions_section_id ON transcriptions(section_id);
+
`,
+
},
];
function getCurrentVersion(): number {
+62 -9
src/index.ts
···
getClassById,
getClassesForUser,
getClassMembers,
+
getClassSections,
getMeetingById,
getMeetingTimesForClass,
getTranscriptionsForClass,
+
getUserSection,
isUserEnrolledInClass,
joinClass,
removeUserFromClass,
searchClassesByCourseCode,
toggleClassArchive,
updateMeetingTime,
+
createClassSection,
} from "./lib/classes";
import { sendEmail } from "./lib/email";
import {
···
const meetingTimeId = formData.get("meeting_time_id") as
| string
| null;
+
const sectionId = formData.get("section_id") as string | null;
if (!file) throw ValidationErrors.missingField("audio");
···
// Create database record
db.run(
-
"INSERT INTO transcriptions (id, user_id, class_id, meeting_time_id, filename, original_filename, status) VALUES (?, ?, ?, ?, ?, ?, ?)",
+
"INSERT INTO transcriptions (id, user_id, class_id, meeting_time_id, section_id, filename, original_filename, status) VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
transcriptionId,
user.id,
classId,
meetingTimeId,
+
sectionId,
filename,
file.name,
"pending",
···
cursor,
);
-
// For admin, return flat array. For users, group by semester/year
-
if (user.role === "admin") {
-
return Response.json(result.data);
-
}
-
-
// Group by semester/year for regular users
+
// Group by semester/year for all users
const grouped: Record<
string,
Array<{
···
semester,
year,
meeting_times,
+
sections: body.sections,
});
return Response.json(newClass, { status: 201 });
···
.all(user.id)
.map((row) => row.class_id);
-
// Add is_enrolled flag to each class
+
// Add is_enrolled flag and sections to each class
const classesWithEnrollment = classes.map((cls) => ({
...cls,
is_enrolled: enrolledClassIds.includes(cls.id),
+
sections: getClassSections(cls.id),
}));
return Response.json({ classes: classesWithEnrollment });
···
const user = requireAuth(req);
const body = await req.json();
const classId = body.class_id;
+
const sectionId = body.section_id || null;
const classIdValidation = validateClassId(classId);
if (!classIdValidation.valid) {
···
);
-
const result = joinClass(classId, user.id);
+
const result = joinClass(classId, user.id, sectionId);
if (!result.success) {
return Response.json({ error: result.error }, { status: 400 });
···
const meetingTimes = getMeetingTimesForClass(classId);
+
const sections = getClassSections(classId);
const transcriptions = getTranscriptionsForClass(classId);
+
const userSection = getUserSection(user.id, classId);
return Response.json({
class: classInfo,
meetingTimes,
+
sections,
+
userSection,
transcriptions,
});
} catch (error) {
···
const meetingTime = createMeetingTime(classId, label);
return Response.json(meetingTime, { status: 201 });
+
} catch (error) {
+
return handleError(error);
+
}
+
},
+
},
+
"/api/classes/:id/sections": {
+
POST: async (req) => {
+
try {
+
requireAdmin(req);
+
const classId = req.params.id;
+
const body = await req.json();
+
const { section_number } = body;
+
+
if (!section_number) {
+
return Response.json({ error: "Section number required" }, { status: 400 });
+
}
+
+
const section = createClassSection(classId, section_number);
+
return Response.json(section);
+
} catch (error) {
+
return handleError(error);
+
}
+
},
+
},
+
"/api/classes/:classId/sections/:sectionId": {
+
DELETE: async (req) => {
+
try {
+
requireAdmin(req);
+
const sectionId = req.params.sectionId;
+
+
// Check if any students are in this section
+
const studentsInSection = db
+
.query<{ count: number }, [string]>(
+
"SELECT COUNT(*) as count FROM class_members WHERE section_id = ?",
+
)
+
.get(sectionId);
+
+
if (studentsInSection && studentsInSection.count > 0) {
+
return Response.json(
+
{ error: "Cannot delete section with enrolled students" },
+
{ status: 400 },
+
);
+
}
+
+
db.run("DELETE FROM class_sections WHERE id = ?", [sectionId]);
+
return new Response(null, { status: 204 });
} catch (error) {
return handleError(error);
+96 -6
src/lib/classes.ts
···
semester: string;
year: number;
archived: boolean;
+
section_number?: string | null;
+
created_at: number;
+
}
+
+
export interface ClassSection {
+
id: string;
+
class_id: string;
+
section_number: string;
created_at: number;
}
···
export interface ClassMember {
class_id: string;
user_id: number;
+
section_id: string | null;
enrolled_at: number;
}
···
semester: string;
year: number;
meeting_times?: string[];
+
sections?: string[];
}): Class {
const id = nanoid();
const now = Math.floor(Date.now() / 1000);
···
if (data.meeting_times && data.meeting_times.length > 0) {
for (const label of data.meeting_times) {
createMeetingTime(id, label);
+
}
+
}
+
+
// Create sections if provided
+
if (data.sections && data.sections.length > 0) {
+
for (const sectionNumber of data.sections) {
+
createClassSection(id, sectionNumber);
}
}
···
/**
* Enroll a user in a class
*/
-
export function enrollUserInClass(userId: number, classId: string): void {
+
export function enrollUserInClass(
+
userId: number,
+
classId: string,
+
sectionId?: string | null,
+
): void {
const now = Math.floor(Date.now() / 1000);
db.run(
-
"INSERT OR IGNORE INTO class_members (class_id, user_id, enrolled_at) VALUES (?, ?, ?)",
-
[classId, userId, now],
+
"INSERT OR IGNORE INTO class_members (class_id, user_id, section_id, enrolled_at) VALUES (?, ?, ?, ?)",
+
[classId, userId, sectionId ?? null, now],
);
}
···
id: string;
user_id: number;
meeting_time_id: string | null;
+
section_id: string | null;
filename: string;
original_filename: string;
status: string;
···
},
[string]
>(
-
`SELECT id, user_id, meeting_time_id, filename, original_filename, status, progress, error_message, created_at, updated_at
+
`SELECT id, user_id, meeting_time_id, section_id, filename, original_filename, status, progress, error_message, created_at, updated_at
FROM transcriptions
WHERE class_id = ?
ORDER BY created_at DESC`,
···
export function joinClass(
classId: string,
userId: number,
+
sectionId?: string | null,
): { success: boolean; error?: string } {
// Find class by ID
const cls = db
···
return { success: false, error: "Already enrolled in this class" };
}
+
// Check if class has sections and require one to be selected
+
const sections = getClassSections(classId);
+
if (sections.length > 0 && !sectionId) {
+
return { success: false, error: "Please select a section" };
+
}
+
+
// If section provided, validate it exists and belongs to this class
+
if (sectionId) {
+
const section = sections.find((s) => s.id === sectionId);
+
if (!section) {
+
return { success: false, error: "Invalid section selected" };
+
}
+
}
+
// Enroll user
db.query(
-
"INSERT INTO class_members (class_id, user_id, enrolled_at) VALUES (?, ?, ?)",
-
).run(cls.id, userId, Math.floor(Date.now() / 1000));
+
"INSERT INTO class_members (class_id, user_id, section_id, enrolled_at) VALUES (?, ?, ?, ?)",
+
).run(cls.id, userId, sectionId ?? null, Math.floor(Date.now() / 1000));
return { success: true };
+
}
+
+
/**
+
* Create a section for a class
+
*/
+
export function createClassSection(
+
classId: string,
+
sectionNumber: string,
+
): ClassSection {
+
const id = nanoid();
+
const now = Math.floor(Date.now() / 1000);
+
+
db.run(
+
"INSERT INTO class_sections (id, class_id, section_number, created_at) VALUES (?, ?, ?, ?)",
+
[id, classId, sectionNumber, now],
+
);
+
+
return {
+
id,
+
class_id: classId,
+
section_number: sectionNumber,
+
created_at: now,
+
};
+
}
+
+
/**
+
* Get all sections for a class
+
*/
+
export function getClassSections(classId: string): ClassSection[] {
+
return db
+
.query<ClassSection, [string]>(
+
"SELECT * FROM class_sections WHERE class_id = ? ORDER BY section_number ASC",
+
)
+
.all(classId);
+
}
+
+
/**
+
* Delete a class section
+
*/
+
export function deleteClassSection(sectionId: string): void {
+
db.run("DELETE FROM class_sections WHERE id = ?", [sectionId]);
+
}
+
+
/**
+
* Get user's enrolled section for a class
+
*/
+
export function getUserSection(userId: number, classId: string): string | null {
+
const result = db
+
.query<{ section_id: string | null }, [string, number]>(
+
"SELECT section_id FROM class_members WHERE class_id = ? AND user_id = ?",
+
)
+
.get(classId, userId);
+
return result?.section_id ?? null;
}
/**