Skip to content

Commit

Permalink
add point export
Browse files Browse the repository at this point in the history
  • Loading branch information
SepsiLaszlo committed Jul 22, 2024
1 parent 0c6cddf commit f85fa66
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 2 deletions.
13 changes: 12 additions & 1 deletion app/controllers/season_admin_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@ def previous

def update
SystemAttribute.update_season(params[:season])
redirect_to root_path, notice: t(:edit_successful)
redirect_to seasons_path, notice: t(:edit_successful)
end

def export
semester = SystemAttribute.semester
export = ExportPointHistory.call(semester.to_s)
csv = CSV.generate do |lines|
export.each do |line|
lines << line
end
end
send_data(csv, filename: "kozossegi-pont-expot-#{semester}.csv", type: "text/csv")
end
end
28 changes: 28 additions & 0 deletions app/services/export_point_history.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class ExportPointHistory
attr_reader :semester

def initialize(semester)
@semester = semester
end

def self.call(semester)
new(semester).call
end

def call
result = []
result << ["PéK id", "Név", "Neptun", "BME-id", "Email", "Felvételi pont", "SVIE tagság", "Körök"]
histories = PointHistory.includes(user: [point_requests: [evaluation: :group]]).where(semester: semester).where.not(point: 0)
histories.map do |history|
user = history.user
point_requests = user.point_requests.select do |point_request|
point_request.accepted? && point_request.evaluation.semester == semester
end
point_requests = point_requests.reject { |point_request| point_request.point == 0 }
groups = point_requests.map { |point_request| point_request.evaluation.group.name }.join(',')
email = URI::MailTo::EMAIL_REGEXP.match?(user.email) ? user.email : nil
result << [user.id, user.full_name, user.neptun, user.bme_id, email, history.point, user.svie_member_type, groups]
end
result
end
end
10 changes: 9 additions & 1 deletion app/views/season_admin/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,21 @@
<%= label :semester_year, 'Kiválasztott félév : ' %>
<%= @semester.to_readable %>
</div>

<% if SystemAttribute.offseason? && PointHistory.exists?(semester: @semester.to_s) %>
<div class="uk-text-center">
<%= link_to "Közösségi pontok exportálása", export_point_history_path,
{class: 'uk-button uk-button-danger uk-margin-small uk-margin-left'} %>
</div>
<% end %>
<%= form_tag(seasons_path, class: 'uk-form') do %>
<div class="uk-margin">
<%= label :season, 'Időszak', class: 'uk-label' %>
<%= select_tag :season, options_for_select(Rails.configuration.x.season_types.invert, @season.value), class: 'uk-select uk-width-auto' %>
</div>
<div class="uk-text-center uk-margin">
<%= submit_tag "Save", class: 'uk-button uk-button-danger uk-margin-small uk-margin-left' %>
<%= submit_tag "Mentés", class: 'uk-text-white uk-button uk-button-danger uk-margin-small uk-margin-left' %>
</div>
<% end %>
</div>
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
end

get '/seasons', to: 'season_admin#index', as: :seasons
get '/seasons/export', to: 'season_admin#export', as: :export_point_history
post '/seasons/next', to: 'season_admin#next', as: :next_semester
post '/seasons/previous', to: 'season_admin#previous', as: :previous_semester
post '/seasons', to: 'season_admin#update'
Expand Down
1 change: 1 addition & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
FactoryBot.create(:post_type_new_member)
FactoryBot.create(:system_attribute_semester)
FactoryBot.create(:system_attribute_app_season)
FactoryBot.create(:system_attribute_max_point_for_semester)
SystemAttribute.update_season(SystemAttribute::OFFSEASON)
end

Expand Down
42 changes: 42 additions & 0 deletions spec/services/export_point_history_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# frozen_string_literal: true

require 'rails_helper'

describe ExportPointHistory do
subject(:export) { ExportPointHistory.call(semester.to_s) }
let!(:membership) { create(:membership, :with_point_request) }
let!(:group) { membership.group }
let!(:user) { membership.user }
let!(:point_request) { user.point_requests.first }
let!(:evaluation) { point_request.evaluation }
let!(:semester) { Semester.new(evaluation.semester) }
let!(:membership_with_zero_point) { create(:membership, :with_point_request) }
let!(:user_with_zero_point) { membership_with_zero_point.user}

before do
point_request.update!(point: 25)
membership_with_zero_point.update!(group: group)
user_with_zero_point.reload
user_with_zero_point.point_requests.first.update!(evaluation: evaluation)
user_with_zero_point.point_requests.first.update!(point: 0)
evaluation.update!(point_request_status: Evaluation::ACCEPTED)
CalculatePointHistory.new(semester).call
end

it 'has the correct length' do
expect(export.length).to eq(2)
end

it 'first row has the correct headers' do
expect(export[0]).to eq(["PéK id", "Név", "Neptun", "BME-id", "Email", "Felvételi pont", "SVIE tagság", "Körök"])
end

it 'second row has the correct data' do
expect(export[1]).to eq([user.id, user.full_name, user.neptun, user.bme_id, user.email, 25,
user.svie_member_type, evaluation.group.name])
end

it 'user with zero points are not int the export' do
expect(export.none?{|row| row[0] == user_with_zero_point.id }).to be true
end
end

0 comments on commit f85fa66

Please sign in to comment.