<commit_before>Decimal = require('big.js')
Moment = require('moment')
_ = require('lodash')

defineProperties = require('utils/define-properties')
sum = require('utils/sum')

### @ngInject ###
module.exports = (AuthService, AllocationModel, BucketModel, FixedCostModel) ->
  class RoundModel
    constructor: (data = {}) ->
      @id = data.id
      @name = data.name
      @groupId = data.groupId

      @startsAt = Moment(data.startsAt)
      @endsAt = Moment(data.endsAt)

      @allocations = _.map data.allocations, (allocation) ->
        new AllocationModel(allocation)
      @buckets = _.map data.buckets, (bucket) ->
        new BucketModel(bucket)
      @fixedCosts = _.map data.fixedCosts, (fixedCost) ->
        new FixedCostModel(fixedCost)

      defineProperties @,
        status:
          deps: ['startsAt, endsAt']
          get: ->
            now = Moment()
            if (@startsAt.isAfter(now))
              changeLabel = "starts"
              changeTime = @startsAt.from(now)
              return {
                name: "pending"
                class: "info"
                label: changeLabel + " " + changeTime
                changeLabel: changeLabel
                changeTime: changeTime
              }
            else if (@startsAt.isBefore(now) and @endsAt.isAfter(now))
              return {
                name: "open"
                class: "success"
                label: "open now, ends " + @endsAt.from(now)
                changeLabel: "time left"
                changeTime: @endsAt.from(now, true)
              }
            else
              changeLabel = "ended"
              changeTime = @endsAt.from(now)
              return {
                name: "closed"
                class: "warning"
                label: changeLabel + " " + changeTime
                changeLabel: changeLabel
                changeTime: changeTime
              }

        myContributions:
          deps: ['buckets']
          get: ->
            _.map @buckets, (bucket) ->
              bucket.myContribution

        myAllocationIndex:
          deps: ['allocations']
          get: ->
            _.findIndex @allocations, (allocation) ->
              allocation.userId == AuthService.getCurrentUser().id

        myAllocation:
          deps: ['myAllocationIndex', 'allocations']
          get: ->
            index = @myAllocationIndex
            if index != -1
              @allocations[index]
            else
              new AllocationModel({
                userId: AuthService.getCurrentUser().id
                roundId: @id
                amountCents: 0
              })

        groupAllocations:
          deps: ['allocations']
          get: ->
            _.filter @allocations, (allocation) ->
              allocation.user.id != AuthService.getCurrentUser().id

        allocationsTotal:
          deps: ['allocations']
          get: ->
            sum(_.pluck @allocations, "amount")

        myContributionsTotal:
          deps: ['myContributions']
          get: ->
            sum(_.pluck @myContributions, "amount")

        myAllocationAmount:
          deps: ['myAllocation']
          get: ->
            @myAllocation.amount

        myAllocationLeft:
          deps: ['myAllocationAmount', 'myContributionsTotal']
          get: ->
            @myAllocationAmount.minus(@myContributionsTotal)

        totalAllocated:
          deps: ['buckets']
          get: ->
            sum(_.pluck @buckets, "contributionTotal")

        totalAllocable:
          deps: ['allocations']
          get: ->
            sum(_.pluck @allocations, "amount")

        balanceStatus:
          deps: ['myAllocationLeft']
          get: ->
            if (@myAllocationLeft.gt(0))
              null
            else if (@myAllocationLeft.eq(0))
              'complete'
            else
              'warning'

    serialize: ->
      {
        id: @id
        name: @name
        groupId: @groupId
        startsAt: @startsAt.toISOString()
        endsAt: @endsAt.toISOString()
      }

    getAllocationsByUser: ->
      # index allocations by user id
      allocationsByUserId = {}
      _.each @allocations, (allocation) ->
        allocationsByUserId[allocation.userId] = allocation
      allocationsByUserId

    getContributors: (memberships) ->
      contributors = {}

      allocationsByUserId = @getAllocationsByUser()

      # for each member
      _.each memberships, (membership) ->
        contributor = {
          user: membership.user
        }

        allocation = allocationsByUserId[contributor.user.id]

        if (allocation)
          contributor.allocation = allocation.amount

        contributors[contributor.user.id] = contributor

      # for each bucket
      _.each @buckets, (bucket) ->
        contributions = bucket.getContributionsByUser()

        # add contributions to contributors
        _.each contributions, (contribution, userId) ->

          contributor = contributors[userId]

          if not contributors or not contributor
            return

          if not contributor.contribution
            contributor.contribution = contribution.amount
          else
            contribution.contribution = contributor.contribution.plus(contribution.amount)<commit_msg>fix typo that is causing contributors page to be incorrect<commit_after>Decimal = require('big.js')
Moment = require('moment')
_ = require('lodash')

defineProperties = require('utils/define-properties')
sum = require('utils/sum')

### @ngInject ###
module.exports = (AuthService, AllocationModel, BucketModel, FixedCostModel) ->
  class RoundModel
    constructor: (data = {}) ->
      @id = data.id
      @name = data.name
      @groupId = data.groupId

      @startsAt = Moment(data.startsAt)
      @endsAt = Moment(data.endsAt)

      @allocations = _.map data.allocations, (allocation) ->
        new AllocationModel(allocation)
      @buckets = _.map data.buckets, (bucket) ->
        new BucketModel(bucket)
      @fixedCosts = _.map data.fixedCosts, (fixedCost) ->
        new FixedCostModel(fixedCost)

      defineProperties @,
        status:
          deps: ['startsAt, endsAt']
          get: ->
            now = Moment()
            if (@startsAt.isAfter(now))
              changeLabel = "starts"
              changeTime = @startsAt.from(now)
              return {
                name: "pending"
                class: "info"
                label: changeLabel + " " + changeTime
                changeLabel: changeLabel
                changeTime: changeTime
              }
            else if (@startsAt.isBefore(now) and @endsAt.isAfter(now))
              return {
                name: "open"
                class: "success"
                label: "open now, ends " + @endsAt.from(now)
                changeLabel: "time left"
                changeTime: @endsAt.from(now, true)
              }
            else
              changeLabel = "ended"
              changeTime = @endsAt.from(now)
              return {
                name: "closed"
                class: "warning"
                label: changeLabel + " " + changeTime
                changeLabel: changeLabel
                changeTime: changeTime
              }

        myContributions:
          deps: ['buckets']
          get: ->
            _.map @buckets, (bucket) ->
              bucket.myContribution

        myAllocationIndex:
          deps: ['allocations']
          get: ->
            _.findIndex @allocations, (allocation) ->
              allocation.userId == AuthService.getCurrentUser().id

        myAllocation:
          deps: ['myAllocationIndex', 'allocations']
          get: ->
            index = @myAllocationIndex
            if index != -1
              @allocations[index]
            else
              new AllocationModel({
                userId: AuthService.getCurrentUser().id
                roundId: @id
                amountCents: 0
              })

        groupAllocations:
          deps: ['allocations']
          get: ->
            _.filter @allocations, (allocation) ->
              allocation.user.id != AuthService.getCurrentUser().id

        allocationsTotal:
          deps: ['allocations']
          get: ->
            sum(_.pluck @allocations, "amount")

        myContributionsTotal:
          deps: ['myContributions']
          get: ->
            sum(_.pluck @myContributions, "amount")

        myAllocationAmount:
          deps: ['myAllocation']
          get: ->
            @myAllocation.amount

        myAllocationLeft:
          deps: ['myAllocationAmount', 'myContributionsTotal']
          get: ->
            @myAllocationAmount.minus(@myContributionsTotal)

        totalAllocated:
          deps: ['buckets']
          get: ->
            sum(_.pluck @buckets, "contributionTotal")

        totalAllocable:
          deps: ['allocations']
          get: ->
            sum(_.pluck @allocations, "amount")

        balanceStatus:
          deps: ['myAllocationLeft']
          get: ->
            if (@myAllocationLeft.gt(0))
              null
            else if (@myAllocationLeft.eq(0))
              'complete'
            else
              'warning'

    serialize: ->
      {
        id: @id
        name: @name
        groupId: @groupId
        startsAt: @startsAt.toISOString()
        endsAt: @endsAt.toISOString()
      }

    getAllocationsByUser: ->
      # index allocations by user id
      allocationsByUserId = {}
      _.each @allocations, (allocation) ->
        allocationsByUserId[allocation.userId] = allocation
      allocationsByUserId

    getContributors: (memberships) ->
      contributors = {}

      allocationsByUserId = @getAllocationsByUser()

      # for each member
      _.each memberships, (membership) ->
        contributor = {
          user: membership.user
        }

        allocation = allocationsByUserId[contributor.user.id]

        if (allocation)
          contributor.allocation = allocation.amount

        contributors[contributor.user.id] = contributor

      # for each bucket
      _.each @buckets, (bucket) ->
        contributions = bucket.getContributionsByUser()

        # add contributions to contributors
        _.each contributions, (contribution, userId) ->

          contributor = contributors[userId]

          if not contributors or not contributor
            return

          if not contributor.contribution
            contributor.contribution = contribution.amount
          else
            contributor.contribution = contributor.contribution.plus(contribution.amount)
{'fraction_non_alphanumeric': 0.10132325141776938, 'fraction_numerical': 0.0007561436672967864, 'mean_word_length': 1.6399700598802396, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 57, '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': '3426727', 'n_tokens_mistral': 2865, 'n_tokens_neox': 2753, 'n_words': 584}