竞赛接口调整

dev_sync_trustie
cxt 5 years ago
parent 797fdb5f3c
commit 9a02763c83

@ -20,7 +20,7 @@ class Admins::CompetitionSettingsController < Admins::BaseController
end end
def basic_form_params def basic_form_params
params.permit(:identifier, :name, :sub_title, :start_time, :end_time, :mode, :identifier, :bonus, :awards_count, :description, :course_id, :teach_start_time, :teach_end_time) params.permit(:identifier, :name, :sub_title, :sponsor_schools, :region_schools, :start_time, :end_time, :mode, :identifier, :bonus, :awards_count, :description, :course_id, :teach_start_time, :teach_end_time)
end end
def nav_form_params def nav_form_params

@ -7,6 +7,7 @@ class Competitions::StudentsController < Competitions::BaseController
end end
students = User.joins(:user_extension).where(status: 1, user_extensions: { identity: 1 }) students = User.joins(:user_extension).where(status: 1, user_extensions: { identity: 1 })
students = students.where(user_extensions: { school_id: current_competition.region_schools.pluck(:school_id) }) if current_competition.region_schools.size > 0
students = students.where.not(id: params[:student_ids]) if params[:student_ids].present? students = students.where.not(id: params[:student_ids]) if params[:student_ids].present?
students = students.where('LOWER(CONCAT(lastname, firstname, login, nickname)) LIKE ?', "%#{keyword}%") students = students.where('LOWER(CONCAT(lastname, firstname, login, nickname)) LIKE ?', "%#{keyword}%")
@students = students.includes(user_extension: :school).limit(20) @students = students.includes(user_extension: :school).limit(20)

@ -22,6 +22,9 @@ class Competition < ApplicationRecord
has_many :attachments, as: :container, dependent: :destroy has_many :attachments, as: :container, dependent: :destroy
has_many :competition_awards, dependent: :destroy has_many :competition_awards, dependent: :destroy
has_many :competition_schools, dependent: :destroy
has_one :sponsor_schools, -> { where(source: :sponsor) }, class_name: 'CompetitionSchool' # 主办方
has_one :region_schools, -> { where(source: :region) }, class_name: 'CompetitionSchool' # 开放范围
after_create :create_competition_modules after_create :create_competition_modules
@ -79,6 +82,12 @@ class Competition < ApplicationRecord
team_members.exists?(user_id: user.id) team_members.exists?(user_id: user.id)
end end
# 是否开放
def open?(user)
user_school_id = user.user_extension&.school_id.to_i
competition.region_schools.size == 0 || competition.region_schools.exists?(school_id: user_school_id)
end
# 是否禁止教师报名 # 是否禁止教师报名
def teacher_enroll_forbidden? def teacher_enroll_forbidden?
teacher_staff.blank? || teacher_staff.maximum.zero? teacher_staff.blank? || teacher_staff.maximum.zero?

@ -0,0 +1,5 @@
class CompetitionSchool < ApplicationRecord
# source sponsor: 主办方, region开放范围
belongs_to :competition
belongs_to :school
end

@ -20,6 +20,7 @@ class Admins::CompetitionBasicSettingService < ApplicationService
competition.save! competition.save!
# 竞赛模式相关设置
if competition.mode == 1 || competition.mode == 4 if competition.mode == 1 || competition.mode == 4
competition.competition_mode_setting&.destroy competition.competition_mode_setting&.destroy
else else
@ -33,6 +34,23 @@ class Admins::CompetitionBasicSettingService < ApplicationService
setting.save! setting.save!
end end
# 主办方设置
new_school_ids = (params[:sponsor_schools] || []) - competition.sponsor_schools.pluck(:school_id)
delete_school_ids = competition.sponsor_schools.pluck(:school_id) - (params[:sponsor_schools] || [])
new_school_ids.each do |school_id|
CompetitionSchool.create!(competition_id: competition.id, school_id: school_id, source: 'sponsor')
end
competition.sponsor_schools.where(school_id: delete_school_ids).destroy_all
# 开放范围设置
new_region_school_ids = (params[:region_schools] || []) - competition.region_schools.pluck(:school_id)
delete_region_school_ids = competition.region_schools.pluck(:school_id) - (params[:region_schools] || [])
new_region_school_ids.each do |school_id|
CompetitionSchool.create!(competition_id: competition.id, school_id: school_id, source: 'region')
end
competition.region_schools.where(school_id: delete_region_school_ids).destroy_all
competition competition
end end
end end

@ -11,6 +11,8 @@ class Competitions::CreatePersonalTeamService < ApplicationService
def call def call
raise Error, '个人赛才能报名' unless competition.personal? raise Error, '个人赛才能报名' unless competition.personal?
raise Error, '本竞赛只对指定单位/学校开放' unless competition.open?(user)
is_teacher = user.is_teacher? is_teacher = user.is_teacher?
raise Error, '本竞赛的参赛者限定为:学生' if is_teacher && competition.teacher_enroll_forbidden? raise Error, '本竞赛的参赛者限定为:学生' if is_teacher && competition.teacher_enroll_forbidden?
raise Error, '本竞赛的参赛者限定为:教师' if !is_teacher && competition.member_enroll_forbidden? raise Error, '本竞赛的参赛者限定为:教师' if !is_teacher && competition.member_enroll_forbidden?

@ -13,6 +13,8 @@ class Competitions::JoinTeamService < ApplicationService
invite_code = params[:invite_code].to_s.strip invite_code = params[:invite_code].to_s.strip
raise Error, '战队邀请码不能为空' if invite_code.blank? raise Error, '战队邀请码不能为空' if invite_code.blank?
raise Error, '本竞赛只对指定单位/学校开放' unless competition.open?(user)
is_teacher = user.is_teacher? is_teacher = user.is_teacher?
raise Error, '本竞赛的参赛者限定为:学生' if is_teacher && competition.teacher_enroll_forbidden? raise Error, '本竞赛的参赛者限定为:学生' if is_teacher && competition.teacher_enroll_forbidden?
raise Error, '本竞赛的参赛者限定为:教师' if !is_teacher && competition.member_enroll_forbidden? raise Error, '本竞赛的参赛者限定为:教师' if !is_teacher && competition.member_enroll_forbidden?

@ -0,0 +1,11 @@
class CreateCompetitionSchools < ActiveRecord::Migration[5.2]
def change
create_table :competition_schools do |t|
t.references :competition, index: true
t.references :school, index: true
t.string :source
t.timestamps
end
end
end

@ -0,0 +1,5 @@
require 'rails_helper'
RSpec.describe CompetitionSchool, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end
Loading…
Cancel
Save