2021-09-28 13:00:19 +00:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
|
|
|
|
2022-06-28 20:40:58 +00:00
|
|
|
import dayjs from "@calcom/dayjs";
|
|
|
|
import prisma from "@calcom/prisma";
|
|
|
|
|
2022-01-03 22:50:59 +00:00
|
|
|
import { TRIAL_LIMIT_DAYS } from "@lib/config/constants";
|
2021-09-28 13:00:19 +00:00
|
|
|
|
|
|
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
2021-10-25 15:16:42 +00:00
|
|
|
const apiKey = req.headers.authorization || req.query.apiKey;
|
2021-09-28 13:00:19 +00:00
|
|
|
if (process.env.CRON_API_KEY !== apiKey) {
|
2021-10-25 15:16:42 +00:00
|
|
|
res.status(401).json({ message: "Not authenticated" });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (req.method !== "POST") {
|
|
|
|
res.status(405).json({ message: "Invalid method" });
|
|
|
|
return;
|
2021-09-28 13:00:19 +00:00
|
|
|
}
|
|
|
|
|
2021-12-21 00:59:06 +00:00
|
|
|
/**
|
|
|
|
* TODO:
|
|
|
|
* We should add and extra check for non-paying customers in Stripe so we can
|
|
|
|
* downgrade them here.
|
|
|
|
*/
|
|
|
|
|
2021-09-28 13:00:19 +00:00
|
|
|
await prisma.user.updateMany({
|
|
|
|
data: {
|
|
|
|
plan: "FREE",
|
|
|
|
},
|
|
|
|
where: {
|
|
|
|
plan: "TRIAL",
|
2022-03-03 19:29:19 +00:00
|
|
|
OR: [
|
|
|
|
/**
|
|
|
|
* If the user doesn't have a trial end date,
|
|
|
|
* use the default 14 day trial from creation.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
createdDate: {
|
|
|
|
lt: dayjs().subtract(TRIAL_LIMIT_DAYS, "day").toDate(),
|
|
|
|
},
|
|
|
|
trialEndsAt: null,
|
|
|
|
},
|
|
|
|
/** If it does, then honor the trial end date. */
|
|
|
|
{
|
|
|
|
trialEndsAt: {
|
|
|
|
lt: dayjs().toDate(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
2021-09-28 13:00:19 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
res.json({ ok: true });
|
|
|
|
}
|