const resp = require('../response.js');

module.exports = (req, res, next) => {
  const acl = req.session?.userAcl;
  if (!Array.isArray(acl)) {
    return res.status(403).json(
      resp(
        'Access Denied',
        'Admin access is required for this operation',
        null,
        req,
        false
      )
    );
  }

  const userAcl = acl.find((item) => item?.name === 'User');
  const gm = userAcl?.GroupModules;
  const isAdminUserMgmt = !!(gm?.create && gm?.read && gm?.update && gm?.delete);

  if (!isAdminUserMgmt) {
    return res.status(403).json(
      resp(
        'Access Denied',
        'Admin access is required for this operation',
        null,
        req,
        false
      )
    );
  }

  return next();
};