Skip to content

Commit

Permalink
slottificate debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
c0repwn3r committed Nov 10, 2024
1 parent 9d5dcd0 commit 2a105f5
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
Binary file modified bun.lockb
Binary file not shown.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "scheddy",
"version": "0.0.11",
"version": "0.0.12",
"type": "module",
"scripts": {
"dev": "vite dev",
Expand Down
39 changes: 35 additions & 4 deletions src/routes/(authed)/schedule/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import { sendEmail } from '$lib/email';
import { new_session } from '$lib/emails/new_session';

function slottificate(
sTypes: typeof sessionTypes.$inferSelect,
mentors: typeof users.$inferSelect,
allSessions: typeof sessions.$inferSelect
sTypes: typeof sessionTypes.$inferSelect[],
mentors: typeof users.$inferSelect[],
allSessions: typeof sessions.$inferSelect[]
): Record<string, { slot: Interval; mentor: number }[]> {
const slotData: Record<string, { slot: Interval; mentor: number }[]> = {};

Expand All @@ -30,15 +30,22 @@ function slottificate(

const sessionsByMentor = {};
for (const sess of allSessions) {
if (sess.mentor == true) continue;

if (!sessionsByMentor[sess.mentor]) {
sessionsByMentor[sess.mentor] = [];
}
sessionsByMentor[sess.mentor].push(sess);
}

console.log("sessionsByMentor", sessionsByMentor);

for (const typ of sTypes) {
console.log("for typ", typ);
const slots = [];
for (const mentor of mentors) {
console.log("for mentor", mentor);

const allowedTypes = JSON.parse(mentor.allowedSessionTypes);
if (!allowedTypes) continue;
if (!allowedTypes.includes(typ.id)) continue;
Expand All @@ -48,6 +55,8 @@ function slottificate(

const mentorsOtherSessions = sessionsByMentor[mentor.id] || [];

console.log("otherSessions", mentorsOtherSessions);

const availablePeriodsMentorsTime: Interval[] = [];
const unavailablePeriodsMentorsTime: Interval[] = [];

Expand All @@ -59,9 +68,16 @@ function slottificate(
unavailablePeriodsMentorsTime.push(Interval.fromDateTimes(start, end));
}

console.log("unavailable", unavailablePeriodsMentorsTime);

// figure out their availability for each day
for (const validDay of validDaysToBook) {
console.log("validDay", validDay);

const dayInMentorsTz = validDay.setZone(mentor.timezone);

console.log("validDayMentorsTz", dayInMentorsTz);

let todaysAvail: DayAvailability | null = null;
// do they have a date exception set?

Expand All @@ -79,7 +95,9 @@ function slottificate(
else if (dayInMentorsTz.weekday == 7) todaysAvail = availability.sunday;
}

// convert the availability back to a UTC interval
console.log("todaysAvail", todaysAvail);

// convert the availability back to an interval
if (todaysAvail && todaysAvail.available) {
// we are available
const start = dayInMentorsTz.set({
Expand All @@ -94,6 +112,7 @@ function slottificate(
second: 0,
millisecond: 0
});
console.log("start", start, "end", end);

const interval = Interval.fromDateTimes(start, end);

Expand Down Expand Up @@ -126,13 +145,18 @@ function slottificate(
);
}

console.log("availablePeriods", availablePeriods);
console.log("unavailablePeriods", unavailablePeriods);

// calculate difference of each availablePeriod to get a list of o.k. slots
const thisMentorAvailability: Interval[] = [];

for (const period of availablePeriods) {
thisMentorAvailability.push(...period.difference(...unavailablePeriods));
}

console.log("thisMentorAvailability", thisMentorAvailability);

// split each available period into individual session slots
const individualSlots: Interval[] = [];

Expand All @@ -142,16 +166,21 @@ function slottificate(
individualSlots.push(...period.splitBy(Duration.fromObject({ minutes: minimumLength })));
}

console.log("individualSlots", individualSlots);

// finally, drop any that are too short or <24h

const validSlots: Interval[] = [];

for (const potentialSlot of individualSlots) {
if (potentialSlot.start == null) continue;
if (potentialSlot.length('minutes') >= minimumLength && potentialSlot.start >= tomorrow) {
validSlots.push(potentialSlot);
}
}

console.log("validSlots", validSlots);

slots.push(
...validSlots.map((u) => {
return {
Expand All @@ -164,6 +193,8 @@ function slottificate(
slotData[typ.id] = slots;
}

console.log("slotData", slotData);

return slotData;
}

Expand Down

0 comments on commit 2a105f5

Please sign in to comment.