'use strict';

let User = rootRequire('app/users/model');
let csv = require('to-csv');
let help = rootRequire('app/helpers/functions');
let path = require('path');
let zip = require('express-zip');

module.exports = {

  /**
  * Download a CSV of all attendees
  * GET /exports/attendees
  */
  attendees: (req, res) => {
    User
      .find()
      .select('email application')
      .where({application: {$exists: true}})
      .sort('-created')
      .populate({
        path: 'application',
        match: req.query
      })
      .exec((err, users) => {
        if (err) return res.internalError();
        if (!users || !users.length) return res.clientError('No attendees match that query');
        let list = users.map((user) => {
          return {
            'Name': user.application.name || '',
            'Email': user.email || '',
            'Phone': user.application.phone || '',
            'School': user.application.school || '',
            'Major': user.application.major || '',
            'Year': user.application.year || '',
            'Age': user.application.age || '',
            'Gender': help.capitalizeFirstLetter(user.application.gender) || '',
            'Shirt Size': user.application.shirt || '',
            'RSVPd?': (user.application.going) ? 'Yes' : 'No',
            'Link': user.application.link || '',
            'Status': help.capitalizeFirstLetter(user.application.status) || '',
            'Checked In?': (user.application.checked) ? 'Yes' : 'No'
          };
        });
        res.setHeader('Content-disposition', 'attachment; filename=attendees.csv');
        res.setHeader('Content-type', 'text/csv');
        return res.status(200).send(csv(list));
      });
  },

  /**
  * Download a resume book
  * GET /exports/resumes
  */
  resumes: (req, res) => {
    User
      .find()
      .select('application')
      .populate({
        path: 'application',
        select: 'resume name'
      })
      .exec((err, users) => {
        if (err) return res.internalError();

        let files = users.filter((user) => {
          return user.application && user.application.resume;
        }).map((user) => {

          let newName = user.application.name.replace(/\s/g, '').toLowerCase();
          let newExt = path.extname(user.application.resume);
          return {
            path: path.join(__dirname, '../../uploads/', user.application.resume),
            name: newName + newExt
          };

        });

        return res.zip(files);

      });
  }

};

@@ -47,24 +47,86 @@
 rs/model');%0A
+let Application = rootRequire('app/users/application/model');%0A
 let csv = re
@@ -1874,43 +1874,9 @@
     
-User%0A      .find()%0A      .select('a
+A
 ppli
@@ -1881,26 +1881,24 @@
 lication
-')
 %0A      .
 populate
@@ -1893,64 +1893,38 @@
    .
-populate(%7B%0A        path: 'application',
+find(req.query)
 %0A      
-  
+.
 select
-: 
+(
 'res
@@ -1932,24 +1932,16 @@
 me name'
-%0A      %7D
 )%0A      
@@ -1944,36 +1944,43 @@
     .exec((err, 
-user
+application
 s) =%3E %7B%0A        
@@ -2041,75 +2041,64 @@
 s = 
-users.filter((user) =%3E %7B%0A          return user.application && user.
+applications.filter((application) =%3E %7B%0A          return 
 appl
@@ -2125,28 +2125,35 @@
     %7D).map((
-user
+application
 ) =%3E %7B%0A%0A    
@@ -2172,21 +2172,16 @@
 wName = 
-user.
 applicat
@@ -2259,21 +2259,16 @@
 extname(
-user.
 applicat
@@ -2356,21 +2356,16 @@
 oads/', 
-user.
 applicat

Add ability to specify a query for the resume book