Skip to content

Commit

Permalink
move reminders to revalidation
Browse files Browse the repository at this point in the history
  • Loading branch information
appsbytom committed Mar 30, 2024
1 parent b057939 commit 0c906e4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 29 deletions.
37 changes: 33 additions & 4 deletions pages/api/revalidate.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,44 @@
import prisma from '@/lib/prisma'
import SERIES_CONFIG from '@/series/config'
import { getAllEvents } from '@/series/fetcher-config'
import dayjs from 'dayjs'
import type { NextApiRequest, NextApiResponse } from 'next'

const handler = async (req: NextApiRequest, res: NextApiResponse) => {
if (req.headers.token !== process.env.REVALIDATE_TOKEN) {
return res.status(401).json({ message: 'invalid token' });
return res.status(401).json({ message: 'invalid token' })
}

try {
await res.revalidate('/');
return res.json({ revalidated: true });
const [_, events] = await Promise.all([res.revalidate('/'), getAllEvents()])
await prisma.$transaction(events
.filter(event => !event.provisional)
.flatMap(event => event.sessions
.filter(session => !session.unconfirmed && dayjs(session.startTime).isAfter(dayjs()))
.map(session => {
const id = `${event.series}-${session.id}`
const title = `${SERIES_CONFIG[event.series].name}: ${event.name}`
const body = `${session.name} starts in a few minutes`
const scheduledFor = dayjs(session.startTime).subtract(5, 'minutes').unix()
return prisma.sessionReminder.upsert({
where: { id },
create: {
id,
title,
body,
scheduledFor,
topic: `${event.series}-${session.type}`
},
update: {
title,
body,
scheduledFor
}
})
})))
return res.json({ revalidated: true })
} catch (err) {
return res.status(500).json({ message: 'server error' });
return res.status(500).json({ message: 'server error' })
}
}

Expand Down
25 changes: 0 additions & 25 deletions scripts/update-data.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,11 @@
import prisma from '@/lib/prisma'
import SERIES_CONFIG from '@/series/config'
import { getAllEvents } from '@/series/fetcher-config'
import axios from 'axios'
import dayjs from 'dayjs'

const CONSOLE_WRAPPER = '---------- UPDATE DATA ----------'

const main = async () => {
console.log(CONSOLE_WRAPPER)

await prisma.$transaction((await getAllEvents()).filter(event => !event.provisional).flatMap(event => event.sessions.filter(session => !session.unconfirmed && dayjs(session.startTime).isAfter(dayjs())).map(session => {
const id = `${event.series}-${session.id}`
const title = `${session.name} starts in 5 minutes`
const body = `${SERIES_CONFIG[event.series].name}: ${event.name}`
const scheduledFor = dayjs(session.startTime).subtract(5, 'minutes').unix()
return prisma.sessionReminder.upsert({
where: { id },
create: {
id,
title,
body,
scheduledFor,
topic: `${event.series}-${session.type}`
},
update: {
title,
body,
scheduledFor
}
})
})))

try {
await axios.get(`${process.env.APP_URL}/api/revalidate`, { headers: { token: process.env.TOKEN }})
console.log('Revalidated!')
Expand Down

0 comments on commit 0c906e4

Please sign in to comment.