diff --git a/apps/api/pages/api/cron/users/sync-subscriptions.ts b/apps/api/pages/api/cron/users/sync-subscriptions.ts new file mode 100644 index 00000000..f210b244 --- /dev/null +++ b/apps/api/pages/api/cron/users/sync-subscriptions.ts @@ -0,0 +1,20 @@ +import type { NextApiRequest, NextApiResponse } from 'next' +import { syncAllUsersSubscription } from '../../../../v1/services/user.service' + +export const dynamic = 'force-dynamic' // static by default, unless reading the request + +export default async function handler( + req: NextApiRequest, + res: NextApiResponse, +) { + const authHeader = req.headers.authorization + + if ( + !process.env.CRON_SECRET || + authHeader !== `Bearer ${process.env.CRON_SECRET}` + ) { + return res.status(401).json({ success: false, message: 'unauthorized' }) + } + + res.status(200).json(await syncAllUsersSubscription()) +} diff --git a/apps/api/vercel.json b/apps/api/vercel.json new file mode 100644 index 00000000..bacb62ea --- /dev/null +++ b/apps/api/vercel.json @@ -0,0 +1,8 @@ +{ + "crons": [ + { + "path": "/api/cron/users/sync-subscriptions", + "schedule": "0 * * * *" + } + ] +}