item_builder 0.1.38 → 0.1.42

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1e5978f4671ec45e2e71e9e2656b7b40e13a48a7beca282c25149963066958e3
4
- data.tar.gz: b89bb4ccea8a89fd7d456c8b45283bf3917c0d290fad4eeb773235456a031764
3
+ metadata.gz: 5e9332c89e3fb06f1d60cbc7b01f9247491900d73e5d7a36a1103cf3bf37f25a
4
+ data.tar.gz: 3add3dd0a79aa29e537c617fe54388dc81cdf5d07439e8b6b6bbca1ac6c3afba
5
5
  SHA512:
6
- metadata.gz: 4462f8fa85ec85e6b9960b6114431b30372eb0df207e57257a42f7d7fcbc55c76b38d92e2c978bf202ec389491d819b5ae8eacc12898a3130a14b81929d6143e
7
- data.tar.gz: 3ef0ce28611e19beb65a84540d732ed3034f7ec59310a52975dc75add648a6e584945e61dc6c32f9ac2f53ce13456bfddde9118d9af075d1ea65c98fdacd2815
6
+ metadata.gz: 8219fd536e925ec65bfdeda6eab8d79a953f1a038e3820e44f9c405186dbc83b560c187792fdf4f73856a6fe77fdb431d14bf48d696c5dcd17818258279c9a82
7
+ data.tar.gz: 41555eb31879b5691467407e95dd88cfbd1b3c9125f2f3e7c955aca932d64a26cbab899216b249b59e66a3e2650bb70e5f19fe7ff7a1fcaca4e7db0a7cf29d09
data/lib/item_builder.rb CHANGED
@@ -10,7 +10,7 @@ require 'item_builder/modes/active_service'
10
10
  require 'item_models'
11
11
  require 'item_builder/zilingo_quantity_service'
12
12
  require 'item_builder/zalora_quantity_service'
13
-
13
+ require 'item_builder/shopify_quantity_service'
14
14
  class ItemBuilder
15
15
  def self.build(listing_ids, mode)
16
16
  new(listing_ids, mode).mode_check
@@ -47,6 +47,11 @@ class ItemBuilder
47
47
  new_param = qty_simple_params(listing)
48
48
  .merge(zalora_reserved_stock: zalora_reserved_stock)
49
49
 
50
+ modes[mode].new(new_param).perform
51
+ elsif listing.channel_id == 2
52
+ new_param = qty_simple_params(listing)
53
+ .merge({shopify_inventory_location: shopify_inventory_location})
54
+
50
55
  modes[mode].new(new_param).perform
51
56
  else
52
57
  modes[mode].new(qty_simple_params(listing)).perform
@@ -144,6 +149,12 @@ class ItemBuilder
144
149
  ).perform
145
150
  end
146
151
 
152
+ def shopify_inventory_location
153
+ @shopify_inventory_location ||= ItemBuilder::ShopifyQuantityService.new(
154
+ listings: listings, skus: skus
155
+ ).perform
156
+ end
157
+
147
158
  def modes
148
159
  {
149
160
  price: ItemBuilder::Modes::PriceService,
@@ -19,6 +19,7 @@ class ItemBuilder
19
19
  attr_reader :reserved_stocks
20
20
  attr_reader :zilingo_quantity
21
21
  attr_reader :zalora_reserved_stock
22
+ attr_reader :shopify_inventory_location
22
23
  def initialize(args)
23
24
  @listing = args.fetch(:listing)
24
25
  @wh_spaces = args.fetch(:wh_spaces, [])
@@ -31,6 +32,7 @@ class ItemBuilder
31
32
  @reserved_stocks = args.fetch(:reserved_stocks, [])
32
33
  @zilingo_quantity = args.fetch(:zilingo_quantity, [])
33
34
  @zalora_reserved_stock = args.fetch(:zalora_reserved_stock, [])
35
+ @shopify_inventory_location = args.fetch(:shopify_inventory_location, [])
34
36
  end
35
37
 
36
38
  def base
@@ -13,23 +13,31 @@ class ItemBuilder
13
13
  include Modes
14
14
 
15
15
  QUANTITY_CHANNEL = {}.tap do |hash|
16
- hash[3] = :Lazada
16
+ hash[2] = :Shopify
17
17
  hash[13] = :Zalora
18
18
  hash[18] = :Zilingo
19
19
  end.freeze
20
20
 
21
21
  def perform
22
- to_h.merge(base)
22
+ if channel_name == "Shopify"
23
+ base.merge(to_h, shopify_inventory_location[listing.local_id])
24
+ else
25
+ to_h.merge(base)
26
+ end
23
27
  end
24
28
 
25
29
  def to_h
26
- {
27
- quantity: [qty, 0].sort[1]
28
- }
30
+ if channel_name == "Zilingo"
31
+ { quantity: qty }
32
+ else
33
+ {
34
+ quantity: [qty, 0].sort[1]
35
+ }
36
+ end
29
37
  end
30
38
 
31
39
  def qty
32
- if channel_name.empty?
40
+ if channel_name.empty? || channel_name == "Shopify"
33
41
  available_quantity
34
42
  else
35
43
  qty_channel
@@ -0,0 +1,118 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'item_builder/modes.rb'
4
+ class ItemBuilder
5
+ class ShopifyQuantityService
6
+ attr_reader :listings, :skus
7
+ def initialize(args)
8
+ @listings = args.fetch(:listings)
9
+ @skus = args.fetch(:skus)
10
+ @account_id = listings[0].profile_channel_association_id
11
+ @variant_listings = {}
12
+ end
13
+
14
+ def perform
15
+ datas = {}
16
+
17
+ @listings.each do |listing|
18
+ @listing = listing
19
+ @sku = listing.local_id
20
+ datas[@sku] = response_process
21
+ end
22
+
23
+ # update variant real_local_id database icava
24
+ VariantListing.update(@variant_listings.keys, @variant_listings.values)
25
+ datas
26
+ end
27
+
28
+ def inventory_item_id
29
+ real_local_id = @listing.real_local_id
30
+ listing_id = @listing.id
31
+
32
+ if real_local_id.nil? || real_local_id.blank?
33
+ # get data from shopify
34
+ args = {method: :post, url: url_variant, payload: data_variant, headers: headers}
35
+
36
+ resp = JSON.parse(rest_client(args, [200, 500, 406]))
37
+
38
+ hash = { listing_id => { "real_local_id" => resp['inventory_item_id'] } }
39
+ @variant_listings = @variant_listings.merge(hash)
40
+
41
+ resp['inventory_item_id']
42
+ else
43
+ # real_local_id exists
44
+ real_local_id
45
+ end
46
+ end
47
+
48
+ def location_id
49
+ cred = JSON.parse(credential)['credential']
50
+ if cred['primary_warehouse_id'].present?
51
+ cred['primary_warehouse_id']
52
+ else
53
+ # get data from shopify
54
+ args = {
55
+ method: :post, url: url_location, payload: data_location, headers: headers
56
+ }
57
+
58
+ resp = JSON.parse(rest_client(args, [200, 500, 406]))
59
+ resp['id']
60
+ end
61
+ end
62
+
63
+ def headers
64
+ { content_type: :json, accept: :json }
65
+ end
66
+
67
+ def data_location
68
+ {
69
+ "credential": JSON.parse(credential)['credential']
70
+ }.to_json
71
+ end
72
+
73
+ def data_variant
74
+ {
75
+ "credential": JSON.parse(credential)['credential'],
76
+ "data": { "local_id": @sku }
77
+ }.to_json
78
+ end
79
+
80
+ def credential
81
+ return @credential if @credential
82
+
83
+ host = ENV['CREDENTIAL_URL'] || raise('credential url is not set')
84
+ url = "#{host}/credential?account_id=#{@account_id}"
85
+ @credential = rest_client(method: :get, url: url)
86
+ end
87
+
88
+ def url_variant
89
+ url = ENV['API_GATEWAY_URL'] || raise('api gateway is not set')
90
+ url + "/shopify/data_variant"
91
+ end
92
+
93
+ def url_location
94
+ url = ENV['API_GATEWAY_URL'] || raise('api gateway is not set')
95
+ url + "/shopify/data_location"
96
+ end
97
+
98
+ def response_process
99
+ hash = Hash.new
100
+
101
+ hash[:inventory_item_id] = inventory_item_id.to_i
102
+ hash[:location_id] = location_id.to_i
103
+
104
+ hash
105
+ end
106
+
107
+ def rest_client(params, rescued_codes = 200)
108
+ RestClient::Request.execute(params.merge(timeout: 3)) do |response|
109
+ code = response.code
110
+ unless Array.wrap(rescued_codes).include?(code)
111
+ raise "Response Code is #{code}"
112
+ end
113
+
114
+ response
115
+ end
116
+ end
117
+ end
118
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class ItemBuilder
4
- VERSION = '0.1.38'
4
+ VERSION = '0.1.42'
5
5
  end
@@ -47,6 +47,8 @@ class ItemBuilder
47
47
  raise "Response Code is #{resp.dig('response_code')}"
48
48
  elsif resp.dig('ErrorResponse', 'Head', 'ErrorMessage') == 'E009: Access Denied'
49
49
  return nil
50
+ elsif resp.dig('ErrorResponse').present?
51
+ return nil
50
52
  end
51
53
 
52
54
  success_handle(resp)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: item_builder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.38
4
+ version: 0.1.42
5
5
  platform: ruby
6
6
  authors:
7
7
  - okaaryanata
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-06-14 00:00:00.000000000 Z
11
+ date: 2021-08-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -143,6 +143,7 @@ files:
143
143
  - lib/item_builder/modes/update/tokopedia_service.rb
144
144
  - lib/item_builder/modes/update/zalora_service.rb
145
145
  - lib/item_builder/modes/update_service.rb
146
+ - lib/item_builder/shopify_quantity_service.rb
146
147
  - lib/item_builder/version.rb
147
148
  - lib/item_builder/zalora_quantity_service.rb
148
149
  - lib/item_builder/zilingo_quantity_service.rb