mentorenwahl/docker/backend/src/backend/worker/jobs/send_teachers_registration_email_job.cr

50 lines
1.7 KiB
Crystal

# 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
# Sends all unregistered teachers a polite registration mail to ask if they may input their data
class SendTeachersRegistrationEmailJob < Mosquito::QueuedJob
# :ditto:
def perform : Nil
users = Db::User.query.where { (x.role == Db::UserRole::Teacher) & (x.teacher_id == nil) }
count = users.count.to_i
channel = Channel(Nil).new(count)
users.each do |user|
spawn do
next unless user.role.to_api.teacher?
ldap_user = Ldap::User.from_username(user.username)
log "Sending teacher registration email to #{ldap_user.email} ##{user.id}"
Mailers::TeacherRegistrationMailer.new(ldap_user).deliver
channel.send(nil)
end
end
count.times do
channel.receive
end
Fiber.yield
end
end
end
end
end