Skip to content

Enhance payment system #3112

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Jun 18, 2025
Merged
Show file tree
Hide file tree
Changes from 15 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
set check_function_bodies = off;

CREATE OR REPLACE FUNCTION public.check_ws_creator(ws_id uuid)
RETURNS boolean
LANGUAGE plpgsql
AS $function$BEGIN
RETURN (
(SELECT creator_id FROM public.workspaces WHERE id = ws_id) = auth.uid()
AND
NOT EXISTS (
SELECT 1 FROM public.workspace_subscription
WHERE public.workspace_subscription.ws_id = ws_id
)
);
END;$function$
;


Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
create table "public"."workspace_subscription_products" (
"id" uuid not null,
"created_at" timestamp with time zone not null default now(),
"name" text,
"description" text,
"price" real,
"recurring_interval" text default 'month'::text
);


alter table "public"."workspace_subscription" add column "product_id" uuid;

CREATE UNIQUE INDEX workspace_subscription_products_pkey ON public.workspace_subscription_products USING btree (id);

alter table "public"."workspace_subscription_products" add constraint "workspace_subscription_products_pkey" PRIMARY KEY using index "workspace_subscription_products_pkey";

alter table "public"."workspace_subscription" add constraint "workspace_subscription_product_id_fkey" FOREIGN KEY (product_id) REFERENCES workspace_subscription_products(id) not valid;

alter table "public"."workspace_subscription" validate constraint "workspace_subscription_product_id_fkey";

grant delete on table "public"."workspace_subscription_products" to "anon";

grant insert on table "public"."workspace_subscription_products" to "anon";

grant references on table "public"."workspace_subscription_products" to "anon";

grant select on table "public"."workspace_subscription_products" to "anon";

grant trigger on table "public"."workspace_subscription_products" to "anon";

grant truncate on table "public"."workspace_subscription_products" to "anon";

grant update on table "public"."workspace_subscription_products" to "anon";

grant delete on table "public"."workspace_subscription_products" to "authenticated";

grant insert on table "public"."workspace_subscription_products" to "authenticated";

grant references on table "public"."workspace_subscription_products" to "authenticated";

grant select on table "public"."workspace_subscription_products" to "authenticated";

grant trigger on table "public"."workspace_subscription_products" to "authenticated";

grant truncate on table "public"."workspace_subscription_products" to "authenticated";

grant update on table "public"."workspace_subscription_products" to "authenticated";

grant delete on table "public"."workspace_subscription_products" to "service_role";

grant insert on table "public"."workspace_subscription_products" to "service_role";

grant references on table "public"."workspace_subscription_products" to "service_role";

grant select on table "public"."workspace_subscription_products" to "service_role";

grant trigger on table "public"."workspace_subscription_products" to "service_role";

grant truncate on table "public"."workspace_subscription_products" to "service_role";

grant update on table "public"."workspace_subscription_products" to "service_role";


13 changes: 13 additions & 0 deletions apps/db/supabase/migrations/20250618082551_new_migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
drop policy "only allow owner of the user to buy subscription" on "public"."workspace_subscription";

create policy "only allow owner of the user to buy subscription"
on "public"."workspace_subscription"
as permissive
for insert
to authenticated
with check ((auth.uid() = ( SELECT workspaces.creator_id
FROM workspaces
WHERE (workspaces.id = workspace_subscription.ws_id))));



13 changes: 13 additions & 0 deletions apps/db/supabase/migrations/20250618083711_new_migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
drop policy "only allow owner of the user to buy subscription" on "public"."workspace_subscription";

create policy "only allow owner of the user to buy subscription"
on "public"."workspace_subscription"
as permissive
for insert
to authenticated
with check ((EXISTS ( SELECT 1
FROM workspaces
WHERE ((workspaces.id = workspace_subscription.ws_id) AND (workspaces.creator_id = auth.uid())))));



19 changes: 19 additions & 0 deletions apps/db/supabase/migrations/20250618085435_new_migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
set check_function_bodies = off;

CREATE OR REPLACE FUNCTION public.check_ws_creator(ws_id uuid)
RETURNS boolean
LANGUAGE plpgsql
AS $function$BEGIN
RETURN (
(SELECT creator_id FROM public.workspaces WHERE id = ws_id)
= auth.uid()
AND
NOT EXISTS (
SELECT 1 FROM public.workspace_subscription
WHERE public.workspace_subscription.ws_id = ws_id
)
);
END;$function$
;


19 changes: 19 additions & 0 deletions apps/db/supabase/migrations/20250618085749_new_migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
set check_function_bodies = off;

CREATE OR REPLACE FUNCTION public.check_ws_creator(ws_id uuid)
RETURNS boolean
LANGUAGE plpgsql
AS $function$BEGIN
RETURN (
(
SELECT creator_id FROM public.workspaces WHERE id = check_ws_creator.ws_id
) = auth.uid()
AND NOT EXISTS (
SELECT 1 FROM public.workspace_subscription
WHERE public.workspace_subscription.ws_id = check_ws_creator.ws_id
)
);
END;$function$
;


13 changes: 13 additions & 0 deletions apps/db/supabase/migrations/20250618090740_new_migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
drop policy "allow select for users that are in the workspace" on "public"."workspace_subscription";

create policy "allow select for users that are in the workspace"
on "public"."workspace_subscription"
as permissive
for select
to authenticated
using ((EXISTS ( SELECT 1
FROM workspace_members wm
WHERE ((wm.user_id = auth.uid()) AND (wm.ws_id = workspace_subscription.ws_id)))));



Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
create policy "allow delete access for creator workspace"
on "public"."workspace_subscription"
as permissive
for delete
to authenticated
using ((EXISTS ( SELECT 1
FROM workspaces
WHERE ((workspaces.id = workspace_subscription.ws_id) AND (workspaces.creator_id = auth.uid())))));



51 changes: 51 additions & 0 deletions apps/db/supabase/migrations/20250618161718_new_migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
drop view if exists "public"."time_tracking_session_analytics";

drop index if exists "public"."idx_time_tracking_sessions_was_resumed";

alter table "public"."time_tracking_sessions" drop column "was_resumed";

alter table "public"."workspace_subscription_products" enable row level security;

create or replace view "public"."time_tracking_session_analytics" as SELECT tts.id,
tts.ws_id,
tts.user_id,
tts.task_id,
tts.category_id,
tts.title,
tts.description,
tts.start_time,
tts.end_time,
tts.duration_seconds,
tts.is_running,
tts.tags,
tts.created_at,
tts.updated_at,
tts.productivity_score,
ttc.name AS category_name,
ttc.color AS category_color,
t.name AS task_name,
EXTRACT(hour FROM tts.start_time) AS start_hour,
EXTRACT(dow FROM tts.start_time) AS day_of_week,
date_trunc('day'::text, tts.start_time) AS session_date,
date_trunc('week'::text, tts.start_time) AS session_week,
date_trunc('month'::text, tts.start_time) AS session_month,
CASE
WHEN (tts.duration_seconds >= 7200) THEN 'long'::text
WHEN (tts.duration_seconds >= 1800) THEN 'medium'::text
WHEN (tts.duration_seconds >= 300) THEN 'short'::text
ELSE 'micro'::text
END AS session_length_category
FROM ((time_tracking_sessions tts
LEFT JOIN time_tracking_categories ttc ON ((tts.category_id = ttc.id)))
LEFT JOIN tasks t ON ((tts.task_id = t.id)));


create policy "allow view for all products"
on "public"."workspace_subscription_products"
as permissive
for select
to authenticated
using (true);



2 changes: 1 addition & 1 deletion apps/web/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ NEXT_PUBLIC_PROXY_API_KEY=YOUR_NEXT_PUBLIC_PROXY_API_KEY

# Payment Polar.sh
POLAR_WEBHOOK_SECRET=YOUR_POLAR_WEBHOOK_SECRET
NEXT_PUBLIC_POLAR_ACCESS_TOKEN=YOUR_NEXT_PUBLIC_POLAR_ACCESS_TOKEN
NEXT_PUBLIC_POLAR_ACCESS_TOKEN=YOUR_NEXT_PUBLIC_POLAR_ACCESS_TOKEN
Loading