<commit_before>import {appendTier, paginateOffset} from '../lib/utils';
import json2csv from 'json2csv';
import moment from 'moment';
import _ from 'lodash';
import * as users from '../controllers/users';
import queries from '../lib/queries';
import models from '../models';
import errors from '../lib/errors';
import required from '../middleware/required_param';

const {
  User
} = models;

/**
 * Fetch backers of a group by tier
 */
export const fetchUsers = (req, res, next) => {
  queries.getUsersFromGroupWithTotalDonations(req.group.id)
    .then(users => appendTier(users, req.group.tiers))
    .then(users => {
      req.users = users;
    })
    .then(next)
    .catch(next);
};

/**
 * Add this middleware before the controller (before calling res.send)
 * `format` should be 'csv'
 */
export const format = (format) => {

  return (req, res, next) => {

    switch (format) {
      case 'csv': {
        const {send} = res;
        res.send = (data) => {
          data = _.map(data, (row) => {
            if (row.createdAt)
              row.createdAt = moment(row.createdAt).format("YYYY-MM-DD HH:mm");

            return row;
          });
          const fields = (data.length > 0) ? Object.keys(data[0]) : [];
          json2csv({data, fields }, (err, csv) => {
            res.setHeader('content-type', 'text/csv');
            send.call(res, csv);
          });
        }
        return next();
      }
      default:
        return next();
    }<commit_msg>change donation amounts to dollars instead of cents in csv<commit_after>import {appendTier, paginateOffset} from '../lib/utils';
import json2csv from 'json2csv';
import moment from 'moment';
import _ from 'lodash';
import * as users from '../controllers/users';
import queries from '../lib/queries';
import models from '../models';
import errors from '../lib/errors';
import required from '../middleware/required_param';

const {
  User
} = models;

/**
 * Fetch backers of a group by tier
 */
export const fetchUsers = (req, res, next) => {
  queries.getUsersFromGroupWithTotalDonations(req.group.id)
    .then(users => appendTier(users, req.group.tiers))
    .then(users => {
      req.users = users;
    })
    .then(next)
    .catch(next);
};

/**
 * Add this middleware before the controller (before calling res.send)
 * `format` should be 'csv'
 */
export const format = (format) => {

  return (req, res, next) => {

    switch (format) {
      case 'csv': {
        const {send} = res;
        res.send = (data) => {
          data = _.map(data, (row) => {
            if (row.createdAt)
              row.createdAt = moment(row.createdAt).format("YYYY-MM-DD HH:mm");
            if (row.totalDonations)
              row.totalDonations = (row.totalDonations/100).toFixed(2); // convert from cents
            return row;
          });
          const fields = (data.length > 0) ? Object.keys(data[0]) : [];
          json2csv({data, fields }, (err, csv) => {
            res.setHeader('content-type', 'text/csv');
            send.call(res, csv);
          });
        }
        return next();
      }
      default:
        return next();
    }
{'fraction_non_alphanumeric': 0.15730695596681557, 'fraction_numerical': 0.004467134652201659, 'mean_word_length': 2.360128617363344, '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': '4020260', 'n_tokens_mistral': 971, 'n_tokens_neox': 952, 'n_words': 256}