import { env } from '$env/dynamic/private' import { error } from '@sveltejs/kit'; import type {CalendarEvent,Exam} from '$lib/data/CMSDataType' import {getBgColorDict} from '$lib/utils' import dayjs from 'dayjs' import type { PageServerLoad } from './$types' export const load: PageServerLoad = async ({fetch}) => { const res = await fetch(env.API_QUALIFYING_URL+"/qualifying") if(!res.ok) throw error(res.status, 'ไม่สามารถอ่านข้อมูลการสอบได้'); const exams:Exam[] = await res.json() const catColor = {}; const events:CalendarEvent[]=[] exams.forEach((ex)=>{ const {id,category,start} = ex const backgroundColor = category? getBgColorDict(category,catColor):"n/a" const url = "/qualifying/"+id if(start){ ex.date = dayjs(start).format("DD MMM BBBB") } if(!ex.announcementExam) return if(ex.exam_date){ const start = ex.exam_date events.push({id,"title":"วันสอบคัดเลือก",start,url,backgroundColor}) } if(ex.announcement_date){ const start = ex.announcement_date const end = ex.announcement_endDate events.push({id,"title":"ผลสอบคัดเลือก",start,end,url,backgroundColor}) } if(ex.register_startDate){ const start = ex.register_startDate const end = ex.register_endDate events.push({id,"title":"ผลสอบคัดเลือก",start,end,url,backgroundColor}) } if(ex.payment_startDate){ const start = ex.payment_startDate const end = ex.payment_endDate events.push({id,"title":"ผลสอบคัดเลือก",start,end,url,backgroundColor}) } }) return {exams,events} }