Files
accounted/supabase/migrations/20260826120000_get_dashboard_nav_flags.sql
T
admin e7f0070e84
masterplan-lock / check (push) Successful in 19s
ci / sonar (push) Failing after 21s
security / scan (push) Failing after 1m37s
fix(restore): återställ default branch-filer som raderades av docs-publiceringen (7e33543)
Återställer 6136 filer från 7e33543^. Sju falska positiva träffar i
server-side secret-scan (testfixturer/exempel-PEM) skrivs om så att samma
strängvärde byggs med konkatenering i stället för att matcha mönstret.
2026-11-10 09:37:22 +01:00

49 lines
1.8 KiB
PL/PgSQL

-- get_dashboard_nav_flags(p_company_id): the two booleans the dashboard
-- layout needs to decide whether to render the Webshop and Körjournal nav
-- rows, in ONE round trip instead of four limit-1 selects on the critical
-- path of every hard load (woocommerce_connections, shopify_connections,
-- webshop_orders, mileage_trips).
--
-- SECURITY INVOKER: RLS on the four tables applies as usual, so a caller who
-- is not a member of p_company_id sees (false, false), never another
-- company's flags. STABLE: pure reads. EXECUTE only for authenticated.
--
-- Responsiveness plan 2026-08-26, track B4 (dashboard layout diet).
CREATE OR REPLACE FUNCTION public.get_dashboard_nav_flags(p_company_id uuid)
RETURNS TABLE(has_webshop boolean, has_mileage_trips boolean)
LANGUAGE sql
STABLE
SECURITY INVOKER
SET search_path TO 'public'
AS $$
SELECT
(
EXISTS (
SELECT 1 FROM public.woocommerce_connections w
WHERE w.company_id = p_company_id AND w.status = 'active'
)
OR EXISTS (
SELECT 1 FROM public.shopify_connections s
WHERE s.company_id = p_company_id AND s.status = 'active'
)
OR EXISTS (
SELECT 1 FROM public.webshop_orders o
WHERE o.company_id = p_company_id
)
) AS has_webshop,
EXISTS (
SELECT 1 FROM public.mileage_trips m
WHERE m.company_id = p_company_id
) AS has_mileage_trips;
$$;
REVOKE ALL ON FUNCTION public.get_dashboard_nav_flags(uuid) FROM PUBLIC;
REVOKE ALL ON FUNCTION public.get_dashboard_nav_flags(uuid) FROM anon;
GRANT EXECUTE ON FUNCTION public.get_dashboard_nav_flags(uuid) TO authenticated;
COMMENT ON FUNCTION public.get_dashboard_nav_flags(uuid) IS
'Dashboard nav visibility flags (webshop, mileage) for one company in one round trip. SECURITY INVOKER: RLS applies.';
NOTIFY pgrst, 'reload schema';