Skip to content

Commit

Permalink
Merge pull request #292 from kortirso/issue_289
Browse files Browse the repository at this point in the history
IS-289 added disable transfers for finishing season
  • Loading branch information
kortirso authored Apr 17, 2024
2 parents 8120508 + e240922 commit 8f6eb34
Show file tree
Hide file tree
Showing 30 changed files with 106 additions and 43 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
## Unreleased
### Added
- editing cups rounds for admins
- disable transfers for finishing season

### Fixed
- bug with creating oracul
Expand Down
1 change: 1 addition & 0 deletions app/contracts/season_contract.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ class SeasonContract < ApplicationContract

params do
required(:name).filled(:string)
required(:status).filled(:string)
end
end
10 changes: 3 additions & 7 deletions app/controllers/admin/seasons_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def new
end

def create
# commento: seasons.name, seasons.status
case create_form.call(params: season_params)
in { errors: errors } then redirect_to new_admin_season_path, alert: errors
else redirect_to admin_seasons_path, notice: t('controllers.admin.seasons.create.success')
Expand All @@ -29,7 +30,7 @@ def find_seasons
@seasons =
Season.joins(:league)
.order(id: :desc)
.hashable_pluck(:id, :uuid, :name, :active, 'leagues.name')
.hashable_pluck(:id, :uuid, :name, :status, 'leagues.name')
.group_by { |season| season[:leagues_name] }
end

Expand All @@ -38,12 +39,7 @@ def find_leagues
end

def season_params
params
.require(:season)
.permit(:name, :league_id)
.to_h
.symbolize_keys
.merge(active: to_bool.call(params[:season][:active]))
params.require(:season).permit(:name, :league_id, :status)
end
end
end
2 changes: 1 addition & 1 deletion app/controllers/api/v1/seasons_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def seasons
expires_in: 24.hours,
race_condition_ttl: 10.seconds
) do
SeasonSerializer.new(Season.active.order(id: :desc), params: serializer_fields).serializable_hash
SeasonSerializer.new(Season.in_progress.order(id: :desc), params: serializer_fields).serializable_hash
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/draft_players_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def show; end
def find_seasons
@seasons =
Rails.cache.fetch('draft_players_show_seasons_v3', expires_in: 4.hours, race_condition_ttl: 10.seconds) do
Season.active.or(Season.coming)
Season.in_progress.or(Season.coming)
.joins(:league)
.hashable_pluck(:id, :uuid, :start_at, :name, :updated_at, 'leagues.slug', 'leagues.sport_kind')
end
Expand Down
7 changes: 7 additions & 0 deletions app/controllers/fantasy_teams/transfers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class TransfersController < ApplicationController

before_action :find_fantasy_team
before_action :find_season, only: %i[show]
before_action :validate_season_finishing, only: %i[show]
before_action :find_lineup, only: %i[show]
before_action :check_season_maintenance, only: %i[show]
before_action :set_watchable_players, only: %i[show]
Expand Down Expand Up @@ -38,6 +39,12 @@ def find_season
@season = @fantasy_team.season
end

def validate_season_finishing
return if @season.open_transfers?

page_not_found
end

def find_lineup
@lineup = @fantasy_team.lineups.joins(:week).where(weeks: { status: Week::COMING }).first
end
Expand Down
9 changes: 8 additions & 1 deletion app/controllers/fantasy_teams_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class FantasyTeamsController < ApplicationController
include Maintenable

before_action :find_fantasy_team, only: %i[show update]
before_action :validate_season_finishing, only: %i[show]
before_action :find_fantasy_team_relationships, only: %i[show]
before_action :find_season, only: %i[create]
before_action :check_season_maintenance, only: %i[show]
Expand Down Expand Up @@ -47,13 +48,19 @@ def find_fantasy_team
@fantasy_team = Current.user.fantasy_teams.find_by!(uuid: params[:id])
end

def validate_season_finishing
return if @fantasy_team.season.open_transfers?

page_not_found
end

def find_fantasy_team_relationships
@lineup = @fantasy_team.lineups.joins(:week).where(weeks: { status: Week::COMING }).first
@season = @fantasy_team.season
end

def find_season
@season = Season.active.find_by!(uuid: params[:season_id])
@season = Season.in_progress.find_by!(uuid: params[:season_id])
end

def fantasy_team_params
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/seasons/best_players_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def index
private

def find_season
@season = Season.active.find_by!(uuid: params[:season_id])
@season = Season.in_progress.find_by!(uuid: params[:season_id])
end

def find_week
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/seasons/players_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def show
private

def find_season
@season = Season.active.find_by!(uuid: params[:season_id])
@season = Season.in_progress.find_by!(uuid: params[:season_id])
end

def find_season_players
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/teams_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def index
private

def find_teams
@teams = Season.active.find_by!(uuid: params[:season_uuid]).teams
@teams = Season.in_progress.find_by!(uuid: params[:season_uuid]).teams
end

def teams
Expand Down
6 changes: 1 addition & 5 deletions app/forms/seasons/create_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@ def call(params:)
errors = validator.call(params: params)
return { errors: errors } if errors.any?

result = ActiveRecord::Base.transaction do
# commento: seasons.name, seasons.active
league.seasons.active.update!(active: false) if params[:active]
league.seasons.create!(params)
end
result = league.seasons.create!(params)

{ result: result }
end
Expand Down
2 changes: 1 addition & 1 deletion app/jobs/injuries/import_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class ImportJob < ApplicationJob
queue_as :default

def perform(import_service: Injuries::ImportService.new)
Season.active.each do |season|
Season.in_progress.each do |season|
import_service.call(season: season)
end
end
Expand Down
4 changes: 2 additions & 2 deletions app/jobs/scheduler_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def perform

def start_seasons
# start season with updating first week as coming
Season.active.where('start_at < ? AND start_at > ?', 15.minutes.after, 15.minutes.ago).each do |season|
Season.in_progress.where('start_at < ? AND start_at > ?', 15.minutes.after, 15.minutes.ago).each do |season|
season.weeks.inactive.order(position: :asc).first.update(status: Week::COMING)
end
end
Expand All @@ -29,7 +29,7 @@ def import_games
# fetch game statistics 1 time, 3 hours after game start and no points before
# game started at 5:00 will be fetched at 8:00
# game started at 5:30 will be fetched at 8:30
Season.active.hashable_pluck(:id, :main_external_source).each do |season|
Season.in_progress.hashable_pluck(:id, :main_external_source).each do |season|
game_ids =
Game
.joins(:week)
Expand Down
2 changes: 1 addition & 1 deletion app/models/league.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class League < ApplicationRecord
include Sportable

has_many :seasons, dependent: :destroy
has_one :active_season, -> { Season.active }, class_name: 'Season', foreign_key: :league_id # rubocop: disable Rails/HasManyOrHasOneDependent
has_one :active_season, -> { Season.in_progress }, class_name: 'Season', foreign_key: :league_id # rubocop: disable Rails/HasManyOrHasOneDependent

has_many :cups, dependent: :destroy
end
23 changes: 21 additions & 2 deletions app/models/season.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ class Season < ApplicationRecord
include Placeable
include Likeable

INACTIVE = 'inactive'
ACTIVE = 'active'
FINISHING = 'finishing'
FINISHED = 'finished'

belongs_to :league

has_many :seasons_teams, class_name: 'Seasons::Team', foreign_key: :season_id, dependent: :destroy
Expand All @@ -26,6 +31,20 @@ class Season < ApplicationRecord
has_many :players, through: :players_seasons
has_many :injuries, through: :players_seasons

scope :active, -> { where(active: true) }
scope :coming, -> { where(active: false).where.not(start_at: nil) }
scope :in_progress, -> { where(status: [ACTIVE, FINISHING]) }
scope :coming, -> { where(status: INACTIVE).where.not(start_at: nil) }

enum status: { INACTIVE => 0, ACTIVE => 1, FINISHING => 2, FINISHED => 3 }

def in_progress?
[ACTIVE, FINISHING].include?(status)
end

def open_transfers?
[INACTIVE, ACTIVE].include?(status)
end

def closed_transfers?
[FINISHING, FINISHED].include?(status)
end
end
5 changes: 4 additions & 1 deletion app/services/games/update_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,10 @@ def add_data_for_games_player_update(games_player, points, statistic)
end

def update_players
@players_seasons_mass_update_job.perform_later(season_id: @game.week.season_id, player_ids: @player_ids)
@players_seasons_mass_update_job.perform_later(
season_id: @game.week.season_id,
player_ids: @player_ids.sort
)
end
end
end
2 changes: 1 addition & 1 deletion app/views/components/page_wrappers/page_component.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<% if @fantasy_team&.completed? %>
<div class="flex-1 flex xl:border-l xl:border-stone-300 pl-4">
<%= link_to t('components.views.shared.fantasy_team_navigation_component.points'), fantasy_team_points_path(@fantasy_team.uuid), class: "user-navigation-link #{'active' if current_page?(fantasy_team_points_path(@fantasy_team.uuid))}" %>
<% if @fantasy_team.user_id == Current.user.id %>
<% if @fantasy_team.user_id == Current.user.id && season_in_progress? %>
<%= link_to t('components.views.shared.fantasy_team_navigation_component.team'), fantasy_team_path(@fantasy_team.uuid), class: "user-navigation-link #{'active' if current_page?(fantasy_team_path(@fantasy_team.uuid))}" %>
<%= link_to t('components.views.shared.fantasy_team_navigation_component.transfers'), fantasy_team_transfers_path(@fantasy_team.uuid), class: "user-navigation-link #{'active' if current_page?(fantasy_team_transfers_path(@fantasy_team.uuid))}" %>
<% end %>
Expand Down
4 changes: 4 additions & 0 deletions app/views/components/page_wrappers/page_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ def global_user_oraculs
.hashable_pluck(:uuid, :name, :placeable_id, :placeable_type)
end

def season_in_progress?
@fantasy_team.season.open_transfers?
end

private

def background_urls
Expand Down
2 changes: 1 addition & 1 deletion app/views/controllers/admin/seasons/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<tr>
<td><%= season[:id] %></td>
<td><%= season[:name] %></td>
<td><%= season[:active] %></td>
<td><%= season[:status] %></td>
<td>
<%= link_to t('views.admin.seasons.index.weeks'), admin_weeks_path(season_id: season[:id]), class: 'btn-primary btn-small' %>
<%= link_to t('views.admin.seasons.index.games'), admin_season_games_path(season[:uuid]), class: 'btn-primary btn-small' %>
Expand Down
4 changes: 2 additions & 2 deletions app/views/controllers/admin/seasons/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
<%= form.text_field :name, class: 'form-value' %>
</div>
<div class="form-field">
<%= form.label :active, t('views.admin.seasons.new.active'), class: 'form-label' %>
<%= form.check_box :active, class: 'form-value' %>
<%= form.label :status, t('views.admin.seasons.new.active'), class: 'form-label' %>
<%= form.select :status, options_for_select(Season.statuses.keys), class: 'form-value' %>
</div>
<div class="form-field">
<%= form.label :league_id, t('views.admin.seasons.new.league_id'), class: 'form-label' %>
Expand Down
9 changes: 9 additions & 0 deletions db/migrate/20240417155344_add_finished_to_seasons.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class AddFinishedToSeasons < ActiveRecord::Migration[7.1]
def change
add_column :seasons, :status, :integer, null: false, default: 0

Season.find_each do |season|
season.update!(status: season.active? ? Season::ACTIVE : Season::INACTIVE)
end
end
end
4 changes: 3 additions & 1 deletion db/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1692,7 +1692,8 @@ CREATE TABLE public.seasons (
start_at timestamp(6) without time zone,
members_count integer DEFAULT 1 NOT NULL,
main_external_source character varying,
maintenance boolean DEFAULT false NOT NULL
maintenance boolean DEFAULT false NOT NULL,
status integer DEFAULT 0 NOT NULL
);


Expand Down Expand Up @@ -3270,6 +3271,7 @@ ALTER TABLE ONLY public.kudos_achievements
SET search_path TO "$user", public;

INSERT INTO "schema_migrations" (version) VALUES
('20240417155344'),
('20240329062740'),
('20240327093404'),
('20240327061925'),
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/admin/seasons_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@

context 'for valid params' do
let(:request) {
post :create, params: { season: { name: 'En', league_id: league.id }, locale: 'en' }
post :create, params: { season: { name: 'En', status: 'active', league_id: league.id }, locale: 'en' }
}

it 'creates season', :aggregate_failures do
Expand Down
10 changes: 10 additions & 0 deletions spec/controllers/fantasy_teams/transfers_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@

expect(response).to render_template :show
end

context 'for finishing season' do
before { fantasy_team.season.update!(status: Season::FINISHING) }

it 'renders 404 page' do
get :show, params: { fantasy_team_id: fantasy_team.uuid, locale: 'en' }

expect(response).to render_template 'shared/404'
end
end
end
end

Expand Down
10 changes: 10 additions & 0 deletions spec/controllers/fantasy_teams_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@
expect(response).to render_template :show
end
end

context 'for finishing season' do
before { fantasy_team.season.update!(status: Season::FINISHING) }

it 'renders 404 page' do
get :show, params: { id: fantasy_team.uuid, locale: 'en' }

expect(response).to render_template 'shared/404'
end
end
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/factories/seasons.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
FactoryBot.define do
factory :season do
name { '2021/2022' }
active { true }
status { Season::ACTIVE }
league
end
end
10 changes: 4 additions & 6 deletions spec/forms/seasons/create_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,22 @@

let!(:instance) { described_class.new }
let!(:league) { create :league }
let!(:season) { create :season, league: league, active: true }

context 'for invalid params' do
let(:params) { { name: '', active: true, league_id: league.id } }
let(:params) { { name: '', status: Season::ACTIVE, league_id: league.id } }

it 'does not create Season', :aggregate_failures do
it 'does not create season', :aggregate_failures do
expect { form }.not_to change(Season, :count)
expect(form[:errors]).not_to be_blank
end
end

context 'for valid params' do
let(:params) { { name: 'New', active: true, league_id: league.id } }
let(:params) { { name: 'New', status: Season::ACTIVE, league_id: league.id } }

it 'creates Season', :aggregate_failures do
it 'creates season', :aggregate_failures do
expect { form }.to change(league.seasons, :count).by(1)
expect(form[:errors]).to be_blank
expect(season.reload.active).to be_falsy
end
end
end
4 changes: 2 additions & 2 deletions spec/jobs/injuries/import_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
describe Injuries::ImportJob do
subject(:job_call) { described_class.perform_now(import_service: import_service) }

let!(:season1) { create :season, active: true }
let!(:season2) { create :season, active: false }
let!(:season1) { create :season, status: Season::ACTIVE }
let!(:season2) { create :season, status: Season::INACTIVE }
let(:import_service) { instance_double(Injuries::ImportService, call: nil) }

it 'imports injuries', :aggregate_failures do
Expand Down
Loading

0 comments on commit 8f6eb34

Please sign in to comment.