<commit_before>/* global
   Members, BookingRecords
*/

/**
 * MembersController
 *
 * @description :: Server-side logic for managing members
 * @help        :: See http://links.sailsjs.org/docs/controllers
 */

var R = require('ramda')
var sendSubscribe = require('../services/email_mailgun.js').sendSubscribe

module.exports = {
  accountPage: function (req, res) {
    res.view('pages/account', {user: req.session.user})
  },
  accountInfo: function (req, res) {
    Members
      .findOne(req.session.user.id)
      .populateAll()
      .exec(function (error, item) {
        if (error) {
          return res.notFound()
        } else {
          return res.send(item)
        }
      })
  },
  updateAccountInfo: function (req, res) {
    Members
      .update({
        primary_email: req.session.user.primary_email
      }, req.body)
      .exec(function (error, items) {
        if (error) {
          return res.serverError({error: error})
        } else {
          return res.send(items[0])
        }
      })
  },
  showMyEvents: function (req, res) {
    res.view('pages/myEvents', {user: req.session.user})},

  getMyEvents: function (req, res) {
    get_user_events(respond_with_event_data(res),
        req.session.user.id)},

  admin_get_user_events: function (req, res) {
    get_user_events(respond_with_event_data(res), req.param('id'))},

  get_user_events: get_user_events,

  welcome: function (req, res) {
    sendSubscribe(req.body, function (err, data) {
      if (err) return res.status(err).end()
      return res.json(data)
    })
  }
}


function get_user_events (cb, id) {
  BookingRecords
    .find({head_member: id})
    .populate('event_id')
    .exec(cb) }

var respond_with_event_data = R.curry(function (res, err, data) {
  if (err) res.serverError({error: error})
  else res.send(R.pluck('event_id', data))})<commit_msg>Refactor members controller<commit_after>/* global
   Members, BookingRecords
*/

/**
 * MembersController
 *
 * @description :: Server-side logic for managing members
 * @help        :: See http://links.sailsjs.org/docs/controllers
 */

var R = require('ramda')
var sendSubscribe = require('../services/email_mailgun.js').sendSubscribe

module.exports = {
  accountPage: function (req, res) {
    res.view('pages/account', {user: req.session.user})
  },
  accountInfo: function (req, res) {
    Members
      .findOne(req.session.user.id)
      .populateAll()
      .exec(function (error, item) {
        if (error) {
          return res.notFound()
        } else {
          return res.send(item)
        }
      })
  },
  updateAccountInfo: function (req, res) {
    Members
      .update({
        primary_email: req.session.user.primary_email
      }, req.body)
      .exec(function (error, items) {
        if (error) {
          return res.serverError({error: error})
        } else {
          return res.send(items[0])
        }
      })
  },
  showMyEvents: function (req, res) {
    res.view('pages/myEvents', {user: req.session.user})
  },

  getMyEvents: function (req, res) {
    get_user_events(respond_with_event_data(res), req.session.user.id)
  },

  admin_get_user_events: function (req, res) {
    get_user_events(respond_with_event_data(res), req.param('id'))
  },

  get_user_events: get_user_events,

  welcome: function (req, res) {
    sendSubscribe(req.body, function (err, data) {
      if (err) return res.status(err).end()
      return res.json(data)
    })
  }
}

function get_user_events (cb, id) {
  BookingRecords
    .find({head_member: id})
    .populate('event_id')
    .exec(cb) }

var respond_with_event_data = R.curry(function (res, err, data) {
  if (err) res.serverError({error: err})
  else res.send(R.pluck('event_id', data))
})
{'fraction_non_alphanumeric': 0.14687751273117128, 'fraction_numerical': 0.0005360493165371214, 'mean_word_length': 2.8316221765913756, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1670209', 'n_tokens_mistral': 1255, 'n_tokens_neox': 1241, 'n_words': 245}