item_builder 0.1.41 → 0.1.45

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: ff44f1879704b4fd441241688415eac4160d7ab8f9ef8fbc39d98d1f7ed7e985
4
- data.tar.gz: cae5a688bb97a68d74e6cb55c4bc18fcfaee523f9f572eb4c5a615ab94945cfd
3
+ metadata.gz: 1f1e518425d71626cb31d4b52998fdfdd6c57adc7d31bae47ae53524f6869331
4
+ data.tar.gz: ad533c38093f28b2f85e7c043c423d9541ec4aca9cd861e2d3ce49a6631a964d
5
5
  SHA512:
6
- metadata.gz: bfa8ec3f6af0a9046c4d60a150789cf075f85060f00f6bc0b46106780b7d341dee8a1fe791c23404439a0b42b22801d52802c5277ae785327b9cbd2eea9ba6eb
7
- data.tar.gz: cd0129f8b41c40fb291057eb76afc377594025431b8f44662871cac3a94a422c6370720fba3b558ff1e1100b2b63b3086a173e0e07998d64ee201de531ff300d
6
+ metadata.gz: 1a8863826c68c15b5fa2ba7caba19586f85b6997e146c329a575293407ae7f698d545749b08f3ecdbecba6298fb520552cc69beaadf4334ef19cfdef4087d645
7
+ data.tar.gz: 38ce7ec15247a53d55afb769e4a702ac78d954561677c6b411501b84b494b48aeb2cf756cf2eb420020496cc2223b1bd7e580e25608214605ea027304aad7713
@@ -122,9 +122,12 @@ class ItemBuilder
122
122
  end
123
123
 
124
124
  def params(allocated_stock)
125
- "channel_id=#{listing.channel_id}&item_variant_id=#{listing.variant_id}
126
- &start_at=#{allocated_stock.start_at.to_date.beginning_of_day}
127
- &end_at=#{allocated_stock.end_at.to_date.end_of_day}"
125
+ {
126
+ channel_id: allocated_stock.variant_listing.channel_id,
127
+ item_variant_id: allocated_stock.variant_listing.variant_id,
128
+ start_at: allocated_stock.start_at.to_date.beginning_of_day,
129
+ end_at: allocated_stock.end_at.to_date.end_of_day
130
+ }.to_query
128
131
  end
129
132
 
130
133
  # one_alloc_rsvd_stock fungsi untuk mendapatkan satu
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'item_builder/modes.rb'
4
+ class ItemBuilder
5
+ class LazadaQuantityService
6
+ attr_reader :listings, :skus
7
+ def initialize(args)
8
+ @listings = args.fetch(:listings)
9
+ @skus = args.fetch(:skus)
10
+ end
11
+
12
+ def perform
13
+ args = {
14
+ method: :post, url: url, payload: data, headers: headers
15
+ }
16
+ resp = JSON.parse(rest_client(args, [200, 500, 406]))
17
+ response_process(resp)
18
+ end
19
+
20
+ def headers
21
+ { content_type: :json, accept: :json }
22
+ end
23
+
24
+ def data
25
+ {
26
+ "credential": JSON.parse(credential)['credential'],
27
+ "data": { "skus": skus.to_json }
28
+ }.to_json
29
+ end
30
+
31
+ def credential
32
+ return @credential if @credential
33
+
34
+ account_id = listings[0].profile_channel_association_id
35
+ host = ENV['CREDENTIAL_URL'] || raise('credential url is not set')
36
+ url = "#{host}/credential?account_id=#{account_id}"
37
+ @credential = rest_client(method: :get, url: url)
38
+ end
39
+
40
+ def url
41
+ url = ENV['API_GATEWAY_URL'] || raise('api gateway is not set')
42
+ url + '/lazada/items'
43
+ end
44
+
45
+ def response_process(resp)
46
+ hash = {}
47
+ resp['data']['products'].each do |product|
48
+ product['skus'].each do |sku|
49
+ qty = sku['multiWarehouseInventories'][0]
50
+ hash[sku['SellerSku']] = qty['occupyQuantity'] + qty['withholdQuantity']
51
+ end
52
+ end
53
+ hash
54
+ end
55
+
56
+ def rest_client(params, rescued_codes = 200)
57
+ RestClient::Request.execute(params.merge(timeout: 3)) do |response|
58
+ code = response.code
59
+ unless Array.wrap(rescued_codes).include?(code)
60
+ raise "Response Code is #{code}"
61
+ end
62
+
63
+ response
64
+ end
65
+ end
66
+ end
67
+ end
@@ -14,13 +14,16 @@ class ItemBuilder
14
14
 
15
15
  QUANTITY_CHANNEL = {}.tap do |hash|
16
16
  hash[2] = :Shopify
17
+ hash[3] = :Lazada
17
18
  hash[13] = :Zalora
18
19
  hash[18] = :Zilingo
19
20
  end.freeze
20
21
 
21
22
  def perform
22
23
  if channel_name == "Shopify"
23
- base.merge(to_h, shopify_inventory_location[listing.local_id])
24
+ dataSIL = shopify_inventory_location[listing.local_id]
25
+
26
+ base.merge(to_h, dataSIL) if dataSIL.present?
24
27
  else
25
28
  to_h.merge(base)
26
29
  end
@@ -64,7 +67,9 @@ class ItemBuilder
64
67
 
65
68
  zalora_reserved_stock[listing.local_id].to_i
66
69
  else
67
- reserved_stocks.find { |rs| rs['variant_id'] == listing.variant_id }['reserved_quantity']
70
+ return 0 if lazada_quantity.blank?
71
+
72
+ lazada_quantity[listing.local_id].to_i
68
73
  end
69
74
  end
70
75
 
@@ -18,6 +18,7 @@ class ItemBuilder
18
18
  attr_reader :variant_listings
19
19
  attr_reader :reserved_stocks
20
20
  attr_reader :zilingo_quantity
21
+ attr_reader :lazada_quantity
21
22
  attr_reader :zalora_reserved_stock
22
23
  attr_reader :shopify_inventory_location
23
24
  def initialize(args)
@@ -31,6 +32,7 @@ class ItemBuilder
31
32
  @variant_listings = args.fetch(:variant_listings, [])
32
33
  @reserved_stocks = args.fetch(:reserved_stocks, [])
33
34
  @zilingo_quantity = args.fetch(:zilingo_quantity, [])
35
+ @lazada_quantity = args.fetch(:lazada_quantity, [])
34
36
  @zalora_reserved_stock = args.fetch(:zalora_reserved_stock, [])
35
37
  @shopify_inventory_location = args.fetch(:shopify_inventory_location, [])
36
38
  end
@@ -13,11 +13,15 @@ class ItemBuilder
13
13
 
14
14
  def perform
15
15
  datas = {}
16
-
17
16
  @listings.each do |listing|
18
- @listing = listing
19
- @sku = listing.local_id
20
- datas[@sku] = response_process
17
+ if listing.local_id.present?
18
+ @listing = listing
19
+ @sku = listing.local_id
20
+ resp = response_process
21
+ next if resp[:inventory_item_id].nil? || resp[:inventory_item_id] == 0
22
+
23
+ datas[@sku] = resp
24
+ end
21
25
  end
22
26
 
23
27
  # update variant real_local_id database icava
@@ -98,8 +102,12 @@ class ItemBuilder
98
102
  def response_process
99
103
  hash = Hash.new
100
104
 
101
- hash[:inventory_item_id] = inventory_item_id.to_i
102
- hash[:location_id] = location_id.to_i
105
+ begin
106
+ hash[:inventory_item_id] = inventory_item_id.to_i
107
+ hash[:location_id] = location_id.to_i
108
+ rescue
109
+ hash
110
+ end
103
111
 
104
112
  hash
105
113
  end
@@ -107,8 +115,9 @@ class ItemBuilder
107
115
  def rest_client(params, rescued_codes = 200)
108
116
  RestClient::Request.execute(params.merge(timeout: 3)) do |response|
109
117
  code = response.code
118
+ resp = response.body.to_str
110
119
  unless Array.wrap(rescued_codes).include?(code)
111
- raise "Response Code is #{code}"
120
+ raise "Response Code is #{code}" unless resp.include?('Response code = 404')
112
121
  end
113
122
 
114
123
  response
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class ItemBuilder
4
- VERSION = '0.1.41'
4
+ VERSION = '0.1.45'
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)
data/lib/item_builder.rb CHANGED
@@ -9,6 +9,7 @@ require 'item_builder/modes/simple_service'
9
9
  require 'item_builder/modes/active_service'
10
10
  require 'item_models'
11
11
  require 'item_builder/zilingo_quantity_service'
12
+ require 'item_builder/lazada_quantity_service'
12
13
  require 'item_builder/zalora_quantity_service'
13
14
  require 'item_builder/shopify_quantity_service'
14
15
  class ItemBuilder
@@ -38,6 +39,8 @@ class ItemBuilder
38
39
 
39
40
  def quantity_simple_mode
40
41
  listings.map do |listing|
42
+ next unless listing.local_id.present?
43
+
41
44
  if listing.channel_id == 18
42
45
  new_param = qty_simple_params(listing)
43
46
  .merge(zilingo_quantity: zilingo_quantity)
@@ -52,11 +55,16 @@ class ItemBuilder
52
55
  new_param = qty_simple_params(listing)
53
56
  .merge({shopify_inventory_location: shopify_inventory_location})
54
57
 
58
+ modes[mode].new(new_param).perform
59
+ elsif listing.channel_id == 3
60
+ new_param = qty_simple_params(listing)
61
+ .merge({lazada_quantity: lazada_quantity})
62
+
55
63
  modes[mode].new(new_param).perform
56
64
  else
57
65
  modes[mode].new(qty_simple_params(listing)).perform
58
66
  end
59
- end
67
+ end.compact
60
68
  end
61
69
 
62
70
  def qty_simple_params(listing)
@@ -71,6 +79,8 @@ class ItemBuilder
71
79
 
72
80
  def default
73
81
  listings.map do |listing|
82
+ next unless listing.local_id.present?
83
+
74
84
  if listing.channel_id == 2 && mode == :active
75
85
  modes[mode].new(qty_simple_params(listing)).perform
76
86
  elsif listing.channel_id == 18 && mode == :active
@@ -130,7 +140,7 @@ class ItemBuilder
130
140
  end
131
141
 
132
142
  def listings
133
- @listings ||= VariantListing.where(id: listing_ids)
143
+ @listings ||= VariantListing.joins(:variant).where(id: listing_ids)
134
144
  end
135
145
 
136
146
  def skus
@@ -155,6 +165,12 @@ class ItemBuilder
155
165
  ).perform
156
166
  end
157
167
 
168
+ def lazada_quantity
169
+ @lazada_quantity ||= ItemBuilder::LazadaQuantityService.new(
170
+ listings: listings, skus: skus
171
+ ).perform
172
+ end
173
+
158
174
  def modes
159
175
  {
160
176
  price: ItemBuilder::Modes::PriceService,
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.41
4
+ version: 0.1.45
5
5
  platform: ruby
6
6
  authors:
7
7
  - okaaryanata
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-07-09 00:00:00.000000000 Z
11
+ date: 2021-09-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -117,6 +117,7 @@ extra_rdoc_files: []
117
117
  files:
118
118
  - lib/item_builder.rb
119
119
  - lib/item_builder/get_quantity_service.rb
120
+ - lib/item_builder/lazada_quantity_service.rb
120
121
  - lib/item_builder/modes.rb
121
122
  - lib/item_builder/modes/active_service.rb
122
123
  - lib/item_builder/modes/base_service.rb