Skip to content

Commit

Permalink
Merge pull request #672 from betagouv/main
Browse files Browse the repository at this point in the history
v1.13
  • Loading branch information
freesteph authored Apr 4, 2024
2 parents 9a942cb + dc94cc6 commit c51ee96
Show file tree
Hide file tree
Showing 46 changed files with 500 additions and 186 deletions.
39 changes: 34 additions & 5 deletions app/jobs/fetch_student_information_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,41 @@ def perform(schooling)
api
.fetch_student_data!(student.ine)
.then do |data|
mapper = api.info_mapper.new(data)
mapper = api.info_mapper.new(data, establishment.uai)

if mapper.attributes.present?
student.assign_attributes(mapper.attributes)
student.save!
end
update_student!(schooling, mapper)
update_schooling!(mapper)
end
rescue Faraday::ResourceNotFound
schooling.student.update!(lost: true)
end

def update_student!(schooling, mapper)
return if mapper.attributes.blank?

schooling.student.update!(mapper.attributes)
end

def update_schooling!(mapper)
schooling = find_schooling(mapper)

return if schooling.nil?

attributes = mapper
.schooling_attributes
.slice(*Schooling.attribute_names.map(&:to_sym))

schooling.update!(attributes)
end

def find_schooling(mapper)
mapper.schooling_finder_attributes => { uai:, label:, mef_code:, ine: }

Schooling
.joins(:establishment, :student, [classe: :mef])
.where("establishments.uai" => uai)
.where("classe.label" => label)
.where("mef.code" => mef_code)
.find_by("student.ine" => ine)
end
end
28 changes: 27 additions & 1 deletion app/models/asp/payment_request_state_machine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,26 @@ class PaymentRequestStateMachine
ASP::StudentFileEligibilityChecker.new(request.student).ready?
end

guard_transition(to: :ready) do |request|
request.schooling.student?
end

guard_transition(to: :ready) do |request|
!request.student.rib.reused?
end

guard_transition(to: :ready) do |request|
request.student.rib.valid?
end

guard_transition(to: :ready) do |request|
request.pfmp.valid?
end

guard_transition(to: :ready) do |request|
!request.student.lost
end

guard_transition(to: :ready) do |request|
!request.student.adult_without_personal_rib?
end
Expand All @@ -46,7 +66,13 @@ class PaymentRequestStateMachine
end

guard_transition(to: :ready) do |request|
request.pfmp.schooling.attributive_decision.attached?
request.schooling.attributive_decision.attached?
end

guard_transition(to: :ready) do |request|
request.pfmp.duplicates.none? do |pfmp|
pfmp.in_state?(:validated)
end
end

guard_transition(from: :ready, to: :sent) do |request|
Expand Down
11 changes: 2 additions & 9 deletions app/models/establishment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ def directors
"telephone" => :telephone,
"mail" => :email,
"code_type_contrat_prive" => :private_contract_type_code,
"ministere_tutelle" => :ministry
"ministere_tutelle" => :ministry,
"code_departement" => :department_code
}.freeze

# Find all codes here : https://infocentre.pleiade.education.fr/bcn/workspace/viewTable/n/N_CONTRAT_ETABLISSEMENT
Expand All @@ -66,14 +67,6 @@ def select_label
[uai, name].compact.join(" - ")
end

def department_code
if postal_code.start_with?("97")
postal_code.first(3)
else
postal_code.first(2)
end
end

def address
[address_line1, address_line2, postal_code, city].join(", ")
end
Expand Down
14 changes: 13 additions & 1 deletion app/models/pfmp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@ class Pfmp < ApplicationRecord

validates :end_date, :start_date, inclusion: Aplypro::SCHOOL_YEAR_RANGE

validates :day_count, numericality: { only_integer: true, allow_nil: true, greater_than: 0 }
validates :day_count,
numericality: {
only_integer: true,
allow_nil: true,
greater_than: 0,
less_than_or_equal_to: ->(pfmp) { (pfmp.end_date - pfmp.start_date).to_i }
}

scope :finished, -> { where("pfmps.end_date <= (?)", Time.zone.today) }

Expand Down Expand Up @@ -106,4 +112,10 @@ def can_be_modified?
def payment_due?
day_count.present?
end

def duplicates
student.pfmps.excluding(self).select do |other|
other.start_date == start_date && other.end_date == end_date
end
end
end
8 changes: 8 additions & 0 deletions app/models/rib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,12 @@ def active?
def inactive?
!active?
end

def reused?
siblings.any?
end

def siblings
Rib.where(iban: iban).excluding(self)
end
end
2 changes: 2 additions & 0 deletions app/models/schooling.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
class Schooling < ApplicationRecord
has_one_attached :attributive_decision

enum :status, { student: 0, apprentice: 1, other: 2 }, validate: { allow_nil: true }

belongs_to :student
belongs_to :classe

Expand Down
1 change: 1 addition & 0 deletions app/models/student.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class Student < ApplicationRecord
scope :asp_ready, lambda {
where(biological_sex: [1, 2])
.where.not(address_postal_code: nil)
.where.not(lost: true)
.where.not(address_country_code: %w[995 990] + [nil])
.where.not(birthplace_country_insee_code: %w[995 990] + [nil])
.where.not(
Expand Down
9 changes: 7 additions & 2 deletions app/models/student/info_mappers/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,20 @@
class Student
module InfoMappers
class Base
attr_reader :payload
attr_reader :payload, :uai

def initialize(payload)
def initialize(payload, uai)
@payload = payload
@uai = uai
end

def attributes
self.class::Mapper.new.call(payload)
end

def schooling_attributes
self.class::SchoolingMapper.new.call(payload)
end
end
end
end
37 changes: 37 additions & 0 deletions app/models/student/info_mappers/fregata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,43 @@ class AddressMapper < Dry::Transformer::Pipe
]
end
end

class SchoolingMapper < Dry::Transformer::Pipe
import Dry::Transformer::HashTransformations
import Dry::Transformer::ArrayTransformations

define! do
deep_symbolize_keys

unwrap :statutApprenant
unwrap :apprenant

rename_keys(
code: :status
)

map_value :status, lambda { |value|
case value
when "2503"
:apprentice
when "2501"
:student
else
raise Student::Mappers::Errors::SchoolingParsingError
end
}

accept_keys %i[status ine]
end
end

# FREGATA uses the same endpoint for listing and extra info so
# we can reuse the other mappers
def schooling_finder_attributes
schooling_attributes
.merge(Student::Mappers::Fregata::ClasseMapper.new.call(payload))
.merge(uai: uai)
end
end
end
end
41 changes: 41 additions & 0 deletions app/models/student/info_mappers/sygne.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,47 @@ class Mapper < Dry::Transformer::Pipe
]
end
end

class SchoolingMapper < Dry::Transformer::Pipe
import Dry::Transformer::HashTransformations
import Dry::Transformer::Coercions

define! do
deep_symbolize_keys

unwrap :scolarite

rename_keys(
classe: :label,
codeStatut: :status,
codeMefRatt: :mef_code,
codeUai: :uai
)

map_value(:mef_code, Dry::Transformer::Coercions[:to_string])

map_value :mef_code, ->(value) { value.chop }

map_value :status, lambda { |value|
case value
when "ST"
:student
when "AP"
:apprentice
when "FQ"
:other
else
raise Student::Mappers::Errors::SchoolingParsingError
end
}

accept_keys %i[ine mef_code label status uai]
end
end

def schooling_finder_attributes
schooling_attributes
end
end
end
end
5 changes: 4 additions & 1 deletion app/models/student/mappers/fregata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class ClasseMapper < Dry::Transformer::Pipe
def map_student_attributes(attrs)
student_attrs = super(attrs)

extra_attrs = Student::InfoMappers::Fregata.new(attrs).attributes
extra_attrs = Student::InfoMappers::Fregata.new(attrs, uai).attributes

student_attrs.merge!(extra_attrs) if extra_attrs.present?

Expand All @@ -51,7 +51,10 @@ def map_student_attributes(attrs)
def map_schooling!(classe, student, entry)
schooling = Schooling.find_or_initialize_by(classe: classe, student: student)

schooling_attributes = Student::InfoMappers::Fregata.new(entry, uai).schooling_attributes

schooling.end_date = left_classe_at(entry)
schooling.status = schooling_attributes[:status]

student.close_current_schooling! if schooling.open? && student.current_schooling != schooling

Expand Down
2 changes: 1 addition & 1 deletion app/services/asp/mappers/prestadoss_mapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def montanttotalengage
end

def valeur
schooling.establishment.department_code.rjust(3, "0")
schooling.establishment.department_code
end

def id_prestation_dossier
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.12.1"
VERSION = "1.13"
end
3 changes: 3 additions & 0 deletions config/locales/fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ fr:
format: "La date de fin %{message}"
inclusion: ne peut pas excéder l'année scolaire en cours
greater_than_or_equal_to: "doit être ultérieure à la date de début"
day_count:
format: "Le %{attribute} %{message}"
less_than_or_equal_to: "n'est pas cohérent avec les dates de début et de fin"
hints:
rib:
name: "Noms et prénoms du titulaire du compte"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class AddDepartmentCodeToEstablishments < ActiveRecord::Migration[7.1]
def change
add_column :establishments, :department_code, :string
end
end
7 changes: 7 additions & 0 deletions db/migrate/20240402160524_add_status_to_schooling.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class AddStatusToSchooling < ActiveRecord::Migration[7.1]
def change
add_column :schoolings, :status, :integer
end
end
7 changes: 7 additions & 0 deletions db/migrate/20240403123520_add_lost_attribute_to_students.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class AddLostAttributeToStudents < ActiveRecord::Migration[7.1]
def change
add_column :students, :lost, :boolean, null: false, default: false
end
end
5 changes: 4 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c51ee96

Please sign in to comment.