mentorenwahl/backend/src/backend/worker/jobs/assignment_job.cr

64 lines
2.2 KiB
Crystal
Raw Normal View History

2022-04-14 14:44:49 +00:00
# Mentorenwahl: A fullstack application for assigning mentors to students based on their whishes.
# Copyright (C) 2022 Dominic Grimm
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
module Backend
module Worker
module Jobs
# Assigns students to teachers when all students voted
class AssignStudentsJob < Mosquito::QueuedJob
2022-11-21 18:48:53 +00:00
# run_every 1.minute
def rescheduleable? : Bool
2022-07-28 12:05:10 +00:00
false
end
2022-11-21 18:48:53 +00:00
alias TeacherVote = {student: Int32, priority: Int32}
alias Assignment = {teacher: Int32, priority: Int32}
2022-04-14 14:44:49 +00:00
# :ditto:
def perform : Nil
2022-07-28 12:05:10 +00:00
teacher_count = Db::Teacher.query.count
student_count = Db::Student.query.count
vote_count = Db::Vote.query.count
2022-11-21 18:48:53 +00:00
if teacher_count == 0
2022-04-14 14:44:49 +00:00
log "No teachers found, skipping assignment"
fail
2022-07-28 12:05:10 +00:00
elsif student_count == 0
2022-04-14 14:44:49 +00:00
log "No students found, skipping assignment"
fail
2022-07-28 12:05:10 +00:00
elsif student_count != Db::User.query.where(role: Db::UserRole::Student).count
log "Not all students registered, skipping assignment"
fail
elsif vote_count < student_count
2022-04-14 14:44:49 +00:00
log "Not all students voted, skipping assignment"
fail
2022-11-21 18:48:53 +00:00
elsif Db::Assignment.query.count > 0
log "Assignment has already run, skipping another assignment"
fail
2022-04-14 14:44:49 +00:00
end
2022-11-21 18:48:53 +00:00
teachers = Db::Teacher.query
2022-07-28 12:05:10 +00:00
.where do
2022-11-21 18:48:53 +00:00
raw("EXISTS (SELECT 1 FROM teacher_votes WHERE teacher_id = teachers.id)") &
2022-11-23 19:17:14 +00:00
max_students > 0
2022-11-21 18:48:53 +00:00
end
2022-04-14 14:44:49 +00:00
end
end
end
end
end