Skip to content

Commit

Permalink
Merge pull request #34 from route06inc/add-handling-canceled-subscrip…
Browse files Browse the repository at this point in the history
…tions

add handling canceled subscriptions
  • Loading branch information
satococoa authored Oct 22, 2024
2 parents d11f5f0 + e0d8679 commit 8d02886
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 12 deletions.
6 changes: 2 additions & 4 deletions app/(auth)/lib/is-route06-user.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
"use server";

import { getUser } from "@/lib/supabase";

const R06_EMAIL_DOMAIN = "route06.co.jp";
import { isEmailFromRoute06 } from "@/lib/utils";

export const isRoute06User = async () => {
const supabaseUser = await getUser();
Expand All @@ -12,6 +11,5 @@ export const isRoute06User = async () => {
throw new Error("No email found for user");
}

const emailDomain = email.split("@")[1];
return emailDomain === R06_EMAIL_DOMAIN;
return isEmailFromRoute06(email);
};
4 changes: 2 additions & 2 deletions middleware.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { retrieveStripeSubscriptionBySupabaseUserId } from "@/services/accounts/actions";
import { retrieveActiveStripeSubscriptionBySupabaseUserId } from "@/services/accounts/actions";
import { NextResponse } from "next/server";
import { supabaseMiddleware } from "./lib/supabase";
import { isEmailFromRoute06 } from "./lib/utils";
Expand All @@ -13,7 +13,7 @@ export default supabaseMiddleware(async (user, request) => {

// Proceeding to check the user's subscription status since the email is not from the route06.co.jp
if (!isEmailFromRoute06(user.email ?? "")) {
const subscription = await retrieveStripeSubscriptionBySupabaseUserId(
const subscription = await retrieveActiveStripeSubscriptionBySupabaseUserId(
user.id,
);
if (subscription == null) {
Expand Down
2 changes: 1 addition & 1 deletion services/accounts/actions.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { retrieveStripeSubscriptionBySupabaseUserId } from "./actions/retrieve-stripe-subscription";
export { retrieveActiveStripeSubscriptionBySupabaseUserId } from "./actions/retrieve-stripe-subscription";
13 changes: 8 additions & 5 deletions services/accounts/actions/retrieve-stripe-subscription.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import {
db,
organizations,
stripeUserMappings,
subscriptions,
supabaseUserMappings,
teamMemberships,
teams,
users,
} from "@/drizzle";
import { stripe } from "@/services/external/stripe";
import { eq } from "drizzle-orm";
import { and, eq } from "drizzle-orm";

export const retrieveStripeSubscriptionBySupabaseUserId = async (
export const retrieveActiveStripeSubscriptionBySupabaseUserId = async (
supabaseUserId: string,
) => {
const [subscription] = await db
Expand All @@ -28,6 +26,11 @@ export const retrieveStripeSubscriptionBySupabaseUserId = async (
supabaseUserMappings,
eq(supabaseUserMappings.userDbId, users.dbId),
)
.where(eq(supabaseUserMappings.supabaseUserId, supabaseUserId));
.where(
and(
eq(supabaseUserMappings.supabaseUserId, supabaseUserId),
eq(subscriptions.status, "active"),
),
);
return subscription;
};

0 comments on commit 8d02886

Please sign in to comment.