<commit_before>import moment from 'moment'
import uniq from 'lodash/uniq'
import xssFilters from 'xss-filters'
import InvitedUsersModel from './InvitedUsersModel'
import UserModel from '../users/UserModel'
import { verifyAndSendInvite } from './utils'
/**
 * conditionally creates a new invited user and sends a sendgrid email if user has not already
 * been invited OR if inviting user has exceeded the max amount of invites in 24 hours
 * @param {object} userContext - The user authorizer object.
 * @param {string} inviterId - The user id.
 * @param {array} invitedEmails - email addresses to send
 * @param {string} inviterName - name to include in ivite email
 * @param {string} inviterMessage - optional personal message to include in email
 * @return {Promise<User>}  A promise that resolves into a invitedUser instance or an error.
 */
const createInvitedUsers = async (
  userContext,
  inviterId,
  invitedEmails,
  inviterName,
  inviterMessage
) => {
  try {
    const endTimeRoundedISO = moment()
      .startOf('hour')
      .toISOString()
    const startTimeRoundedISO = moment(endTimeRoundedISO)
      .subtract(30, 'days')
      .endOf('hour')
      .toISOString()
    const sanitizedEmails = uniq(
      invitedEmails.map(email => xssFilters.inHTMLData(email))
    )
    const [invitingUser, userInvites] = await Promise.all([
      UserModel.get(userContext, inviterId),
      InvitedUsersModel.query(userContext, inviterId)
        .usingIndex('InvitesByInviter')
        .where('created')
        .between(startTimeRoundedISO, endTimeRoundedISO)
        .execute(),
    ])
    if (userInvites.length + sanitizedEmails.length > 50) {
      throw new Error('user is trying to invite too many people in 24 hours')
    }
    const sanitizedMessage = xssFilters.inHTMLData(inviterMessage)
    const santiziedInviterName = xssFilters.inHTMLData(inviterName)
    const verifiedAndSentEmails = await Promise.all(
      sanitizedEmails.map(inviteEmail =>
        verifyAndSendInvite(
          userContext,
          inviterId,
          inviteEmail,
          invitingUser,
          santiziedInviterName,
          sanitizedMessage
        )
      )
    )
    const sortedResults = verifiedAndSentEmails.reduce(
      (acum, item) => {
        acum[
          item.error ? 'failedEmailAddresses' : 'successfulEmailAddresses'
        ].push(item)
        return acum
      },
      {
        successfulEmailAddresses: [],
        failedEmailAddresses: [],
      }
    )
    return sortedResults
  } catch (e) {
    throw e
  }
}

export default createInvitedUsers
<commit_msg>user context not required for querying invitedUserModel<commit_after>import moment from 'moment'
import uniq from 'lodash/uniq'
import xssFilters from 'xss-filters'
import InvitedUsersModel from './InvitedUsersModel'
import UserModel from '../users/UserModel'
import { verifyAndSendInvite } from './utils'
import {
  ADMIN_MANAGEMENT,
  getPermissionsOverride,
} from '../../utils/permissions-overrides'

const override = getPermissionsOverride(ADMIN_MANAGEMENT)
/**
 * conditionally creates a new invited user and sends a sendgrid email if user has not already
 * been invited OR if inviting user has exceeded the max amount of invites in 24 hours
 * @param {object} userContext - The user authorizer object.
 * @param {string} inviterId - The user id.
 * @param {array} invitedEmails - email addresses to send
 * @param {string} inviterName - name to include in ivite email
 * @param {string} inviterMessage - optional personal message to include in email
 * @return {Promise<User>}  A promise that resolves into a invitedUser instance or an error.
 */
const createInvitedUsers = async (
  userContext,
  inviterId,
  invitedEmails,
  inviterName,
  inviterMessage
) => {
  try {
    const endTimeRoundedISO = moment()
      .startOf('hour')
      .toISOString()
    const startTimeRoundedISO = moment(endTimeRoundedISO)
      .subtract(30, 'days')
      .endOf('hour')
      .toISOString()
    const sanitizedEmails = uniq(
      invitedEmails.map(email => xssFilters.inHTMLData(email))
    )
    const [invitingUser, userInvites] = await Promise.all([
      UserModel.get(userContext, inviterId),
      InvitedUsersModel.query(override, inviterId)
        .usingIndex('InvitesByInviter')
        .where('created')
        .between(startTimeRoundedISO, endTimeRoundedISO)
        .execute(),
    ])
    if (userInvites.length + sanitizedEmails.length > 50) {
      throw new Error('user is trying to invite too many people in 24 hours')
    }
    const sanitizedMessage = xssFilters.inHTMLData(inviterMessage)
    const santiziedInviterName = xssFilters.inHTMLData(inviterName)
    const verifiedAndSentEmails = await Promise.all(
      sanitizedEmails.map(inviteEmail =>
        verifyAndSendInvite(
          userContext,
          inviterId,
          inviteEmail,
          invitingUser,
          santiziedInviterName,
          sanitizedMessage
        )
      )
    )
    const sortedResults = verifiedAndSentEmails.reduce(
      (acum, item) => {
        acum[
          item.error ? 'failedEmailAddresses' : 'successfulEmailAddresses'
        ].push(item)
        return acum
      },
      {
        successfulEmailAddresses: [],
        failedEmailAddresses: [],
      }
    )
    return sortedResults
  } catch (e) {
    throw e
  }
}

export default createInvitedUsers

{'fraction_non_alphanumeric': 0.08876069966505397, 'fraction_numerical': 0.00297729810197246, 'mean_word_length': 3.0750568612585294, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, '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': '4377488', 'n_tokens_mistral': 1582, 'n_tokens_neox': 1576, 'n_words': 426}