-- 084_loan_manage_package.sql
-- Satukan flow aktif modul peminjaman ke loan.manage

INSERT INTO permissions (code, name, module, created_at)
SELECT 'loan.manage', 'Kelola Peminjaman', 'loan', NOW()
WHERE NOT EXISTS (
  SELECT 1 FROM permissions WHERE code = 'loan.manage'
);

UPDATE permissions
SET name = 'Kelola Peminjaman',
    module = 'loan'
WHERE code = 'loan.manage';

INSERT IGNORE INTO role_permissions (role_id, permission_id)
SELECT DISTINCT rp.role_id, p_manage.id
FROM role_permissions rp
JOIN permissions p_old ON p_old.id = rp.permission_id
JOIN permissions p_manage ON p_manage.code = 'loan.manage'
WHERE p_old.code IN ('loan.view', 'loan.print', 'loan.close', 'loan.scan');
