Hooked up calendar viewing to API, still needs a lot more polish

This commit is contained in:
2025-11-23 17:00:47 -05:00
parent b8bf809c14
commit 531371d059
11 changed files with 312 additions and 240 deletions

View File

@@ -46,6 +46,7 @@ const { status, memberStatus } = require('./routes/statuses')
const authRouter = require('./routes/auth')
const { roles, memberRoles } = require('./routes/roles');
const { courseRouter, eventRouter } = require('./routes/course');
const { calendarRouter } = require('./routes/calendar')
const morgan = require('morgan');
app.use('/application', applicationsRouter);
@@ -59,6 +60,7 @@ app.use('/roles', roles)
app.use('/memberRoles', memberRoles)
app.use('/course', courseRouter)
app.use('/courseEvent', eventRouter)
app.use('/calendar', calendarRouter)
app.use('/', authRouter)
app.get('/ping', (req, res) => {

View File

@@ -1,4 +1,6 @@
import { Request, Response } from "express";
import { getEventAttendance, getEventDetails, getShortEventsInRange } from "../services/calendarService";
import { CalendarEvent } from "@app/shared/types/calendar";
const express = require('express');
const r = express.Router();
@@ -9,16 +11,24 @@ function addMonths(date: Date, months: number): Date {
return d
}
//get calendar events paged
//get calendar events paged, requires a query string with from= and to= as mariadb ISO strings
r.get('/', async (req, res) => {
const viewDate: Date = req.body.date;
//generate date range
const backDate: Date = addMonths(viewDate, -1);
const frontDate: Date = addMonths(viewDate, 2);
try {
const fromDate: string = req.query.from;
const toDate: string = req.query.to;
const events = getShortEventsInRange(backDate, frontDate);
if (fromDate === undefined || toDate === undefined) {
res.status(400).send("Missing required query parameters 'from' and 'to'");
return;
}
res.status(200).json(events);
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) => {
@@ -26,19 +36,17 @@ r.get('/upcoming', async (req, res) => {
})
//get event details
r.get('/:id', async (req, res) => {
r.get('/:id', async (req: Request, res: Response) => {
try {
const eventID: number = req.params.id;
const eventID: number = Number(req.params.id);
let details = getEventDetails(eventID);
let attendance = await getEventAttendance(eventID);
let out = { ...details, attendance }
console.log(out);
res.status(200).json(out);
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);
res.status(500).json(err);
}
})
@@ -47,4 +55,4 @@ r.post('/', async (req, res) => {
})
module.exports.calendar = r;
module.exports.calendarRouter = r;

View File

@@ -1,6 +0,0 @@
export declare function createEvent(eventObject: any): Promise<void>;
export declare function updateEvent(eventObject: any): Promise<void>;
export declare function cancelEvent(eventID: any): Promise<void>;
export declare function getShortEventsInRange(startDate: any, endDate: any): Promise<void>;
export declare function getEventDetailed(eventID: any): Promise<void>;
//# sourceMappingURL=calendarService.d.ts.map

View File

@@ -1 +0,0 @@
{"version":3,"file":"calendarService.d.ts","sourceRoot":"","sources":["calendarService.ts"],"names":[],"mappings":"AAEA,wBAAsB,WAAW,CAAC,WAAW,KAAA,iBAE5C;AAED,wBAAsB,WAAW,CAAC,WAAW,KAAA,iBAE5C;AAED,wBAAsB,WAAW,CAAC,OAAO,KAAA,iBAExC;AAED,wBAAsB,qBAAqB,CAAC,SAAS,KAAA,EAAE,OAAO,KAAA,iBAE7D;AAED,wBAAsB,gBAAgB,CAAC,OAAO,KAAA,iBAE7C"}

View File

@@ -1,19 +0,0 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createEvent = createEvent;
exports.updateEvent = updateEvent;
exports.cancelEvent = cancelEvent;
exports.getShortEventsInRange = getShortEventsInRange;
exports.getEventDetailed = getEventDetailed;
const pool = require('../db');
async function createEvent(eventObject) {
}
async function updateEvent(eventObject) {
}
async function cancelEvent(eventID) {
}
async function getShortEventsInRange(startDate, endDate) {
}
async function getEventDetailed(eventID) {
}
//# sourceMappingURL=calendarService.js.map

View File

@@ -1 +0,0 @@
{"version":3,"file":"calendarService.js","sourceRoot":"","sources":["calendarService.ts"],"names":[],"mappings":";;AAEA,kCAEC;AAED,kCAEC;AAED,kCAEC;AAED,sDAEC;AAED,4CAEC;AApBD,MAAM,IAAI,GAAG,OAAO,CAAC,OAAO,CAAC,CAAA;AAEtB,KAAK,UAAU,WAAW,CAAC,WAAW;AAE7C,CAAC;AAEM,KAAK,UAAU,WAAW,CAAC,WAAW;AAE7C,CAAC;AAEM,KAAK,UAAU,WAAW,CAAC,OAAO;AAEzC,CAAC;AAEM,KAAK,UAAU,qBAAqB,CAAC,SAAS,EAAE,OAAO;AAE9D,CAAC;AAEM,KAAK,UAAU,gBAAgB,CAAC,OAAO;AAE9C,CAAC"}

View File

@@ -1,18 +1,5 @@
import pool from '../db';
export interface CalendarEvent {
id: number;
name: string;
start: Date; // DATETIME -> Date
end: Date; // DATETIME -> Date
location: string;
color: string; // 7 character hex string
description?: string | null;
creator?: number | null; // foreign key to members.id, nullable
cancelled: boolean; // TINYINT(1) -> boolean
created_at: Date; // TIMESTAMP -> Date
updated_at: Date; // TIMESTAMP -> Date
}
import { CalendarEventShort, CalendarSignup, CalendarEvent } from "@app/shared/types/calendar"
export type Attendance = 'attending' | 'maybe' | 'not_attending';
@@ -29,7 +16,7 @@ export async function createEvent(eventObject: Omit<CalendarEvent, 'id' | 'creat
eventObject.location,
eventObject.color,
eventObject.description ?? null,
eventObject.creator,
eventObject.creator_id,
];
const result = await pool.query(sql, params);
@@ -78,17 +65,19 @@ export async function cancelEvent(eventID: number) {
}
export async function getShortEventsInRange(startDate: Date, endDate: Date) {
export async function getShortEventsInRange(startDate: string, endDate: string): Promise<CalendarEventShort[]> {
const sql = `
SELECT id, name, start, end, color
FROM calendar_events
WHERE start BETWEEN ? AND ?
ORDER BY start ASC
`;
return await pool.query(sql, [startDate, endDate]);
const res: CalendarEventShort[] = await pool.query(sql, [startDate, endDate]);
console.log(res);
return res;
}
export async function getEventDetails(eventID: number) {
export async function getEventDetails(eventID: number): Promise<CalendarEvent> {
const sql = `
SELECT
e.id,
@@ -101,14 +90,14 @@ export async function getEventDetails(eventID: number) {
e.cancelled,
e.created_at,
e.updated_at,
m.id AS creator_id,
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 = ?
`;
return await pool.query(sql, [eventID])
let vals: CalendarEvent[] = await pool.query(sql, [eventID]);
return vals[0];
}
export async function getUpcomingEvents(date: Date, limit: number) {
@@ -135,7 +124,7 @@ export async function setAttendanceStatus(memberID: number, eventID: number, sta
return { success: true }
}
export async function getEventAttendance(eventID: number) {
export async function getEventAttendance(eventID: number): Promise<CalendarSignup[]> {
const sql = `
SELECT
s.member_id,