-- Grant Assets/addForProject to whoever already has Assets/add.
INSERT INTO user_groups_perms (user_group_id, controller_allowed, action_allowed, created_by, created_on)
SELECT DISTINCT p.user_group_id, 'Assets', 'addForProject', 0, NOW()
FROM user_groups_perms p
WHERE p.controller_allowed = 'Assets' AND p.action_allowed = 'add'
  AND NOT EXISTS (
    SELECT 1 FROM user_groups_perms x
     WHERE x.user_group_id = p.user_group_id AND x.controller_allowed = 'Assets' AND x.action_allowed = 'addForProject'
  );

-- Also grant to PC (they run the Services onboarding and register site equipment).
INSERT INTO user_groups_perms (user_group_id, controller_allowed, action_allowed, created_by, created_on)
SELECT g.c0d, 'Assets', a.act, 0, NOW()
FROM (SELECT 'add' AS act UNION ALL SELECT 'addForProject') a
CROSS JOIN user_groups g
WHERE g.user_group_name = 'Projects Coordinator'
  AND NOT EXISTS (SELECT 1 FROM user_groups_perms p WHERE p.user_group_id = g.c0d AND p.controller_allowed = 'Assets' AND p.action_allowed = a.act);
