Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion apps/page/lib/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,5 +357,6 @@ export {
fetchRenderData,
isSubscriptionActive,
PAGINATION_LIMIT,
translateHostToPageIdentifier,
translateHostToPageIdentifier
};

16 changes: 15 additions & 1 deletion apps/web/pages/account/billing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,21 @@ export default function Billing() {
<div className="mt-5 md:mt-0 md:col-span-2">
{!billingDetails && <SpinnerWithSpacing />}

{billingDetails && (
{billingDetails?.has_active_subscription &&
!billingDetails?.subscription ? (
<div className="shadow overflow-hidden sm:rounded-md">
<div className="px-4 py-3 bg-white dark:bg-black sm:p-3">
<h2
id="billing-history-heading"
className="text-lg leading-6 font-medium text-gray-900 dark:text-gray-100"
>
You have a free Pro subscription.
</h2>
</div>
</div>
) : null}

{billingDetails?.subscription && (
<div className="shadow overflow-hidden sm:rounded-md">
<div className="px-4 py-3 bg-white dark:bg-black sm:p-3">
<h2
Expand Down
19 changes: 19 additions & 0 deletions apps/web/pages/api/billing/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const getBillingStatus = async (
const { user } = await getSupabaseServerClient({ req, res });

const {
pro_gifted,
stripe_customer_id,
stripe_subscription,
has_active_subscription,
Expand All @@ -25,6 +26,24 @@ const getBillingStatus = async (
process.env.STRIPE_PRICE_ID
);

if (pro_gifted) {
console.log(
"getBillingStatus",
user?.id,
"user has gifted pro subscription"
);

return res.status(200).json({
invoice: null,
subscription: null,
price: {
unit_amount,
},
usage: null,
has_active_subscription: true,
});
}

if (!stripe_customer_id || !stripe_subscription) {
console.log(
"getBillingStatus",
Expand Down
2 changes: 1 addition & 1 deletion apps/web/utils/useDatabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const getUserById = async (user_id: string): Promise<IUser> => {

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;
Expand Down
3 changes: 1 addition & 2 deletions packages/supabase/migrations/0_schema.sql
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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.
Expand Down
32 changes: 32 additions & 0 deletions packages/supabase/migrations/15_pro_gift.sql
Original file line number Diff line number Diff line change
@@ -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;
3 changes: 3 additions & 0 deletions packages/supabase/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
Loading