Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mgf 645 import des donnees sygne #366

Open
wants to merge 10 commits into
base: review
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
217 changes: 110 additions & 107 deletions Gemfile.lock

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions app/jobs/import_data_from_sygne_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class ImportDataFromSygneJob < ActiveJob::Base
queue_as :default

def perform(school)
omogen = Services::Sygne::Omogen.new
omogen.sygne_import_by_schools(school.code_uai)
end
end
173 changes: 0 additions & 173 deletions app/libs/services/omogen.rb
Original file line number Diff line number Diff line change
@@ -1,173 +0,0 @@
module Services
# Manage Captcha services
class Omogen
def net_synchro
uri = URI(ENV['NET_SYNCHRO_URL'])

http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true if uri.scheme == 'https'

request = Net::HTTP::Get.new(uri.request_uri)
request['Authorization'] = "Bearer #{@token}"
request['Code-Application'] = 'FRE'
request['Code-RNE'] = '0595121W'
request['Compression-Zip'] = 'non'
request['Contexte-Annee-Scolaire'] = '2021'
request['Perimetre-Applicatif'] = 'A09'

response = http.request(request)

case response
when Net::HTTPSuccess
JSON.parse(response.body)
else
raise "Failed to get netsynchro resource : #{response.message}"
end
end

def sygne
uri = URI(ENV['SYGNE_URL'])

http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true if uri.scheme == 'https'

request = Net::HTTP::Get.new(uri.request_uri)
request['Authorization'] = "Bearer #{@token}"
request['Code-Application'] = 'FRE'
request['Code-RNE'] = '0595121W'
request['Compression-Zip'] = 'non'
end

def sygne_status
uri = URI(ENV['SYGNE_URL'] + '/version')
puts uri

http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true if uri.scheme == 'https'

request = Net::HTTP::Get.new(uri.request_uri)
request['Authorization'] = "Bearer #{@token}"

response = http.request(request)

case response
when Net::HTTPSuccess
puts response.body
response.body
else
puts response
puts response.body
puts response.code
puts response.message

raise "Failed to get sygne status : #{response.message}"
end
end

# Sygne eleves
# {
# "ine"=>"001291528AA",
# "nom"=>"SABABADICHETTY",
# "prenom"=>"Felix",
# "dateNaissance"=>"2003-05-28",
# "codeSexe"=>"1",
# "codeUai"=>"0590116F",
# "anneeScolaire"=>2023,
# "niveau"=>"2212",
# "libelleNiveau"=>"1ERE G-T",
# "codeMef"=>"20110019110",
# "libelleLongMef"=>"PREMIERE GENERALE",
# "codeMefRatt"=>"20110019110",
# "classe"=>"3E4",
# "codeRegime"=>"2",
# "libelleRegime"=>"DP DAN",
# "codeStatut"=>"ST",
# "libelleLongStatut"=>"SCOLAIRE",
# "dateDebSco"=>"2023-09-05",
# "adhesionTransport"=>false
# }

def sygne_eleves
uri = URI(ENV['SYGNE_URL'] + '/etablissements/0590116F/eleves')
puts uri

http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true if uri.scheme == 'https'

request = Net::HTTP::Get.new(uri.request_uri)
request['Authorization'] = "Bearer #{@token}"
request['Compression-Zip'] = 'non'

puts 'request', request
puts 'uri', uri
puts '----'

response = http.request(request)

case response
when Net::HTTPSuccess
puts response.body
JSON.parse(response.body)
else
puts response
puts response.body
raise "Failed to get sygne eleves : #{response.message}"
end
end

def sygne_responsables
# http://{context-root}/sygne/api/v{version.major}/eleves/{ine}/responsables + queryParams
uri = URI(ENV['SYGNE_URL'] + '/eleves/001291528AA/responsables')
puts uri

http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true if uri.scheme == 'https'

request = Net::HTTP::Get.new(uri.request_uri)
request['Authorization'] = "Bearer #{@token}"
request['Compression-Zip'] = 'non'

puts 'request', request
puts 'uri', uri
puts '----'

response = http.request(request)

case response
when Net::HTTPSuccess
JSON.parse(response.body).map do |responsable|
{
name: responsable['nomFamille'],
first_name: responsable['prenom'],
email: responsable['email'],
phone: responsable['telephonePersonnel'],
address: format_address(responsable['adrResidenceResp']),
level: responsable['codeNiveauResponsabilite'],
sexe: responsable['codeCivilite'] == '1' ? 'M' : 'F'
}
end
else
puts response
raise "Failed to get sygne eleves : #{response.message}"
end
end

def format_address(address_hash)
"#{address_hash['adresseLigne1']}, #{address_hash['adresseLigne2']} #{address_hash['codePostal']} #{address_hash['libelleCommune']}"
end

def get_oauth_token
uri = URI(ENV['OMOGEN_OAUTH_URL'])
response = Net::HTTP.post_form(uri, {
grant_type: 'client_credentials',
client_id: ENV['OMOGEN_CLIENT_ID'],
client_secret: ENV['OMOGEN_CLIENT_SECRET']
})
JSON.parse(response.body)['access_token']
end

def initialize
@token = get_oauth_token
end
end
end
186 changes: 186 additions & 0 deletions app/libs/services/sygne/omogen.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
module Services::Sygne
# Manage Captcha services
class Omogen
# MEFSTAT4_CODES=%w[2114 ,2122,2434,2115,2381 (2nde pro ?),2116,2117, 2121,2211,2433]
MEFSTAT4_CODES = %w[2114 2122 2434 2115 2116 2117 2121 2211 2433]
def net_synchro
uri = URI(ENV['NET_SYNCHRO_URL'])

http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true if uri.scheme == 'https'

request = Net::HTTP::Get.new(uri.request_uri)
request['Authorization'] = "Bearer #{@token}"
request['Code-Application'] = 'FRE'
request['Code-RNE'] = '0595121W'
request['Compression-Zip'] = 'non'
request['Contexte-Annee-Scolaire'] = '2021'
request['Perimetre-Applicatif'] = 'A09'

response = http.request(request)

case response
when Net::HTTPSuccess
JSON.parse(response.body)
else
raise "Failed to get netsynchro resource : #{response.message}"
end
end

def sygne
uri = URI(ENV['SYGNE_URL'])

http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true if uri.scheme == 'https'

request = Net::HTTP::Get.new(uri.request_uri)
request['Authorization'] = "Bearer #{@token}"
request['Code-Application'] = 'FRE'
request['Code-RNE'] = '0595121W'
request['Compression-Zip'] = 'non'
end

def sygne_status
uri = URI(ENV['SYGNE_URL'] + '/version')
puts uri

http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true if uri.scheme == 'https'

request = Net::HTTP::Get.new(uri.request_uri)
request['Authorization'] = "Bearer #{@token}"

response = http.request(request)

case response
when Net::HTTPSuccess
puts response.body
response.body
else
puts response
puts response.body
puts response.code
puts response.message

raise "Failed to get sygne status : #{response.message}"
end
end

# Sygne eleves
# {
# "ine"=>"001291528AA",
# "nom"=>"SABABADICHETTY",
# "prenom"=>"Felix",
# "dateNaissance"=>"2003-05-28",
# "codeSexe"=>"1",
# "codeUai"=>"0590116F",
# "anneeScolaire"=>2023,
# "niveau"=>"2212",
# "libelleNiveau"=>"1ERE G-T",
# "codeMef"=>"20110019110",
# "libelleLongMef"=>"PREMIERE GENERALE",
# "codeMefRatt"=>"20110019110",
# "classe"=>"3E4",
# "codeRegime"=>"2",
# "libelleRegime"=>"DP DAN",
# "codeStatut"=>"ST",
# "libelleLongStatut"=>"SCOLAIRE",
# "dateDebSco"=>"2023-09-05",
# "adhesionTransport"=>false
# }

def sygne_import_by_schools(code_uai = '0590116F')
MEFSTAT4_CODES.each do |niveau|
students = sygne_eleves(code_uai, niveau: niveau)
students.each do |student|
student.make_student
end
end
end

def sygne_eleves(code_uai = '0590116F', niveau: '2211')
students = []
uri = URI("#{ENV['SYGNE_URL']}/etablissements/#{code_uai}/eleves?niveau=#{niveau}")

response = sygne_eleves_request(uri)
case response
when Net::HTTPSuccess
# puts response.body
# puts JSON.parse(response.body)
data_student = JSON.parse(response.body, symbolize_names: true)
if data_student.fetch(:codeMef, false) && Grade.code_mef_ok?(code_mef: data_student[:codeMef])
students << SygneEleve.new(data_student)
end
when Net::HTTPNotFound
puts response.body
Rails.logger.error "Failed to get sygne eleves : #{response.message}"
end
students
end

def sygne_responsable(ine = '001291528AA')
response = sygne_responsables_request(ine)
case response
when Net::HTTPSuccess
responsibles = JSON.parse(response.body, symbolize_names: true)
return nil unless responsibles.is_a?(Array) && responsibles.any?

responsibles.sort_by! { |responsible| responsible[:codeNiveauResponsabilite] }
SygneResponsible.new(responsibles.first)
else
puts response
raise "Failed to get sygne eleves : #{response.message}"
end
end

def get_oauth_token
uri = URI(ENV['OMOGEN_OAUTH_URL'])
response = Net::HTTP.post_form(uri, {
grant_type: 'client_credentials',
client_id: ENV['OMOGEN_CLIENT_ID'],
client_secret: ENV['OMOGEN_CLIENT_SECRET']
})
JSON.parse(response.body)['access_token']
end

def initialize
@token = get_oauth_token
end

private

def sygne_eleves_request(uri)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true if uri.scheme == 'https'

request = Net::HTTP::Get.new(uri.request_uri)
request['Authorization'] = "Bearer #{@token}"
request['Compression-Zip'] = 'non'

# puts 'request', request
puts 'uri', uri
puts '----'

response = http.request(request)
end

def sygne_responsables_request(ine = '001291528AA')
# http://{context-root}/sygne/api/v{version.major}/eleves/{ine}/responsables + queryParams
uri = URI("#{ENV['SYGNE_URL']}/eleves/#{ine}/responsables")
puts uri

http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true if uri.scheme == 'https'

request = Net::HTTP::Get.new(uri.request_uri)
request['Authorization'] = "Bearer #{@token}"
request['Compression-Zip'] = 'non'

puts 'request', request
puts 'uri', uri
puts '----'

http.request(request)
end
end
end
Loading
Loading