Skip to content

Commit c4bbb85

Browse files
committed
Add support for gifted pro subscriptions
1 parent 6fc1ebb commit c4bbb85

File tree

7 files changed

+73
-5
lines changed

7 files changed

+73
-5
lines changed

apps/page/lib/data.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,5 +357,6 @@ export {
357357
fetchRenderData,
358358
isSubscriptionActive,
359359
PAGINATION_LIMIT,
360-
translateHostToPageIdentifier,
360+
translateHostToPageIdentifier
361361
};
362+

apps/web/pages/account/billing.tsx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,21 @@ export default function Billing() {
5858
<div className="mt-5 md:mt-0 md:col-span-2">
5959
{!billingDetails && <SpinnerWithSpacing />}
6060

61-
{billingDetails && (
61+
{billingDetails?.has_active_subscription &&
62+
!billingDetails?.subscription ? (
63+
<div className="shadow overflow-hidden sm:rounded-md">
64+
<div className="px-4 py-3 bg-white dark:bg-black sm:p-3">
65+
<h2
66+
id="billing-history-heading"
67+
className="text-lg leading-6 font-medium text-gray-900 dark:text-gray-100"
68+
>
69+
You have a free Pro subscription.
70+
</h2>
71+
</div>
72+
</div>
73+
) : null}
74+
75+
{billingDetails?.subscription && (
6276
<div className="shadow overflow-hidden sm:rounded-md">
6377
<div className="px-4 py-3 bg-white dark:bg-black sm:p-3">
6478
<h2

apps/web/pages/api/billing/index.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const getBillingStatus = async (
1616
const { user } = await getSupabaseServerClient({ req, res });
1717

1818
const {
19+
pro_gifted,
1920
stripe_customer_id,
2021
stripe_subscription,
2122
has_active_subscription,
@@ -25,6 +26,24 @@ const getBillingStatus = async (
2526
process.env.STRIPE_PRICE_ID
2627
);
2728

29+
if (pro_gifted) {
30+
console.log(
31+
"getBillingStatus",
32+
user?.id,
33+
"user has gifted pro subscription"
34+
);
35+
36+
return res.status(200).json({
37+
invoice: null,
38+
subscription: null,
39+
price: {
40+
unit_amount,
41+
},
42+
usage: null,
43+
has_active_subscription: true,
44+
});
45+
}
46+
2847
if (!stripe_customer_id || !stripe_subscription) {
2948
console.log(
3049
"getBillingStatus",

apps/web/utils/useDatabase.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export const getUserById = async (user_id: string): Promise<IUser> => {
2020

2121
return {
2222
...user,
23-
has_active_subscription: ["trialing", "active"].includes(
23+
has_active_subscription: user.pro_gifted ?? ["trialing", "active"].includes(
2424
(user?.stripe_subscription as unknown as Stripe.Subscription)?.status
2525
),
2626
} as unknown as IUser;

packages/supabase/migrations/0_schema.sql

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* USERS
3-
* Note: This table contains user data. Users should only be able to view and update their own data.
3+
* Note: This table contains user data. Users should only be able to view their own data.
44
*/
55
create table users (
66
-- UUID from auth.users
@@ -14,7 +14,6 @@ create table users (
1414
);
1515
alter table users enable row level security;
1616
create policy "Can view own user data." on users for select using (auth.uid() = id);
17-
create policy "Can update own user data." on users for update using (auth.uid() = id);
1817

1918
/**
2019
* This trigger automatically creates a user entry when a new user signs up via Supabase Auth.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
ALTER TABLE public.users ADD COLUMN pro_gifted boolean DEFAULT false;
2+
3+
CREATE
4+
OR REPLACE FUNCTION is_subscription_active (user_id uuid) RETURNS BOOLEAN AS $$
5+
DECLARE
6+
subscription_status TEXT;
7+
is_gifted boolean;
8+
BEGIN
9+
SELECT pro_gifted INTO is_gifted
10+
FROM public.users
11+
WHERE id = user_id;
12+
13+
IF is_gifted IS TRUE THEN
14+
RETURN TRUE;
15+
END IF;
16+
17+
SELECT
18+
stripe_subscription->>'status' INTO subscription_status
19+
FROM public.users
20+
WHERE id = user_id;
21+
22+
IF subscription_status IS NULL THEN
23+
RETURN TRUE;
24+
END IF;
25+
26+
IF subscription_status IS NOT NULL AND subscription_status != 'canceled' THEN
27+
RETURN TRUE;
28+
ELSE
29+
RETURN FALSE;
30+
END IF;
31+
END;
32+
$$ LANGUAGE plpgsql;

packages/supabase/types/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ export type Database = {
342342
avatar_url: string | null
343343
full_name: string | null
344344
id: string
345+
pro_gifted: boolean | null
345346
stripe_customer_id: string | null
346347
stripe_subscription: Json | null
347348
stripe_subscription_id: string | null
@@ -350,6 +351,7 @@ export type Database = {
350351
avatar_url?: string | null
351352
full_name?: string | null
352353
id: string
354+
pro_gifted?: boolean | null
353355
stripe_customer_id?: string | null
354356
stripe_subscription?: Json | null
355357
stripe_subscription_id?: string | null
@@ -358,6 +360,7 @@ export type Database = {
358360
avatar_url?: string | null
359361
full_name?: string | null
360362
id?: string
363+
pro_gifted?: boolean | null
361364
stripe_customer_id?: string | null
362365
stripe_subscription?: Json | null
363366
stripe_subscription_id?: string | null

0 commit comments

Comments
 (0)