Skip to content

Commit

Permalink
Except unavailable events
Browse files Browse the repository at this point in the history
  • Loading branch information
rlho committed Sep 27, 2024
1 parent 0dc53cf commit 97809e1
Showing 1 changed file with 29 additions and 8 deletions.
37 changes: 29 additions & 8 deletions booking-app/app/api/syncCalendars/route.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { NextApiRequest, NextApiResponse } from "next";
import { toFirebaseTimestampFromString } from "@/components/src/client/utils/serverDate";
import { TableNames } from "@/components/src/policy";
import { Booking, MediaServices } from "@/components/src/types";
import admin from "@/firebaseAdmin";
import admin from "@/lib/firebase/server/firebaseAdmin";
import { getCalendarClient } from "@/lib/googleClient";
import { toFirebaseTimestampFromString } from "@/components/src/client/utils/serverDate";
import { Timestamp } from "@firebase/firestore";
import { NextResponse } from "next/server";
import { TableNames } from "@/components/src/policy";

const db = admin.firestore();
const createBookingWithDefaults = (
Expand Down Expand Up @@ -67,28 +66,45 @@ export async function POST(request: Request) {
}));

let totalNewBookings = 0;
let targetBookings = 0;
for (const resource of resources) {
try {
// Fetch events for each calendar
let pageToken: string | undefined;
const now = new Date();
const threeMonthsAgo = new Date(now.getFullYear(), now.getMonth(), 1);
const threeMonthsLater = new Date(
now.getFullYear(),
now.getMonth() + 4,
0,
);
const timeMin = threeMonthsAgo.toISOString();
const timeMax = threeMonthsLater.toISOString();
const events = await calendar.events.list({
calendarId: resource.calendarId,
timeMin: now.toISOString(),
maxResults: 100, // Adjust as needed
timeMin: timeMin,
timeMax: timeMax,
maxResults: 500, // Maximum allowed by Google Calendar API
singleEvents: true,
orderBy: "startTime",
pageToken: pageToken,
});

for (const event of events.data.items || []) {
const bookingRef = db
.collection("bookings")
.where("calendarEventId", "==", event.id);
const bookingSnapshot = await bookingRef.get();
const nyuEmail = findNyuEmail(event);
if (bookingSnapshot.empty && nyuEmail) {
targetBookings++;
console.log("calendarEventId", event.id);
console.log("title", event.summary);
}

if (bookingSnapshot.empty) {
if (bookingSnapshot.empty && nyuEmail) {
// Create a new booking
const calendarEventId = event.id;
const nyuEmail = findNyuEmail(event);
const newBooking = createBookingWithDefaults({
title: event.summary || "",
description: event.description || "",
Expand All @@ -104,6 +120,7 @@ export async function POST(request: Request) {
roomId: resource.roomId,
mediaServices: MediaServices.CHECKOUT_EQUIPMENT,
});
console.log("newBooking", newBooking);
const bookingDocRef = await db
.collection(TableNames.BOOKING)
.add(newBooking);
Expand All @@ -117,6 +134,7 @@ export async function POST(request: Request) {
firstApprovedAt: admin.firestore.FieldValue.serverTimestamp(),
finalApprovedAt: admin.firestore.FieldValue.serverTimestamp(),
};
console.log("newBookingStatus", newBookingStatus);
const statusDocRef = await db
.collection(TableNames.BOOKING_STATUS)
.add(newBookingStatus);
Expand All @@ -126,7 +144,10 @@ export async function POST(request: Request) {

totalNewBookings++;
}
pageToken = events.data.nextPageToken;
}
while (pageToken);
console.log("targetBookings", targetBookings);
} catch (error) {
console.error(
`Error processing calendar ${resource.calendarId}:`,
Expand Down

0 comments on commit 97809e1

Please sign in to comment.