<commit_before>'use strict';


/**
 * The user account rights list service
 * Get available rights beetween two dates
 */






/**
 * Create the service
 * @param   {Object} services
 * @param   {Object} app
 * @returns {listItemsService}
 */
exports = module.exports = function(services, app)
{
    
    var service = new services.list(app);
    
    
    
    
    /**
     * 
     * @param {Document} user
     * @param {Array}    rights array of mongoose documents
     * @param {Date}     dtstart
     * @param {Date}     dtend
     */
    function resolveAccountRights(user, rights, dtstart, dtend)
    {
        var async = require('async');
        
        /**
         * Get the promise for the available quantity
         * @param   {Right} right
         * @param   {RightRenewal} renewal
         * @returns {Promise} resolve to a number
         */
        function getRenewalAvailableQuantity(right, renewal) {

            if (!right.validateRules(renewal, user, dtstart, dtend)) {
                return null;
            }

            if (user.roles.account.arrival > renewal.finish) {
                return null;
            }

            return renewal.getUserAvailableQuantity(user);
        }








        var output = [];


        /**
         * add renewals into the right object
         * @param {Right} rightDocument
         * @param {object} right
         * @param {Array} renewals
         * @param {function} callback
         */
        function processRenewals(rightDocument, right, renewals, callback)
        {
            async.each(renewals, function(renewalDocument, renewalCallback) {
                var p = getRenewalAvailableQuantity(rightDocument, renewalDocument);

                if (null === p) {
                    // no error but the right is discarded in this renewal because of the rules or missing renewal
                    return renewalCallback();
                }

                p.then(function(quantity) {

                    var renewalObj = renewalDocument.toObject();
                    renewalObj.available_quantity = quantity;
                    right.renewals.push(renewalObj);
                    right.available_quantity += quantity;

                    renewalCallback();

                }, renewalCallback);

            }, callback);
        }


        async.each(rights, function(rightDocument, cb) {

            var right = rightDocument.toObject();
            right.disp_unit = rightDocument.getDispUnit();

            
            rightDocument.getAllRenewals().then(function(renewals) {

                /**
                 * Store available quantity for each accessibles renewals
                 * renewals with right rules not verified will not be included
                 */
                right.renewals = [];

                /**
                 * Sum of quantities from the accessibles renewals
                 */
                right.available_quantity = 0;

                processRenewals(rightDocument, right, renewals, function done(err) {

                    if (err) {
                        return cb(err);
                    }<commit_msg>Fix output sort in accountrights<commit_after>'use strict';


/**
 * The user account rights list service
 * Get available rights beetween two dates
 */






/**
 * Create the service
 * @param   {Object} services
 * @param   {Object} app
 * @returns {listItemsService}
 */
exports = module.exports = function(services, app)
{
    
    var service = new services.list(app);
    
    
    
    
    /**
     * 
     * @param {Document} user
     * @param {Array}    rights array of mongoose documents
     * @param {Date}     dtstart
     * @param {Date}     dtend
     */
    function resolveAccountRights(user, rights, dtstart, dtend)
    {
        var async = require('async');
        
        /**
         * Get the promise for the available quantity
         * @param   {Right} right
         * @param   {RightRenewal} renewal
         * @returns {Promise} resolve to a number
         */
        function getRenewalAvailableQuantity(right, renewal) {

            if (!right.validateRules(renewal, user, dtstart, dtend)) {
                return null;
            }

            if (user.roles.account.arrival > renewal.finish) {
                return null;
            }

            return renewal.getUserAvailableQuantity(user);
        }








        var output = [];


        /**
         * add renewals into the right object
         * @param {Right} rightDocument
         * @param {object} right
         * @param {Array} renewals
         * @param {function} callback
         */
        function processRenewals(rightDocument, right, renewals, callback)
        {
            async.each(renewals, function(renewalDocument, renewalCallback) {
                var p = getRenewalAvailableQuantity(rightDocument, renewalDocument);

                if (null === p) {
                    // no error but the right is discarded in this renewal because of the rules or missing renewal
                    return renewalCallback();
                }

                p.then(function(quantity) {

                    var renewalObj = renewalDocument.toObject();
                    renewalObj.available_quantity = quantity;
                    right.renewals.push(renewalObj);
                    right.available_quantity += quantity;

                    renewalCallback();

                }, renewalCallback);

            }, callback);
        }

        // async.eachSeries is used instead of async.each to maintain order in the output variable
        async.eachSeries(rights, function(rightDocument, cb) {

            var right = rightDocument.toObject();
            right.disp_unit = rightDocument.getDispUnit();

            
            rightDocument.getAllRenewals().then(function(renewals) {

                /**
                 * Store available quantity for each accessibles renewals
                 * renewals with right rules not verified will not be included
                 */
                right.renewals = [];

                /**
                 * Sum of quantities from the accessibles renewals
                 */
                right.available_quantity = 0;

                processRenewals(rightDocument, right, renewals, function done(err) {

                    if (err) {
                        return cb(err);
                    }
{'fraction_non_alphanumeric': 0.08663251047973917, 'fraction_numerical': 0.0003105107902499612, 'mean_word_length': 1.5382190701339638, 'pattern_counts': {'":': 0, '<': 3, '<?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': '5925048', 'n_tokens_mistral': 1685, 'n_tokens_neox': 1502, 'n_words': 425}