-- ============================================
-- 此文件由 Meoo Cloud 自动生成，请勿修改
-- This file is auto-generated by Meoo Cloud, DO NOT MODIFY
-- ============================================


-- 为分享链接场景添加特殊的 RLS 策略
-- 允许工厂查看通过 plan_factories 关联的计划

-- production_plans：工厂可以查看关联的计划
CREATE POLICY factory_select_plans_via_share ON public.production_plans
FOR SELECT USING (
 EXISTS (
 SELECT 1 FROM public.plan_factories
 WHERE plan_id = production_plans.id
 AND factory_id = public.get_user_master_company_id()
 )
);

-- yarn_ratios：工厂可以查看关联计划的纱线配比
CREATE POLICY factory_select_yarn_ratios_via_share ON public.yarn_ratios
FOR SELECT USING (
 EXISTS (
 SELECT 1 FROM public.plan_factories
 WHERE plan_id = yarn_ratios.plan_id
 AND factory_id = public.get_user_master_company_id()
 )
);

-- companies：允许查看所有公司（用于显示采购商名称）
-- 删除旧的限制策略，创建新的开放查询策略
DROP POLICY IF EXISTS users_select_own_company ON public.companies;

-- 允许所有登录用户查看所有公司信息（用于分享链接显示采购商名称）
CREATE POLICY users_select_companies ON public.companies
FOR SELECT USING (true);

