Co-authored-by: wolfoftyreso-debug <250630591+wolfoftyreso-debug@users.noreply.github.com>
This commit is contained in:
gpt-engineer-app[bot]
2026-07-11 18:19:14 +00:00
parent 05c1c84707
commit c1d75b1ee9
2 changed files with 124 additions and 0 deletions
+64
View File
@@ -186,6 +186,66 @@ export type Database = {
} }
Relationships: [] 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: { user_roles: {
Row: { Row: {
created_at: string created_at: string
@@ -212,6 +272,10 @@ export type Database = {
[_ in never]: never [_ in never]: never
} }
Functions: { Functions: {
has_active_kaka_subscription: {
Args: { user_uuid: string }
Returns: boolean
}
has_role: { has_role: {
Args: { Args: {
_role: Database["public"]["Enums"]["app_role"] _role: Database["public"]["Enums"]["app_role"]
@@ -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())
)
);
$$;