import pool from '../db'; import { CalendarEventShort, CalendarSignup, CalendarEvent, CalendarAttendance } from "@app/shared/types/calendar" import { toDateTime } from "@app/shared/utils/time" export async function createEvent(eventObject: Omit) { const sql = ` INSERT INTO calendar_events (name, start, end, location, color, description, creator) VALUES (?, ?, ?, ?, ?, ?, ?) `; const params = [ eventObject.name, eventObject.start, eventObject.end, eventObject.location, eventObject.color, eventObject.description ?? null, eventObject.creator_id, ]; const result = await pool.query(sql, params); return { id: result.insertId, ...eventObject }; } export async function updateEvent(eventObject: CalendarEvent) { if (!eventObject.id) { throw new Error("updateEvent: Missing event ID."); } const sql = ` UPDATE calendar_events SET name = ?, start = ?, end = ?, location = ?, color = ?, description = ?, WHERE id = ? `; const params = [ eventObject.name, toDateTime(eventObject.start), toDateTime(eventObject.end), eventObject.location, eventObject.color, eventObject.description ?? null, eventObject.id ]; await pool.query(sql, params); return { success: true }; } export async function cancelEvent(eventID: number) { const sql = ` UPDATE calendar_events SET cancelled = 1 WHERE id = ? `; await pool.query(sql, [eventID]); return { success: true }; } export async function getShortEventsInRange(startDate: string, endDate: string): Promise { const sql = ` SELECT id, name, start, end, color, cancelled FROM calendar_events WHERE start BETWEEN ? AND ? ORDER BY start ASC `; const res: CalendarEventShort[] = await pool.query(sql, [startDate, endDate]); console.log(res); return res; } export async function getEventDetails(eventID: number): Promise { const sql = ` SELECT e.id, e.name, e.start, e.end, e.location, e.color, e.description, e.cancelled, e.created_at, e.updated_at, e.creator AS creator_id, m.name AS creator_name FROM calendar_events e LEFT JOIN members m ON e.creator = m.id WHERE e.id = ? `; let vals: CalendarEvent[] = await pool.query(sql, [eventID]); return vals[0]; } export async function getUpcomingEvents(date: Date, limit: number) { const sql = ` SELECT id, name, start, end, color FROM calendar_events WHERE start >= ? AND cancelled = 0 ORDER BY start ASC LIMIT ? `; return await pool.query(sql, [date, limit]); } export async function setAttendanceStatus(memberID: number, eventID: number, status: CalendarAttendance) { const sql = ` INSERT INTO calendar_events_signups (member_id, event_id, status) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE status = VALUES(status), updated_at = CURRENT_TIMESTAMP `; await pool.query(sql, [memberID, eventID, status]); return { success: true } } export async function getEventAttendance(eventID: number): Promise { const sql = ` SELECT s.member_id, s.status, m.name AS member_name FROM calendar_events_signups s LEFT JOIN members m ON s.member_id = m.id WHERE s.event_id = ? `; return await pool.query(sql, [eventID]); }