<commit_before>"use strict";

//Load dependencies
var async = require("async");
var EmailTemplates = require('swig-email-templates');
var applicationStorage = process.require("core/applicationStorage.js");
var bnetAPI = process.require("core/api/bnet.js");
var userModel = process.require("users/userModel.js");
var updateModel = process.require("updates/updateModel.js");
var guildModel = process.require("guilds/guildModel.js");
var guildService = process.require("guilds/guildService.js");
var characterModel = process.require("characters/characterModel.js");
var characterService = process.require("characters/characterService.js");


/**
 * Set battlenet id on user's guild
 * @param id
 */
module.exports.updateGuildsId = function (id) {
    var config = applicationStorage.config;
    var logger = applicationStorage.logger;
    var self = this;
    async.each(config.bnetRegions, function (region, callback) {
        async.waterfall([
            function (callback) {
                //Get user guilds from Bnet
                self.getGuilds(region, id, function (error, guilds) {
                    callback(error, guilds);
                });
            },
            function (guilds, callback) {
                // Set the ID for each bnet guilds
                var bnetGuilds = [];
                async.each(guilds, function (guild, callback) {
                    guildService.sanitizeAndSetId(region, guild.realm, guild.name, id, function (error, guild) {
                        bnetGuilds.push(guild);
                        if (!error) {
                            logger.verbose("Set id %s to guild %s-%s-%s", id, region, guild.realm, guild.name);
                        } else {
                            logger.error(error.message);
                        }
                        callback();
                    });
                }, function (error) {
                    callback(error, bnetGuilds);
                });
            },
            function (bnetGuilds, callback) {
                //Clean guild ID if people have leave a guild
                guildModel.find({region: region, id: id}, {realm: 1, name: 1}, function (error, guilds) {
                    async.each(guilds, function (guild, callback) {
                        var isOk = false;
                        async.each(bnetGuilds, function (bnetGuild, callback) {
                            if (bnetGuild.realm === guild.realm && bnetGuild.name === guild.name) {
                                isOk = true;
                            }
                            callback();
                        }, function () {
                            if (isOk == false) {
                                guildModel.removeId(region, guild.realm, guild.name, id, function (error) {
                                    logger.verbose("Remove id %s to guild %s-%s-%s", id, region, guild.realm, guild.name);
                                    callback(error);
                                });
                            }
                        });
                    }, function (error) {
                        callback(error);
                    });
                });
            }
        ], function (error) {
            if (error) {
                logger.error(error.message);
            }
            callback();
        });
    });
};
<commit_msg>Fix bug when bnet return a guild without a realm ...<commit_after>"use strict";

//Load dependencies
var async = require("async");
var EmailTemplates = require('swig-email-templates');
var applicationStorage = process.require("core/applicationStorage.js");
var bnetAPI = process.require("core/api/bnet.js");
var userModel = process.require("users/userModel.js");
var updateModel = process.require("updates/updateModel.js");
var guildModel = process.require("guilds/guildModel.js");
var guildService = process.require("guilds/guildService.js");
var characterModel = process.require("characters/characterModel.js");
var characterService = process.require("characters/characterService.js");


/**
 * Set battlenet id on user's guild
 * @param id
 */
module.exports.updateGuildsId = function (id) {
    var config = applicationStorage.config;
    var logger = applicationStorage.logger;
    var self = this;
    async.each(config.bnetRegions, function (region, callback) {
        async.waterfall([
            function (callback) {
                //Get user guilds from Bnet
                self.getGuilds(region, id, function (error, guilds) {
                    callback(error, guilds);
                });
            },
            function (guilds, callback) {
                // Set the ID for each bnet guilds
                var bnetGuilds = [];
                async.each(guilds, function (guild, callback) {
                    guildService.sanitizeAndSetId(region, guild.realm, guild.name, id, function (error, guild) {
                        bnetGuilds.push(guild);
                        if (!error) {
                            logger.verbose("Set id %s to guild %s-%s-%s", id, region, guild.realm, guild.name);
                        } else {
                            logger.error(error.message);
                        }
                        callback();
                    });
                }, function (error) {
                    callback(error, bnetGuilds);
                });
            },
            function (bnetGuilds, callback) {
                //Clean guild ID if people have leave a guild
                guildModel.find({region: region, id: id}, {realm: 1, name: 1}, function (error, guilds) {
                    async.each(guilds, function (guild, callback) {
                        var isOk = false;
                        async.each(bnetGuilds, function (bnetGuild, callback) {
                            if (bnetGuild && bnetGuild.realm === guild.realm && bnetGuild.name === guild.name) {
                                isOk = true;
                            }
                            callback();
                        }, function () {
                            if (isOk == false) {
                                guildModel.removeId(region, guild.realm, guild.name, id, function (error) {
                                    logger.verbose("Remove id %s to guild %s-%s-%s", id, region, guild.realm, guild.name);
                                    callback(error);
                                });
                            }
                        });
                    }, function (error) {
                        callback(error);
                    });
                });
            }
        ], function (error) {
            if (error) {
                logger.error(error.message);
            }
            callback();
        });
    });
};

{'fraction_non_alphanumeric': 0.11360964138741916, 'fraction_numerical': 0.0005878894767783657, 'mean_word_length': 1.3710801393728222, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4309465', 'n_tokens_mistral': 1790, 'n_tokens_neox': 1668, 'n_words': 387}