Move cron jobs into GitHub actions (#1006)
This commit is contained in:
parent
8d6fec79d3
commit
a9df3b9ad0
4 changed files with 116 additions and 58 deletions
20
.github/workflows/cron-bookingReminder.yml
vendored
Normal file
20
.github/workflows/cron-bookingReminder.yml
vendored
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
name: Cron - bookingReminder
|
||||||
|
|
||||||
|
on:
|
||||||
|
# "Scheduled workflows run on the latest commit on the default or base branch."
|
||||||
|
# — https://docs.github.com/en/actions/learn-github-actions/events-that-trigger-workflows#schedule
|
||||||
|
schedule:
|
||||||
|
# Runs “At minute 0, 15, 30, and 45.” (see https://crontab.guru)
|
||||||
|
- cron: "0,15,30,45 * * * *"
|
||||||
|
jobs:
|
||||||
|
cron:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: cURL request
|
||||||
|
if: ${{ secrets.APP_URL && secrets.CRON_API_KEY }}
|
||||||
|
run: |
|
||||||
|
curl ${{ secrets.APP_URL }}/api/cron/bookingReminder \
|
||||||
|
-X POST
|
||||||
|
-H 'content-type: application/json' \
|
||||||
|
-H 'authorization: ${{ secrets.CRON_API_KEY }}' \
|
||||||
|
--fail
|
20
.github/workflows/cron-downgradeUsers.yml
vendored
Normal file
20
.github/workflows/cron-downgradeUsers.yml
vendored
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
name: Cron - downgradeUsers
|
||||||
|
|
||||||
|
on:
|
||||||
|
# "Scheduled workflows run on the latest commit on the default or base branch."
|
||||||
|
# — https://docs.github.com/en/actions/learn-github-actions/events-that-trigger-workflows#schedule
|
||||||
|
schedule:
|
||||||
|
# Runs “At minute 0, 15, 30, and 45.” (see https://crontab.guru)
|
||||||
|
- cron: "0,15,30,45 * * * *"
|
||||||
|
jobs:
|
||||||
|
cron:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: cURL request
|
||||||
|
if: ${{ secrets.APP_URL && secrets.CRON_API_KEY }}
|
||||||
|
run: |
|
||||||
|
curl ${{ secrets.APP_URL }}/api/cron/downgradeUsers \
|
||||||
|
-X POST
|
||||||
|
-H 'content-type: application/json' \
|
||||||
|
-H 'authorization: ${{ secrets.CRON_API_KEY }}' \
|
||||||
|
--fail
|
|
@ -7,69 +7,82 @@ import EventOrganizerRequestReminderMail from "@lib/emails/EventOrganizerRequest
|
||||||
import prisma from "@lib/prisma";
|
import prisma from "@lib/prisma";
|
||||||
|
|
||||||
export default async function handler(req: NextApiRequest, res: NextApiResponse): Promise<void> {
|
export default async function handler(req: NextApiRequest, res: NextApiResponse): Promise<void> {
|
||||||
const apiKey = req.query.apiKey;
|
const apiKey = req.headers.authorization || req.query.apiKey;
|
||||||
if (process.env.CRON_API_KEY != apiKey) {
|
if (process.env.CRON_API_KEY !== apiKey) {
|
||||||
return res.status(401).json({ message: "Not authenticated" });
|
res.status(401).json({ message: "Not authenticated" });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (req.method !== "POST") {
|
||||||
|
res.status(405).json({ message: "Invalid method" });
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (req.method == "POST") {
|
const reminderIntervalMinutes = [48 * 60, 24 * 60, 3 * 60];
|
||||||
const reminderIntervalMinutes = [48 * 60, 24 * 60, 3 * 60];
|
let notificationsSent = 0;
|
||||||
let notificationsSent = 0;
|
for (const interval of reminderIntervalMinutes) {
|
||||||
for (const interval of reminderIntervalMinutes) {
|
const bookings = await prisma.booking.findMany({
|
||||||
const bookings = await prisma.booking.findMany({
|
where: {
|
||||||
where: {
|
confirmed: false,
|
||||||
confirmed: false,
|
rejected: false,
|
||||||
rejected: false,
|
createdAt: {
|
||||||
createdAt: {
|
lte: dayjs().add(-interval, "minutes").toDate(),
|
||||||
lte: dayjs().add(-interval, "minutes").toDate(),
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
select: {
|
},
|
||||||
title: true,
|
select: {
|
||||||
description: true,
|
title: true,
|
||||||
startTime: true,
|
description: true,
|
||||||
endTime: true,
|
startTime: true,
|
||||||
attendees: true,
|
endTime: true,
|
||||||
user: true,
|
attendees: true,
|
||||||
id: true,
|
user: true,
|
||||||
uid: true,
|
id: true,
|
||||||
|
uid: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const reminders = await prisma.reminderMail.findMany({
|
||||||
|
where: {
|
||||||
|
reminderType: ReminderType.PENDING_BOOKING_CONFIRMATION,
|
||||||
|
referenceId: {
|
||||||
|
in: bookings.map((b) => b.id),
|
||||||
},
|
},
|
||||||
});
|
elapsedMinutes: {
|
||||||
|
gte: interval,
|
||||||
const reminders = await prisma.reminderMail.findMany({
|
|
||||||
where: {
|
|
||||||
reminderType: ReminderType.PENDING_BOOKING_CONFIRMATION,
|
|
||||||
referenceId: {
|
|
||||||
in: bookings.map((b) => b.id),
|
|
||||||
},
|
|
||||||
elapsedMinutes: {
|
|
||||||
gte: interval,
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
});
|
},
|
||||||
|
});
|
||||||
|
|
||||||
for (const booking of bookings.filter((b) => !reminders.some((r) => r.referenceId == b.id))) {
|
for (const booking of bookings.filter((b) => !reminders.some((r) => r.referenceId == b.id))) {
|
||||||
const evt: CalendarEvent = {
|
const { user } = booking;
|
||||||
type: booking.title,
|
const name = user?.name || user?.username;
|
||||||
title: booking.title,
|
if (!user || !name || !user.timeZone) {
|
||||||
description: booking.description,
|
console.error(`Booking ${booking.id} is missing required properties for booking reminder`, { user });
|
||||||
startTime: booking.startTime.toISOString(),
|
continue;
|
||||||
endTime: booking.endTime.toISOString(),
|
|
||||||
organizer: { email: booking.user.email, name: booking.user.name, timeZone: booking.user.timeZone },
|
|
||||||
attendees: booking.attendees,
|
|
||||||
};
|
|
||||||
|
|
||||||
await new EventOrganizerRequestReminderMail(evt, booking.uid).sendEmail();
|
|
||||||
await prisma.reminderMail.create({
|
|
||||||
data: {
|
|
||||||
referenceId: booking.id,
|
|
||||||
reminderType: ReminderType.PENDING_BOOKING_CONFIRMATION,
|
|
||||||
elapsedMinutes: interval,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
notificationsSent++;
|
|
||||||
}
|
}
|
||||||
|
const evt: CalendarEvent = {
|
||||||
|
type: booking.title,
|
||||||
|
title: booking.title,
|
||||||
|
description: booking.description || undefined,
|
||||||
|
startTime: booking.startTime.toISOString(),
|
||||||
|
endTime: booking.endTime.toISOString(),
|
||||||
|
organizer: {
|
||||||
|
email: user.email,
|
||||||
|
name,
|
||||||
|
timeZone: user.timeZone,
|
||||||
|
},
|
||||||
|
attendees: booking.attendees,
|
||||||
|
};
|
||||||
|
|
||||||
|
await new EventOrganizerRequestReminderMail(evt, booking.uid).sendEmail();
|
||||||
|
await prisma.reminderMail.create({
|
||||||
|
data: {
|
||||||
|
referenceId: booking.id,
|
||||||
|
reminderType: ReminderType.PENDING_BOOKING_CONFIRMATION,
|
||||||
|
elapsedMinutes: interval,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
notificationsSent++;
|
||||||
}
|
}
|
||||||
res.status(200).json({ notificationsSent });
|
|
||||||
}
|
}
|
||||||
|
res.status(200).json({ notificationsSent });
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,9 +6,14 @@ import prisma from "@lib/prisma";
|
||||||
const TRIAL_LIMIT_DAYS = 14;
|
const TRIAL_LIMIT_DAYS = 14;
|
||||||
|
|
||||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
const apiKey = req.query.apiKey;
|
const apiKey = req.headers.authorization || req.query.apiKey;
|
||||||
if (process.env.CRON_API_KEY !== apiKey) {
|
if (process.env.CRON_API_KEY !== apiKey) {
|
||||||
return res.status(401).json({ message: "Not authenticated" });
|
res.status(401).json({ message: "Not authenticated" });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (req.method !== "POST") {
|
||||||
|
res.status(405).json({ message: "Invalid method" });
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
await prisma.user.updateMany({
|
await prisma.user.updateMany({
|
||||||
|
|
Loading…
Reference in a new issue