calcom/pages/api/cron/downgradeUsers.ts
Omar López 4cd7a4ce5b
Adds trial banner and in-app upgrade (#1402)
* WIP trial banner

* Fixes days left count

* Defers stripe loading until needed

* Fixes auth issues

* Checkout fixes

* Allows for signup testing

* Debugging checkout

* Adds tests for trial banner

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
2022-01-03 22:50:59 +00:00

36 lines
926 B
TypeScript

import dayjs from "dayjs";
import type { NextApiRequest, NextApiResponse } from "next";
import { TRIAL_LIMIT_DAYS } from "@lib/config/constants";
import prisma from "@lib/prisma";
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
const apiKey = req.headers.authorization || req.query.apiKey;
if (process.env.CRON_API_KEY !== apiKey) {
res.status(401).json({ message: "Not authenticated" });
return;
}
if (req.method !== "POST") {
res.status(405).json({ message: "Invalid method" });
return;
}
/**
* TODO:
* We should add and extra check for non-paying customers in Stripe so we can
* downgrade them here.
*/
await prisma.user.updateMany({
data: {
plan: "FREE",
},
where: {
plan: "TRIAL",
createdDate: {
lt: dayjs().subtract(TRIAL_LIMIT_DAYS, "day").toDate(),
},
},
});
res.json({ ok: true });
}