<commit_before>'use strict'

var async = require('async');
var _ = require('lodash');
var moment = require('moment');

function bulkApplyApplications(args, callback) {
  var seneca = this;
  var plugin = args.role;
  var requestingUser = args.user;
  var applications = args.applications;
  var updateAction = applications[0].updateAction || '';
  delete applications[0].updateAction;
  var locality = args.locality || 'en_US';
  var eventDateFormat = 'Do MMMM YY';
  var emailSubjectData = applications[0].emailSubject || {};
  delete applications[0].emailSubject;
  var eventData;
  if(_.isEmpty(applications)) return callback(null, {error: 'args.applications is empty'});

  async.waterfall([
    loadValidationData,
    validateRequest,
    saveApplications,
    generateEmailContent
  ], function (err, applications) {
    if(err) return callback(null, {ok: false, why: err.message});
    return callback(null, applications);
  });

  function loadValidationData(done) {
    if(!updateAction) return done();
    seneca.act({role: plugin, cmd: 'loadApplication', id: applications[0].id}, function (err, application) {
      if(err) return done(err);
      seneca.act({role: plugin, cmd: 'getEvent', id: application.eventId}, function (err, event) {
        if(err) return done(err);
        eventData = event;
        return done();
      });
    });
  }

  function validateRequest(done) {
    if(!updateAction) return done();
    seneca.act({role: 'cd-dojos', cmd: 'load_usersdojos', query:{userId: requestingUser.id, dojoId: eventData.dojoId}}, function (err, usersDojos) {
      if(err) return done(err);
      var userDojo = usersDojos[0];
      var isTicketingAdmin = _.find(userDojo.userPermissions, function (userPermission) {
        return userPermission.name === 'ticketing-admin';
      });
      if(!isTicketingAdmin) return done(new Error('You must be a ticketing admin of this Dojo to update applications.'));
      return done();
    });
  }

  function saveApplications(done) {
    async.map(applications, function (application, cb) {
      seneca.act({role: 'cd-profiles', cmd: 'list', query: {userId: application.userId}}, function (err, profiles) {
        if(err) return cb(err);
        if(_.isEmpty(profiles)) return cb();
        var userProfile = profiles[0];
        application.name = userProfile.name;
        application.dateOfBirth = userProfile.dob;
        if(!application.status) application.status = 'pending';
        seneca.act({role: plugin, cmd: 'saveApplication', application: application}, cb);
      });
    }, done);
  }

  function generateEmailContent(applications, done) {
    if(updateAction === 'disapprove' || updateAction === 'checkin' || updateAction === 'delete') return done(null, applications);
    async.waterfall([
      retrieveProfiles,
      retrieveParentsForUser,
      retrieveEventAndSessionData,
      retrieveDojoData,
      retrieveTicketsData,
      sendEmail
    ], function (err, res) {
      if(err) return done(err);
      return done(null, applications);
    });

    function retrieveProfiles(done) {
      async.map(applications, function (application, cb) {
        seneca.act({role: 'cd-profiles', cmd: 'list', query: {userId: application.userId}}, function (err, profiles) {
          if(err) return cb(err);
          var profile = profiles[0];
          return cb(null, profile);
        });
      }, done); 
    }

    function retrieveParentsForUser(profiles, done) {
      if(applications[0].status !== 'approved') return done(null, profiles);
      seneca.act({role: 'cd-profiles', cmd: 'load_parents_for_user', userId: profiles[0].userId}, function (err, parents) {
        if(err) return done(err);
        profiles[0].parents = parents;
        return done(null, profiles);
      });
    }<commit_msg>make sure applications are unique<commit_after>'use strict'

var async = require('async');
var _ = require('lodash');
var moment = require('moment');

function bulkApplyApplications(args, callback) {
  var seneca = this;
  var plugin = args.role;
  var requestingUser = args.user;
  var applications = args.applications;
  var updateAction = applications[0].updateAction || '';
  delete applications[0].updateAction;
  var locality = args.locality || 'en_US';
  var eventDateFormat = 'Do MMMM YY';
  var emailSubjectData = applications[0].emailSubject || {};
  delete applications[0].emailSubject;
  var eventData;
  if(_.isEmpty(applications)) return callback(null, {error: 'args.applications is empty'});

  async.waterfall([
    loadValidationData,
    validateRequest,
    saveApplications,
    generateEmailContent
  ], function (err, applications) {
    if(err) return callback(null, {ok: false, why: err.message});
    return callback(null, applications);
  });

  function loadValidationData(done) {
    if(!updateAction) return done();
    seneca.act({role: plugin, cmd: 'loadApplication', id: applications[0].id}, function (err, application) {
      if(err) return done(err);
      seneca.act({role: plugin, cmd: 'getEvent', id: application.eventId}, function (err, event) {
        if(err) return done(err);
        eventData = event;
        return done();
      });
    });
  }

  function validateRequest(done) {
    if(!updateAction) return done();
    seneca.act({role: 'cd-dojos', cmd: 'load_usersdojos', query:{userId: requestingUser.id, dojoId: eventData.dojoId}}, function (err, usersDojos) {
      if(err) return done(err);
      var userDojo = usersDojos[0];
      var isTicketingAdmin = _.find(userDojo.userPermissions, function (userPermission) {
        return userPermission.name === 'ticketing-admin';
      });
      if(!isTicketingAdmin) return done(new Error('You must be a ticketing admin of this Dojo to update applications.'));
      return done();
    });
  }

  function saveApplications(done) {
    async.map(applications, function (application, cb) {
      seneca.act({role: plugin, cmd: 'searchApplications', query: {sessionId: application.sessionId, userId: application.userId}}, function (err, applications) {
        if(err) return cb(err);
        if(applications.length > 0) {
          var ticketFound = _.find(applications, function (applicationFound) {
            return applicationFound.ticketName === application.ticketName && applicationFound.ticketType === application.ticketType;
          });
          if(ticketFound) return cb();
        }
        seneca.act({role: 'cd-profiles', cmd: 'list', query: {userId: application.userId}}, function (err, profiles) {
          if(err) return cb(err);
          if(_.isEmpty(profiles)) return cb();
          var userProfile = profiles[0];
          application.name = userProfile.name;
          application.dateOfBirth = userProfile.dob;
          if(!application.status) application.status = 'pending';
          seneca.act({role: plugin, cmd: 'saveApplication', application: application}, cb);
        });
      });
    }, done);
  }

  function generateEmailContent(applications, done) {
    applications = _.compact(applications);
    if(_.isEmpty(applications)) return done(null, applications);
    if(updateAction === 'disapprove' || updateAction === 'checkin' || updateAction === 'delete') return done(null, applications);
    async.waterfall([
      retrieveProfiles,
      retrieveParentsForUser,
      retrieveEventAndSessionData,
      retrieveDojoData,
      retrieveTicketsData,
      sendEmail
    ], function (err, res) {
      if(err) return done(err);
      return done(null, applications);
    });

    function retrieveProfiles(done) {
      async.map(applications, function (application, cb) {
        seneca.act({role: 'cd-profiles', cmd: 'list', query: {userId: application.userId}}, function (err, profiles) {
          if(err) return cb(err);
          var profile = profiles[0];
          return cb(null, profile);
        });
      }, done); 
    }

    function retrieveParentsForUser(profiles, done) {
      if(applications[0].status !== 'approved') return done(null, profiles);
      seneca.act({role: 'cd-profiles', cmd: 'load_parents_for_user', userId: profiles[0].userId}, function (err, parents) {
        if(err) return done(err);
        profiles[0].parents = parents;
        return done(null, profiles);
      });
    }
{'fraction_non_alphanumeric': 0.13719068413391558, 'fraction_numerical': 0.002789907811741873, 'mean_word_length': 3.5153340635268346, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '7196157', 'n_tokens_mistral': 2372, 'n_tokens_neox': 2361, 'n_words': 580}