import { query } from "@/lib/db"; import type { TaxonomyRef } from "@/lib/types"; export interface CourseCategoryRecord extends TaxonomyRef { sortOrder: number; showInFilter: boolean; published: boolean; } export interface CourseTagRecord extends TaxonomyRef { sortOrder: number; showInNav: boolean; showInFilter: boolean; published: boolean; } type CategoryRow = { id: string; slug: string; label: string; sort_order: number; show_in_filter: boolean; published: boolean; }; type TagRow = { id: string; slug: string; label: string; sort_order: number; show_in_nav: boolean; show_in_filter: boolean; published: boolean; }; function mapCategory(row: CategoryRow): CourseCategoryRecord { return { id: row.id, slug: row.slug, label: row.label, sortOrder: row.sort_order, showInFilter: row.show_in_filter, published: row.published, }; } function mapTag(row: TagRow): CourseTagRecord { return { id: row.id, slug: row.slug, label: row.label, sortOrder: row.sort_order, showInNav: row.show_in_nav, showInFilter: row.show_in_filter, published: row.published, }; } export async function listPublishedCourseCategories(opts?: { filterOnly?: boolean; }): Promise { const filterOnly = opts?.filterOnly ?? false; const res = await query( `SELECT * FROM course_categories WHERE published = true AND ($1::boolean = false OR show_in_filter = true) ORDER BY sort_order ASC, label ASC`, [filterOnly], ); return res.rows.map(mapCategory); } export async function listPublishedCourseTags(opts?: { navOnly?: boolean; filterOnly?: boolean; }): Promise { const navOnly = opts?.navOnly ?? false; const filterOnly = opts?.filterOnly ?? false; const res = await query( `SELECT * FROM course_tags WHERE published = true AND ($1::boolean = false OR show_in_nav = true) AND ($2::boolean = false OR show_in_filter = true) ORDER BY sort_order ASC, label ASC`, [navOnly, filterOnly], ); return res.rows.map(mapTag); }