Skip to content
This repository has been archived by the owner on Jun 16, 2021. It is now read-only.

Commit

Permalink
Create KpsIdentitySaveJob close #92
Browse files Browse the repository at this point in the history
* Create KpsIdentityUpdateJob

* Add test for KpsIdentityUpdateJob

* Merge KpsIdentityCreateJob and KpsIdentityUpdateJob jobs with name KpsIdentityCreateOrUpdate

* Fix rubocop offenses

* Rename KpsIdentityCreateOrUpdateJob to KpsIdentitySaveJob

* Clean test codes

* Remove unused variables

* Rename data to response
  • Loading branch information
sinansh authored and isubas committed Jul 24, 2018
1 parent b322c5a commit 350c383
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 23 deletions.
17 changes: 0 additions & 17 deletions app/jobs/kps_identity_create_job.rb

This file was deleted.

20 changes: 20 additions & 0 deletions app/jobs/kps_identity_save_job.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion app/models/accounts/student.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def build_address_information
end

def build_identity_information
KpsIdentityCreateJob.perform_later(self)
KpsIdentitySaveJob.perform_later(self)
end

# custom methods
Expand Down
31 changes: 31 additions & 0 deletions test/jobs/kps_identity_job_test.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions test/models/accounts/student_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test/models/user_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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: '[email protected]',
Expand Down

0 comments on commit 350c383

Please sign in to comment.