Files
milsim-site-v4/api/src/routes/calendar.ts
2025-11-25 13:11:08 -05:00

71 lines
2.1 KiB
TypeScript

import { Request, Response } from "express";
import { getEventAttendance, getEventDetails, getShortEventsInRange, setAttendanceStatus } from "../services/calendarService";
import { CalendarAttendance, CalendarEvent } from "@app/shared/types/calendar";
const express = require('express');
const r = express.Router();
function addMonths(date: Date, months: number): Date {
const d = new Date(date)
d.setMonth(d.getMonth() + months)
return d
}
//get calendar events paged, requires a query string with from= and to= as mariadb ISO strings
r.get('/', async (req, res) => {
try {
const fromDate: string = req.query.from;
const toDate: string = req.query.to;
if (fromDate === undefined || toDate === undefined) {
res.status(400).send("Missing required query parameters 'from' and 'to'");
return;
}
const events = await getShortEventsInRange(fromDate, toDate);
res.status(200).json(events);
} catch (error) {
console.error('Error fetching calendar events:', error);
res.status(500).send('Error fetching calendar events');
}
});
r.get('/upcoming', async (req, res) => {
res.sendStatus(501);
})
r.post('/:id/attendance', async (req: Request, res: Response) => {
try {
let member = req.user.id;
let event = Number(req.params.id);
let state = req.query.state as CalendarAttendance;
setAttendanceStatus(member, event, state);
res.sendStatus(200);
} catch (error) {
console.error('Failed to set attendance:', error);
res.status(500).json(error);
}
})
//get event details
r.get('/:id', async (req: Request, res: Response) => {
try {
const eventID: number = Number(req.params.id);
let details: CalendarEvent = await getEventDetails(eventID);
details.eventSignups = await getEventAttendance(eventID);
console.log(details);
res.status(200).json(details);
} catch (err) {
console.error('Insert failed:', err);
res.status(500).json(err);
}
})
//post a new calendar event
r.post('/', async (req, res) => {
})
module.exports.calendarRouter = r;