2021-09-28 13:00:19 +00:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
2023-03-28 21:55:43 +00:00
|
|
|
import { z } from "zod";
|
2021-09-28 13:00:19 +00:00
|
|
|
|
2023-03-28 19:38:01 +00:00
|
|
|
import { updateQuantitySubscriptionFromStripe } from "@calcom/features/ee/teams/lib/payments";
|
|
|
|
import prisma from "@calcom/prisma";
|
|
|
|
|
2023-03-28 21:55:43 +00:00
|
|
|
const querySchema = z.object({
|
|
|
|
page: z.coerce.number().min(0).optional().default(0),
|
|
|
|
});
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-03-28 19:38:01 +00:00
|
|
|
const delay = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
|
|
const pageSize = 90; // Adjust this value based on the total number of teams and the available processing time
|
2023-03-28 21:55:43 +00:00
|
|
|
let { page: pageNumber } = querySchema.parse(req.query);
|
2023-03-28 19:38:01 +00:00
|
|
|
|
|
|
|
while (true) {
|
|
|
|
const teams = await prisma.team.findMany({
|
|
|
|
where: {
|
|
|
|
slug: {
|
|
|
|
not: null,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
select: {
|
|
|
|
id: true,
|
|
|
|
},
|
|
|
|
skip: pageNumber * pageSize,
|
|
|
|
take: pageSize,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (teams.length === 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const team of teams) {
|
|
|
|
await updateQuantitySubscriptionFromStripe(team.id);
|
|
|
|
await delay(100); // Adjust the delay as needed to avoid rate limiting
|
|
|
|
}
|
|
|
|
|
|
|
|
pageNumber++;
|
|
|
|
}
|
2021-12-21 00:59:06 +00:00
|
|
|
|
2021-09-28 13:00:19 +00:00
|
|
|
res.json({ ok: true });
|
|
|
|
}
|