<commit_before>'use strict';

const moment = require('moment');
const inherits = require('util').inherits;
const Router = require('./index');
const middleware = require('storj-service-middleware');
const errors = require('storj-service-error-types');
const Promise = require('bluebird');

function PaymentProcessorsRouter(options) {
  if (!(this instanceof PaymentProcessorsRouter)) {
    return new PaymentProcessorsRouter(options);
  }

  Router.apply(this, arguments);

  this.models = options.storage.models;
  this.authenticate = middleware.authenticate(options.storage);
}

inherits(PaymentProcessorsRouter, Router);

PaymentProcessorsRouter.prototype._addPaymentProcessor = function(req) {
  const PaymentProcessor = this.models.PaymentProcessor;

  return new Promise((resolve, reject) => {
    const pp = new PaymentProcessor({
      user: req.user.id,
      name: req.body.processor.name,
      default: req.body.processor.default
    });

    pp.adapter.register(req.body.data, req.user.id)
      .then((result) => {
        pp.data = result;
        pp.save()
          .then((pp) => resolve(pp))
          .catch((err) => reject(err));
      })
      .catch((err) => reject(err));
  });
};

PaymentProcessorsRouter.prototype._setUserFreeTier = function(req, isFreeTier) {
  return new Promise((resolve, reject) => {
    const user = req.user;
    user.isFreeTier = isFreeTier;
    user.save()
      .then((user) => resolve(user))
      .catch((err) => reject(err));
  });
};

PaymentProcessorsRouter.prototype.addPaymentMethod = function(req, res, next) {
  const self = this;
  const PaymentProcessor = this.models.PaymentProcessor;

  Promise.coroutine(function* () {
    const processor = yield PaymentProcessor.findOne({
      user: req.user.id, name: req.body.processor.name
    });

    const pp = !processor
      ? yield self._addPaymentProcessor(req)
      : yield processor.addPaymentMethod(req.body.data);

    const user = yield self._setUserFreeTier(req, false);

    res.status(200).send({ pp: pp.toObject(), user });
  })().catch((err) => {
    console.error('ADD PAYMENT METHOD ERROR', err);
    res.status(500).send(new errors.InternalError(err));<commit_msg>Retrieve payment processor after adding<commit_after>'use strict';

const moment = require('moment');
const inherits = require('util').inherits;
const Router = require('./index');
const middleware = require('storj-service-middleware');
const errors = require('storj-service-error-types');
const Promise = require('bluebird');

function PaymentProcessorsRouter(options) {
  if (!(this instanceof PaymentProcessorsRouter)) {
    return new PaymentProcessorsRouter(options);
  }

  Router.apply(this, arguments);

  this.models = options.storage.models;
  this.authenticate = middleware.authenticate(options.storage);
}

inherits(PaymentProcessorsRouter, Router);

PaymentProcessorsRouter.prototype._addPaymentProcessor = function(req) {
  const PaymentProcessor = this.models.PaymentProcessor;

  return new Promise((resolve, reject) => {
    const pp = new PaymentProcessor({
      user: req.user.id,
      name: req.body.processor.name,
      default: req.body.processor.default
    });

    pp.adapter.register(req.body.data, req.user.id)
      .then((result) => {
        pp.data = result;
        pp.save()
          .then((pp) => resolve(pp))
          .catch((err) => reject(err));
      })
      .catch((err) => reject(err));
  });
};

PaymentProcessorsRouter.prototype._setUserFreeTier = function(req, isFreeTier) {
  return new Promise((resolve, reject) => {
    const user = req.user;
    user.isFreeTier = isFreeTier;
    user.save()
      .then((user) => resolve(user))
      .catch((err) => reject(err));
  });
};

PaymentProcessorsRouter.prototype.addPaymentMethod = function(req, res, next) {
  const self = this;
  const PaymentProcessor = this.models.PaymentProcessor;
  const userId = req.user.id;
  const processorName = req.body.processor.name;

  Promise.coroutine(function* () {
    const processor = yield PaymentProcessor.findOne({
      user: userId, name: processorName
    });

    !processor
      ? yield self._addPaymentProcessor(req)
      : yield processor.addPaymentMethod(req.body.data);

    Promise.join(
      PaymentProcessor.findOne({ user: userId, name: processorName }),
      self._setUserFreeTier(req, false),
      function (pp, user) {
        res.status(200).send({ pp: pp.toObject(), user });
      }
    );
  })().catch((err) => {
    console.error('ADD PAYMENT METHOD ERROR', err);
    res.status(500).send(new errors.InternalError(err));
{'fraction_non_alphanumeric': 0.14513661202185793, 'fraction_numerical': 0.002622950819672131, 'mean_word_length': 3.786610878661088, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, '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': '397549', 'n_tokens_mistral': 1376, 'n_tokens_neox': 1348, 'n_words': 269}