diff --git a/src/app/(proper_react)/(redesign)/(authenticated)/user/(dashboard)/settings/page.tsx b/src/app/(proper_react)/(redesign)/(authenticated)/user/(dashboard)/settings/page.tsx index c6f0d82363c..223483277b8 100644 --- a/src/app/(proper_react)/(redesign)/(authenticated)/user/(dashboard)/settings/page.tsx +++ b/src/app/(proper_react)/(redesign)/(authenticated)/user/(dashboard)/settings/page.tsx @@ -24,7 +24,7 @@ import { getLocale } from "../../../../../../functions/universal/getLocale"; import { getCountryCode } from "../../../../../../functions/server/getCountryCode"; import { getSubscriberById } from "../../../../../../../db/tables/subscribers"; import { checkSession } from "../../../../../../functions/server/checkSession"; -import { checkUserHasMonthlySubscription } from "../../../../../../functions/universal/user"; +import { checkUserHasMonthlySubscription } from "../../../../../../functions/server/user"; type Props = { searchParams: { diff --git a/src/app/functions/server/user.ts b/src/app/functions/server/user.ts new file mode 100644 index 00000000000..ee4bd38d12a --- /dev/null +++ b/src/app/functions/server/user.ts @@ -0,0 +1,45 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +import { Session } from "next-auth"; +import { getBillingAndSubscriptions } from "../../../utils/fxa"; + +/* c8 ignore start */ +export async function checkUserHasMonthlySubscription(user: Session["user"]) { + if (!user.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( + user.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); +} +/* c8 ignore stop */ diff --git a/src/app/functions/universal/user.ts b/src/app/functions/universal/user.ts index c710c3c6b07..507bd867909 100644 --- a/src/app/functions/universal/user.ts +++ b/src/app/functions/universal/user.ts @@ -3,7 +3,6 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ import { Session } from "next-auth"; -import { getBillingAndSubscriptions } from "../../../utils/fxa"; import { ISO8601DateString } from "../../../utils/parse"; // TODO: Add unit test when changing this code: @@ -41,42 +40,3 @@ export function meetsAgeRequirement(dateOfBirth: ISO8601DateString): boolean { return age >= USER_MIN_AGE; } - -/* c8 ignore start */ -export async function checkUserHasMonthlySubscription(user: Session["user"]) { - if (!user.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( - user.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); -} -/* c8 ignore stop */