Skip to content

Commit

Permalink
update expired spot clean up pubsup
Browse files Browse the repository at this point in the history
  • Loading branch information
juancwu committed May 15, 2024
1 parent 00f3601 commit 0362f1b
Showing 1 changed file with 79 additions and 29 deletions.
108 changes: 79 additions & 29 deletions functions/src/rsvp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -500,45 +500,95 @@ export const joinWaitlist = functions.https.onCall(async (_, context) => {
});

export const expiredSpotCleanup = functions.pubsub
.schedule("every 1 minutes")
.schedule("every 2 minutes")
.onRun(async () => {
functions.logger.info("Start expired spot clean up");
const snap = await admin
.firestore()
.collection(SPOTS_COLLECTION)
.where("expiresAt", "<", Timestamp.now())
.get();
const count = snap.size;
functions.logger.info(`Found ${count} expired spots.`);
const { sent } = (
await admin
.firestore()
.collection("expired-spots-record")
.doc("email-record")
.get()
).data() ?? { sent: false };
if (count > 0 && !sent) {
await resend.emails.send({
to: config.exec.jc.email,
from: NOREPLY_EMAIL,
subject: "[HawkHacks] Found expired spots",
html: `<p>There are ${count} expired spots.</p>`,
});
await admin
.firestore()
.collection("expired-spots-record")
.doc("email-record")
.set({ sent: true });
functions.logger.info(`Found ${snap.size} expired spots.`);
if (snap.size) {
for (let i = 0; i < snap.size; i++) {
const expiredSpotDoc = snap.docs[i];
const data = expiredSpotDoc.data();
const user = await admin.auth().getUser(data.uid);
await admin.firestore().runTransaction(async (tx) => {
const counterDoc = await tx.get(
admin
.firestore()
.collection(SPOTS_COLLECTION)
.doc(SPOTS_COUNTER_DOCUMENT)
);
const waitlistSnap = await tx.get(
admin
.firestore()
.collection(WAITLIST_COLLECTION)
.orderBy("joinAt", "asc")
.limit(1)
);
const waitlistDoc = waitlistSnap.docs[i];
if (waitlistDoc) {
const expires = Timestamp.now().toDate();
// 24 hours in milliseconds
const oneDayInMs = 86400000;
expires.setTime(expires.getTime() + oneDayInMs);
const expiresAt = Timestamp.fromDate(expires);
// create new spot
tx.create(
admin
.firestore()
.collection(SPOTS_COLLECTION)
.doc(waitlistDoc.id),
{
...waitlistDoc.data(),
expiresAt,
}
);
// delete expired spot
tx.delete(expiredSpotDoc.ref);
// delete waitlist
tx.delete(
admin
.firestore()
.collection(WAITLIST_COLLECTION)
.doc(waitlistDoc.id)
);
} else {
tx.update(
admin
.firestore()
.collection(SPOTS_COLLECTION)
.doc(SPOTS_COUNTER_DOCUMENT),
{
count:
(counterDoc.data() ?? { count: 0 }).count +
1,
}
);
}
});
const app = (
await admin
.firestore()
.collection("applications")
.where("applicantId", "==", user.uid)
.get()
).docs[0]?.data();
await sendSpotAvailableEmail(
app?.firstName ?? user.displayName ?? "",
user.email
).catch((error) =>
functions.logger.error(
"Failed to send notification email about new available spot.",
{ error }
)
);
}
} else {
functions.logger.info("No expired spots found.");
}
// const batch = admin.firestore().batch();
// snap.forEach((doc) => {
// batch.delete(doc.ref);
// });
// const spots = (await admin.firestore().collection(SPOTS_COLLECTION).doc(SPOTS_COUNTER_DOCUMENT).get()).data()?.count ?? 0;
// batch.update(admin.firestore().collection(SPOTS_COLLECTION).doc(SPOTS_COUNTER_DOCUMENT), { count: spots + count });
// await batch.commit();
});

// export const moveToSpots = functions.pubsub.schedule("every 1 minutes").onRun(async () => {
Expand Down

0 comments on commit 0362f1b

Please sign in to comment.