Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 14 additions & 19 deletions components/committee/CommitteeInterviewTimes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useSession } from "next-auth/react";
import FullCalendar from "@fullcalendar/react";
import timeGridPlugin from "@fullcalendar/timegrid";
import interactionPlugin from "@fullcalendar/interaction";
import { periodType, committeeInterviewType } from "../../lib/types/types";
import { periodType, committeeInterviewType, AvailableTime } from "../../lib/types/types";
import toast from "react-hot-toast";
import NotFound from "../../pages/404";
import Button from "../Button";
Expand All @@ -19,8 +19,8 @@ import { XMarkIcon } from "@heroicons/react/24/solid";
interface Interview {
id: string;
title: string;
start: string;
end: string;
start: Date;
end: Date;
}

interface Props {
Expand Down Expand Up @@ -63,8 +63,8 @@ const CommitteeInterviewTimes = ({
useEffect(() => {
if (period) {
setVisibleRange({
start: new Date(period!.interviewPeriod.start).toISOString(),
end: new Date(period!.interviewPeriod.end).toISOString(),
start: period.interviewPeriod.start.toISOString(),
end: period.interviewPeriod.end.toISOString(),
});
}
}, [period]);
Expand Down Expand Up @@ -98,11 +98,11 @@ const CommitteeInterviewTimes = ({
if (cleanCommittee === cleanSelectedCommittee) {
setHasAlreadySubmitted(true);
const events = committeeInterviewTimes.availabletimes.map(
(at: any) => ({
(availableTime: AvailableTime) => ({
id: crypto.getRandomValues(new Uint32Array(1))[0].toString(),
title: at.room,
start: new Date(at.start).toISOString(),
end: new Date(at.end).toISOString(),
title: availableTime.room,
start: availableTime.start.toISOString(),
end: availableTime.end.toISOString(),
})
);

Expand Down Expand Up @@ -262,17 +262,12 @@ const CommitteeInterviewTimes = ({
);
};

const formatEventsForExport = (events: Interview[]) => {
return events.map((event) => {
const startDateTime = new Date(event.start);
const endDateTime = new Date(event.end);
return {
const formatEventsForExport = (events: Interview[]) =>
events.map((event) => ({
room: event.title,
start: startDateTime.toISOString(),
end: endDateTime.toISOString(),
};
});
};
start: event.start.toISOString(),
end: event.end.toISOString(),
}));

const handleTimeslotSelection = (e: React.ChangeEvent<HTMLSelectElement>) => {
setSelectedTimeslot(e.target.value);
Expand Down
25 changes: 21 additions & 4 deletions components/form/DatePickerInput.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
import { useEffect, useState } from "react";
import { fromZonedTime } from 'date-fns-tz';
import { timezone } from "../../lib/utils/dateUtils";

interface Props {
label?: string;
updateDates: (dates: { start: string; end: string }) => void;
updateDates: (dates: { start: Date; end: Date }) => void;
}

const DatePickerInput = (props: Props) => {
const [fromDate, setFromDate] = useState("");
const [toDate, setToDate] = useState("");

useEffect(() => {
const startDate = fromDate ? `${fromDate}T00:00` : "";
const endDate = toDate ? `${toDate}T23:59` : "";
if (!fromDate || !toDate) return;


// Convert to Date objects in correct timezone
const startDate = fromZonedTime(
`${fromDate}T00:00:00`,
timezone
);
const endDate = fromZonedTime(
`${toDate}T23:59:59`,
timezone
);

props.updateDates({ start: startDate, end: endDate });
}, [fromDate, toDate]);

Expand Down Expand Up @@ -38,8 +52,11 @@ const DatePickerInput = (props: Props) => {
className="border text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5 border-gray-300 text-gray-900 dark:border-gray-600 dark:bg-online-darkBlue dark:text-gray-200"
/>
</div>
<p className="mt-1.5 text-xs text-gray-500 dark:text-gray-400">
NB: Alle tider er i norsk tidssone (GMT+1)
</p>
</div>
);
};

export default DatePickerInput;
export default DatePickerInput;
33 changes: 16 additions & 17 deletions lib/mongo/applicants.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Collection, Db, MongoClient, ObjectId } from "mongodb";
import clientPromise from "./mongodb";
import { applicantType, periodType, preferencesType } from "../types/types";
import { applicantType, Nullable, periodType, preferencesType } from "../types/types";
import { getPeriodById } from "./periods";
import { addDays, isAfter } from 'date-fns';

let client: MongoClient;
let db: Db;
Expand Down Expand Up @@ -153,7 +154,7 @@ export const getApplicantsForCommittee = async (
// Filtrerer søkerne slik at kun brukere som er i komiteen som har blitt søkt på ser søkeren
// Fjerner prioriterings informasjon
const filteredApplicants = result
.map((applicant) => {
.map((applicant: Nullable<applicantType>) => {
let preferencesArray: string[] = [];
if (isPreferencesType(applicant.preferences)) {
preferencesArray = [
Expand Down Expand Up @@ -182,24 +183,22 @@ export const getApplicantsForCommittee = async (

applicant.optionalCommittees = [];

const today = new Date();
const sevenDaysAfterInterviewEnd = new Date(period.interviewPeriod.end);
sevenDaysAfterInterviewEnd.setDate(
sevenDaysAfterInterviewEnd.getDate() + 5
);
const now = new Date();
const sevenDaysAfterInterviewEnd = addDays(period.interviewPeriod.end, 7);

// Sletter sensitiv informasjon etter intervju perioden + 7 dager, for å forhindre snoking i tidligere søknader
if (
new Date(period.applicationPeriod.end) > today ||
today > sevenDaysAfterInterviewEnd
isAfter(now, period.applicationPeriod.end) ||
isAfter(now, sevenDaysAfterInterviewEnd)
) {
applicant.owId = "Skjult";
applicant.name = "Skjult";
applicant.date = today;
applicant.phone = "Skjult";
applicant.email = "Skjult";
applicant.about = "Skjult";
applicant.grade = "-";
applicant.selectedTimes = [{ start: "Skjult", end: "Skjult" }];
applicant.owId = null;
applicant.name = null;
applicant.phone = null;
applicant.grade = null;
applicant.email = null;
applicant.about = null;
applicant.selectedTimes = null;
applicant.date = null;
}

const isSelectedCommitteePresent =
Expand Down
2 changes: 1 addition & 1 deletion lib/mongo/committees.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Collection, Db, MongoClient, ObjectId, UpdateResult } from "mongodb";
import { Collection, Db, MongoClient, ObjectId } from "mongodb";
import clientPromise from "./mongodb";
import { committeeInterviewType } from "../types/types";

Expand Down
14 changes: 7 additions & 7 deletions lib/mongo/periods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,19 +59,19 @@ export const getCurrentPeriods = async () => {
try {
if (!periods) await init();

const currentDate = new Date().toISOString();
const now = new Date();

const filter = {
$or: [
{
// Check if current ISO date string is within the application period
"applicationPeriod.start": { $lte: currentDate },
"applicationPeriod.end": { $gte: currentDate },
// Check if current date is within the application period
"applicationPeriod.start": { $lte: now },
"applicationPeriod.end": { $gte: now },
},
{
// Check if current ISO date string is within the interview period
"interviewPeriod.start": { $lte: currentDate },
"interviewPeriod.end": { $gte: currentDate },
// Check if current date is within the interview period
"interviewPeriod.start": { $lte: now },
"interviewPeriod.end": { $gte: now },
},
],
};
Expand Down
32 changes: 13 additions & 19 deletions lib/sendInterviewTimes/formatInterviewEmail.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { compareAsc } from "date-fns";
import {
emailApplicantInterviewType,
emailCommitteeInterviewType,
Expand All @@ -11,25 +12,22 @@ export const formatApplicantInterviewEmail = (
) => {
let emailBody = `<p>Hei <strong>${applicant.applicantName}</strong>,</p><p>Her er dine intervjutider for ${applicant.period_name}:</p><ul><br/>`;

applicant.committees.sort((a, b) => {
return (
new Date(a.interviewTime.start).getTime() -
new Date(b.interviewTime.start).getTime()
);
});
// Sort committees by interview start time
applicant.committees.sort((a, b) =>
compareAsc(a.interviewTime.start, b.interviewTime.start)
);

applicant.committees.forEach((committee) => {
emailBody += `<li><b>Komité:</b> ${changeDisplayName(
committee.committeeName
)}<br>`;

if (committee.interviewTime.start !== "Ikke satt") {
if (committee.interviewTime.start != null) {
emailBody += `<b>Tid:</b> ${formatDateHours(
committee.interviewTime.start,
committee.interviewTime.end
)}<br>`;
}
if (committee.interviewTime.start === "Ikke satt") {
} else {
emailBody += `<b>Tid:</b> Ikke satt. Komitéen vil ta kontakt med deg for å avtale tidspunkt.<br>`;
}

Expand All @@ -50,25 +48,21 @@ export const formatCommitteeInterviewEmail = (
committee.applicants.length
} søkere:</p><ul>`;

committee.applicants.sort((a, b) => {
return (
new Date(a.interviewTime.start).getTime() -
new Date(b.interviewTime.start).getTime()
);
});
// Sort applicants by interview start time
committee.applicants.sort((a, b) =>
compareAsc(a.interviewTime.start, b.interviewTime.start)
);

committee.applicants.forEach((applicant) => {
emailBody += `<li><b>Navn:</b> ${applicant.applicantName}<br>`;
emailBody += `<b>Telefon:</b> ${applicant.applicantPhone} <br> `;

if (applicant.interviewTime.start !== "Ikke satt") {
if (applicant.interviewTime.start != null) {
emailBody += `<b>Tid:</b> ${formatDateHours(
applicant.interviewTime.start,
applicant.interviewTime.end
)}<br>`;
}

if (applicant.interviewTime.start === "Ikke satt") {
} else {
emailBody += `<b>Tid:</b> Ikke satt. Ta kontakt med søker for å avtale tidspunkt.`;
}
emailBody += `<b>Rom:</b> ${applicant.interviewTime.room}</li><br>`;
Expand Down
17 changes: 7 additions & 10 deletions lib/sendInterviewTimes/formatInterviewSMS.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
import { compareAsc } from "date-fns";
import { emailApplicantInterviewType } from "../types/types";
import { formatDateHours } from "../utils/dateUtils";
import { changeDisplayName } from "../utils/toString";

export const formatInterviewSMS = (applicant: emailApplicantInterviewType) => {
let phoneBody = `Hei ${applicant.applicantName}, her er dine intervjutider for ${applicant.period_name}: \n \n`;

applicant.committees.sort((a, b) => {
return (
new Date(a.interviewTime.start).getTime() -
new Date(b.interviewTime.start).getTime()
);
});
// Sort committees by interview start time
applicant.committees.sort((a, b) =>
compareAsc(a.interviewTime.start, b.interviewTime.start)
);

applicant.committees.forEach((committee) => {
phoneBody += `Komité: ${changeDisplayName(committee.committeeName)} \n`;

if (committee.interviewTime.start !== "Ikke satt") {
if (committee.interviewTime.start != null) {
phoneBody += `Tid: ${formatDateHours(
committee.interviewTime.start,
committee.interviewTime.end
)}\n`;
}

if (committee.interviewTime.start === "Ikke satt") {
} else {
phoneBody += `Tid: Ikke satt. Komitéen vil ta kontakt for å avtale tidspunkt. \n`;
}

Expand Down
26 changes: 15 additions & 11 deletions lib/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ export type DeepPartial<T> = {
[P in keyof T]?: T[P] extends object ? DeepPartial<T[P]> : T[P];
};

export type Nullable<T> = {
[K in keyof T]: T[K] | null;
}

export type preferencesType = {
first: string;
second: string;
Expand All @@ -27,8 +31,8 @@ export type applicantType = {
optionalCommittees: string[];
selectedTimes: [
{
start: string;
end: string;
start: Date;
end: Date;
},
];
date: Date;
Expand Down Expand Up @@ -69,8 +73,8 @@ export type periodType = {
};

export type AvailableTime = {
start: string;
end: string;
start: Date;
end: Date;
room: string;
};

Expand All @@ -80,7 +84,7 @@ export type committeeInterviewType = {
committee: string;
committeeEmail: string;
availabletimes: AvailableTime[];
timeslot: string;
timeslot: string; // duration of each interview in minutes
message: string;
};

Expand Down Expand Up @@ -110,8 +114,8 @@ export interface OwGroup {
export type algorithmType = {
applicantId: string;
interviews: {
start: string;
end: string;
start: Date;
end: Date;
committeeName: string;
room: string;
}[];
Expand All @@ -132,8 +136,8 @@ export type emailCommitteeInterviewType = {
applicantPhone: string;
applicantEmail: string;
interviewTime: {
start: string;
end: string;
start: Date;
end: Date;
room: string;
};
}[];
Expand All @@ -149,8 +153,8 @@ export type emailApplicantInterviewType = {
committeeName: string;
committeeEmail: string;
interviewTime: {
start: string;
end: string;
start: Date;
end: Date;
room: string;
};
}[];
Expand Down
Loading