-- Add inquiry_id to v_funnel_order_rows and give funnel access to more views

DROP VIEW IF EXISTS public.v_funnel_order_rows;
-- Same as before except one new row:
-- order_rows.data->>'inquiryid' AS inquiryid,
CREATE VIEW v_funnel_order_rows AS
    SELECT
    r.id,
    r.order_id,
    r.inserted,
    r.type,
    r.sub_type,
    r.status,
    r.price,
    r.commission_amount,
    r.external_status,
    r.data->>'productId' AS product_id,
    r.data->>'material' AS material,
    r.data->>'artNo' AS artNo,
    r.data->>'designer' AS designer,
    (r.data->>'inquiryid')::int AS inquiry_id
    FROM order_rows r;

GRANT SELECT ON v_funnel_order_rows TO funnel;

-- Give Funnel access to some of our old views as well
GRANT SELECT ON v_analytics_contract_customer_discounts TO funnel;
GRANT SELECT ON v_analytics_inquiries TO funnel;
GRANT SELECT ON v_analytics_orders_not_delivered TO funnel;
GRANT SELECT ON v_analytics_product_categories TO funnel;
GRANT SELECT ON v_analytics_products TO funnel;
