<commit_before>const keystone = require('keystone');
const utils = require('keystone-utils');
const Email = require('keystone-email');
const mailConfig = require('../../config').mail;

const Volunteer = keystone.list('Volunteer');
const Mission = keystone.list('Mission');

const getToken = () => utils.randomString(64);

const setCookie = (res, token) => {
   const maxAge = 1000 * 60 * 60 * 24 * 30; // 30 days
   res.cookie('volunteer', token, { maxAge, httpOnly: true, signed: true });
};

/**
 * Set Token as Cookie and redirect
 */
exports.setToken = (req, res) => {
   setCookie(res, req.params.token);
   res.redirect('/volunteer');
};

/**
 * List all Volunteers
 */
exports.all = (req, res) => {
   Volunteer.model.find((err, volunteers) => {
      if (err) return res.apiError('database error', err);
      res.apiResponse({ volunteers });
   });
};

/**
 * Get Volunteer by ID
 */
exports.one = (req, res) => {
   Volunteer.model
      .findOne({ token: req.token })
      .exec((err, volunteer) => {
         if (err) return res.apiError('database error', err);
         if (!volunteer) return res.apiError('not found');

         Mission.model
            .find({ crew: volunteer })
            .select('name status start end crew area')
            .sort('-start')
            .populate('crew', 'name')
            .populate('area')
            .exec((err2, missions) => {
               if (err2) return res.apiError('database error', err);
               res.apiResponse({ volunteer, missions });
            });
      });
};

/**
 * Create a Volunteer
 */
exports.create = (req, res) => {
   const volunteer = new Volunteer.model();

   volunteer.getUpdateHandler(req).process(req.body, (err) => {
      if (err) return res.apiError('error', err);

      new Email('templates/emails/volunteer-created.jade', { transport: 'nodemailer' })
         .send({
            name: volunteer.name.first,
            link: `/volunteer/${volunteer.token}`,
            host: `${req.protocol}://${req.get('host')}`,
         }, {
            from: mailConfig.sender,
            to: volunteer.email,
            subject: 'crewing account created',
            nodemailerConfig: mailConfig.nodemailerConfig,
         }, () => res.apiResponse({ success: true }));
   });
};

/**
 * Update a Volunteers Data
 */
exports.update = (req, res) => {
   Volunteer.model
      .findOne({ token: req.token })
      .exec((err, volunteer) => {
         if (err) return res.apiError('database error', err);
         if (!volunteer) return res.apiError('not found');

         volunteer.getUpdateHandler(req).process(req.body, (err2) => {
            if (err2) return res.apiError('update error', err2);
            res.apiResponse({ volunteer });
         });
      });
};

/**
 * Update a Volunteers Token
 */
exports.changeToken = (req, res) => {
   Volunteer.model
      .findOne({ email: req.body.email })
      .exec((err, volunteer) => {
         if (err) return res.apiError('database error', err);
         if (!volunteer) return res.apiError('not found');

         volunteer.token = getToken();
         volunteer.save((err2) => {
            if (err2) return res.apiError('update error', err2);

            new Email('templates/emails/volunteer-token-changed.jade', { transport: 'nodemailer' })
               .send({
                  name: volunteer.name.first,
                  link: `/volunteer/${volunteer.token}`,
                  host: `${req.protocol}://${req.get('host')}`,
               }, {
                  from: mailConfig.sender,
                  to: volunteer.email,
                  subject: 'crewing account login link',
                  nodemailerConfig: mailConfig.nodemailerConfig,
               }, () => res.apiResponse({ success: true }));
         });
      });
};

/**
 * Delete Volunteer by ID
 */
exports.remove = (req, res) => {
   Volunteer.model
      .findOne({ token: req.token })
      .exec((err, volunteer) => {
         if (err) return res.apiError('database error', err);
         if (!volunteer) return res.apiError('not found');

         volunteer.remove((err2) => {
            if (err2) return res.apiError('database error', err2);
            return res.apiResponse({ success: true });
         });
      });
};
<commit_msg>mark volunteers email to verified #11<commit_after>const keystone = require('keystone');
const utils = require('keystone-utils');
const Email = require('keystone-email');
const mailConfig = require('../../config').mail;

const Volunteer = keystone.list('Volunteer');
const Mission = keystone.list('Mission');

const getToken = () => utils.randomString(64);

const setCookie = (res, token) => {
   const maxAge = 1000 * 60 * 60 * 24 * 30; // 30 days
   res.cookie('volunteer', token, { maxAge, httpOnly: true, signed: true });
};

/**
 * Set Token as Cookie and redirect
 */
exports.setToken = (req, res) => {
   setCookie(res, req.params.token);
   res.redirect('/volunteer');
};

/**
 * List all Volunteers
 */
exports.all = (req, res) => {
   Volunteer.model.find((err, volunteers) => {
      if (err) return res.apiError('database error', err);
      res.apiResponse({ volunteers });
   });
};

/**
 * Get Volunteer by ID
 */
exports.one = (req, res) => {
   Volunteer.model
      .findOne({ token: req.token })
      .exec((err, volunteer) => {
         if (err) return res.apiError('database error', err);
         if (!volunteer) return res.apiError('not found');

         volunteer.verifyEmail();

         Mission.model
            .find({ crew: volunteer })
            .select('name status start end crew area')
            .sort('-start')
            .populate('crew', 'name')
            .populate('area')
            .exec((err2, missions) => {
               if (err2) return res.apiError('database error', err);
               res.apiResponse({ volunteer, missions });
            });
      });
};

/**
 * Create a Volunteer
 */
exports.create = (req, res) => {
   const volunteer = new Volunteer.model();

   volunteer.getUpdateHandler(req).process(req.body, (err) => {
      if (err) return res.apiError('error', err);

      new Email('templates/emails/volunteer-created.jade', { transport: 'nodemailer' })
         .send({
            name: volunteer.name.first,
            link: `/volunteer/${volunteer.token}`,
            host: `${req.protocol}://${req.get('host')}`,
         }, {
            from: mailConfig.sender,
            to: volunteer.email,
            subject: 'crewing account created',
            nodemailerConfig: mailConfig.nodemailerConfig,
         }, () => res.apiResponse({ success: true }));
   });
};

/**
 * Update a Volunteers Data
 */
exports.update = (req, res) => {
   Volunteer.model
      .findOne({ token: req.token })
      .exec((err, volunteer) => {
         if (err) return res.apiError('database error', err);
         if (!volunteer) return res.apiError('not found');

         volunteer.getUpdateHandler(req).process(req.body, (err2) => {
            if (err2) return res.apiError('update error', err2);
            res.apiResponse({ volunteer });
         });
      });
};

/**
 * Update a Volunteers Token
 */
exports.changeToken = (req, res) => {
   Volunteer.model
      .findOne({ email: req.body.email })
      .exec((err, volunteer) => {
         if (err) return res.apiError('database error', err);
         if (!volunteer) return res.apiError('not found');

         volunteer.token = getToken();
         volunteer.save((err2) => {
            if (err2) return res.apiError('update error', err2);

            new Email('templates/emails/volunteer-token-changed.jade', { transport: 'nodemailer' })
               .send({
                  name: volunteer.name.first,
                  link: `/volunteer/${volunteer.token}`,
                  host: `${req.protocol}://${req.get('host')}`,
               }, {
                  from: mailConfig.sender,
                  to: volunteer.email,
                  subject: 'crewing account login link',
                  nodemailerConfig: mailConfig.nodemailerConfig,
               }, () => res.apiResponse({ success: true }));
         });
      });
};

/**
 * Delete Volunteer by ID
 */
exports.remove = (req, res) => {
   Volunteer.model
      .findOne({ token: req.token })
      .exec((err, volunteer) => {
         if (err) return res.apiError('database error', err);
         if (!volunteer) return res.apiError('not found');

         volunteer.remove((err2) => {
            if (err2) return res.apiError('database error', err2);
            return res.apiResponse({ success: true });
         });
      });
};

{'fraction_non_alphanumeric': 0.16749854904236797, 'fraction_numerical': 0.006500290191526407, 'mean_word_length': 2.234234234234234, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 45, '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': '5142919', 'n_tokens_mistral': 2596, 'n_tokens_neox': 2541, 'n_words': 578}