-- Slice 8S-OverheadPools · perms
-- Overhead CRUD lives at the same level as pricing line CRUD.
-- Everyone who already has pricingAddLineAj gets the four overhead endpoints.

INSERT INTO user_groups_perms (user_group_id, controller_allowed, action_allowed, created_by, created_on)
SELECT g.c0d, 'Tenders', act.action_allowed, 0, NOW()
FROM user_groups g
JOIN (SELECT DISTINCT user_group_id FROM user_groups_perms
      WHERE controller_allowed='Tenders' AND action_allowed='pricingAddLineAj') w
     ON w.user_group_id = g.c0d
CROSS JOIN (
    SELECT 'pricingOverheadsAddAj'      AS action_allowed UNION ALL
    SELECT 'pricingOverheadsUpdateAj'                       UNION ALL
    SELECT 'pricingOverheadsRemoveAj'                       UNION ALL
    SELECT 'pricingOverheadsLoadTemplateAj'
) act
WHERE NOT EXISTS (
    SELECT 1 FROM user_groups_perms p
    WHERE p.user_group_id = g.c0d
      AND p.controller_allowed = 'Tenders'
      AND p.action_allowed     = act.action_allowed
);
