Skip to main content

Reporting Module

Location: backend/src/reporting/

The reporting module handles the persistence layer for student reports -generating report books, managing report status workflows, and storing/serving exported files (PDFs, CSVs, Excel). It works alongside the Calculation Module, which provides the live grade data.

Files

FilePurpose
reporting.module.tsModule definition
report.controller.tsAPI endpoints for reports and report entries
report.service.tsBusiness logic for report generation, updates, and file management
report.guard.tsPrevents modification of reports after ministry submission
dto/generate-report.dto.tsDTO for batch report generation
dto/update-report.dto.tsDTO for updating report metadata (remarks, conduct, attendance)
dto/update-report-entry.dto.tsDTO for updating per-subject entries (letter grade, remark)
dto/save-pdf.dto.tsDTO for recording a PDF file reference

Key Concepts

Report Book

A report_book row represents a single student's report for a specific term. It stores:

  • Metadata: student, term, class, report type, academic year
  • Aggregates: overall average, class position, total students
  • Class teacher fields: remark, conduct, attendance percentage
  • Status: draftpublishedsent_to_ministry

Report Book Entries

Each report book has multiple report_book_entry rows -one per subject. Entries store the persisted snapshot of grades at generation time plus editable fields:

FieldSourceEditable
coursework_averageCalculated at generation timeNo
exam_averageCalculated at generation timeNo
term_compositeCalculated at generation timeNo
year_gradeCalculated at generation timeNo
letter_gradeTeacher inputYes
teacher_remarkTeacher inputYes

Live Grades vs Persisted Grades

The frontend displays live calculated grades from the /calculations/ endpoints. The reporting schema is used for:

  • Persisting metadata (remarks, letter grades, conduct, attendance)
  • Managing report status workflow (draft → published → ministry)
  • Storing and serving exported files (PDF, CSV, XLSX)
  • Recording PDF generation history

Report Status Workflow

draft → published → sent_to_ministry (locked)

Once a report is marked sent_to_ministry, the ReportGuard prevents any further modifications.

Report Types

TypeDescription
termSingle-term report
year_endYear-end report (year_based grading model only)

File Storage

Exported files (PDFs, CSVs, XLSX) are stored in the report-books Supabase Storage bucket. The module handles:

  • Individual report PDFs: uploaded per-student via multipart upload
  • Class summary files: PDF, CSV, and XLSX uploaded per-class-per-term

API Endpoints

All endpoints require AuthGuard. Most also require ClassTeacherGuard (admin or class teacher).

Report Generation

POST /api/reports/generate

Guards: AuthGuard, ClassTeacherGuard

Generates (or regenerates) report book entries for all students in a class for a given term. Calls the calculation engine internally to compute grades.

Body (GenerateReportDto):

FieldRequiredDescription
studentGroupIdYesClass UUID
termIdYesTerm UUID
reportTypeYesterm or year_end

Report Queries

GET /api/reports

Guards: AuthGuard, ClassTeacherGuard

Lists all report books for a class + term combination.

ParamRequiredDescription
studentGroupIdYesClass UUID
termIdYesTerm UUID
reportTypeNoFilter by term or year_end

GET /api/reports/:id

Guards: AuthGuard, ClassTeacherGuard

Returns a single report book with all entries, student info, and PDF history.

GET /api/reports/student

Guards: AuthGuard, ClassTeacherGuard

Finds a student's report by student ID, term, and report type.

ParamRequiredDescription
studentIdYesStudent UUID
termIdYesTerm UUID
reportTypeYesterm or year_end

Report Updates

PATCH /api/reports/:id

Guards: AuthGuard, ClassTeacherGuard, ReportGuard

Updates class teacher metadata on a report.

Body (UpdateReportDto):

FieldTypeDescription
classTeacherRemarkstringClass teacher's remark
conductstringe.g., "Excellent"
attendancePercentagenumber0–100

PATCH /api/reports/:id/regenerate

Guards: AuthGuard, ClassTeacherGuard, ReportGuard

Re-runs grade calculations for a single report and updates all entries.

PATCH /api/reports/:id/publish

Guards: AuthGuard, ClassTeacherGuard, ReportGuard

Sets status to published.

PATCH /api/reports/:id/send-to-ministry

Guards: AuthGuard, ClassTeacherGuard

Sets status to sent_to_ministry. The report becomes locked -the ReportGuard blocks further edits.

Report Entry Updates

PATCH /api/report-entries/:entryId

Guards: AuthGuard, ReportGuard

Updates editable fields on a single subject entry.

Body (UpdateReportEntryDto):

FieldTypeDescription
letterGradestringe.g., "A", "B+"
teacherRemarkstringSubject-specific comment

PDF Management

POST /api/reports/:id/pdf

Guards: AuthGuard, ClassTeacherGuard, ReportGuard

Records a PDF file reference (path + size) without uploading.

POST /api/reports/:id/pdf/upload

Guards: AuthGuard, ClassTeacherGuard, ReportGuard

Uploads a PDF blob to Supabase Storage and records the reference. Accepts multipart form data with the file and an optional objectPath field.

GET /api/reports/:id/pdfs

Returns the PDF generation history for a report.

GET /api/reports/:id/pdf/latest

Returns the most recent PDF record.

GET /api/reports/:id/pdf/:pdfId/download

Downloads a PDF file from Supabase Storage. Returns the file as a binary response with appropriate headers.

Class Summary Files

POST /api/reports/class-summary/upload

Guards: AuthGuard, ClassTeacherGuard

Uploads a class summary file (PDF, CSV, or XLSX) to Supabase Storage.

Accepts multipart form data with fields: studentGroupId, termId, reportType, fileType.

GET /api/reports/class-summary/download

Downloads a stored class summary file by type.

ParamRequiredDescription
studentGroupIdYesClass UUID
termIdYesTerm UUID
reportTypeYesterm or year_end
fileTypeYespdf, csv, or xlsx

GET /api/reports/class-summary/files

Lists all stored class summary files for a class + term + report type.

Database Tables

reporting.report_book

ColumnTypeDescription
iduuidPrimary key
student_iduuidFK to student
academic_year_iduuidFK to academic year
term_iduuidFK to term
student_group_iduuidFK to class
report_typetextterm or year_end
statustextdraft, published, sent_to_ministry
overall_averagenumericComputed at generation time
positionintegerClass ranking
total_studentsintegerClass size at generation time
class_teacher_remarktextEditable by class teacher
conducttextEditable by class teacher
attendance_percentagenumericEditable by class teacher

reporting.report_book_entry

ColumnTypeDescription
iduuidPrimary key
report_book_iduuidFK to report_book
subject_iduuidFK to subject
coursework_averagenumericSnapshot from calculation
exam_averagenumericSnapshot from calculation
term_compositenumericSnapshot from calculation
year_gradenumericSnapshot from calculation
letter_gradetextTeacher input
teacher_remarktextTeacher input
sort_orderintegerDisplay ordering

reporting.report_book_pdf

ColumnTypeDescription
iduuidPrimary key
report_book_iduuidFK to report_book
file_pathtextSupabase Storage object path
file_sizeintegerFile size in bytes
generated_byuuidFK to user_profile
generated_attimestamptzWhen the PDF was generated

reporting.class_report_file

ColumnTypeDescription
iduuidPrimary key
student_group_iduuidFK to class
term_iduuidFK to term
report_typetextterm or year_end
file_typetextpdf, csv, or xlsx
file_pathtextSupabase Storage object path
file_sizeintegerFile size in bytes
generated_byuuidFK to user_profile
generated_attimestamptzUpload timestamp