<commit_before>const Stripe = require('stripe');
const app = require('../index');
const models = app.set('models');
const slackLib = require('../app/lib/slack');

const done = (err) => {
  if (err) console.log('err', err);
  console.log('done!');
  process.exit();
}

var message = [];

models.Transaction.findAll({
   include: [
    { model: models.Group },
    { model: models.User },
    { model: models.Card }
  ]
})
.map((transaction) => {
  if (!transaction.stripeSubscriptionId) {
    return Promise.resolve();
  }

  var accountId;

  return new Promise((resolve, reject) => {
    transaction.Group.getStripeAccount((err, account) => {
      if (err) return reject(err);
      accountId = account.stripeUserId;
      return resolve(Stripe(account.accessToken)); // connected account client
    });
  })
  .then(stripe => {
    return stripe.customers.retrieveSubscription(
      transaction.Card.serviceId, // cus_
      transaction.stripeSubscriptionId // sub_
    )
    .catch((err) => {
      if (err.type === 'StripeInvalidRequest') {
        return;
      }

      return Promise.reject(err);
    });
  })
  .then(subscription => {
    if (!subscription) return ;

    const currency = subscription.plan.currency.toUpperCase();
    const amount = subscription.plan.amount / 100;
    const url = `https://dashboard.stripe.com/${accountId}/plans/${subscription.plan.id}`;

    if (amount !== transaction.amount) {
      message.push(`Missmatched amount: ${transaction.id}, url: ${url}`);
    }

    if (!transaction.interval) {
      message.push(`Interval missing: ${transaction.id}, url: ${url}`);
    } else if (transaction.interval !== subscription.plan.interval) {
      message.push(`Missmatched interval: ${transaction.id}, url: ${url}`);
    }

    if (currency !== transaction.currency) {
      message.push(`Missmatched currency: ${transaction.id}, url: ${url}`);
    }
  });
})
.then(() => {
  const str = message.join('\n');
  console.log(str);
  return slackLib.postMessage(str);
})
.then(() => done())
.catch(done)<commit_msg>post check transactions to critical<commit_after>const Stripe = require('stripe');
const app = require('../index');
const models = app.set('models');
const slackLib = require('../app/lib/slack');

const done = (err) => {
  if (err) console.log('err', err);
  console.log('done!');
  process.exit();
}

var message = [];

models.Transaction.findAll({
   include: [
    { model: models.Group },
    { model: models.User },
    { model: models.Card }
  ]
})
.map((transaction) => {
  if (!transaction.stripeSubscriptionId) {
    return Promise.resolve();
  }

  var accountId;

  return new Promise((resolve, reject) => {
    transaction.Group.getStripeAccount((err, account) => {
      if (err) return reject(err);
      accountId = account.stripeUserId;
      return resolve(Stripe(account.accessToken)); // connected account client
    });
  })
  .then(stripe => {
    return stripe.customers.retrieveSubscription(
      transaction.Card.serviceId, // cus_
      transaction.stripeSubscriptionId // sub_
    )
    .catch((err) => {
      if (err.type === 'StripeInvalidRequest') {
        return;
      }

      return Promise.reject(err);
    });
  })
  .then(subscription => {
    if (!subscription) return ;

    const currency = subscription.plan.currency.toUpperCase();
    const amount = subscription.plan.amount / 100;
    const url = `https://dashboard.stripe.com/${accountId}/plans/${subscription.plan.id}`;

    if (amount !== transaction.amount) {
      message.push(`Missmatched amount: ${transaction.id}, url: ${url}`);
    }

    if (!transaction.interval) {
      message.push(`Interval missing: ${transaction.id}, url: ${url}`);
    } else if (transaction.interval !== subscription.plan.interval) {
      message.push(`Missmatched interval: ${transaction.id}, url: ${url}`);
    }

    if (currency !== transaction.currency) {
      message.push(`Missmatched currency: ${transaction.id}, url: ${url}`);
    }
  });
})
.then(() => {
  const str = message.join('\n');

  if (str.length > 0) {
    return slackLib.postMessage(str, [], '#critical');
  }
})
.then(() => done())
.catch(done)
{'fraction_non_alphanumeric': 0.1794315992292871, 'fraction_numerical': 0.0016859344894026974, 'mean_word_length': 3.277033985581874, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 22, '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': '4138696', 'n_tokens_mistral': 1284, 'n_tokens_neox': 1283, 'n_words': 256}