<commit_before>module.exports =
  resolve:
    userValidated: ($auth) ->
      $auth.validateUser()
    membershipsLoaded: ->
      global.cobudgetApp.membershipsLoaded
  url: '/groups/:groupId/manage_funds/review_upload'
  template: require('./review-bulk-allocation-page.html')
  params:
    people: null
    groupId: null
  controller: (config, CurrentUser, Error, LoadBar, $location, Dialog, Records, $scope, $stateParams, $timeout, UserCan) ->

    LoadBar.start()
    groupId = parseInt($stateParams.groupId)
    unless $stateParams.people
      $location.path("/groups/#{groupId}/manage_funds")

    Records.groups.findOrFetchById(groupId)
      .then (group) ->
        LoadBar.stop()
        if UserCan.manageFundsForGroup(group)
          $scope.authorized = true
          Error.clear()
          $scope.group = group
          $scope.currentUser = CurrentUser()
          Records.memberships.fetchByGroupId(groupId)
          $scope.preparePeopleList()
        else
          $scope.authorized = false
          Error.set("you can't view this page")
      .catch ->
        LoadBar.stop()
        Error.set('group not found')

    $scope.abs = (val) ->
      Math.abs(val)

    $scope.preparePeopleList = ->
      $scope.people = _.map $stateParams.people, (person) ->
        person.allocation_amount = parseFloat(person.allocation_amount)
        person
      $scope.peopleWithPositiveAllocations = []
      $scope.peopleWithNegativeAllocations = []
      $scope.newPeople = []
      _.each $scope.people, (person) ->
        if person.allocation_amount > 0
          $scope.peopleWithPositiveAllocations.push(person)
        else if person.allocation_amount < 0
          $scope.peopleWithNegativeAllocations.push(person)
        if person.new_member
          $scope.newPeople.push(person)

    $scope.summedAllocationsFrom = (people) ->
      callback = (sum, person) ->
        sum + Math.abs(parseFloat(person.allocation_amount))
      _.reduce(people, callback, 0)

    $scope.openUploadCSVPrimerDialog = ->
      uploadCSVPrimerDialog = require('./../upload-csv-primer-dialog/upload-csv-primer-dialog.coffee')({
        scope: $scope
      })
      Dialog.open(uploadCSVPrimerDialog)

    $scope.cancel = ->
      $location.path("/groups/#{groupId}")

    $scope.confirmBulkAllocations = ->
      _.each $scope.newPeople, (newPerson) ->
        params = {group_id: groupId, email: newPerson.email}
        Records.memberships.remote.create(params)

    return<commit_msg>confirm also creates allocations and invites for all new folx<commit_after>module.exports =
  resolve:
    userValidated: ($auth) ->
      $auth.validateUser()
    membershipsLoaded: ->
      global.cobudgetApp.membershipsLoaded
  url: '/groups/:groupId/manage_funds/review_upload'
  template: require('./review-bulk-allocation-page.html')
  params:
    people: null
    groupId: null
  controller: (config, CurrentUser, Error, LoadBar, $location, Dialog, Records, $scope, $stateParams, $timeout, UserCan) ->

    LoadBar.start()
    groupId = parseInt($stateParams.groupId)
    unless $stateParams.people
      $location.path("/groups/#{groupId}/manage_funds")

    Records.groups.findOrFetchById(groupId)
      .then (group) ->
        LoadBar.stop()
        if UserCan.manageFundsForGroup(group)
          $scope.authorized = true
          Error.clear()
          $scope.group = group
          $scope.currentUser = CurrentUser()
          Records.memberships.fetchByGroupId(groupId)
          $scope.preparePeopleList()
        else
          $scope.authorized = false
          Error.set("you can't view this page")
      .catch ->
        LoadBar.stop()
        Error.set('group not found')

    $scope.abs = (val) ->
      Math.abs(val)

    $scope.preparePeopleList = ->
      $scope.people = _.map $stateParams.people, (person) ->
        person.allocation_amount = parseFloat(person.allocation_amount)
        person
      $scope.peopleWithPositiveAllocations = []
      $scope.peopleWithNegativeAllocations = []
      $scope.newPeople = []
      _.each $scope.people, (person) ->
        if person.allocation_amount > 0
          $scope.peopleWithPositiveAllocations.push(person)
        else if person.allocation_amount < 0
          $scope.peopleWithNegativeAllocations.push(person)
        if person.new_member
          $scope.newPeople.push(person)

    $scope.summedAllocationsFrom = (people) ->
      callback = (sum, person) ->
        sum + Math.abs(parseFloat(person.allocation_amount))
      _.reduce(people, callback, 0)

    $scope.openUploadCSVPrimerDialog = ->
      uploadCSVPrimerDialog = require('./../upload-csv-primer-dialog/upload-csv-primer-dialog.coffee')({
        scope: $scope
      })
      Dialog.open(uploadCSVPrimerDialog)

    $scope.cancel = ->
      $location.path("/groups/#{groupId}")

    $scope.confirmBulkAllocations = ->
      _.each $scope.newPeople, (newPerson) ->
        params = {group_id: groupId, email: newPerson.email}
        Records.memberships.remote.create(params).then (data) ->
          params = {groupId: groupId, userId: data.users[0].id, amount: newPerson.allocation_amount}
          allocation = Records.allocations.build(params)
          allocation.save().then ->
            newMembership = data.memberships[0]
            Records.memberships.invite(newMembership)

    return
{'fraction_non_alphanumeric': 0.12408759124087591, 'fraction_numerical': 0.0014972861688190156, 'mean_word_length': 2.695712309820194, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 37, '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': '7662915', 'n_tokens_mistral': 1609, 'n_tokens_neox': 1549, 'n_words': 262}