export async function checkUserHasMonthlySubscription()

in src/app/functions/server/user.ts [10:50]


export async function checkUserHasMonthlySubscription(user: Session["user"]) {
  if (!user.subscriber?.fxa_uid) {
    console.error("FXA UID not set");
    return false;
  }
  const subscriber = await getSubscriberByFxaUid(user.subscriber.fxa_uid);
  if (!subscriber || !subscriber.fxa_access_token) {
    console.error("FXA token not set");
    return false;
  }

  if (!process.env.PREMIUM_PLAN_ID_MONTHLY_US) {
    console.error("Monthly Plan ID not set");
    return false;
  }

  const billingAndSubscriptionInfo = await getBillingAndSubscriptions(
    subscriber.fxa_access_token,
  );

  if (billingAndSubscriptionInfo === null) {
    return false;
  }

  const monthlyPlanId = process.env.PREMIUM_PLAN_ID_MONTHLY_US;
  const yearlyPlanId = process.env.PREMIUM_PLAN_ID_YEARLY_US ?? "";

  const subscriptions = billingAndSubscriptionInfo.subscriptions;

  const planIds: string[] = [];
  subscriptions.forEach((subscription) => {
    planIds.push(subscription.plan_id);
  });

  if (planIds.includes(yearlyPlanId)) {
    console.error("User has yearly plan set");
    return false;
  }

  return planIds.includes(monthlyPlanId);
}