Skip to content

Open source CapCut alternative.

TypeScript
6.8 KB
Raw Blame
import type { CreditOffer } from "@/generated/prisma/client";
import { getActiveSubscription } from "@/lib/billing/pro-subscription";
import { getSetting, type ConfigContext } from "@/lib/config/effective";
import { creditMicrosToString, creditStringToMicros, zeroCreditMicros } from "@/lib/credits/amounts";
import { creditOfferOpen } from "@/lib/credits/offers";
import { SUBSCRIBE_BONUS_KIND } from "@/lib/credits/offerKinds";
import { bestOfferForSubscribing, findOffersForSubscribing } from "@/lib/credits/subscribe-bonus-claim";
import { prisma } from "@/lib/prisma";

// The subscribe bonus: an account that has spent enough of its signup grant
// is offered credit for subscribing to Pro within a window. The offer is a
// credit offer of this kind, opened on the first read that finds the account
// past the share; it lands from the subscription webhook
// (src/lib/credits/subscribe-bonus-claim.ts). The app reads one offer for
// subscribing at a time, and a promotion emailed with a subscribe claim is
// read through the same view.

export const SIGNUP_GRANT_SOURCE = "signup";

export type SubscribeBonusStatus = "open" | "closed" | "claimed";

export type SubscribeBonusView = {
  // Where the offer reached the person: the app opened the bonus on its own,
  // a promotion came by email.
  origin: "app" | "email";
  // USD, as a credit string.
  dollars: string;
  openedAt: string;
  closesAt: string;
  // The credit's life once it lands: a fixed last moment, or days from the
  // claim. Both null keeps it forever.
  creditsExpireAt: string | null;
  creditsLifetimeDays: number | null;
  status: SubscribeBonusStatus;
};

/** The last moment of a UTC day named as YYYY-MM-DD. */
export function endOfUtcDay(day: string): Date {
  return new Date(`${day}T23:59:59.999Z`);
}

/** Whether a grant is spent past the share the offer waits for. Credit that
 * lapsed at expiry was never spent, so it comes off the spent side. */
export function crossedSpentShare(
  grant: { originalAmountMicros: bigint; remainingAmountMicros: bigint },
  lapsedMicros: bigint,
  spentPercent: number,
): boolean {
  if (grant.originalAmountMicros <= zeroCreditMicros) return false;
  const spent = grant.originalAmountMicros - grant.remainingAmountMicros - lapsedMicros;
  return spent * BigInt(100) >= grant.originalAmountMicros * BigInt(spentPercent);
}

/** An offer is open while it can still be taken up: unclaimed, inside its
 * window, and by an account that does not already hold Pro. */
export function subscribeBonusStatus(
  offer: { closesAt: Date | null; claimedAt: Date | null },
  facts: { now: Date; pro: boolean },
): SubscribeBonusStatus {
  if (offer.claimedAt) return "claimed";
  return !facts.pro && creditOfferOpen(offer, facts.now) ? "open" : "closed";
}

export function subscribeBonusView(offer: CreditOffer, facts: { now: Date; pro: boolean }): SubscribeBonusView {
  return {
    origin: offer.kind === SUBSCRIBE_BONUS_KIND ? "app" : "email",
    dollars: creditMicrosToString(offer.amountMicros),
    openedAt: offer.createdAt.toISOString(),
    closesAt: (offer.closesAt ?? offer.createdAt).toISOString(),
    creditsExpireAt: offer.expiresAt?.toISOString() ?? null,
    creditsLifetimeDays: offer.expiresAt ? null : offer.expiresAfterDays,
    status: subscribeBonusStatus(offer, facts),
  };
}

/** The offer the app shows, of the account's offers for subscribing: the one
 * a checkout would carry while any is open; past that, the latest to land,
 * so the settings card can say so; past that, the latest to close. */
export function shownOfferForSubscribing(offers: CreditOffer[], now: Date): CreditOffer | null {
  const open = bestOfferForSubscribing(offers, now);
  if (open) return open;
  const latest = (rows: CreditOffer[]) =>
    rows.reduce<CreditOffer | null>((best, row) => (!best || row.createdAt > best.createdAt ? row : best), null);
  return latest(offers.filter((o) => o.claimedAt !== null)) ?? latest(offers);
}

/** Whether credit expiring at the day's end outlives the whole window, so a
 * subscription at the last minute of the window still lands live credit. */
export function creditOutlivesWindow(expiresAt: Date | null, closesAt: Date): boolean {
  return expiresAt === null || expiresAt.getTime() > closesAt.getTime();
}

/** The account's offer for subscribing as it stands, opening the subscribe
 * bonus when the account has just crossed the share. Null while there is
 * nothing to offer: no promotion reached the account, the setting makes no
 * bonus for it, the account holds Pro, or the signup grant is not yet spent
 * far enough. */
export async function readSubscribeBonusOffer(ctx: ConfigContext, now = new Date()): Promise<SubscribeBonusView | null> {
  if (!ctx.hasAccount) return null;
  // The indexed reads come first: the offer rows, then the two facts that rule
  // most accounts out before the configuration is resolved.
  const [offers, pro, grant] = await Promise.all([
    findOffersForSubscribing(ctx.userId),
    getActiveSubscription(ctx.userId),
    prisma.userCreditGrant.findFirst({
      where: { userId: ctx.userId, source: SIGNUP_GRANT_SOURCE },
      select: { id: true, originalAmountMicros: true, remainingAmountMicros: true },
    }),
  ]);
  const facts = { now, pro: pro !== null };
  const shown = () => {
    const offer = shownOfferForSubscribing(offers, now);
    return offer ? subscribeBonusView(offer, facts) : null;
  };
  if (offers.some((o) => o.kind === SUBSCRIBE_BONUS_KIND)) return shown();
  if (pro || !grant || grant.remainingAmountMicros >= grant.originalAmountMicros) return shown();

  const setting = await getSetting("subscribeBonus", ctx);
  if (setting.dollars <= 0) return shown();
  const lapsed = await prisma.userCreditLedgerEntry.aggregate({
    _sum: { amountMicros: true },
    where: { grantId: grant.id, type: "expiration" },
  });
  const lapsedMicros = -(lapsed._sum.amountMicros ?? zeroCreditMicros);
  if (!crossedSpentShare(grant, lapsedMicros, setting.spentPercent)) return shown();

  // A promise of credit that would be dead by the time it lands is no offer:
  // the setting's last day has to lie past the whole window.
  const closesAt = new Date(now.getTime() + setting.windowHours * 60 * 60 * 1000);
  const expiresAt = setting.creditsExpireOn ? endOfUtcDay(setting.creditsExpireOn) : null;
  if (!creditOutlivesWindow(expiresAt, closesAt)) return shown();

  await prisma.creditOffer.create({
    data: {
      amountMicros: creditStringToMicros(String(setting.dollars)),
      closesAt,
      createdAt: now,
      description: "Pro subscribe bonus",
      expiresAt,
      kind: SUBSCRIBE_BONUS_KIND,
      userId: ctx.userId,
    },
  });
  // Re-read rather than trust the create: two first reads that raced both
  // wrote, and the earliest row is the one that stands.
  const offer = shownOfferForSubscribing(await findOffersForSubscribing(ctx.userId), now);
  return offer ? subscribeBonusView(offer, facts) : null;
}