Skip to content

Commit

Permalink
ignore old reminders
Browse files Browse the repository at this point in the history
  • Loading branch information
appsbytom committed Sep 22, 2024
1 parent 8861f83 commit 6005ddc
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions pages/api/reminders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,18 @@ import dayjs from 'dayjs'
import { NextApiRequest, NextApiResponse } from 'next'

const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const reminders = await prisma.sessionReminder.findMany({ where: { scheduledFor: { lte: dayjs().unix() } } })
const now = dayjs()
const reminders = await prisma.sessionReminder.findMany({
where: {
scheduledFor: {
lte: now.unix(),
gte: now.subtract(5, 'minutes').unix()
}
}
})

if (reminders.length === 0) {
return res.json({ success: true, message: 'No reminders to send' })
return res.send('No reminders to send')
}
await Promise.all([
messaging.sendEach(reminders.map(reminder => ({
Expand All @@ -22,7 +30,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
}))),
prisma.sessionReminder.deleteMany({ where: { id: { in: reminders.map(reminder => reminder.id) } } })
])
res.json({ success: true })
res.status(200).end()
}

export default handler

0 comments on commit 6005ddc

Please sign in to comment.