// Store the preference locale in cookies and (if available) session to use
// on next requests.


'use strict';


var _ = require('lodash');


var LOCALE_COOKIE_MAX_AGE = 0xFFFFFFFF; // Maximum 32-bit unsigned integer.


module.exports = function (N, apiPath) {
  N.validate(apiPath, {
    locale: { type: 'string' }
  });

  N.wire.on(apiPath, function set_language(env, callback) {
    var locale = env.params.locale;

    if (!_.contains(N.config.locales.enabled, env.params.locale)) {
      // User sent a non-existent or disabled locale - reply with the default.
      locale = N.config.locales['default'];
    }

    env.extras.setCookie('locale', locale, {
      path: '/'
    , maxAge: LOCALE_COOKIE_MAX_AGE
    });

    if (env.session) {
      env.session.locale = locale;
    }

    if (env.session && env.session.user_id) {
      N.models.users.User.findByIdAndUpdate(env.session.user_id, { locale: locale }, callback);
    } else {
      callback();
    }
  });
};

@@ -860,26 +860,22 @@
 ser.
-findByIdAndUpdate(
+update(%7B _id: 
 env.
@@ -889,16 +889,18 @@
 .user_id
+ %7D
 , %7B loca

Update user locale without fetching user's account.