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

Commit

Permalink
Merge pull request #133 from omu/mernisten_getir
Browse files Browse the repository at this point in the history
Mernisten getir
  • Loading branch information
msdundar authored Aug 1, 2018
2 parents 20f2b0c + 853bcf1 commit 32e76ed
Show file tree
Hide file tree
Showing 16 changed files with 88 additions and 112 deletions.
29 changes: 17 additions & 12 deletions app/controllers/account/addresses_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

module Account
class AddressesController < ApplicationController
before_action :set_address, only: %i[edit update destroy mernis]
include LastUpdateFromMernis

before_action :set_address, only: %i[edit update destroy]
before_action :check_formality, only: %i[edit update destroy]
before_action :set_elapsed_time, only: %i[save_from_mernis]

def index
@addresses = current_user.addresses.includes(district: [:city])
Expand All @@ -28,27 +31,29 @@ def destroy
@address.destroy ? redirect_with('success') : redirect_with('warning')
end

def mernis
if (Time.zone.now - @address.updated_at) / 1.day < 7
redirect_with('wait')
else
KpsAddressUpdateJob.perform_later(address)
redirect_with('will_update')
end
def save_from_mernis
KpsAddressSaveJob.perform_later(current_user)
redirect_with('will_update')
end

private

def set_address
@address = current_user.addresses.find(params[:id])
end

def check_formality
redirect_with('warning') if @address.formal?
end

def redirect_with(message)
redirect_to(addresses_path, notice: t(".#{message}"))
def set_elapsed_time
formal_address = current_user.addresses.formal
return if formal_address.blank?
elapsed_time(formal_address)
end

def set_address
@address = current_user.addresses.find(params[:id])
def redirect_with(message)
redirect_to(addresses_path, notice: t(".#{message}"))
end

def address_params
Expand Down
29 changes: 17 additions & 12 deletions app/controllers/account/identities_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

module Account
class IdentitiesController < ApplicationController
before_action :set_identity, only: %i[edit update destroy mernis]
include LastUpdateFromMernis

before_action :set_identity, only: %i[edit update destroy]
before_action :check_formality, only: %i[edit update destroy]
before_action :set_elapsed_time, only: %i[save_from_mernis]

def index
@identities = current_user.identities
Expand All @@ -28,27 +31,29 @@ def destroy
@identity.destroy ? redirect_with('success') : redirect_with('warning')
end

def mernis
if (Time.zone.now - @identity.updated_at) / 1.day < 7
redirect_with('wait')
else
# TODO: KpsIdentityUpdateJob.perform_later(identity)
redirect_with('will_update')
end
def save_from_mernis
KpsIdentitySaveJob.perform_later(current_user)
redirect_with('will_update')
end

private

def set_identity
@identity = current_user.identities.find(params[:id])
end

def check_formality
redirect_with('warning') if @identity.formal?
end

def redirect_with(message)
redirect_to(identities_path, notice: t(".#{message}"))
def set_elapsed_time
formal_identity = current_user.identities.formal
return if formal_identity.blank?
elapsed_time(formal_identity)
end

def set_identity
@identity = current_user.identities.find(params[:id])
def redirect_with(message)
redirect_to(identities_path, notice: t(".#{message}"))
end

def identity_params
Expand Down
10 changes: 10 additions & 0 deletions app/controllers/concerns/last_update_from_mernis.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

module LastUpdateFromMernis
extend ActiveSupport::Concern

def elapsed_time(resource)
elapsed_time = (Time.zone.now - resource.first.updated_at) / 1.day
redirect_with('wait') if elapsed_time.present? && elapsed_time < 7
end
end
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

class KpsAddressCreateJob < ApplicationJob
class KpsAddressSaveJob < ApplicationJob
queue_as :high

# slow operation
Expand All @@ -11,6 +11,8 @@ def perform(user)
# callbacks
after_perform do |job|
district = District.find_by(mernis_code: @response[:district_id])
job.arguments.first.addresses.formal.create(district: district, full_address: @response[:full_address])
address = job.arguments.first.addresses.formal
response = { district: district, full_address: @response[:full_address] }
address.present? ? address.update(response) : address.create(response)
end
end
16 changes: 0 additions & 16 deletions app/jobs/kps_address_update_job.rb

This file was deleted.

6 changes: 2 additions & 4 deletions app/jobs/kps_identity_save_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@ class KpsIdentitySaveJob < ApplicationJob

# 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)
formal_address = job.arguments.first.identities.formal
formal_address.present? ? formal_address.update(response) : formal_address.create(response)
end
end
2 changes: 1 addition & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class User < ApplicationRecord
after_create_commit :build_identity_information, if: proc { identities.formal.empty? }

def build_address_information
KpsAddressCreateJob.perform_later(self)
KpsAddressSaveJob.perform_later(self)
end

def build_identity_information
Expand Down
12 changes: 2 additions & 10 deletions app/views/account/addresses/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<div class="alert alert-light">
<%= link_to_new(new_address_path, t('.new_address')) %>
<%= link_to (@addresses.formal.present? ? t('.update_from_mernis') : t('.create_from_mernis') ), save_from_mernis_addresses_path, class: "btn btn-outline-primary btn-sm" %>

</div>

<div class="row">
Expand Down Expand Up @@ -35,16 +37,6 @@
<td><%= t('.district_and_city') %></td>
<td><%= address.district.name %> / <%= address.district.city.name %></td>
</tr>
<tr>
<td colspan="2">
<% if address.formal? %>
<%= link_to(t('.update_from_mernis'), mernis_address_path(address), class: 'btn btn-outline-primary btn-sm') %>
<% else %>
<%= link_to_edit(edit_address_path(address)) %>
<%= link_to_destroy(address) %>
<% end %>
</td>
</tr>
</table>
</div>
</div>
Expand Down
11 changes: 1 addition & 10 deletions app/views/account/identities/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<div class="alert alert-light">
<%= link_to_new(new_identity_path, t('.new_identity')) %>
<%= link_to (@identities.formal.present? ? t('.update_from_mernis') : t('.create_from_mernis') ), save_from_mernis_identities_path, class: "btn btn-outline-primary btn-sm" %>
</div>

<div class="row">
Expand Down Expand Up @@ -57,16 +58,6 @@
<td><%= t('.registered_to') %></td>
<td><%= identity.registered_to %></td>
</tr>
<tr>
<td colspan="2">
<% if identity.formal? %>
<%= link_to(t('.update_from_mernis'), mernis_identity_path(identity), class: 'btn btn-outline-primary btn-sm') %>
<% else %>
<%= link_to_edit(edit_identity_path(identity)) %>
<%= link_to_destroy(identity) %>
<% end %>
</td>
</tr>
</table>
</div>
</div>
Expand Down
5 changes: 3 additions & 2 deletions config/locales/models/addresses/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ en:
card_header: Address Information
district_and_city: District / City
update_from_mernis: Update from MERNİS
create_from_mernis: Create from MERNIS
new:
form_title: Create Address
edit:
Expand All @@ -46,6 +47,6 @@ en:
destroy:
success: Address successfully deleted!
warning: Address can not be deleted!
mernis:
save_from_mernis:
wait: You recently created or updated your address. You can update your address once every week.
will_update: We got your address update request. In a couple of seconds/minutes you will be able to see your new address.
will_update: We got your address create or update request. In a couple of seconds/minutes you will be able to see your new address.
5 changes: 3 additions & 2 deletions config/locales/models/addresses/tr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ tr:
card_header: Adres Bilgileri
district_and_city: İlçe / İl
update_from_mernis: MERNİS'ten Güncelle
create_from_mernis: MERNİS'ten Oluştur
new:
form_title: Adres Oluştur
edit:
Expand All @@ -46,6 +47,6 @@ tr:
destroy:
success: Adres başarıyla silindi!
warning: Adres silinemedi!
mernis:
save_from_mernis:
wait: Adresinizi yakın bir zamanda oluşturdunuz veya güncellediniz. Adresinizi haftada bir güncelleyebilirsiniz.
will_update: Adres güncelleme talebinizi aldık, bir kaç saniye/dakika içerisinde adresiniz güncellenecek.
will_update: Adres oluşturma veya güncelleme talebinizi aldık, bir kaç saniye/dakika içerisinde adresiniz güncellenecek.
5 changes: 3 additions & 2 deletions config/locales/models/identities/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ en:
new_identity: Add a New Identity
card_header: Identity Information
update_from_mernis: Update from MERNIS
create_from_mernis: Create from MERNIS
edit:
warning: You can not edit your formal identity!
form_title: Edit Identity
Expand All @@ -54,6 +55,6 @@ en:
destroy:
success: Identity successfully deleted!
warning: Identity can not be deleted!
mernis:
save_from_mernis:
wait: You recently created or updated your identity. You can only update your identity once a week from MERNIS.
will_update: We have received your identity update request. Your identity will be updated in a couple of seconds/minutes.
will_update: We have received your identity create or update request. Your identity will be updated in a couple of seconds/minutes.
5 changes: 3 additions & 2 deletions config/locales/models/identities/tr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ tr:
new_identity: Yeni Kimlik Ekle
card_header: Kimlik Bilgileri
update_from_mernis: MERNİS'ten Güncelle
create_from_mernis: MERNİS'ten Oluştur
edit:
warning: Yasal kimlik bilgilerinizi düzenleyemezsiniz!
form_title: Kimliği Düzenle
Expand All @@ -54,6 +55,6 @@ tr:
destroy:
success: Kimlik başarıyla silindi!
warning: Kimlik silinemedi!
mernis:
save_from_mernis:
wait: Kimliğinizi yakın bir zamanda oluşturdunuz veya güncellediniz. Kimliğinizi haftada bir güncelleyebilirsiniz.
will_update: Kimlik güncelleme talebinizi aldık, bir kaç saniye/dakika içerisinde adresiniz güncellenecek.
will_update: Kimlik oluşturma veya güncelleme talebinizi aldık, bir kaç saniye/dakika içerisinde adresiniz güncellenecek.
4 changes: 2 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
# Account home page
scope module: :account do
resources :identities, except: [:show] do
get 'mernis', on: :member
get 'save_from_mernis', on: :collection
end
resources :addresses, except: :show do
get 'mernis', on: :member
get 'save_from_mernis', on: :collection
end
end

Expand Down
51 changes: 18 additions & 33 deletions test/jobs/kps_address_job_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,29 @@
require 'test_helper'

class KpsAddressJobTest < ActiveJob::TestCase
%w[
KpsAddressCreateJob
KpsAddressUpdateJob
].each do |property|
test "can enqueue #{property}" do
assert_enqueued_jobs 0
property.constantize.perform_later(addresses(:formal))
assert_enqueued_jobs 1
end
test 'can enqueue KpsAddressSaveJob' do
assert_enqueued_jobs 0
KpsAddressSaveJob.perform_later(users(:serhat))
assert_enqueued_jobs 1
end

test "can initialize KPS queries as #{property} for given user and address" do
client = Minitest::Mock.new
def client.sorgula
true
end
test 'can initialize KPS queries as KpsAddressSaveJob for given user and address' do
client = Minitest::Mock.new
def client.sorgula
true
end

Services::Kps::Omu::Adres.stub :new, client do
assert property.constantize.perform_later({}) # dummy hash
end
Services::Kps::Omu::Adres.stub :new, client do
assert KpsAddressSaveJob.perform_later({}) # dummy hash
end
end

%w[
KpsAddressCreateJob
KpsAddressUpdateJob
].each do |property|
test "can perform enqueued jobs for #{property}" do
skip 'this block on CircleCI since it needs IP permissions to run.' if ENV['CI']
assert_performed_jobs 0
args = if property.eql?('KpsAddressCreateJob')
users(:serhat)
else
addresses(:formal)
end
perform_enqueued_jobs do
property.constantize.perform_later(args)
end
assert_performed_jobs 1
test 'can perform enqueued jobs for KpsAddressSaveJob' do
skip 'this block on CircleCI since it needs IP permissions to run.' if ENV['CI']
assert_performed_jobs 0
perform_enqueued_jobs do
KpsAddressSaveJob.perform_later(users(:serhat))
end
assert_performed_jobs 1
end
end
4 changes: 2 additions & 2 deletions test/models/user_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ class UserTest < ActiveSupport::TestCase
end

# callback tests
test 'user runs KpsAddressCreateJob after being created' do
assert_enqueued_with(job: KpsAddressCreateJob) do
test 'user runs KpsAddressSaveJob after being created' do
assert_enqueued_with(job: KpsAddressSaveJob) do
User.create(
id_number: '12345678912',
email: '[email protected]',
Expand Down

0 comments on commit 32e76ed

Please sign in to comment.