// Includes
var http = require('../util/http.js').func;
var getGeneralToken = require('../util/getGeneralToken.js').func;

// Args
exports.required = ['group', 'requestId', 'accept'];
exports.optional = ['jar'];

// Define
function handleJoinRequestId (jar, token, accept, requestId) {
  var httpOpt = {
    url: '//www.roblox.com/group/handle-join-request',
    options: {
      method: 'POST',
      jar: jar,
      form: {
        groupJoinRequestId: requestId,
        accept: accept
      },
      headers: {
        'X-CSRF-TOKEN': token
      }
    }
  };
  return http(httpOpt)
  .then(function (body) {
    if (!JSON.parse(body).success) {
      throw new Error('Invalid permissions, make sure the user is in the group and is allowed to handle join requests');
    }
  });
}

exports.func = function (args) {
  var jar = args.jar;
  var requestId = args.requestId;
  return getGeneralToken({jar: jar})
  .then(function (xcsrf) {
    return handleJoinRequestId(jar, xcsrf, args.accept, requestId);
  });
};

// Includes
var http = require('../util/http.js').func;
var getGeneralToken = require('../util/getGeneralToken.js').func;

// Args
exports.required = ['group', 'requestId', 'accept'];
exports.optional = ['jar'];

// Define
function handleJoinRequestId (jar, token, accept, requestId) {
  var httpOpt = {
    url: '//www.roblox.com/group/handle-join-request',
    options: {
      method: 'POST',
      jar: jar,
      form: {
        groupJoinRequestId: requestId
      },
      headers: {
        'X-CSRF-TOKEN': token
      },
      resolveWithFullResponse: true
    }
  };
  if (!accept) {
    httpOpt.options.form.accept = false;
  }
  return http(httpOpt)
  .then(function (res) {
    if (res.statusCode === 200) {
      if (!JSON.parse(res.body).success) {
        throw new Error('Invalid permissions, make sure the user is in the group and is allowed to handle join requests');
      }
    } else {
      throw new Error('Invalid status: ' + res.statusCode + ' ' + res.statusMessage);
    }
  });
}

exports.func = function (args) {
  var jar = args.jar;
  var requestId = args.requestId;
  return getGeneralToken({jar: jar})
  .then(function (xcsrf) {
    return handleJoinRequestId(jar, xcsrf, args.accept, requestId);
  });
};

Add accept only for false, check result statusCode
Add accept only for false, check result statusCode

Technically the request is not sent with the `accept` option when you do want to accept the user. Not sure if this behavior is really important to match but people have been having problems with this function.