add excused field to gw individual scores

- excused boolean and excuse_reason fields
- excuse_reason only visible to crew officers
- include excused in blueprints
This commit is contained in:
Justin Edmund 2025-12-18 23:15:27 -08:00
parent b7aeb2bdfe
commit b3dadf24ef
5 changed files with 33 additions and 11 deletions

View file

@ -52,10 +52,11 @@ module Api
field :crew_scores do |participation|
GwCrewScoreBlueprint.render_as_hash(participation.gw_crew_scores.order(:round))
end
field :individual_scores do |participation|
field :individual_scores do |participation, options|
GwIndividualScoreBlueprint.render_as_hash(
participation.gw_individual_scores.includes(:crew_membership).order(:round),
view: :with_member
view: :with_member,
current_user: options[:current_user]
)
end
end

View file

@ -3,7 +3,12 @@
module Api
module V1
class GwCrewScoreBlueprint < ApiBlueprint
fields :round, :crew_score, :opponent_score, :opponent_name, :opponent_granblue_id, :victory
fields :crew_score, :opponent_score, :opponent_name, :opponent_granblue_id, :victory
# Return round as integer value instead of enum string
field :round do |score|
GwCrewScore.rounds[score.round]
end
end
end
end

View file

@ -3,7 +3,7 @@
module Api
module V1
class GwIndividualScoreBlueprint < ApiBlueprint
fields :round, :score, :is_cumulative
fields :round, :score, :is_cumulative, :excused
field :player_name do |score|
score.player_name
@ -17,6 +17,12 @@ module Api
end
end
# Only return excuse_reason to crew officers
field :excuse_reason do |score, options|
current_user = options[:current_user]
score.excuse_reason if current_user&.crew_officer?
end
view :with_member do
field :member do |score|
if score.crew_membership.present?

View file

@ -24,7 +24,7 @@ module Api
score.recorded_by = current_user
if score.save
render json: GwIndividualScoreBlueprint.render(score, view: :with_member, root: :individual_score), status: :created
render json: GwIndividualScoreBlueprint.render(score, view: :with_member, root: :individual_score, current_user: current_user), status: :created
else
render_validation_error_response(score)
end
@ -37,7 +37,7 @@ module Api
end
if @score.update(score_params.except(:crew_membership_id))
render json: GwIndividualScoreBlueprint.render(@score, view: :with_member, root: :individual_score)
render json: GwIndividualScoreBlueprint.render(@score, view: :with_member, root: :individual_score, current_user: current_user)
else
render_validation_error_response(@score)
end
@ -69,7 +69,7 @@ module Api
score.recorded_by = current_user
if score.save
render json: GwIndividualScoreBlueprint.render(score, view: :with_member, root: :individual_score), status: :created
render json: GwIndividualScoreBlueprint.render(score, view: :with_member, root: :individual_score, current_user: current_user), status: :created
else
render_validation_error_response(score)
end
@ -104,11 +104,11 @@ module Api
end
def score_params
params.require(:individual_score).permit(:crew_membership_id, :round, :score, :is_cumulative)
params.require(:individual_score).permit(:crew_membership_id, :round, :score, :is_cumulative, :excused, :excuse_reason)
end
def score_params_with_player
params.require(:individual_score).permit(:crew_membership_id, :phantom_player_id, :round, :score, :is_cumulative)
params.require(:individual_score).permit(:crew_membership_id, :phantom_player_id, :round, :score, :is_cumulative, :excused, :excuse_reason)
end
def can_record_score_for?(membership_id)
@ -132,6 +132,8 @@ module Api
score.assign_attributes(
score: score_data[:score],
is_cumulative: score_data[:is_cumulative] || false,
excused: score_data[:excused] || false,
excuse_reason: score_data[:excuse_reason],
recorded_by: current_user
)
@ -143,9 +145,9 @@ module Api
end
if errors.empty?
render json: GwIndividualScoreBlueprint.render(results, view: :with_member, root: :individual_scores), status: :created
render json: GwIndividualScoreBlueprint.render(results, view: :with_member, root: :individual_scores, current_user: current_user), status: :created
else
render json: { individual_scores: GwIndividualScoreBlueprint.render_as_hash(results, view: :with_member), errors: errors },
render json: { individual_scores: GwIndividualScoreBlueprint.render_as_hash(results, view: :with_member, current_user: current_user), errors: errors },
status: :multi_status
end
end

View file

@ -0,0 +1,8 @@
# frozen_string_literal: true
class AddExcusedToGwIndividualScores < ActiveRecord::Migration[8.0]
def change
add_column :gw_individual_scores, :excused, :boolean, default: false, null: false
add_column :gw_individual_scores, :excuse_reason, :text
end
end