Skip to content

Commit

Permalink
Fixes a ts error in stripe.ts and a minor bug in useIsSubscribed cust…
Browse files Browse the repository at this point in the history
…om hook
  • Loading branch information
deepsingh132 committed Jul 4, 2024
1 parent 667b356 commit badbefe
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
8 changes: 6 additions & 2 deletions convex/stripe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export const cancelSubscription = action({
{
clerkId: user.subject,
}
);
) as { subscriptionId: string | undefined};

if (!subscriptionId) {
throw new Error("No subscription found for this user!");
Expand Down Expand Up @@ -128,7 +128,11 @@ export const createCustomerPortal = action({
{
clerkId: user.subject,
}
);
) as { customerId: string | undefined };

if (!customerId) {
throw new Error("No customer id found for this user!");
}

const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!, {
apiVersion: "2024-04-10",
Expand Down
4 changes: 2 additions & 2 deletions convex/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@ export const isUserSubscribed = async (ctx: QueryCtx | MutationCtx) => {

export const getSubscriptionByClerkId = query({
args: { clerkId: v.string() },
handler: async (ctx, args) => {
handler: async (ctx, args) : Promise<{ subscriptionId: string | undefined, endsOn: number | undefined, plan: string | undefined, customerId: string | undefined} | null | undefined> =>
{
const user = await ctx.db
.query("users")
.filter((q) => q.eq(q.field("clerkId"), args.clerkId))
.first();

if (!user) {
// throw new ConvexError("User not found");
return null;
}

Expand Down
6 changes: 3 additions & 3 deletions hooks/useIsSubscribed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ export function useIsSubscribed(id: string) {

const { setIsFetching } = useIsFetching();

const user = useQuery(api.users.getSubscriptionByClerkId, { clerkId: id });
const user = useQuery(api.users.getSubscriptionByClerkId, { clerkId: id }); // will be undefined if fetching and null if no subscription or user

useEffect(() => {
if (!user) return;
if (user === undefined) return;
setIsFetching(false);
}, [user]);

Expand All @@ -26,7 +26,7 @@ export function useGetPlan(id: string) {
const user = useQuery(api.users.getSubscriptionByClerkId, { clerkId: id });

useEffect(() => {
if (!user) return;
if (user === undefined) return;
setIsFetching(false);
}, [user]);

Expand Down

0 comments on commit badbefe

Please sign in to comment.