Co-authored-by: renee-png <262607627+renee-png@users.noreply.github.com>
This commit is contained in:
gpt-engineer-app[bot]
2026-04-18 17:49:57 +00:00
co-authored by renee-png
parent d566d0df8f
commit bd97829760
4 changed files with 154 additions and 0 deletions
@@ -0,0 +1,49 @@
-- Payment requests for homeowner Stripe checkout
CREATE TABLE IF NOT EXISTS public.payment_requests (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
homeowner_id uuid REFERENCES public.homeowners(id) ON DELETE SET NULL,
collection_id uuid REFERENCES public.collections(id) ON DELETE SET NULL,
case_id uuid REFERENCES public.cases(id) ON DELETE SET NULL,
client_id uuid REFERENCES public.clients(id) ON DELETE SET NULL,
recipient_name text NOT NULL,
recipient_email text,
description text,
-- amounts in cents
base_amount_cents integer NOT NULL CHECK (base_amount_cents > 0),
fee_amount_cents integer NOT NULL DEFAULT 0,
total_amount_cents integer NOT NULL,
currency text NOT NULL DEFAULT 'usd',
status text NOT NULL DEFAULT 'pending', -- pending | paid | canceled | expired
stripe_session_id text,
stripe_payment_intent_id text,
stripe_checkout_url text,
paid_at timestamptz,
email_sent_at timestamptz,
notes text,
created_by uuid,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now()
);
CREATE INDEX IF NOT EXISTS idx_payment_requests_status ON public.payment_requests(status);
CREATE INDEX IF NOT EXISTS idx_payment_requests_homeowner ON public.payment_requests(homeowner_id);
CREATE INDEX IF NOT EXISTS idx_payment_requests_session ON public.payment_requests(stripe_session_id);
ALTER TABLE public.payment_requests ENABLE ROW LEVEL SECURITY;
-- Authenticated users can view all (firm-internal)
CREATE POLICY "pr_select_auth" ON public.payment_requests
FOR SELECT TO authenticated USING (true);
CREATE POLICY "pr_insert_auth" ON public.payment_requests
FOR INSERT TO authenticated WITH CHECK (auth.uid() IS NOT NULL);
CREATE POLICY "pr_update_owner_or_admin" ON public.payment_requests
FOR UPDATE TO authenticated USING (is_admin(auth.uid()) OR created_by = auth.uid());
CREATE POLICY "pr_delete_owner_or_admin" ON public.payment_requests
FOR DELETE TO authenticated USING (is_admin(auth.uid()) OR created_by = auth.uid());
CREATE TRIGGER trg_payment_requests_updated_at
BEFORE UPDATE ON public.payment_requests
FOR EACH ROW EXECUTE FUNCTION public.tg_set_updated_at();