Skip to content

Commit

Permalink
fix client import, move checkUserHasMonthlySubscription from universa…
Browse files Browse the repository at this point in the history
…l to server function (#4890)
  • Loading branch information
rhelmer authored Jul 30, 2024
1 parent 4e79747 commit bfa2a6c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
45 changes: 45 additions & 0 deletions src/app/functions/server/user.ts
Original file line number Diff line number Diff line change
@@ -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 */
40 changes: 0 additions & 40 deletions src/app/functions/universal/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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 */

0 comments on commit bfa2a6c

Please sign in to comment.