From b3dadf24ef669be7bf0bcba8d8ddc9c18d5cd359 Mon Sep 17 00:00:00 2001 From: Justin Edmund Date: Thu, 18 Dec 2025 23:15:27 -0800 Subject: [PATCH] add excused field to gw individual scores - excused boolean and excuse_reason fields - excuse_reason only visible to crew officers - include excused in blueprints --- .../api/v1/crew_gw_participation_blueprint.rb | 5 +++-- app/blueprints/api/v1/gw_crew_score_blueprint.rb | 7 ++++++- .../api/v1/gw_individual_score_blueprint.rb | 8 +++++++- .../api/v1/gw_individual_scores_controller.rb | 16 +++++++++------- ...200954_add_excused_to_gw_individual_scores.rb | 8 ++++++++ 5 files changed, 33 insertions(+), 11 deletions(-) create mode 100644 db/migrate/20251217200954_add_excused_to_gw_individual_scores.rb diff --git a/app/blueprints/api/v1/crew_gw_participation_blueprint.rb b/app/blueprints/api/v1/crew_gw_participation_blueprint.rb index 725675d..18ea211 100644 --- a/app/blueprints/api/v1/crew_gw_participation_blueprint.rb +++ b/app/blueprints/api/v1/crew_gw_participation_blueprint.rb @@ -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 diff --git a/app/blueprints/api/v1/gw_crew_score_blueprint.rb b/app/blueprints/api/v1/gw_crew_score_blueprint.rb index b9c659e..5300d27 100644 --- a/app/blueprints/api/v1/gw_crew_score_blueprint.rb +++ b/app/blueprints/api/v1/gw_crew_score_blueprint.rb @@ -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 diff --git a/app/blueprints/api/v1/gw_individual_score_blueprint.rb b/app/blueprints/api/v1/gw_individual_score_blueprint.rb index d024d8b..241d9d4 100644 --- a/app/blueprints/api/v1/gw_individual_score_blueprint.rb +++ b/app/blueprints/api/v1/gw_individual_score_blueprint.rb @@ -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? diff --git a/app/controllers/api/v1/gw_individual_scores_controller.rb b/app/controllers/api/v1/gw_individual_scores_controller.rb index bd335fe..cdb0130 100644 --- a/app/controllers/api/v1/gw_individual_scores_controller.rb +++ b/app/controllers/api/v1/gw_individual_scores_controller.rb @@ -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 diff --git a/db/migrate/20251217200954_add_excused_to_gw_individual_scores.rb b/db/migrate/20251217200954_add_excused_to_gw_individual_scores.rb new file mode 100644 index 0000000..a1a6be7 --- /dev/null +++ b/db/migrate/20251217200954_add_excused_to_gw_individual_scores.rb @@ -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