Skip to content

Commit

Permalink
hot commit for fremvisning
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgengaldal committed Sep 27, 2024
1 parent fcb7d4c commit 6afc3e3
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 23 deletions.
27 changes: 7 additions & 20 deletions components/RoomOverview.tsx → components/admin/RoomOverview.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,14 @@
import { useState } from "react";
import { periodType, committeeInterviewType } from "../lib/types/types";
import Button from "./Button";
import Table, { RowType } from "./Table"
import { ColumnType } from "./Table";
import TextInput from "./form/TextInput";
import DateInput from "./form/DateInput";
import TimeRangeInput from "./form/TimeRangeInput";
import { periodType, committeeInterviewType, RoomBooking } from "../../lib/types/types";
import Button from "../Button";
import Table, { RowType } from "../Table"
import { ColumnType } from "../Table";
import TextInput from "../form/TextInput";
import DateInput from "../form/DateInput";
import TimeRangeInput from "../form/TimeRangeInput";

import toast from "react-hot-toast";

interface Interview {
id: string;
title: string;
start: string;
end: string;
}

interface RoomBooking {
room: String,
startDate: String
endDate: String
}

interface Props {
period: periodType | null;
}
Expand Down
15 changes: 14 additions & 1 deletion lib/api/periodApi.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { QueryFunctionContext } from "@tanstack/react-query";
import { periodType } from "../types/types";
import { periodType, RoomBooking } from "../types/types";

export const fetchPeriodById = async (context: QueryFunctionContext) => {
const id = context.queryKey[1];
Expand All @@ -25,3 +25,16 @@ export const createPeriod = async (period: periodType) => {
},
});
};

export const updateRoomsForPeriod = async (
id: string,
rooms: RoomBooking[]
) => {
return fetch(`/api/periods${id}`, {
method: "PATCH",
body: JSON.stringify(rooms),
headers: {
"Content-Type": "application/json",
},
});
};
28 changes: 27 additions & 1 deletion lib/mongo/periods.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Collection, Db, MongoClient, ObjectId } from "mongodb";
import clientPromise from "./mongodb";
import { periodType } from "../types/types";
import { periodType, RoomBooking } from "../types/types";

let client: MongoClient;
let db: Db;
Expand Down Expand Up @@ -85,6 +85,32 @@ export const getCurrentPeriods = async () => {
}
};

export const updateRoomsForPeriod = async (
id: string | ObjectId,
rooms: RoomBooking[]
) => {
try {
if (!periods) await init();

// Checks if period exists
const period = await getPeriodById(id);
if (!period.exists) {
return { error: "Period not found" };
}

const response = await periods.updateOne(
{ _id: new ObjectId(id) },
{ $set: { rooms: rooms } }
);

return response.modifiedCount >= 1
? { message: "Period updated with rooms" }
: { message: "Period not updated" };
} catch (error) {
return { error: "Failed to update rooms for period" };
}
};

export const getPeriodById = async (id: string | ObjectId) => {
try {
if (!periods) await init();
Expand Down
7 changes: 7 additions & 0 deletions lib/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export type periodType = {
committees: string[];
optionalCommittees: string[];
hasSentInterviewTimes: boolean;
rooms?: RoomBooking[];
};

export type AvailableTime = {
Expand Down Expand Up @@ -141,3 +142,9 @@ export type emailApplicantInterviewType = {
};
}[];
};

export interface RoomBooking {
room: String;
startDate: String;
endDate: String;
}
6 changes: 6 additions & 0 deletions lib/utils/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
committeeInterviewType,
periodType,
preferencesType,
RoomBooking,
} from "../types/types";

export const isApplicantType = (
Expand Down Expand Up @@ -191,3 +192,8 @@ export const isPeriodType = (data: any): data is periodType => {

return hasBasicFields;
};

export const isRoomBookings = (data: any): data is RoomBooking => {
// TODO: Implement
return true;
};
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"mongodb": "^6.1.0",
"next": "^12.3.4",
"next-auth": "^4.24.5",
"online-opptak": "file:",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-hot-toast": "^2.4.1",
Expand Down
2 changes: 1 addition & 1 deletion pages/admin/[period-id]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import router from "next/router";
import { periodType } from "../../../lib/types/types";
import NotFound from "../../404";
import ApplicantsOverview from "../../../components/applicantoverview/ApplicantsOverview";
import RoomOverview from "../../../components/RoomOverview";
import RoomOverview from "../../../components/admin/RoomOverview";
import { Tabs } from "../../../components/Tabs";
import { CalendarIcon, InboxIcon, BuildingOffice2Icon } from "@heroicons/react/24/solid";
import Button from "../../../components/Button";
Expand Down
18 changes: 18 additions & 0 deletions pages/api/periods/[id].ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { getServerSession } from "next-auth";
import { authOptions } from "../auth/[...nextauth]";
import { deletePeriodById, getPeriodById } from "../../../lib/mongo/periods";
import { hasSession, isAdmin } from "../../../lib/utils/apiChecks";
import { updateRoomsForPeriod } from "../../../lib/api/periodApi";
import { isRoomBookings } from "../../../lib/utils/validators";
import { RoomBooking } from "../../../lib/types/types";

const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const session = await getServerSession(req, res, authOptions);
Expand All @@ -28,7 +31,22 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
}

return res.status(200).json({ exists, period });
} else if (req.method === "PATCH") {
if (!isAdmin(res, session)) {
return res.status(403).json({ error: "Unauthorized" });
}

if (!isRoomBookings(req.body)) {
return res.status(400).json({ error: "Invalid data format" });
}

const bookings: RoomBooking[] = req.body as RoomBooking[];

const { error } = await updateRoomsForPeriod(id, bookings);
if (error) throw new Error(error);
return res.status(200).json({ message: updated });
} else if (req.method === "DELETE") {
// TODO: The next line is probably supposed to be !isAdmin(res, session)?
if (!isAdmin) return res.status(403).json({ error: "Unauthorized" });

const { error } = await deletePeriodById(id);
Expand Down

0 comments on commit 6afc3e3

Please sign in to comment.