(req: NextApiRequest, res: NextApiResponse)
| 8 | import { Quota } from "@/types"; |
| 9 | |
| 10 | const handler = async (req: NextApiRequest, res: NextApiResponse) => { |
| 11 | if (req.method !== "GET" && req.method !== "POST") { |
| 12 | return res.status(405).json([]); |
| 13 | } |
| 14 | |
| 15 | const endUser = await getEndUser(req, res); |
| 16 | |
| 17 | // Get from server session if available |
| 18 | const serverSession = await getServerSession(req, res, authOptions); |
| 19 | let subscripion = serverSession?.user?.subscription; |
| 20 | if (!subscripion) { |
| 21 | subscripion = await getSubscriptionByEmail(endUser); |
| 22 | } |
| 23 | |
| 24 | let usage = 0; |
| 25 | if (req.method === "GET") { |
| 26 | usage = await getCurrentMonthUsage(endUser); |
| 27 | } else if (req.method === "POST") { |
| 28 | const model = getModel((req.headers["x-openai-model"] as string) || ""); |
| 29 | usage = await addUsage(endUser, model.cost_per_call); |
| 30 | } |
| 31 | |
| 32 | const quota: Quota = { |
| 33 | current: usage, |
| 34 | limit: subscripion.quota, |
| 35 | }; |
| 36 | |
| 37 | res.status(200).json(quota); |
| 38 | }; |
| 39 | |
| 40 | export default handler; |
nothing calls this directly
no test coverage detected