calcom/pages/api/cron/downgradeUsers.ts
Alex Johansson a4fbe7b2b4
downgrade users when trial ends (#767)
* wip

* wip

* wip

* wtf

* should be all the logic

* comment

* fix receiver name

* safeguard a bit more

* downgrade users cron job

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
2021-09-28 14:00:19 +01:00

26 lines
630 B
TypeScript

import dayjs from "dayjs";
import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@lib/prisma";
const TRIAL_LIMIT_DAYS = 14;
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
const apiKey = req.query.apiKey;
if (process.env.CRON_API_KEY !== apiKey) {
return res.status(401).json({ message: "Not authenticated" });
}
await prisma.user.updateMany({
data: {
plan: "FREE",
},
where: {
plan: "TRIAL",
createdDate: {
lt: dayjs().subtract(TRIAL_LIMIT_DAYS, "day").toDate(),
},
},
});
res.json({ ok: true });
}