Skip to content

Commit

Permalink
Change definition of a closed schooling (#1026)
Browse files Browse the repository at this point in the history
- [x] we consider that a schooling closed in the future is not closed
anymore
- [x] impact on former scope
- [x] contains also a minor job fix and a stats fix
  • Loading branch information
pskl authored Jul 12, 2024
1 parent e2bafbb commit b8efa4e
Show file tree
Hide file tree
Showing 12 changed files with 45 additions and 14 deletions.
3 changes: 3 additions & 0 deletions app/jobs/janitor_job.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

class JanitorJob < ApplicationJob
sidekiq_options retry: false

def perform
reset_attributive_decision_version_overflow
end
Expand All @@ -11,5 +13,6 @@ def reset_attributive_decision_version_overflow
Schooling.where("attributive_decision_version > ?", 9).find_each do |schooling|
schooling.update!(attributive_decision_version: 9)
end
true
end
end
4 changes: 4 additions & 0 deletions app/jobs/sync/student_schoolings_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ class StudentSchoolingsJob < ApplicationJob
retry_on Faraday::TooManyRequestsError, wait: 1.hour, attempts: 3

def perform(student)
# There are students with no schoolings at all
# Student.where.missing(:schoolings).count
return true if student.establishment.blank?

# XXX: Sygne temporarily disabled see issue:
# https://github.com/orgs/betagouv/projects/71/views/1?pane=issue&itemId=70119483
if (student.establishment.provided_by?(:sygne) && Rails.env.production?) || student.current_schooling.nil?
Expand Down
2 changes: 1 addition & 1 deletion app/models/pfmp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def can_be_modified?
def can_be_destroyed?
return true if latest_payment_request.blank?

latest_payment_request.in_state?(:pending)
latest_payment_request.in_state?(:pending, :incomplete)
end

def stalled_payment_request?
Expand Down
2 changes: 1 addition & 1 deletion app/models/rib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Rib < ApplicationRecord

validates :iban, :bic, :name, presence: true

validates :student_id, uniqueness: { scope: :archived_at }, unless: :archived?
validates :student_id, uniqueness: { scope: :archived_at, message: :unarchivable_rib }, unless: :archived?

scope :multiple_ibans, -> { Rib.select(:iban).group(:iban).having("count(iban) > 1") }

Expand Down
4 changes: 2 additions & 2 deletions app/models/schooling.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Schooling < ApplicationRecord
has_one :establishment, through: :classe

scope :current, -> { where(end_date: nil) }
scope :former, -> { where.not(end_date: nil) }
scope :former, -> { where.not(end_date: nil).where(end_date: ..Date.current) }

scope :with_attributive_decisions, -> { joins(:attributive_decision_attachment) }
scope :without_attributive_decisions, -> { where.missing(:attributive_decision_attachment) }
Expand Down Expand Up @@ -46,7 +46,7 @@ def generate_administrative_number
end

def closed?
end_date.present?
end_date.present? && end_date <= Date.current
end

def abrogated?
Expand Down
2 changes: 1 addition & 1 deletion app/models/stats/main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def bops_data

def menj_academies_data
titles = ["Académie", *indicators_titles]
academies = Establishment.distinct.order(:academy_label).reject(&:excluded?).map(&:academy_label)
academies = Establishment.distinct.order(:academy_label).reject(&:excluded?).map(&:academy_label).uniq

academy_lines = academies.map do |academy|
[
Expand Down
2 changes: 1 addition & 1 deletion config/initializers/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Aplypro
VERSION = "1.17.9"
VERSION = "1.17.10"
end
2 changes: 2 additions & 0 deletions config/locales/fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ fr:
taken: ": cet email est déjà autorisé."
rib:
attributes:
student_id:
unarchivable_rib: "le RIB actuel est en cours d'utilisation pour un paiement"
iban:
sepa: "Le code IBAN ne fait pas partie de la zone SEPA"
pfmp:
Expand Down
4 changes: 2 additions & 2 deletions spec/factories/schoolings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
factory :schooling do
student
classe
start_date { "#{SchoolYear.current.start_year}-08-26" }
start_date { Date.yesterday - 1.month }
status { :student }

trait :with_attributive_decision do
Expand All @@ -25,7 +25,7 @@
end

trait :closed do
end_date { "#{SchoolYear.current.start_year}-08-27" }
end_date { Date.yesterday }
end
end
end
10 changes: 5 additions & 5 deletions spec/models/asp/payment_request_validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
context "when the student transferred and the schooling is abrogated and there is a schooling with attribution" do
before do
schooling = asp_payment_request.schooling
schooling.update!(end_date: Date.parse("#{SchoolYear.current.start_year}-10-10"))
schooling.update!(end_date: Date.yesterday)
AttributiveDecisionHelpers.generate_fake_attributive_decision(schooling)
AttributiveDecisionHelpers.generate_fake_abrogation_decision(schooling)
create(:schooling, :with_attributive_decision, student: asp_payment_request.student)
Expand All @@ -147,12 +147,12 @@
context "when the pfmp dates dont match the schooling" do
before do
schooling = asp_payment_request.schooling
pfmp = create(:pfmp, :validated, day_count: 2, start_date: schooling.end_date + 1.day,
end_date: schooling.end_date + 3.days, schooling: schooling)
schooling.classe = create(:classe, school_year: SchoolYear.find_by!(start_year: 2023))
schooling.save!
pfmp = create(:pfmp, :validated, day_count: 2, start_date: schooling.start_date - 1.day,
end_date: schooling.end_date, schooling: schooling)
asp_payment_request.pfmp = pfmp
asp_payment_request.save!
asp_payment_request.pfmp.update!(start_date: schooling.end_date + 5.days,
end_date: schooling.end_date + 10.days)
end

it "adds an error" do
Expand Down
2 changes: 1 addition & 1 deletion spec/models/rib_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
it { is_expected.to validate_presence_of(:iban) }
it { is_expected.to validate_presence_of(:bic) }
it { is_expected.to validate_presence_of(:name) }
it { is_expected.to validate_uniqueness_of(:student_id).scoped_to(:archived_at) }
it { is_expected.to validate_uniqueness_of(:student_id).scoped_to(:archived_at).with_message(:unarchivable_rib) }

context "when the IBAN is from outside the SEPA zone" do
subject(:rib) { build(:rib, iban: Faker::Bank.iban(country_code: "br")) }
Expand Down
22 changes: 22 additions & 0 deletions spec/models/schooling_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,22 @@
end
end

describe "#closed?" do
subject(:schooling) { create(:schooling, end_date: Date.tomorrow) }

it "returns false" do
expect(schooling.closed?).to be false
end

context "when the schooling is closed in the past" do
subject(:schooling) { create(:schooling, end_date: Date.yesterday) }

it "returns true" do
expect(schooling.closed?).to be true
end
end
end

describe ".former" do
subject { described_class.former }

Expand All @@ -154,6 +170,12 @@
it { is_expected.to include schooling }
end

context "when the schooling has an end_date in the future" do
let(:schooling) { create(:schooling, start_date: Date.yesterday, end_date: Date.tomorrow) }

it { is_expected.not_to include schooling }
end

context "when the schooling is active" do
let(:schooling) { create(:schooling) }

Expand Down

0 comments on commit b8efa4e

Please sign in to comment.