From 7906ced3bf5ce45a748191c78c849ac52c7d751c Mon Sep 17 00:00:00 2001 From: Dustin Do Date: Sun, 22 Sep 2024 07:22:25 +0700 Subject: [PATCH] feat(api): add sync user subscriptions crob job (#359) --- .../api/cron/users/sync-subscriptions.ts | 20 +++++++++++++++++++ apps/api/vercel.json | 8 ++++++++ 2 files changed, 28 insertions(+) create mode 100644 apps/api/pages/api/cron/users/sync-subscriptions.ts create mode 100644 apps/api/vercel.json 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 * * * *" + } + ] +}