Open source CapCut alternative.
TypeScript
import { NextResponse } from "next/server";
import { z } from "zod";
import { requestLocale } from "@/i18n/requestLocale";
import { getActiveSubscription } from "@/lib/billing/pro-subscription";
import { creditMicrosToString } from "@/lib/credits/amounts";
import { formatCredits } from "@/lib/credits/format-credits";
import { getCreditBalance } from "@/lib/credits/inference";
import { isLinkClaimedKind, isSubscribeClaimedKind } from "@/lib/credits/offerKinds";
import {
claimCreditOffer,
CreditOfferClosedError,
CreditOfferNotYoursError,
creditOfferOpen,
verifyCreditOfferToken,
} from "@/lib/credits/offers";
import { forbiddenResponse, notFoundResponse, withDonkeyAuth } from "@/lib/donkey-api-auth";
import { prisma } from "@/lib/prisma";
const claimRequestSchema = z.object({ token: z.string().min(1) }).strict();
// Describes the offer a claim link names, for the dialog that presents it.
// The token proves the link came from the offer's email; the session has to be
// the offered account's, and a different account is told so before it tries.
// An offer whose claim window closed unclaimed is gone (410). An offer landed
// by subscribing is presented to an account that already holds Pro as one it
// cannot take up (409).
const goneResponse = () => NextResponse.json({ error: "This offer has closed." }, { status: 410 });
const proHeldResponse = () =>
NextResponse.json({ error: "This offer is for accounts without Pro." }, { status: 409 });
export const GET = withDonkeyAuth(async (request) => {
const token = request.nextUrl.searchParams.get("token");
const offerId = token ? verifyCreditOfferToken(token) : null;
if (!offerId) return notFoundResponse();
const offer = await prisma.creditOffer.findUnique({
select: {
amountMicros: true,
claimedAt: true,
closesAt: true,
expiresAfterDays: true,
grant: { select: { expiresAt: true } },
kind: true,
userId: true,
},
where: { id: offerId },
});
const claim = !offer ? null : isLinkClaimedKind(offer.kind) ? "link" : isSubscribeClaimedKind(offer.kind) ? "subscribe" : null;
if (!offer || !claim) return notFoundResponse();
if (offer.userId !== request.donkey.userId) return forbiddenResponse();
if (!offer.claimedAt && !creditOfferOpen(offer, new Date())) return goneResponse();
if (!offer.claimedAt && claim === "subscribe" && (await getActiveSubscription(offer.userId))) {
return proHeldResponse();
}
return NextResponse.json({
claim,
claimed: offer.claimedAt !== null,
closesAt: offer.closesAt?.toISOString() ?? null,
// Read by claim dialogs from builds before the catalog; the current one
// words the amount from `dollars`.
credits: formatCredits(creditMicrosToString(offer.amountMicros), requestLocale(request)),
dollars: creditMicrosToString(offer.amountMicros),
expiresAt: offer.grant?.expiresAt?.toISOString() ?? null,
lifetimeDays: offer.expiresAfterDays,
});
});
// Claims a credit offer for the signed-in account. The token names the offer
// and proves the link came from its email; the session has to be the offered
// account's.
export const POST = withDonkeyAuth(async (request) => {
const parsed = claimRequestSchema.safeParse(await request.json());
if (!parsed.success) {
return NextResponse.json({ error: "Invalid request" }, { status: 400 });
}
const offerId = verifyCreditOfferToken(parsed.data.token);
if (!offerId) return notFoundResponse();
try {
const claimed = await claimCreditOffer(offerId, request.donkey.userId);
if (!claimed) return notFoundResponse();
const balance = await getCreditBalance(request.donkey.userId);
return NextResponse.json({
balance: balance.balance,
credits: creditMicrosToString(claimed.grant.originalAmountMicros),
expiresAt: claimed.grant.expiresAt?.toISOString() ?? null,
});
} catch (error) {
if (error instanceof CreditOfferNotYoursError) return forbiddenResponse();
if (error instanceof CreditOfferClosedError) return goneResponse();
throw error;
}
});