<commit_before>class PaymentsTrackerController < ApplicationController
  require 'open-uri'

  #
  # Stores the teamsnap division ids by the name of the division (this is very sensitive since the user can edit these names)
  #
  def teamsnap_divs_by_id
    ids_by_div_name = Hash.new
    ids_by_div_name['1. Dima Division'] = 27394
    ids_by_div_name['2. Stonewall Division'] = 27395
    ids_by_div_name['3. Fitzpatrick Division'] = 27397
    ids_by_div_name['4. Rainbow Division'] = 27398
    ids_by_div_name['5. Sachs Division'] = 27400
    ids_by_div_name["1. Women's Competitive Division"] = 27403
    ids_by_div_name["2. Women's Recreational Division"] = 27404
    ids_by_div_name["Big Apple Softball League"] = 16139 
    ids_by_div_name
  end

  def index
  end

  def new_account
    @teamsnap_sync_account = TeamsnapScanAccount.new
    render 'payments_tracker/account/new'
  end

  def create_account
    @account = TeamsnapScanAccount.new(teamsnap_scan_account_params)

    respond_to do |format|
      if @account.save
        format.html { redirect_to '/payments/tracker', notice: 'Scans Account was successfully created.' }
      else
        format.html { redirect_to 'payments_tracker#new_account', notice: 'Error Saving Account.' }
      end
    end

  end

  # home page for the page tracker
  def home
    mechanize = Mechanize.new
    @players = log_in_get_player_info(mechanize)
  end

  def admin
    @scans = TeamsnapPaymentsSync.order('created_at DESC').all
    @latest_sync = TeamsnapPaymentsSync.order('created_at DESC').first
  end

  #
  # Gets a list of paid players on unassigned teams
  #
  def unassigned
    mechanize = Mechanize.new
    players = log_in_get_player_info(mechanize)
    ids_by_div_name = teamsnap_divs_by_id
    @unassigned_player = Array.new
    @league_id = ids_by_div_name["Big Apple Softball League"]
    players_by_payments = sort_players(players)
    players_by_payments[:paid].each do |player|
      if player['division_id'] == @league_id
        @unassigned_player.push(player)
      end
    end
  end 

  def sync
    @response_data = Hash.new
    mechanize = Mechanize.new

    # create a new sync object
    # get all the players from the scrapper
    players = log_in_get_player_info(mechanize)
    players_by_payments = sort_players(players)

    sync = TeamsnapPaymentsSync.new(
      :total_players => players.length,
      :total_paid_players => players_by_payments[:paid].length,
      :total_unpaid_players => players_by_payments[:unpaid].length,
      :total_players_updated => players_update_count,
      :is_success => true
    )

    sync.save
    @response_data[:sync] = sync
    @response_data[:sync_created_string] = sync.created_at.strftime('%B %e at %l:%M %p')
    @response_data[:players_updated_count] = players_update_count
    @response_data[:scan_row_html] = render_to_string(:template => "payments_tracker/_payments_row.haml", :locals => {:scan => sync})
    @response_data[:new_paid_players] = players_by_payments[:new_paid]

    log_in_update_players_on_teamsnap(players_by_payments[:new_paid])

    respond_to do |format|
      format.json { render :json=> @response_data}
      format.html { render :layout => false }
    end
  end

  def sort_players(players)
    players_by_payments = Hash.new
    players_by_payments[:paid] = Array.new
    players_by_payments[:unpaid] = Array.new
    players_by_payments[:new_paid] = Array.new
    players_update_count = 0
    # get all paid players
    players.each do |player|
      if player['has_paid?']
        players_by_payments[:paid].push(player)

        teamsnap_payment = TeamsnapPayment.new(
            :teamsnap_player_id => player['teamsnap_id'],
            :teamsnap_player_name => player['name'],
            :teamsnap_player_email => player['email']
          )
        # make sure we can save the payment
        if teamsnap_payment.valid?<commit_msg>Fixing bug with sync page<commit_after>class PaymentsTrackerController < ApplicationController
  require 'open-uri'

  #
  # Stores the teamsnap division ids by the name of the division (this is very sensitive since the user can edit these names)
  #
  def teamsnap_divs_by_id
    ids_by_div_name = Hash.new
    ids_by_div_name['1. Dima Division'] = 27394
    ids_by_div_name['2. Stonewall Division'] = 27395
    ids_by_div_name['3. Fitzpatrick Division'] = 27397
    ids_by_div_name['4. Rainbow Division'] = 27398
    ids_by_div_name['5. Sachs Division'] = 27400
    ids_by_div_name["1. Women's Competitive Division"] = 27403
    ids_by_div_name["2. Women's Recreational Division"] = 27404
    ids_by_div_name["Big Apple Softball League"] = 16139 
    ids_by_div_name
  end

  def index
  end

  def new_account
    @teamsnap_sync_account = TeamsnapScanAccount.new
    render 'payments_tracker/account/new'
  end

  def create_account
    @account = TeamsnapScanAccount.new(teamsnap_scan_account_params)

    respond_to do |format|
      if @account.save
        format.html { redirect_to '/payments/tracker', notice: 'Scans Account was successfully created.' }
      else
        format.html { redirect_to 'payments_tracker#new_account', notice: 'Error Saving Account.' }
      end
    end

  end

  # home page for the page tracker
  def home
    mechanize = Mechanize.new
    @players = log_in_get_player_info(mechanize)
  end

  def admin
    @scans = TeamsnapPaymentsSync.order('created_at DESC').all
    @latest_sync = TeamsnapPaymentsSync.order('created_at DESC').first
  end

  #
  # Gets a list of paid players on unassigned teams
  #
  def unassigned
    mechanize = Mechanize.new
    players = log_in_get_player_info(mechanize)
    ids_by_div_name = teamsnap_divs_by_id
    @unassigned_player = Array.new
    @league_id = ids_by_div_name["Big Apple Softball League"]
    players_by_payments = sort_players(players)
    players_by_payments[:paid].each do |player|
      if player['division_id'] == @league_id
        @unassigned_player.push(player)
      end
    end
  end 

  def sync
    @response_data = Hash.new
    mechanize = Mechanize.new

    # create a new sync object
    # get all the players from the scrapper
    players = log_in_get_player_info(mechanize)
    players_by_payments = sort_players(players)

    sync = TeamsnapPaymentsSync.new(
      :total_players => players.length,
      :total_paid_players => players_by_payments[:paid].length,
      :total_unpaid_players => players_by_payments[:unpaid].length,
      :total_players_updated => players_by_payments[:new_paid].length,
      :is_success => true
    )

    sync.save
    @response_data[:sync] = sync
    @response_data[:sync_created_string] = sync.created_at.strftime('%B %e at %l:%M %p')
    @response_data[:players_updated_count] = players_by_payments[:new_paid].length
    @response_data[:scan_row_html] = render_to_string(:template => "payments_tracker/_payments_row.haml", :locals => {:scan => sync})
    @response_data[:new_paid_players] = players_by_payments[:new_paid]

    log_in_update_players_on_teamsnap(players_by_payments[:new_paid])

    respond_to do |format|
      format.json { render :json=> @response_data}
      format.html { render :layout => false }
    end
  end

  def sort_players(players)
    players_by_payments = Hash.new
    players_by_payments[:paid] = Array.new
    players_by_payments[:unpaid] = Array.new
    players_by_payments[:new_paid] = Array.new
    players_update_count = 0
    # get all paid players
    players.each do |player|
      if player['has_paid?']
        players_by_payments[:paid].push(player)

        teamsnap_payment = TeamsnapPayment.new(
            :teamsnap_player_id => player['teamsnap_id'],
            :teamsnap_player_name => player['name'],
            :teamsnap_player_email => player['email']
          )
        # make sure we can save the payment
        if teamsnap_payment.valid?
{'fraction_non_alphanumeric': 0.09083949987241643, 'fraction_numerical': 0.012248022454707833, 'mean_word_length': 3.624778761061947, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 29, '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': '5003385', 'n_tokens_mistral': 2726, 'n_tokens_neox': 2591, 'n_words': 545}