generated from betagouv/rails-template
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
# frozen_string_literal: true | ||
|
||
OUTSIDE_CONTRACT = [ | ||
%w[0382170C 2472000831], | ||
%w[0690652J 2472340732], | ||
%w[0690652J 2472340733], | ||
%w[0690652J 2412010122], | ||
%w[0690652J 2412344721], | ||
%w[0690652J 2412344722], | ||
%w[0690652J 2412521921], | ||
%w[0690652J 2412521922], | ||
%w[0690652J 2412543422], | ||
%w[0691875N 2473360333], | ||
%w[0691875N 2473360531], | ||
%w[0691875N 2473360632], | ||
%w[0691875N 2413361521], | ||
%w[0691875N 2413361522], | ||
%w[0442083A 2473121131], | ||
%w[0442083A 2473121332], | ||
%w[0442083A 2473121333], | ||
%w[0442227G 2403320511], | ||
%w[0910838S 2473000433], | ||
%w[0910838S 2473121432] | ||
].freeze | ||
|
||
PRIVATE_HEALTH_ESTABLISHMENTS = %w[0541769E 0010212A 0930075B].freeze | ||
|
||
class Student | ||
scope :lives_in_france, -> { where(address_country_code: %w[100 99100]) } | ||
end | ||
|
||
def select_perfect_pfmps | ||
perfect = [] | ||
|
||
Pfmp | ||
.joins(Pfmp.most_recent_transition_join) | ||
.joins(:payment_requests, :schooling, :establishment, student: :rib) | ||
.where(start_date: Aplypro::SCHOOL_YEAR_RANGE, end_date: Aplypro::SCHOOL_YEAR_RANGE) | ||
.order(end_date: :asc) | ||
.merge(Schooling.with_attributive_decisions) | ||
.merge(Student.lives_in_france) | ||
.where.not("establishments.uai": PRIVATE_HEALTH_ESTABLISHMENTS) | ||
.in_state(:validated) | ||
.preload(:schooling, student: :rib) | ||
.find_in_batches | ||
.with_index do |pfmps, index| | ||
puts "looking for perfect PFMPs in batch number #{index} (have #{perfect.count} perfect PFMPs so far)" | ||
|
||
ready = pfmps | ||
.select(&:valid?) | ||
.reject { |pfmp| outside_contract?(pfmp) } | ||
.reject { |pfmp| needs_abrogated_da?(pfmp) } | ||
.each { |pfmp| grab_missing_status(pfmp) } | ||
.select { |pfmp| pfmp.payment_requests.last.can_transition_to?(:ready) } | ||
|
||
perfect.concat(ready) | ||
|
||
break if perfect.count > 1000 | ||
end | ||
|
||
puts "all good! pfmps: #{perfect.count}" | ||
end | ||
|
||
def outside_contract?(pfmp) | ||
OUTSIDE_CONTRACT.any? { |uai, mef| pfmp.establishment.uai == uai && pfmp.mef.code == mef } | ||
end | ||
|
||
def grab_missing_status(pfmp) | ||
return if pfmp.schooling.status.present? | ||
|
||
puts "fetching student information again..." | ||
FetchStudentInformationJob.perform_now(pfmp.schooling) | ||
puts "done." | ||
end | ||
|
||
def needs_abrogated_da?(pfmp) | ||
pfmp.student.pfmps.joins(:mef).select(:"mefs.id").distinct.count > 1 && | ||
pfmp.student.schoolings.joins(:classe).select(:"establishment_id").distinct.count > 1 | ||
|
||
# # plusieurs schoolings même MEF dans le même étab = plusieurs DA = bon quand même | ||
# pfmp.student.attributive_decisions.uniq.many? | ||
end |