diff --git a/apps/page/lib/data.ts b/apps/page/lib/data.ts index 12e42c6..aac06fe 100644 --- a/apps/page/lib/data.ts +++ b/apps/page/lib/data.ts @@ -357,5 +357,6 @@ export { fetchRenderData, isSubscriptionActive, PAGINATION_LIMIT, - translateHostToPageIdentifier, + translateHostToPageIdentifier }; + diff --git a/apps/web/pages/account/billing.tsx b/apps/web/pages/account/billing.tsx index 12e293d..d10e0d4 100644 --- a/apps/web/pages/account/billing.tsx +++ b/apps/web/pages/account/billing.tsx @@ -58,7 +58,21 @@ export default function Billing() {
{!billingDetails && } - {billingDetails && ( + {billingDetails?.has_active_subscription && + !billingDetails?.subscription ? ( +
+
+

+ You have a free Pro subscription. +

+
+
+ ) : null} + + {billingDetails?.subscription && (

=> { return { ...user, - has_active_subscription: ["trialing", "active"].includes( + has_active_subscription: user.pro_gifted ?? ["trialing", "active"].includes( (user?.stripe_subscription as unknown as Stripe.Subscription)?.status ), } as unknown as IUser; diff --git a/packages/supabase/migrations/0_schema.sql b/packages/supabase/migrations/0_schema.sql index eaf1b70..8a98ae8 100644 --- a/packages/supabase/migrations/0_schema.sql +++ b/packages/supabase/migrations/0_schema.sql @@ -1,6 +1,6 @@ /** * USERS -* Note: This table contains user data. Users should only be able to view and update their own data. +* Note: This table contains user data. Users should only be able to view their own data. */ create table users ( -- UUID from auth.users @@ -14,7 +14,6 @@ create table users ( ); alter table users enable row level security; create policy "Can view own user data." on users for select using (auth.uid() = id); -create policy "Can update own user data." on users for update using (auth.uid() = id); /** * This trigger automatically creates a user entry when a new user signs up via Supabase Auth. diff --git a/packages/supabase/migrations/15_pro_gift.sql b/packages/supabase/migrations/15_pro_gift.sql new file mode 100644 index 0000000..43647c3 --- /dev/null +++ b/packages/supabase/migrations/15_pro_gift.sql @@ -0,0 +1,32 @@ +ALTER TABLE public.users ADD COLUMN pro_gifted boolean DEFAULT false; + +CREATE +OR REPLACE FUNCTION is_subscription_active (user_id uuid) RETURNS BOOLEAN AS $$ +DECLARE + subscription_status TEXT; + is_gifted boolean; +BEGIN + SELECT pro_gifted INTO is_gifted + FROM public.users + WHERE id = user_id; + + IF is_gifted IS TRUE THEN + RETURN TRUE; + END IF; + + SELECT + stripe_subscription->>'status' INTO subscription_status + FROM public.users + WHERE id = user_id; + + IF subscription_status IS NULL THEN + RETURN TRUE; + END IF; + + IF subscription_status IS NOT NULL AND subscription_status != 'canceled' THEN + RETURN TRUE; + ELSE + RETURN FALSE; + END IF; +END; +$$ LANGUAGE plpgsql; diff --git a/packages/supabase/types/index.ts b/packages/supabase/types/index.ts index cd7e05c..285dd59 100644 --- a/packages/supabase/types/index.ts +++ b/packages/supabase/types/index.ts @@ -342,6 +342,7 @@ export type Database = { avatar_url: string | null full_name: string | null id: string + pro_gifted: boolean | null stripe_customer_id: string | null stripe_subscription: Json | null stripe_subscription_id: string | null @@ -350,6 +351,7 @@ export type Database = { avatar_url?: string | null full_name?: string | null id: string + pro_gifted?: boolean | null stripe_customer_id?: string | null stripe_subscription?: Json | null stripe_subscription_id?: string | null @@ -358,6 +360,7 @@ export type Database = { avatar_url?: string | null full_name?: string | null id?: string + pro_gifted?: boolean | null stripe_customer_id?: string | null stripe_subscription?: Json | null stripe_subscription_id?: string | null