<commit_before>

Accounts.config({
	sendVerificationEmail: true,
	forbidClientAccountCreation: false
});


var _activeDistribution = function(account, distributionName) {

	var accounts;
	if (distributionName === 'revenue') {
		accounts = _.pairs(account.contributors);
	}
	else if (distributionName === 'dividends') {
		accounts = _.pairs(account.contributions);
	}
	
	var distribution = {};

	_.map(accounts, function(accountInfo) {

  	var accountId = accountInfo[0];
  	var contributionIds = accountInfo[1];

		// Make sure this contributor is active.

		var account = TimeAccounts.findOne({ _id:accountId });
		var contributorIsActive = account.status === 'active';
  	d_('Account: ' + account._id + ': "' + account.status + '"' + (contributorIsActive ? '  *' : ''));

  	_.map(contributionIds, function(contributionId) {

  		var contribution = Contributions.findOne({ _id:contributionId });
  		var contributionIsActive = !_.isNull(contribution.dateActivated);
  		d_('  ' + contributionId + ': "' + (contributionIsActive ? 'active' : 'pending' ) + '": ' + contribution.amountOutstanding + '/' + contribution.amountReported + '  ' + (contributionIsActive ? '*' : ''));

  		if (contributorIsActive && contributionIsActive) {
				d_('+ {' + accountId + ':[' + contributionId + '] }')
  			if (_.has(distribution, accountId)) {
  				// d_('Distribution already has account ' + accountId);
  				distribution[accountId].push(contribution);
  			}
  			else {
  				// d_('Distribution does not have account ' + accountId);
  				distribution[accountId] = [contribution];
  			}
  		}
  	});
	});

	return distribution;
};















/**
 * Core Functionality
 */

_.extend(Helpers, {


	/*
	* Contributes a given amount from a given account to a given account.
	* If the `toAccountId` is not provided, the contribution is made both from and to the `fromAccountId`.
	*/
  contribute: function(fromAccountId, amount, toAccountId) {
		d_('\nMaking contribution...');
		d_('From contributor: '+fromAccountId);
		d_('To business account: '+toAccountId);

		var contributionId = 0;
		var liabilityLimit = h_.liabilityLimit();
		
		// Makes sure we've set a liability limit.
		if (!_.isUndefined(liabilityLimit)) {

			// Gets a snapshot of the given to-account.
			// If `toAccountId` is not provided, use `fromAccountId` in its place.
			if (_.isUndefined(toAccountId)) {
				toAccountId = fromAccountId;
			}


			var account = TimeAccounts.findOne(toAccountId);

			// Makes sure the account is valid and active.
			if (!_.isUndefined(account)) {
				if (account.status === 'active') {

					// Makes sure the given amount is valid.
					if (h_.isInteger(amount) && amount >= 0) {

						// Sees if there's enough room within the liability limit to take on
						// more debt.
						var availableDebt = liabilityLimit - h_.getOutstandingContributionAmount(toAccountId);
						var remaining = availableDebt - amount;

						if (remaining < 0) {
							// Not enough room to take on the full amount, so we cap it.
							remaining = Math.abs(remaining);
							amount -= remaining;
						}

						// The loan amount we issue cannot be negative.
						if (amount < 0)
							amount = 0;

						// Increment the contributor's credit.
						TimeAccounts.update({ _id:fromAccountId }, { $inc:{ credit:amount } });
<commit_msg>Fixed contribution cap<commit_after>

Accounts.config({
	sendVerificationEmail: true,
	forbidClientAccountCreation: false
});


var _activeDistribution = function(account, distributionName) {

	var accounts;
	if (distributionName === 'revenue') {
		accounts = _.pairs(account.contributors);
	}
	else if (distributionName === 'dividends') {
		accounts = _.pairs(account.contributions);
	}
	
	var distribution = {};

	_.map(accounts, function(accountInfo) {

  	var accountId = accountInfo[0];
  	var contributionIds = accountInfo[1];

		// Make sure this contributor is active.

		var account = TimeAccounts.findOne({ _id:accountId });
		var contributorIsActive = account.status === 'active';
  	d_('Account: ' + account._id + ': "' + account.status + '"' + (contributorIsActive ? '  *' : ''));

  	_.map(contributionIds, function(contributionId) {

  		var contribution = Contributions.findOne({ _id:contributionId });
  		var contributionIsActive = !_.isNull(contribution.dateActivated);
  		d_('  ' + contributionId + ': "' + (contributionIsActive ? 'active' : 'pending' ) + '": ' + contribution.amountOutstanding + '/' + contribution.amountReported + '  ' + (contributionIsActive ? '*' : ''));

  		if (contributorIsActive && contributionIsActive) {
				d_('+ {' + accountId + ':[' + contributionId + '] }')
  			if (_.has(distribution, accountId)) {
  				// d_('Distribution already has account ' + accountId);
  				distribution[accountId].push(contribution);
  			}
  			else {
  				// d_('Distribution does not have account ' + accountId);
  				distribution[accountId] = [contribution];
  			}
  		}
  	});
	});

	return distribution;
};















/**
 * Core Functionality
 */

_.extend(Helpers, {


	/*
	* Contributes a given amount from a given account to a given account.
	* If the `toAccountId` is not provided, the contribution is made both from and to the `fromAccountId`.
	*/
  contribute: function(fromAccountId, amount, toAccountId) {
		d_('\nMaking contribution...');
		d_('From contributor: '+fromAccountId);
		d_('To business account: '+toAccountId);

		var contributionId = 0;
		var liabilityLimit = h_.liabilityLimit();
		
		// Makes sure we've set a liability limit.
		if (!_.isUndefined(liabilityLimit)) {

			// Gets a snapshot of the given to-account.
			// If `toAccountId` is not provided, use `fromAccountId` in its place.
			if (_.isUndefined(toAccountId)) {
				toAccountId = fromAccountId;
			}


			var account = TimeAccounts.findOne(toAccountId);

			// Makes sure the account is valid and active.
			if (!_.isUndefined(account)) {
				if (account.status === 'active') {

					// Makes sure the given amount is valid.
					if (h_.isInteger(amount) && amount >= 0) {

						// Sees if there's enough room within the liability limit to take on
						// more debt.
						var availableDebt = liabilityLimit - h_.getTotalOutstandingContributionAmount(toAccountId);
						var remaining = availableDebt - amount;

						if (remaining < 0) {
							// Not enough room to take on the full amount, so we cap it.
							remaining = Math.abs(remaining);
							amount -= remaining;
						}

						// The loan amount we issue cannot be negative.
						if (amount < 0)
							amount = 0;

						// Increment the contributor's credit.
						TimeAccounts.update({ _id:fromAccountId }, { $inc:{ credit:amount } });

{'fraction_non_alphanumeric': 0.1317131713171317, 'fraction_numerical': 0.0021002100210021, 'mean_word_length': 3.2655150351887396, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 5, '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': '3677713', 'n_tokens_mistral': 2376, 'n_tokens_neox': 1995, 'n_words': 532}