module Backend::Cli
Defined in:
backend/cli.cr
Constant Summary
-
API =
Commander::Command.new do |cmd|
cmd.use = "api"
cmd.short = "Mentorenwahl backend API CLI"
cmd.run do
puts(cmd.help)
end
cmd.commands.add do |c|
c.use = "version"
c.short = "Prints version"
c.long = c.short
c.run do
puts(VERSION)
end
end
cmd.commands.add do |c|
c.use = "authors"
c.short = "Prints authors"
c.long = c.short
c.run do
puts(AUTHORS.join("\n"))
end
end
cmd.commands.add do |c|
c.use = "licenses"
c.short = "Prints licenses of projects used by this programs"
c.long = c.short
c.run do
puts(LICENSES)
end
end
cmd.commands.add do |c|
c.use = "run"
c.short = "Run the backend"
c.long = c.short
c.run do
Runner.new.run
end
end
cmd.commands.add do |c|
c.use = "schema"
c.short = "Prints out GraphQL schema"
c.long = c.short
c.run do
puts(Api::Schema::SCHEMA.document.to_s)
end
end
cmd.commands.add do |c|
c.use = "user:import"
c.short = "Imports users from Untis"
c.long = c.short
c.run do
users = Auth.users
users.classes.each do |cl|
c_db = Db::Class.create!({name: cl.name})
cl.students.each do |s|
password = Password.generate(Password::DEFAULT_LEN)
user = Db::User.create!({username: s.username, password: password, initial_password: password, password_changed: false, first_name: s.first_name, last_name: s.last_name, role: Db::UserRole::Student, admin: false})
Db::Student.create!({user_id: user.id, class_id: c_db.id})
end
end
Db::User.import(users.teachers.map do |t|
password = Password.generate(Password::DEFAULT_LEN)
Db::User.new({username: t.username, password: password, initial_password: password, password_changed: false, first_name: t.first_name, last_name: t.last_name, role: Db::UserRole::Teacher, admin: false})
end)
end
end
cmd.commands.add do |cmd|
cmd.use = "user:list"
cmd.short = "Lists all users"
cmd.long = cmd.short
cmd.run do
users = Db::User.query.to_a
table = Tallboy.table do
header(["id", "username", "first_name", "last_name", "role", "admin"])
users.each do |user|
row([user.id, user.username, user.first_name, user.last_name, user.role, user.admin])
end
end
puts(table)
end
end
cmd.commands.add do |cmd|
cmd.use = "user:export"
cmd.short = "Generates report for all users"
cmd.long = cmd.short
cmd.run do
time = Time.local
students = [] of Templates::Users::Student
teachers = [] of Templates::Users::User
Db::User.query.with_student(&.with_class_model).with_teacher.each do |user|
password = user.password_changed ? nil : user.initial_password
case user.role.to_api
in Api::Schema::UserRole::Student
students << Templates::Users::Student.new(class_name: user.student.not_nil!.class_model.name, user: Templates::Users::User.new(first_name: user.first_name, last_name: user.last_name, username: user.username, password: password))
in Api::Schema::UserRole::Teacher
teachers << Templates::Users::User.new(first_name: user.first_name, last_name: user.last_name, username: user.username, password: password)
end
end
html = (Templates::Users.new(time, Db::Class.query.with_students.to_a.map do |cl|
{cl.name, cl.students.count.to_i32}
end, students, teachers)).to_s
puts("Filepath: #{(Auth.generate_pdf(html)).filename}")
end
end
cmd.commands.add do |cmd|
cmd.use = "user:admin <id> <admin>"
cmd.short = "Gives or removed admin rights"
cmd.long = cmd.short
cmd.run do |_opts, args|
user = Db::User.find!(args[0].to_i)
user.admin = args[1].to_b
user.save!
table = Tallboy.table do
header(["id", "username", "first_name", "last_name", "role", "admin"])
row([user.id, user.username, user.first_name, user.last_name, user.role, user.admin])
end
puts(table)
end
end
cmd.commands.add do |cmd|
cmd.use = "user:reset <id>"
cmd.short = "Reset user's password"
cmd.long = cmd.short
cmd.run do |_opts, args|
user = Db::User.find!(args[0].to_i)
puts("Changing password for user:")
table = Tallboy.table do
header(["id", "username", "first_name", "last_name", "role", "admin"])
row([user.id, user.username, user.first_name, user.last_name, user.role, user.admin])
end
puts(table)
print("New password: ")
new_password = gets(chomp: true).not_nil!
puts("New password is: \"#{Regex.escape(new_password)}\"")
print("Reenter new password: ")
if gets(chomp: true).not_nil! == new_password
user.password = new_password
user.password_changed = true
user.save!
Db::Token.query.where do
user_id == user.id
end.to_update.set(active: false).execute
end
end
end
cmd.commands.add do |cmd|
cmd.use = "assignments:export"
cmd.short = "Generates report for all assignments"
cmd.long = cmd.short
cmd.run do
time = Time.local
a_id = (Db::Assignment.query.select(:id)).where do
active
end.first!.id
html = (Templates::Assignments.new(time, Db::Teacher.query.with_student_assignments do |__arg1|
__arg1.where do
assignment_id == a_id
end.with_student do |__arg2|
__arg2.with_user.with_class_model
end
end.to_a.select do |__arg3|
__arg3.student_assignments.count.positive?
end.map do |t|
Templates::Assignments::Assignment.new(Templates::Assignments::User.new(t.user.first_name, t.user.last_name), t.student_assignments.map do |sa|
{user: Templates::Assignments::User.new(sa.student.user.first_name, sa.student.user.last_name), class: sa.student.class_model.name}
end.sort_by! do |sa|
{sa[:class], sa[:user].last_name, sa[:user].first_name}
end)
end.sort_by! do |a|
{a.teacher.last_name, a.teacher.first_name}
end)).to_s
puts("Filepath: #{(Auth.generate_pdf(html)).filename}")
end
end
end
-
WORKER =
Commander::Command.new do |cmd|
cmd.use = "worker"
cmd.short = "Mentorenwahl backend worker CLI"
cmd.run do
puts(cmd.help)
end
cmd.commands.add do |c|
c.use = "run"
c.short = "Start the worker"
c.long = c.short
c.run do
Mosquito::Runner.start
end
end
end