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


-- 修复 production_plans SELECT 策略条件3：移除 factory_type = 'textile' 硬编码
-- 改为不限制 factory_type，允许纺织厂和水洗厂都通过 share_links 查看 production_plans
-- 因为 production_plans 是纺织计划，水洗厂也需要在导入页面查看纺织计划详情（用于创建 washing_plans）
DROP POLICY IF EXISTS users_select_plans ON production_plans;

CREATE POLICY users_select_plans ON production_plans
 FOR SELECT
 USING (
 purchaser_id = get_user_master_company_id()
 OR EXISTS (
 SELECT 1 FROM plan_factories
 WHERE plan_factories.plan_id = production_plans.id
 AND plan_factories.factory_id = get_user_master_company_id()
 )
 OR EXISTS (
 SELECT 1 FROM share_links
 WHERE share_links.plan_id = production_plans.id
 AND (share_links.used_at IS NULL OR share_links.used_at > NOW() - INTERVAL '1 hour')
 AND share_links.expires_at > NOW()
 )
 );

