<commit_before>var config             = require('config');
var mongoose           = require('mongoose');
var MongooseUserPlugin = require('mongoose-user-plugin');
var https              = require('https');

var status = [ 'leader', 'human-resource', 'consultant' ];

var UserSchema = new mongoose.Schema();
UserSchema.plugin(MongooseUserPlugin, {languages: ['fr', 'en']});

UserSchema.add({
  admin:          { type: Boolean, 'default': false },
  status:         { type: String,  'default': 'consultant', enum: status },
  technologies:   { type: String },
  missionAddress: { type: String },
  map:            {
    latitude:  { type: Number },
    longitude: { type: Number }
  }
});

if (config.signin.emailDomain !== null) {
    UserSchema.add({email: { type: String, required: true, unique: true, match: new RegExp(config.signin.emailDomain) }})
}

var googleGeocodeUrl = 'https://maps.googleapis.com/maps/api/geocode/json?sensor=false&key=AIzaSyAPP8pToHqfF96uis3EHCoDantTKR483XM';

UserSchema.pre('save', function (next) {
  var self = this;
  var googleGeocodeUrlWithAddress = googleGeocodeUrl + '&address=' + encodeURIComponent(self.missionAddress);

  console.log('User pre save. Call: ' + googleGeocodeUrlWithAddress);

  var req = https.get(googleGeocodeUrlWithAddress, function(res) {
    if (res.statusCode == 200) {
      var body = '';
      res.on('data', function(chunk) {
        body += chunk;
      });

      res.on('end', function() {
        var response = JSON.parse(body)
        if (response && response.results && response.results.length > 0) {
          self.map = {
            latitude  : response.results[0].geometry.location.lat,
            longitude : response.results[0].geometry.location.lng<commit_msg>Fix save user when no missionAddress<commit_after>var config             = require('config');
var mongoose           = require('mongoose');
var MongooseUserPlugin = require('mongoose-user-plugin');
var https              = require('https');

var status = [ 'leader', 'human-resource', 'consultant' ];

var UserSchema = new mongoose.Schema();
UserSchema.plugin(MongooseUserPlugin, {languages: ['fr', 'en']});

UserSchema.add({
  admin:          { type: Boolean, 'default': false },
  status:         { type: String,  'default': 'consultant', enum: status },
  technologies:   { type: String },
  missionAddress: { type: String },
  map:            {
    latitude:  { type: Number },
    longitude: { type: Number }
  }
});

if (config.signin.emailDomain !== null) {
    UserSchema.add({email: { type: String, required: true, unique: true, match: new RegExp(config.signin.emailDomain) }})
}

var googleGeocodeUrl = 'https://maps.googleapis.com/maps/api/geocode/json?sensor=false&key=AIzaSyAPP8pToHqfF96uis3EHCoDantTKR483XM';

UserSchema.pre('save', function (next) {
  var self = this;
  if (!self.missionAddress) {
    return next();
  }

  var googleGeocodeUrlWithAddress = googleGeocodeUrl + '&address=' + encodeURIComponent(self.missionAddress);

  console.log('User pre save. Call: ' + googleGeocodeUrlWithAddress);

  var req = https.get(googleGeocodeUrlWithAddress, function(res) {
    if (res.statusCode == 200) {
      var body = '';
      res.on('data', function(chunk) {
        body += chunk;
      });

      res.on('end', function() {
        var response = JSON.parse(body)
        if (response && response.results && response.results.length > 0) {
          self.map = {
            latitude  : response.results[0].geometry.location.lat,
            longitude : response.results[0].geometry.location.lng
{'fraction_non_alphanumeric': 0.14815856058476243, 'fraction_numerical': 0.007309530503233062, 'mean_word_length': 3.3284671532846715, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 2, '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': '6679939', 'n_tokens_mistral': 1126, 'n_tokens_neox': 1110, 'n_words': 230}