<commit_before>###

  isAccountType

  @example check to see if schema is of a certain AccountType

    isAccountType(schema, type)

  @param schema [SimpleSchema] generally passed as @ or this
  @param type [String] the type of account compared to the schema's AccountType

###
isAccountType = (schema, type) ->
  isCredit = schema.field('AccountType').value == type
  if isCredit and !schema.isSet and (!schema.operator or schemavalue == null or schema.value == '')
    'required'

###
  Schema used for validating give transaction data
###
giveTransactionSchema = new SimpleSchema(
  Email:
    type: String
    regEx: Apollos.regex.email
  AccountType:
    type: String
    regEx: /^(checking|savings|credit)$/
  'AmountDetails.TargetAccountId':
    type: Number
  'AmountDetails.Amount':
    type: Number
  AccountNumber:
    type: String
  RoutingNumber:
    type: String
    optional: true
    custom: ->
      'required' if isAccountType(@, 'checking') or isAccountType(@, 'savings')
  CCV:
    type: String
    optional: true
    custom: ->
      'required' if isAccountType(@, 'credit')
  ExpirationMonth:
    type: Number
    optional: true
    custom: ->
      'required' if isAccountType(@, 'credit')
  ExpirationYear:
    type: Number
    optional: true
    custom: ->
      'required' if isAccountType(@, 'credit')
  AccountId:
    type: Number
    optional: true
  PersonId:
    type: Number
    optional: true
  FirstName:
    type: String
  LastName:
    type: String
  Street1:
    type: String
  Street2:
    type: String
  City:
    type: String
  State:
    type: String
  PostalCode:
    type: String
  Country:
    type: String
  PhoneNumber:
    type: String
  CampusId:
    type: Number
)

###

  Apollos.giveTransaction

  @example validate and pass transaction data to Rock

    Apollos.giveTransation(data)

  @param data [Object] give transaction info. see schema

###

Apollos.giveTransaction = (data) ->

  giveContext = giveTransactionSchema.newContext()

  return false unless giveContext.validate(data)

  Rock.apiRequest "POST", "api/Give", data, (error, data) ->
    if error
      debug "Apollos give transaction failed:"
      debug error
      return false

    return true
<commit_msg>Update schema for give transaction<commit_after>###

  requiredIfNotFieldSet

  @example check to see if schema is representing the given state

    requiredIfNotFieldSet(schema, fieldName, value)

  @param schema [SimpleSchema] generally passed as @ or this
  @param fieldName [String] the name of a field within the schema
  @param value [String|Number] the value to check that the field is set to or,
  if ommitted, indicates that the field should simply be falsey

###
requiredIfNotFieldSet = (schema, fieldName, value) ->
  if value?
    isOfType = schema.field(fieldName).value == value
  else
    isOfType = not schema.field(fieldName).value?

  if isOfType and !schema.isSet and (!schema.operator or schemavalue == null or schema.value == '')
    return "required"


###
  Schema used for validating give transaction data
###
amountDetail = new SimpleSchema
  Amount:
    type: Number
  TargetAccountId:
    type: Number
    decimal: false

giveTransactionSchema = new SimpleSchema(
  SourceAccountId:
    decimal: false
    type: Number
    optional: true
  Email:
    type: String
    regEx: Apollos.regex.email
  AccountType:
    type: String
    optional: true
    regEx: /^(checking|savings|credit)$/
    custom: ->
      requiredIfNotFieldSet @, "SourceAccountId"
  AmountDetails:
    type: [amountDetail]
  AccountNumber:
    type: String
    optional: true
    custom: ->
      requiredIfNotFieldSet @, "SourceAccountId"
  RoutingNumber:
    type: String
    optional: true
    custom: ->
      requiredIfNotFieldSet(@, "AccountType", "checking") or requiredIfNotFieldSet @, "AccountType", "savings"
  CCV:
    type: String
    optional: true
    custom: ->
      requiredIfNotFieldSet @, "AccountType", "credit"
  ExpirationMonth:
    type: Number
    optional: true
    custom: ->
      requiredIfNotFieldSet @, "AccountType", "credit"
  ExpirationYear:
    type: Number
    optional: true
    custom: ->
      requiredIfNotFieldSet @, "AccountType", "credit"
  PersonId:
    type: Number
    decimal: false
    optional: true
  FirstName:
    type: String
  LastName:
    type: String
  Street1:
    type: String
  Street2:
    type: String
  City:
    type: String
  State:
    type: String
  PostalCode:
    type: String
  Country:
    type: String
  PhoneNumber:
    type: String
  CampusId:
    decimal: false
    type: Number
)

###

  Apollos.giveTransaction

  @example validate and pass transaction data to Rock

    Apollos.giveTransation(data)

  @param data [Object] give transaction info. see schema

###

Apollos.giveTransaction = (data) ->

  giveContext = giveTransactionSchema.newContext()

  return false unless giveContext.validate(data)
  
  Rock.apiRequest "POST", "api/Give", data, (error, data) ->
    if error
      debug "Apollos give transaction failed:"
      debug error
      return false

    return true
{'fraction_non_alphanumeric': 0.09294998030720757, 'fraction_numerical': 0.0007877116975187081, 'mean_word_length': 3.183690280065898, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, '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': '4625947', 'n_tokens_mistral': 1558, 'n_tokens_neox': 1501, 'n_words': 477}