Commit 7c43861c authored by Kaspar Vollenweider's avatar Kaspar Vollenweider 👻
Browse files

Merge branch 'feat/semester-process-volunteers-initial-builders-before-creation' into 'develop'

Feat/semester process volunteers initial builders before creation

Closes #459

See merge request !823
parents 070cba91 74074de3
Pipeline #28647 passed with stage
in 35 minutes and 13 seconds
......@@ -6,6 +6,8 @@ class SemesterProcess < ApplicationRecord
inverse_of: 'semester_process_reminder_mail_posted', optional: true
has_many :semester_process_volunteers, dependent: :destroy
accepts_nested_attributes_for :semester_process_volunteers, allow_destroy: true
has_many :volunteers, through: :semester_process_volunteers
has_many :semester_feedbacks, through: :semester_process_volunteers
has_many :hours, through: :semester_process_volunteers
......@@ -24,4 +26,26 @@ class SemesterProcess < ApplicationRecord
# will only return an array, not a AD-result
delegate :missions, to: :semester_process_volunteers
# creates semester date range from string '[year],[semester_number]' e.g. '2018,2'
def semester=(semester)
if semester.is_a?(String)
super(Semester.new(*semester.split(',').map(&:to_i)).current)
else
super(semester)
end
end
def build_semester_volunteers(volunteers, selected = nil)
volunteers = volunteers.where(id: selected) if selected
semester_process_volunteers << volunteers.to_a.map do |volunteer|
spv = SemesterProcessVolunteer.new(volunteer: volunteer, selected: true)
spv.build_missions(semester)
spv
end
end
def build_volunteers_hours_feedbacks_and_mails
semester_process_volunteers.map(&:build_hours_feedbacks_and_mails)
end
end
class SemesterProcessVolunteer < ApplicationRecord
attr_accessor :selected
belongs_to :volunteer
belongs_to :semester_process
delegate :semester, to: :semester_process
delegate :creator, to: :semester_process
belongs_to :responsible, -> { with_deleted }, class_name: 'User',
inverse_of: 'semester_processes', optional: true
belongs_to :reviewed_by, -> { with_deleted }, class_name: 'User',
......@@ -23,6 +28,23 @@ class SemesterProcessVolunteer < ApplicationRecord
# will only return an array, not a AD-result
def missions
assignments + group_assignments
semester_process_volunteer_missions.map(&:mission)
end
def build_missions(semester)
new_missions = volunteer.assignments.active_between(semester.begin, semester.end) +
volunteer.group_assignments.active_between(semester.begin, semester.end)
semester_process_volunteer_missions << new_missions.map do |mission|
SemesterProcessVolunteerMission.new(mission: mission)
end
end
def build_hours_feedbacks_and_mails
missions.each do |mission|
hours << mission.hours.date_between_inclusion(:meeting_date, semester.begin, semester.end)
semester_feedbacks << SemesterFeedback.new(mission: mission, volunteer: mission.volunteer)
end
semester_process_mails << SemesterProcessMail.new(kind: :mail, sent_by: creator)
end
end
......@@ -28,4 +28,60 @@ class SemesterProcessVolunteerTest < ActiveSupport::TestCase
assert_not @subject.reload.reminders.include? mail
assert @subject.reload.reminders.include? reminder
end
test '#build_missions' do
travel_to time_z(2018, 7, 15)
semester = Semester.new
semester_process = SemesterProcess.new(semester: semester.previous, creator: create(:user))
@assignment.update(period_start: nil, period_end: nil)
@group_assignment.update(period_start: nil, period_end: nil)
subject = SemesterProcessVolunteer.new(semester_process: semester_process, volunteer: @volunteer)
subject.build_missions(semester.previous)
assert_equal 0, subject.semester_process_volunteer_missions.size
@assignment.update(period_start: time_z(2015, 7, 15))
subject = SemesterProcessVolunteer.new(semester_process: semester_process, volunteer: @volunteer)
subject.build_missions(semester.previous)
assert_equal 1, subject.semester_process_volunteer_missions.size
@group_assignment.update(period_start: time_z(2018, 7, 1))
subject = SemesterProcessVolunteer.new(semester_process: semester_process, volunteer: @volunteer)
subject.build_missions(semester.previous)
assert_equal 1, subject.semester_process_volunteer_missions.size
@group_assignment.update(period_start: time_z(2018, 7, 1).advance(weeks: -2))
subject = SemesterProcessVolunteer.new(semester_process: semester_process, volunteer: @volunteer)
subject.build_missions(semester.previous)
assert_equal 1, subject.semester_process_volunteer_missions.size
end
test '#build_hours_feedbacks_and_mails' do
travel_to time_z(2018, 7, 15)
semester = Semester.new
semester_process = SemesterProcess.new(semester: semester.previous, creator: create(:user))
@assignment.update(period_start: time_z(2018, 1, 15), period_end: nil)
@group_assignment.update(period_start: time_z(2018, 1, 15), period_end: nil)
hour_assignment = create :hour, hourable: @assignment, meeting_date: time_z(2018, 3, 15),
volunteer: @volunteer
subject = SemesterProcessVolunteer.new(semester_process: semester_process, volunteer: @volunteer)
subject.build_missions(semester.previous)
subject.build_hours_feedbacks_and_mails
assert subject.hours.include? hour_assignment
assert_equal @assignment, subject.semester_feedbacks.find { |fb| fb.assignment_id == @assignment.id }.mission
hour_group_assignment = create :hour, hourable: @group_assignment.group_offer,
meeting_date: time_z(2018, 3, 15), volunteer: @volunteer
subject = SemesterProcessVolunteer.new(semester_process: semester_process, volunteer: @volunteer)
subject.build_missions(semester.previous)
subject.build_hours_feedbacks_and_mails
assert subject.hours.include? hour_assignment
assert subject.hours.include? hour_group_assignment
assert_equal @assignment, subject.semester_feedbacks.find { |fb| fb.assignment_id == @assignment.id }.mission
assert_equal @group_assignment, subject.semester_feedbacks.find { |fb| fb.group_assignment_id == @group_assignment.id }.mission
end
end
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment