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>
This commit is contained in:
Alex Johanssonandkodiakhq[bot] authored and GitHub committed 2021-09-28 14:00:19 +01:00
1 parent 0372289fe6
commit a4fbe7b2b4
6 files changed
+129 -25

No files matched your search

+1 -1
View File
@@ -25,7 +25,7 @@ type AugmentedNextPageContext = Omit<NextPageContext, "err"> & {
const log = logger.getChildLogger({ prefix: ["[error]"] });
export function getErrorFromUnknown(cause: unknown): Error {
export function getErrorFromUnknown(cause: unknown): Error & { statusCode?: number } {
if (cause instanceof Error) {
return cause;
}
+26
View File
@@ -0,0 +1,26 @@
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 });
}