diff --git a/src/integrations/supabase/types.ts b/src/integrations/supabase/types.ts index 6ba0e35..cd38c86 100644 --- a/src/integrations/supabase/types.ts +++ b/src/integrations/supabase/types.ts @@ -186,6 +186,66 @@ export type Database = { } Relationships: [] } + subscriptions: { + Row: { + cancel_at_period_end: boolean + collection_method: string | null + company_name: string | null + created_at: string + current_period_end: string | null + current_period_start: string | null + customer_email: string + environment: string + id: string + kg_per_week: number | null + monthly_amount: number | null + price_id: string | null + status: string + stripe_customer_id: string + stripe_subscription_id: string + updated_at: string + user_id: string | null + } + Insert: { + cancel_at_period_end?: boolean + collection_method?: string | null + company_name?: string | null + created_at?: string + current_period_end?: string | null + current_period_start?: string | null + customer_email: string + environment?: string + id?: string + kg_per_week?: number | null + monthly_amount?: number | null + price_id?: string | null + status?: string + stripe_customer_id: string + stripe_subscription_id: string + updated_at?: string + user_id?: string | null + } + Update: { + cancel_at_period_end?: boolean + collection_method?: string | null + company_name?: string | null + created_at?: string + current_period_end?: string | null + current_period_start?: string | null + customer_email?: string + environment?: string + id?: string + kg_per_week?: number | null + monthly_amount?: number | null + price_id?: string | null + status?: string + stripe_customer_id?: string + stripe_subscription_id?: string + updated_at?: string + user_id?: string | null + } + Relationships: [] + } user_roles: { Row: { created_at: string @@ -212,6 +272,10 @@ export type Database = { [_ in never]: never } Functions: { + has_active_kaka_subscription: { + Args: { user_uuid: string } + Returns: boolean + } has_role: { Args: { _role: Database["public"]["Enums"]["app_role"] diff --git a/supabase/migrations/20260711181911_52085f03-1c74-4baa-bffd-133ac1ffb57e.sql b/supabase/migrations/20260711181911_52085f03-1c74-4baa-bffd-133ac1ffb57e.sql new file mode 100644 index 0000000..a26a105 --- /dev/null +++ b/supabase/migrations/20260711181911_52085f03-1c74-4baa-bffd-133ac1ffb57e.sql @@ -0,0 +1,60 @@ + +CREATE TABLE public.subscriptions ( + id UUID PRIMARY KEY DEFAULT gen_random_uuid(), + user_id UUID REFERENCES auth.users(id) ON DELETE SET NULL, + customer_email TEXT NOT NULL, + company_name TEXT, + stripe_subscription_id TEXT NOT NULL UNIQUE, + stripe_customer_id TEXT NOT NULL, + price_id TEXT, + kg_per_week NUMERIC, + monthly_amount NUMERIC, + status TEXT NOT NULL DEFAULT 'active', + collection_method TEXT, + current_period_start TIMESTAMPTZ, + current_period_end TIMESTAMPTZ, + cancel_at_period_end BOOLEAN NOT NULL DEFAULT false, + environment TEXT NOT NULL DEFAULT 'sandbox', + created_at TIMESTAMPTZ NOT NULL DEFAULT now(), + updated_at TIMESTAMPTZ NOT NULL DEFAULT now() +); + +CREATE INDEX idx_subscriptions_user_id ON public.subscriptions(user_id); +CREATE INDEX idx_subscriptions_customer_email ON public.subscriptions(customer_email); +CREATE INDEX idx_subscriptions_stripe_id ON public.subscriptions(stripe_subscription_id); + +GRANT SELECT ON public.subscriptions TO authenticated; +GRANT ALL ON public.subscriptions TO service_role; + +ALTER TABLE public.subscriptions ENABLE ROW LEVEL SECURITY; + +CREATE POLICY "Users can view own subscriptions" + ON public.subscriptions FOR SELECT + TO authenticated + USING (auth.uid() = user_id); + +CREATE POLICY "Admins can view all subscriptions" + ON public.subscriptions FOR SELECT + TO authenticated + USING (public.has_role(auth.uid(), 'admin'::app_role)); + +CREATE TRIGGER update_subscriptions_updated_at + BEFORE UPDATE ON public.subscriptions + FOR EACH ROW EXECUTE FUNCTION public.update_updated_at_column(); + +CREATE OR REPLACE FUNCTION public.has_active_kaka_subscription(user_uuid UUID) +RETURNS BOOLEAN +LANGUAGE SQL +STABLE +SECURITY DEFINER +SET search_path = public +AS $$ + SELECT EXISTS ( + SELECT 1 FROM public.subscriptions + WHERE user_id = user_uuid + AND ( + (status IN ('active','trialing','past_due') AND (current_period_end IS NULL OR current_period_end > now())) + OR (status = 'canceled' AND current_period_end > now()) + ) + ); +$$;