-- Slice 8T-SubmissionAttachments · perms
-- Anyone who can save submission details can upload / remove proof attachments.

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='saveSubmissionAj') w
     ON w.user_group_id = g.c0d
CROSS JOIN (
    SELECT 'submissionAttachmentUploadAj' AS action_allowed UNION ALL
    SELECT 'submissionAttachmentRemoveAj'                    UNION ALL
    SELECT 'submissionAttachmentDownload'
) 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
);
