From d33d1c5c9fd22144815c6372079485be09503032 Mon Sep 17 00:00:00 2001 From: SepsiLaszlo Date: Sat, 13 Jul 2024 11:27:11 +0200 Subject: [PATCH] add point export --- app/controllers/season_admin_controller.rb | 13 +++++++++- app/services/export_point_history.rb | 28 ++++++++++++++++++++ app/views/season_admin/index.html.erb | 10 +++++++- config/routes.rb | 1 + spec/rails_helper.rb | 1 + spec/services/export_point_history_spec.rb | 30 ++++++++++++++++++++++ 6 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 app/services/export_point_history.rb create mode 100644 spec/services/export_point_history_spec.rb diff --git a/app/controllers/season_admin_controller.rb b/app/controllers/season_admin_controller.rb index bae65b85..47b6a28e 100644 --- a/app/controllers/season_admin_controller.rb +++ b/app/controllers/season_admin_controller.rb @@ -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 diff --git a/app/services/export_point_history.rb b/app/services/export_point_history.rb new file mode 100644 index 00000000..e5450348 --- /dev/null +++ b/app/services/export_point_history.rb @@ -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 diff --git a/app/views/season_admin/index.html.erb b/app/views/season_admin/index.html.erb index 26494cd2..85651750 100644 --- a/app/views/season_admin/index.html.erb +++ b/app/views/season_admin/index.html.erb @@ -23,13 +23,21 @@ <%= label :semester_year, 'Kiválasztott félév : ' %> <%= @semester.to_readable %> + + <% if SystemAttribute.offseason? && PointHistory.exists?(semester: @semester.to_s) %> +
+ <%= 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'} %> +
+ <% end %> + <%= form_tag(seasons_path, class: 'uk-form') do %>
<%= 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' %>
- <%= 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' %>
<% end %> diff --git a/config/routes.rb b/config/routes.rb index a270a79f..72537d6c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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' diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 305af5c3..c53798a6 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -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 diff --git a/spec/services/export_point_history_spec.rb b/spec/services/export_point_history_spec.rb new file mode 100644 index 00000000..6151562a --- /dev/null +++ b/spec/services/export_point_history_spec.rb @@ -0,0 +1,30 @@ +# 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!(:user) { membership.user } + let!(:point_request) { user.point_requests.first } + let!(:evaluation) { point_request.evaluation } + let!(:semester) { Semester.new(evaluation.semester) } + + before do + point_request.update!(point: 25) + 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 +end