feat: link end customers directly to companies
All checks were successful
Staging Build / build (push) Successful in 3m37s

This commit is contained in:
DanielS
2026-07-04 15:17:04 +02:00
parent d3bf4b1db9
commit 5f65f890ee
5 changed files with 110 additions and 72 deletions

View File

@@ -96,7 +96,7 @@ BEGIN
IF NOT EXISTS (SELECT 1 FROM pg_tables WHERE schemaname = 'public' AND tablename = 'end_customers') THEN
CREATE TABLE public.end_customers (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
partner_id UUID NOT NULL REFERENCES public.profiles(id) ON DELETE CASCADE,
partner_id UUID NOT NULL REFERENCES public.companies(id) ON DELETE CASCADE,
company_name TEXT NOT NULL,
vat_id TEXT,
first_name TEXT,
@@ -153,7 +153,7 @@ BEGIN
-- end_customers Policies
DROP POLICY IF EXISTS "Partner sehen nur eigene Endkunden" ON public.end_customers;
CREATE POLICY "Partner sehen nur eigene Endkunden" ON public.end_customers FOR ALL USING (auth.uid() = partner_id) WITH CHECK (auth.uid() = partner_id);
CREATE POLICY "Partner sehen nur eigene Endkunden" ON public.end_customers FOR ALL USING (partner_id = (SELECT company_id FROM public.users WHERE id = auth.uid())) WITH CHECK (partner_id = (SELECT company_id FROM public.users WHERE id = auth.uid()));
-- pos_modules Policies
DROP POLICY IF EXISTS "Allow authenticated read on pos_modules" ON public.pos_modules;
@@ -164,14 +164,14 @@ BEGIN
CREATE POLICY "Partner sehen Lizenzen eigener Kunden" ON public.licenses FOR SELECT USING (
EXISTS (
SELECT 1 FROM public.end_customers c
WHERE c.id = licenses.end_customer_id AND c.partner_id = auth.uid()
WHERE c.id = licenses.end_customer_id AND c.partner_id = (SELECT company_id FROM public.users WHERE id = auth.uid())
)
);
DROP POLICY IF EXISTS "Partner erstellen Lizenzen eigener Kunden" ON public.licenses;
CREATE POLICY "Partner erstellen Lizenzen eigener Kunden" ON public.licenses FOR INSERT WITH CHECK (
EXISTS (
SELECT 1 FROM public.end_customers c
WHERE c.id = licenses.end_customer_id AND c.partner_id = auth.uid()
WHERE c.id = licenses.end_customer_id AND c.partner_id = (SELECT company_id FROM public.users WHERE id = auth.uid())
)
);

View File

@@ -0,0 +1,51 @@
-- Migration: Update end_customers.partner_id to reference public.companies.id
-- and update RLS policies accordingly.
-- 1. Drop old foreign key constraint if exists
ALTER TABLE public.end_customers
DROP CONSTRAINT IF EXISTS end_customers_partner_id_fkey;
-- 2. Add new foreign key constraint to public.companies(id)
-- Note: the column is still called partner_id, but references companies now.
ALTER TABLE public.end_customers
ADD CONSTRAINT end_customers_partner_id_fkey
FOREIGN KEY (partner_id)
REFERENCES public.companies(id)
ON DELETE CASCADE;
-- 3. Recreate RLS policies for end_customers
DROP POLICY IF EXISTS "Partner sehen nur eigene Endkunden" ON public.end_customers;
CREATE POLICY "Partner sehen nur eigene Endkunden" ON public.end_customers
FOR ALL
USING (
partner_id = (SELECT company_id FROM public.users WHERE id = auth.uid())
)
WITH CHECK (
partner_id = (SELECT company_id FROM public.users WHERE id = auth.uid())
);
-- 4. Recreate RLS policies for licenses (since partner_id now matches company_id)
DROP POLICY IF EXISTS "Partner sehen Lizenzen eigener Kunden" ON public.licenses;
CREATE POLICY "Partner sehen Lizenzen eigener Kunden" ON public.licenses
FOR SELECT
USING (
EXISTS (
SELECT 1 FROM public.end_customers c
WHERE c.id = licenses.end_customer_id
AND c.partner_id = (SELECT company_id FROM public.users WHERE id = auth.uid())
)
);
DROP POLICY IF EXISTS "Partner erstellen Lizenzen eigener Kunden" ON public.licenses;
CREATE POLICY "Partner erstellen Lizenzen eigener Kunden" ON public.licenses
FOR INSERT
WITH CHECK (
EXISTS (
SELECT 1 FROM public.end_customers c
WHERE c.id = licenses.end_customer_id
AND c.partner_id = (SELECT company_id FROM public.users WHERE id = auth.uid())
)
);
-- Reload Schema Cache
NOTIFY pgrst, 'reload schema';