diff --git a/app/jobs/kps_identity_create_job.rb b/app/jobs/kps_identity_create_job.rb deleted file mode 100644 index c0013be17..000000000 --- a/app/jobs/kps_identity_create_job.rb +++ /dev/null @@ -1,17 +0,0 @@ -# frozen_string_literal: true - -class KpsIdentityCreateJob < ApplicationJob - queue_as :high - - # slow operation - def perform(user, student_id = nil) - @user = user - @student_id = student_id - @response = Services::Kps::Omu::Kimlik.new.sorgula(user.id_number.to_i) - end - - # callbacks - after_perform do |_job| - @user.identities.formal.create(@response.merge(student_id: @student_id)) - end -end diff --git a/app/jobs/kps_identity_save_job.rb b/app/jobs/kps_identity_save_job.rb new file mode 100644 index 000000000..5574d1dfc --- /dev/null +++ b/app/jobs/kps_identity_save_job.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +class KpsIdentitySaveJob < ApplicationJob + queue_as :high + + # slow operation + def perform(user, student_id = nil) + @user = user + @student_id = student_id + @response = Services::Kps::Omu::Kimlik.new.sorgula(user.id_number.to_i) + end + + # callbacks + after_perform do |_job| + response = @response.merge(student_id: @student_id) + formal = @user.identities.formal + # Eğer kişinin formal kimliği varsa; kimlik bilgilerini güncelle, yoksa formal kimlik oluştur. + formal.present? ? formal.update(response) : formal.create(response) + end +end diff --git a/app/models/accounts/student.rb b/app/models/accounts/student.rb index cc49805a9..97ea0b805 100644 --- a/app/models/accounts/student.rb +++ b/app/models/accounts/student.rb @@ -17,6 +17,6 @@ class Student < ApplicationRecord after_create_commit :build_identity_information, if: proc { identity.nil? } def build_identity_information - KpsIdentityCreateJob.perform_later(user, id) + KpsIdentitySaveJob.perform_later(user, id) end end diff --git a/app/models/user.rb b/app/models/user.rb index 7612fb2da..605dd8e4c 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -42,7 +42,7 @@ def build_address_information end def build_identity_information - KpsIdentityCreateJob.perform_later(self) + KpsIdentitySaveJob.perform_later(self) end # custom methods diff --git a/test/jobs/kps_identity_job_test.rb b/test/jobs/kps_identity_job_test.rb new file mode 100644 index 000000000..779a9f92b --- /dev/null +++ b/test/jobs/kps_identity_job_test.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +require 'test_helper' + +class KpsIdentityJobTest < ActiveJob::TestCase + test 'can enqueue KpsIdentitySaveJob' do + assert_enqueued_jobs 0 + KpsIdentitySaveJob.perform_later(users(:serhat)) + assert_enqueued_jobs 1 + end + + test 'can initialize KPS queries as KpsIdentitySaveJob for given user and identities' do + client = Minitest::Mock.new + def client.sorgula + true + end + + Services::Kps::Omu::Kimlik.stub :new, client do + assert KpsIdentitySaveJob.perform_later({}) # dummy hash + end + end + + test 'can perform enqueued jobs for KpsIdentitySaveJob' do + skip 'this block on CircleCI since it needs IP permissions to run.' if ENV['CI'] + assert_performed_jobs 0 + perform_enqueued_jobs do + KpsIdentitySaveJob.perform_later(users(:serhat)) + end + assert_performed_jobs 1 + end +end diff --git a/test/models/accounts/student_test.rb b/test/models/accounts/student_test.rb index 0e4d42ecc..d684207a3 100644 --- a/test/models/accounts/student_test.rb +++ b/test/models/accounts/student_test.rb @@ -37,8 +37,8 @@ class StudentTest < ActiveSupport::TestCase end # callback tests - test 'student runs KpsIdentityCreateJob after being created' do - assert_enqueued_with(job: KpsIdentityCreateJob) do + test 'student runs KpsIdentitySaveJob after being created' do + assert_enqueued_with(job: KpsIdentitySaveJob) do Student.create(student_number: '1234', user: users(:serhat), unit: units(:omu)) end end diff --git a/test/models/user_test.rb b/test/models/user_test.rb index ef9beef1a..fb8b9c19b 100644 --- a/test/models/user_test.rb +++ b/test/models/user_test.rb @@ -95,8 +95,8 @@ class UserTest < ActiveSupport::TestCase end end - test 'user runs KpsIdentityCreateJob after being created' do - assert_enqueued_with(job: KpsIdentityCreateJob) do + test 'user runs KpsIdentitySaveJob after being created' do + assert_enqueued_with(job: KpsIdentitySaveJob) do User.create( id_number: '98765432198', email: 'anotherfakeuser@fakemail.com',