Skip to content

Commit

Permalink
added job for fetching order blocs
Browse files Browse the repository at this point in the history
  • Loading branch information
OlegPhenomenon committed Oct 16, 2023
1 parent 06465c2 commit 4545833
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 29 deletions.
43 changes: 43 additions & 0 deletions app/jobs/fetch_godaddy_bsa_block_order_list_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# frozen_string_literal: true

# rubocop:disable Metrics

class FetchGodaddyBsaBlockOrderListJob < ApplicationJob
queue_as :default

LIMIT = 20
LIMIT_MESSAGE = 'Limit reached. No more block orders to fetch'

def perform
fetch_block_order_list(offset: 0)
end

def fetch_block_order_list(offset:)
res = Bsa::BlockOrderListService.call(offset: offset, limit: LIMIT)
return res.error.inspect unless res.result?
return LIMIT_MESSAGE if res.body.total.zero? || res.body.list.blank?

bsa_attributes = collect_bsa_values(res)
BsaProtectedDomain.upsert_all(bsa_attributes)
offset_limit = res.body.total / LIMIT
return LIMIT_MESSAGE if offset >= offset_limit

offset += 1
fetch_block_order_list(offset: offset)
end

def collect_bsa_values(res)
res.body.list.map do |block_order|
{
order_id: block_order['blockOrder']['blockOrderId'],
suborder_id: block_order['blockSubOrderId'],
domain_name: "#{block_order['label']}#{block_order['tld']['displayName']}",
state: block_order['blockOrderStatus']['blockOrderStatusId'],
registration_code: SecureRandom.hex,
create_date: DateTime.parse(block_order['createdDt']),
created_at: Time.zone.now,
updated_at: Time.zone.now
}
end
end
end
1 change: 1 addition & 0 deletions app/models/bsa_protected_domain.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
class BsaProtectedDomain < ApplicationRecord
# validate :suborder_id, uniqueness: true, presence: true
end
21 changes: 11 additions & 10 deletions app/services/bsa/download_non_blocked_name_list_service.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# frozen_string_literal: true

module Bsa
class DownloadNonBlockedNameListService
include ApplicationService
include Core::TokenHelper

attr_reader :suborder_id, :filename

def self.call(suborder_id:, filename: Time.now.strftime("%Y-%m-%d_%H-%M-%S"))
def self.call(suborder_id:, filename: Time.now.strftime('%Y-%m-%d_%H-%M-%S'))
new(suborder_id: suborder_id, filename: filename).call
end

Expand All @@ -18,16 +20,15 @@ def call
http = connect(url: base_url)
response = http.get(endpoint, headers.merge(token_format(token)))

File.open("#{filename}.csv", 'wb') do |file|
file.write(response.body)
end

puts '----'
puts response.inspect
puts '-------'
if [OK, ACCEPTED].include? response.code
File.open("#{filename}.csv", 'wb') do |file|
file.write(response.body)
end

# TODO: finish with response
# struct_response(response)
Struct.new(:result?, :body).new(true, OpenStruct.new(message: "Data was added to #{filename}.csv file"))
else
Struct.new(:result?, :error).new(false, OpenStruct.new(message: response.message, code: response.code))
end
end

private
Expand Down
8 changes: 4 additions & 4 deletions db/migrate/20231003073022_create_bsa_protected_domains.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
class CreateBsaProtectedDomains < ActiveRecord::Migration[6.1]
def change
create_table :bsa_protected_domains do |t|
t.integer :order_id, null: false
t.integer :suborder_id, null: false
t.string :order_id, null: false
# t.string :suborder_id, null: false, index: { unique: true, name: 'unique_suborder_id' }
t.string :suborder_id, null: false
t.string :domain_name, null: false
t.integer :state, null: false, default: 0
t.string :registration_code, null: false
t.datetime :create_date
t.datetime :update_date

# t.timestamps
t.timestamps
end
end
end
7 changes: 4 additions & 3 deletions db/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -544,13 +544,14 @@ ALTER SEQUENCE public.bounced_mail_addresses_id_seq OWNED BY public.bounced_mail

CREATE TABLE public.bsa_protected_domains (
id bigint NOT NULL,
order_id integer NOT NULL,
suborder_id integer NOT NULL,
order_id character varying NOT NULL,
suborder_id character varying NOT NULL,
domain_name character varying NOT NULL,
state integer DEFAULT 0 NOT NULL,
registration_code character varying NOT NULL,
create_date timestamp without time zone,
update_date timestamp without time zone
created_at timestamp(6) without time zone NOT NULL,
updated_at timestamp(6) without time zone NOT NULL
);


Expand Down
2 changes: 1 addition & 1 deletion test/services/bsa/block_order_list_service_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def test_for_failed_block_order_list
end

def test_parse_query_parameters
instance_serive = Bsa::BlockOrderListService.new(sort_by: 'createdAt', order: 'desc', offset: 0, limit: 100, q: { 'tld' => 'test' })
instance_serive = Bsa::BlockOrderListService.new(sort_by: 'createdBy', order: 'desc', offset: 0, limit: 100, q: { 'tld' => 'test' })

result = instance_serive.send(:query_string)

Expand Down
24 changes: 13 additions & 11 deletions test/services/bsa/block_order_view_service_test.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
# frozen_string_literal: true

require 'test_helper'

RESPONSE = {
"list": [
"label1",
"label2",
"label3",
"label4",
"label5"
"list": %w[
label1
label2
label3
label4
label5
],
"offset": 0,
"limit": 5,
"count": 5,
"total": 12
}
}.freeze

INVALID_RESPONSE = {
INVALID_RESPONSE = {
"message": 'Unsupported Media Type',
"description": 'The server is refusing to service the request because the entity of the request is in a format' \
' not supported by the requested resource for the requested method'
}
' not supported by the requested resource for the requested method'
}.freeze

class Bsa::BlockOrderViewServiceTest < ActiveSupport::TestCase
setup do
Expand Down Expand Up @@ -75,4 +77,4 @@ def stub_succesfull_request(token)
headers: { 'Content-Type' => 'application/json' }
)
end
end
end
58 changes: 58 additions & 0 deletions test/services/bsa/download_non_blocked_name_list_service_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# frozen_string_literal: true

require 'test_helper'

RESPONSE = 'some,csv,data'

class Bsa::BlockOrderViewServiceTest < ActiveSupport::TestCase
setup do
@filename = 'mock-csv'
token = generate_test_bsa_token(Time.zone.now + 20.minute)
stub_succesfull_request(token)
end

def teardown
File.delete("#{@filename}.csv") if File.exist?("#{@filename}.csv")
end

def test_for_succesfull_downloaded_non_blocked_name
stub_request(:get, 'https://api-ote.bsagateway.co/bsa/api/blockrsporder/1/nonblockednames')
.to_return(
status: 200,
body: RESPONSE.to_json,
headers: { 'Content-Type' => 'text/csv',
'Content-Disposition' => 'attachment; filename="mock-csv.csv"' }
)

result = Bsa::DownloadNonBlockedNameListService.call(suborder_id: 1, filename: @filename)

assert File.exist?("#{@filename}.csv")
assert_equal RESPONSE, File.read("#{@filename}.csv").gsub('"', '')
assert result.result?
assert_equal "Data was added to #{@filename}.csv file", result.body.message
end

def test_for_failed_downloaded_non_blocked_name
stub_request(:get, 'https://api-ote.bsagateway.co/bsa/api/blockrsporder/1/nonblockednames')
.to_return(
status: 404,
headers: { 'Content-Type' => 'application/json' }
)

result = Bsa::DownloadNonBlockedNameListService.call(suborder_id: 1, filename: @filename)

refute File.exist?("#{@filename}.csv")
refute result.result?
end

private

def stub_succesfull_request(token)
stub_request(:post, 'https://api-ote.bsagateway.co/iam/api/authenticate/apiKey')
.to_return(
status: 200,
body: { id_token: token }.to_json,
headers: { 'Content-Type' => 'application/json' }
)
end
end

0 comments on commit 4545833

Please sign in to comment.