added service with base function to get course and event attendees

This commit is contained in:
2025-11-16 00:48:30 -05:00
parent 9a65784f8b
commit ca152f7955
5 changed files with 163 additions and 0 deletions

View File

@@ -0,0 +1,51 @@
import pool from "../db"
import { Course, CourseAttendee, RawAttendeeRow } from "@app/shared/types/course"
export async function getAllCourses(): Promise<Course[]> {
const sql = "SELECT * FROM courses WHERE deleted = false;"
const res = await pool.query(sql);
return res;
}
function buildAttendee(row: RawAttendeeRow): CourseAttendee {
return {
passed: !!row.passed,
attendee_id: row.attendee_id,
course_event_id: row.course_event_id,
created_at: row.created_at,
updated_at: row.updated_at,
remarks: row.remarks,
attendee_role_id: row.attendee_role_id,
role: row.role_id
? {
id: row.role_id,
name: row.role_name,
description: row.role_description,
deleted: !!row.role_deleted,
created_at: row.role_created_at,
updated_at: row.role_updated_at,
}
: null
};
}
export async function getCourseAttendees(id: number): Promise<CourseAttendee[]> {
const sql = `SELECT
ca.*,
ar.id AS role_id,
ar.name AS role_name,
ar.description AS role_description,
ar.deleted AS role_deleted,
ar.created_at AS role_created_at,
ar.updated_at AS role_updated_at
FROM course_attendees ca
LEFT JOIN course_attendee_roles ar ON ar.id = ca.attendee_role_id
WHERE ca.course_event_id = ?;`;
const res:RawAttendeeRow[] = await pool.query(sql, [id]);
return res.map((row) => buildAttendee(row))
}