<commit_before>//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

const express = require('express');
const router = express.Router();
const async = require('async');
const utils = require('../utils');

router.use(function (req, res, next) {
  const memberOfOrganizations = [];
  const operations = req.app.settings.providers.operations;
  const username = req.legacyUserContext.usernames.github;
  async.each(operations.organizations, function (organization, callback) {
    organization.getMembership(username, function (error, result) {
      let state = null;
      if (result && result.state) {
        state = result.state;
      }
      if (state == 'active' || state == 'pending') {
        memberOfOrganizations.push(organization);
      }
      callback(error);
    });
  }, function (error) {
    if (error) {
      return next(error);
    }
    req.currentOrganizationMemberships = memberOfOrganizations;
    next();
  });
});

router.get('/', function (req, res, next) {
  const link = req.legacyUserContext.entities.link;
  const id = req.legacyUserContext.id.github;
  const operations = req.app.settings.providers.operations;
  const account = operations.getAccount(id);
<<<<<<< HEAD
  account.getOperationalOrganizationMemberships((error, currentOrganizationMemberships) => {
=======
  account.getManagedOrganizationMemberships((error, currentOrganizationMemberships) => {
>>>>>>> db1291b957a5d4a9c28b2ffdc7ceaf9b53e0b41d
    if (error) {
      return next(error);
    }
    if (link && link.ghu) {
      return req.legacyUserContext.render(req, res, 'unlink', 'Remove corporate link and organization memberships', {
        organizations: currentOrganizationMemberships,
      });
    } else {
      return next('No link could be found.');
    }
  });
});

router.post('/', function (req, res, next) {
  const id = req.legacyUserContext.id.github;
  const operations = req.app.settings.providers.operations;
  const account = operations.getAccount(id);
  const insights = req.insights;
  account.terminate(error => {
    insights.trackEvent('PortalUserUnlink');
    // If the cache is bad, the storage entity will already be gone
    if (error && error.statusCode === 404) {
      insights.trackEvent('PortalUserUnlinkAlreadyUnlinked', error);
      error = null;
    }
    req.legacyUserContext.invalidateLinkCache(error => {
      if (error) {
        console.dir(error);
      }
    });
    if (error) {
      insights.trackException(error);
      return next(utils.wrapError(error, 'You were successfully removed from all of your organizations. However, a minor failure happened during a data housecleaning operation. Double check that you are happy with your current membership status on GitHub.com before continuing. Press Report Bug if you would like this handled for sure.'));<commit_msg>Keying off of ghid for unlink page<commit_after>//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

const express = require('express');
const router = express.Router();
const async = require('async');
const utils = require('../utils');

router.use(function (req, res, next) {
  const memberOfOrganizations = [];
  const operations = req.app.settings.providers.operations;
  const username = req.legacyUserContext.usernames.github;
  async.each(operations.organizations, function (organization, callback) {
    organization.getMembership(username, function (error, result) {
      let state = null;
      if (result && result.state) {
        state = result.state;
      }
      if (state == 'active' || state == 'pending') {
        memberOfOrganizations.push(organization);
      }
      callback(error);
    });
  }, function (error) {
    if (error) {
      return next(error);
    }
    req.currentOrganizationMemberships = memberOfOrganizations;
    next();
  });
});

router.get('/', function (req, res, next) {
  const link = req.legacyUserContext.entities.link;
  const id = req.legacyUserContext.id.github;
  const operations = req.app.settings.providers.operations;
  const account = operations.getAccount(id);
  account.getManagedOrganizationMemberships((error, currentOrganizationMemberships) => {
    if (error) {
      return next(error);
    }
    var link = req.oss.entities.link;
    if (link && link.ghid) {
      return req.oss.render(req, res, 'unlink', 'Remove corporate link and organization memberships', {
        orgs: currentOrganizationMemberships,
      });
    } else {
      return next(new Error('No link could be found.'));
    }
  });
});

router.post('/', function (req, res, next) {
  const id = req.legacyUserContext.id.github;
  const operations = req.app.settings.providers.operations;
  const account = operations.getAccount(id);
  const insights = req.insights;
  account.terminate(error => {
    insights.trackEvent('PortalUserUnlink');
    // If the cache is bad, the storage entity will already be gone
    if (error && error.statusCode === 404) {
      insights.trackEvent('PortalUserUnlinkAlreadyUnlinked', error);
      error = null;
    }
    req.legacyUserContext.invalidateLinkCache(error => {
      if (error) {
        console.dir(error);
      }
    });
    if (error) {
      insights.trackException(error);
      return next(utils.wrapError(error, 'You were successfully removed from all of your organizations. However, a minor failure happened during a data housecleaning operation. Double check that you are happy with your current membership status on GitHub.com before continuing. Press Report Bug if you would like this handled for sure.'));
{'fraction_non_alphanumeric': 0.12048192771084337, 'fraction_numerical': 0.004539898725336127, 'mean_word_length': 3.6156325543916195, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2175438', 'n_tokens_mistral': 1623, 'n_tokens_neox': 1602, 'n_words': 447}