<commit_before>'use strict';

const logger = require('heroku-logger');

const helpers = require('../../helpers');
const northstar = require('../../northstar');
const NotFoundError = require('../../../app/exceptions/NotFoundError');

module.exports = function getNorthstarUser() {
  return (req, res, next) => {
    if (!req.userId) {
      return next();
    }
    return northstar.fetchUserById(req.userId)
      .then((user) => {
        try {
          /**
           * Phone number in Northstar profile is not guaranteed to be returned in E.164 format?
           * Mobile returned from a Northstar-thor user was not E.164 compliant
           */
          req.platformUserId = helpers.formatMobileNumber(user.mobile);
        } catch (error) {
          logger.error('getNorthstarUser: Fatal error formatting Northstar user\'s mobile.');
          return helpers.sendErrorResponse(res, error);
        }
        return next();
      })
      .catch((err) => {
        if (err && err.status === 404) {
          const error = new NotFoundError('Northstar user not found.');
          return helpers.sendErrorResponse(res, error);
        }

        return helpers.sendErrorResponse(res, err);
      });
  };
};<commit_msg>send suppress headers to Blink when northstar user is not found or lacks a valid phone number<commit_after>'use strict';

const logger = require('heroku-logger');

const helpers = require('../../helpers');
const northstar = require('../../northstar');
const NotFoundError = require('../../../app/exceptions/NotFoundError');

module.exports = function getNorthstarUser() {
  return (req, res, next) => {
    if (!req.userId) {
      return next();
    }
    return northstar.fetchUserById(req.userId)
      .then((user) => {
        try {
          /**
           * Phone number in Northstar profile is not guaranteed to be returned in E.164 format?
           * Mobile returned from a Northstar-thor user was not E.164 compliant
           */
          req.platformUserId = helpers.formatMobileNumber(user.mobile);
        } catch (error) {
          helpers.addBlinkSuppressHeaders(res);
          logger.error('getNorthstarUser: Fatal error formatting Northstar user\'s mobile.');
          return helpers.sendErrorResponse(res, error);
        }
        return next();
      })
      .catch((err) => {
        if (err && err.status === 404) {
          helpers.addBlinkSuppressHeaders(res);
          const error = new NotFoundError('Northstar user not found.');
          return helpers.sendErrorResponse(res, error);
        }

        return helpers.sendErrorResponse(res, err);
      });
  };
};
{'fraction_non_alphanumeric': 0.13318112633181126, 'fraction_numerical': 0.00684931506849315, 'mean_word_length': 2.4276401564537156, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '3298482', 'n_tokens_mistral': 730, 'n_tokens_neox': 712, 'n_words': 192}