Skip to content

Commit

Permalink
Do event mapping in ics file
Browse files Browse the repository at this point in the history
  • Loading branch information
ChinemeremChigbo committed Nov 25, 2024
1 parent 83b626f commit 2ae1b52
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 41 deletions.
73 changes: 34 additions & 39 deletions app/api/getAbsences/route.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,61 @@
import { prisma } from '../../../utils/prisma';
import { NextResponse } from 'next/server';

export interface AbsenceWithRelations {
lessonDate: Date;
lessonPlan: string | null;
reasonOfAbsence: string;
notes: string | null;
absentTeacher: {
firstName: string;
lastName: string;
email: string;
};
substituteTeacher: {
firstName: string;
lastName: string;
email: string;
} | null;
location: {
name: string;
abbreviation: string;
};
subject: {
name: string;
abbreviation: string;
};
}

export async function GET() {
try {
const absences = await prisma.absence.findMany({
const absences: AbsenceWithRelations[] = await prisma.absence.findMany({
select: {
id: true,
lessonDate: true,
subject: true,
subject: {
select: {
name: true,
abbreviation: true,
},
},
lessonPlan: true,
reasonOfAbsence: true,
notes: true,
absentTeacher: {
select: {
id: true,
firstName: true,
lastName: true,
email: true,
},
},
substituteTeacher: {
select: {
id: true,
firstName: true,
lastName: true,
email: true,
},
},
location: {
select: {
id: true,
name: true,
abbreviation: true,
},
Expand All @@ -40,39 +67,7 @@ export async function GET() {
return NextResponse.json({ events: [] }, { status: 200 });
}

const events = absences.map((absence) => {
function extractTimeArray(date: Date, offset = 0) {
const newDate = new Date(date);
newDate.setDate(date.getDate() + offset);
return [
newDate.getFullYear(),
newDate.getMonth() + 1,
newDate.getDate(),
];
}
if (absence?.substituteTeacher) {
var substituteTeacherString = `(${absence.substituteTeacher?.firstName} ${absence.substituteTeacher?.lastName[0]})`;
} else {
var substituteTeacherString = '';
}

if (absence?.lessonPlan) {
var lessonString = absence.lessonPlan;
} else {
var lessonString = 'Lesson Plan Not Submitted.';
}

return {
start: extractTimeArray(absence.lessonDate),
end: extractTimeArray(absence.lessonDate, 1),
title:
`${absence.subject.abbreviation}: ${absence.absentTeacher.firstName} ${absence.absentTeacher.lastName[0]}` +
substituteTeacherString,
description: `Subject: ${absence.subject.name}\nLesson Plan: ${lessonString}`,
location: absence.location.name,
};
});
return NextResponse.json({ events }, { status: 200 });
return NextResponse.json({ events: absences }, { status: 200 });
} catch (err) {
console.error('Error in GET /api/getAbsences:', err.message || err);
return NextResponse.json(
Expand Down
31 changes: 29 additions & 2 deletions src/pages/ics.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { useState } from 'react';
import { EventAttributes, createEvents } from 'ics';
import React, { useState } from 'react';
import { AbsenceWithRelations } from '../../app/api/getAbsences/route';

export default function CalendarDownload() {
const [error, setError] = useState<string | null>(null);
Expand All @@ -14,7 +15,33 @@ export default function CalendarDownload() {
if (!data.events || !Array.isArray(data.events)) {
throw new Error('Invalid data format.');
}
return data.events;
return data.events.map((absence: AbsenceWithRelations) => {
const substituteTeacherString = absence.substituteTeacher
? `(${absence.substituteTeacher.firstName} ${absence.substituteTeacher.lastName[0]})`
: '';
const lessonString = absence.lessonPlan || 'Lesson Plan Not Submitted';
const notesLine = absence.notes ? `\nNotes: ${absence.notes}` : '';

const startDate = new Date(absence.lessonDate);
const endDate = new Date(absence.lessonDate);
endDate.setDate(startDate.getDate() + 1);

return {
start: [
startDate.getFullYear(),
startDate.getMonth() + 1,
startDate.getDate(),
],
end: [
endDate.getFullYear(),
endDate.getMonth() + 1,
endDate.getDate(),
],
title: `${absence.subject.name}: ${absence.absentTeacher.firstName} ${absence.absentTeacher.lastName[0]}${substituteTeacherString}`,
description: `Subject: ${absence.subject.name}\nLesson Plan: ${lessonString}${notesLine}`,
location: absence.location.name,
};
});
} catch (err) {
console.error('Error fetching absences:', err);
throw err;
Expand Down

0 comments on commit 2ae1b52

Please sign in to comment.