Open source CapCut alternative.
TypeScript
import { NextResponse } from "next/server";
import { isPaidPlan } from "@/lib/billing/plans";
import { getActiveSubscription, scheduledProEnd } from "@/lib/billing/pro-subscription";
import { creditMicrosToString, zeroCreditMicros } from "@/lib/credits/amounts";
import { withDonkeyAuth } from "@/lib/donkey-api-auth";
import { prisma } from "@/lib/prisma";
// The account's plan and subscription status for the settings UI, plus how much of this period's
// included allowance is left — the remaining balance on the active
// "pro_subscription" grant(s).
export const GET = withDonkeyAuth(async (request) => {
const userId = request.donkey.userId;
const now = new Date();
const [subscription, grants] = await Promise.all([
getActiveSubscription(userId),
prisma.userCreditGrant.findMany({
select: { originalAmountMicros: true, remainingAmountMicros: true },
where: {
OR: [{ expiresAt: null }, { expiresAt: { gt: now } }],
source: "pro_subscription",
status: "active",
userId,
},
}),
]);
const allowanceRemainingMicros = grants.reduce(
(sum, grant) => sum + grant.remainingAmountMicros,
zeroCreditMicros,
);
// Denominator = what was actually granted this period, so "X of Y left" stays
// consistent even if the price changed mid-period (the grant is frozen per
// period). Fall back to the configured allowance before the first grant lands.
const allowanceGrantedMicros = grants.reduce(
(sum, grant) => sum + grant.originalAmountMicros,
zeroCreditMicros,
);
const monthlyAllowanceMicros =
allowanceGrantedMicros > zeroCreditMicros
? allowanceGrantedMicros
: (subscription?.monthlyAllowanceMicros ?? zeroCreditMicros);
return NextResponse.json({
allowanceRemaining: creditMicrosToString(allowanceRemainingMicros),
currentPeriodEnd: subscription?.currentPeriodEnd?.toISOString() ?? null,
// When a scheduled cancel ends Pro; null while the plan renews.
endsAt: subscription ? (scheduledProEnd(subscription)?.toISOString() ?? null) : null,
isActive: Boolean(subscription),
// A row from before the ladder carries "pro".
plan: subscription ? (isPaidPlan(subscription.planKey) ? subscription.planKey : "pro") : null,
monthlyAllowance: subscription
? creditMicrosToString(monthlyAllowanceMicros)
: null,
status: subscription?.status ?? null,
});
});