-- Slice 8c-Retention · WorksRetention perms

/* ── Read pool: everyone who reads the project report ─────────────── */
INSERT INTO user_groups_perms (user_group_id, controller_allowed, action_allowed, created_by, created_on)
SELECT g.c0d, 'WorksRetention', 'pool', 0, NOW()
FROM user_groups g
WHERE g.user_group_name IN (
    'Projects Coordinator','Project Manager','Field Supervisor',
    'Procurement','Finance Manager','Accountant','Executive','Administrator'
)
  AND NOT EXISTS (
    SELECT 1 FROM user_groups_perms p
    WHERE p.user_group_id = g.c0d
      AND p.controller_allowed = 'WorksRetention'
      AND p.action_allowed     = 'pool'
  );

/* ── Release + set terms: FM and Admin only ────────────────────────── */
INSERT INTO user_groups_perms (user_group_id, controller_allowed, action_allowed, created_by, created_on)
SELECT g.c0d, 'WorksRetention', a.act, 0, NOW()
FROM (SELECT 'releaseAj' AS act UNION ALL SELECT 'setTermsAj') a
CROSS JOIN user_groups g
WHERE g.user_group_name IN ('Finance Manager','Administrator')
  AND NOT EXISTS (
    SELECT 1 FROM user_groups_perms p
    WHERE p.user_group_id = g.c0d
      AND p.controller_allowed = 'WorksRetention'
      AND p.action_allowed     = a.act
  );
