<commit_before>const crypto = require('crypto');
const { HackerApplication, ApplicationResponse, Team, TeamMember } = require('js/server/models');
const { sendEmail } = require('js/server/email');
const emailTemplates = require('./email-templates');
const generate = require("adjective-adjective-animal");

exports.createApplicationFromForm = function (formData, user) {
  return generate().then(slug => {
    return HackerApplication.create({
      // Foreign key
      hackerId: user.id,
      // Application
      applicationSlug: slug,
      cv: formData.cv.location,
      countryTravellingFrom: formData.countryTravellingFrom,
      developmentRoles: formData.development,
      learningGoal: formData.learn,
      interests: formData.interests,
      recentAccomplishment: formData.accomplishment,
      links: formData.links,
      inTeam: formData.team_apply,
      wantsTeam: formData.team_placement,
    })
  }).then(application => {
    sendEmail({
      to: user.email, 
      contents: emailTemplates.applied({
        name: user.firstName,
        applicationSlug: application.applicationSlug,
        inTeam: application.inTeam,
      }),
    }).catch(console.log.bind(console));

    console.log(`An application was successfully made by ${user.firstName} ${user.lastName}.`);
    return application;
  }).catch(err => {
    if (err.name == 'SequelizeUniqueConstraintError' && err.errors[0].path === 'applicationSlug') {
      // slug was not unique, try again with new slug
      console.log('Application slug collision detected');
      return exports.createApplicationFromForm(formData, user);
    } else {
      console.log('Failed to add an application to the database');
      return Promise.reject(err);
    }
    
  });
};

exports.createTeamFromForm = function (formData, user, errors) {
  const members = new Set();
  const hackerIds = [];
  const applicationSlugs = {
    'memberB': formData.memberB,
    'memberC': formData.memberC,
    'memberD': formData.memberD,
  };
  // Ensure application slugs are unique and not the applicant's own
  return user.getHackerApplication().then(application => {
    // Start off with the current hacker's application slug — we already know they're in the team
    members.add(application.applicationSlug);<commit_msg>Fix team application<commit_after>const crypto = require('crypto');
const { HackerApplication, ApplicationResponse, Team, TeamMember } = require('js/server/models');
const { sendEmail } = require('js/server/email');
const emailTemplates = require('./email-templates');
const generate = require("adjective-adjective-animal");

exports.createApplicationFromForm = function (formData, user) {
  return generate().then(slug => {
    return HackerApplication.create({
      // Foreign key
      hackerId: user.id,
      // Application
      applicationSlug: slug,
      cv: formData.cv.location,
      countryTravellingFrom: formData.countryTravellingFrom,
      developmentRoles: formData.development,
      learningGoal: formData.learn,
      interests: formData.interests,
      recentAccomplishment: formData.accomplishment,
      links: formData.links,
      inTeam: formData.team_apply,
      wantsTeam: formData.team_placement,
    })
  }).then(application => {
    sendEmail({
      to: user.email, 
      contents: emailTemplates.applied({
        name: user.firstName,
        applicationSlug: application.applicationSlug,
        inTeam: application.inTeam,
      }),
    }).catch(console.log.bind(console));

    console.log(`An application was successfully made by ${user.firstName} ${user.lastName}.`);
    return application;
  }).catch(err => {
    if (err.name == 'SequelizeUniqueConstraintError' && err.errors[0].path === 'applicationSlug') {
      // slug was not unique, try again with new slug
      console.log('Application slug collision detected');
      return exports.createApplicationFromForm(formData, user);
    } else {
      console.log('Failed to add an application to the database');
      return Promise.reject(err);
    }
    
  });
};

exports.createTeamFromForm = function (formData, user, errors) {
  const members = new Set();
  const hackerIds = [user.id];
  const applicationSlugs = {
    'memberB': formData.memberB,
    'memberC': formData.memberC,
    'memberD': formData.memberD,
  };
  // Ensure application slugs are unique and not the applicant's own
  return user.getHackerApplication().then(application => {
    // Start off with the current hacker's application slug — we already know they're in the team
    members.add(application.applicationSlug);
{'fraction_non_alphanumeric': 0.11861074705111402, 'fraction_numerical': 0.00043687199650502403, 'mean_word_length': 3.6159274193548385, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '1853935', 'n_tokens_mistral': 1291, 'n_tokens_neox': 1262, 'n_words': 312}