item_builder 0.1.44 → 0.1.48

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: 20043fbc1916ef6f98c6429d101469353b46a0d9f8106be6ea94193a685aeb84
4
- data.tar.gz: d4a1aefc9f150740af16fad42a5734088e3f584ce6b896a2c4b6b645fe4d629d
3
+ metadata.gz: a7466c11e90ea16a599fe729ade0dd42c06a237505f4f167cc6b1d067da23d79
4
+ data.tar.gz: 0414a129507a7e55a1fa5ffab08f29b70e10292c856f5c48c4dcfadf4def70f4
5
5
  SHA512:
6
- metadata.gz: 692cf4418adce24b9ff97045629804663b4043bf6bcf1305b87037f084d819b5d111a3674e79e1f4140e6aabe49d4107db13d94cebe2fe8fb4103ea785315f45
7
- data.tar.gz: 1fc6d2ecb1425e180a7a069ca7f227fbb472664b50688be36176d5590e6ffa86d8ff4cc165eb3833df8d438fcf154f38bcab24e9a1bce199340feeaa8cb05ca9
6
+ metadata.gz: bc0f76ff7d8572691d6a65d5d1f3016af21f627cbfcd1beabab235e004c3cb53330eac0b7f93dc802ac19ef5cf215a231be240676fdef5545f774b5ba1989f8a
7
+ data.tar.gz: 9cdaf496fa275f4a759163a4615e6fab983ffac7587129a38fe23bf9b23173f652c8e2ee0a845bae3824f4c844bbf29d874c6bface83134cd11eb536db37f351
@@ -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 if resp['data'].present?
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,6 +14,7 @@ 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
@@ -66,7 +67,9 @@ class ItemBuilder
66
67
 
67
68
  zalora_reserved_stock[listing.local_id].to_i
68
69
  else
69
- 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
70
73
  end
71
74
  end
72
75
 
@@ -18,8 +18,10 @@ 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
24
+ attr_reader :wh_id
23
25
  def initialize(args)
24
26
  @listing = args.fetch(:listing)
25
27
  @wh_spaces = args.fetch(:wh_spaces, [])
@@ -31,8 +33,10 @@ class ItemBuilder
31
33
  @variant_listings = args.fetch(:variant_listings, [])
32
34
  @reserved_stocks = args.fetch(:reserved_stocks, [])
33
35
  @zilingo_quantity = args.fetch(:zilingo_quantity, [])
36
+ @lazada_quantity = args.fetch(:lazada_quantity, [])
34
37
  @zalora_reserved_stock = args.fetch(:zalora_reserved_stock, [])
35
38
  @shopify_inventory_location = args.fetch(:shopify_inventory_location, [])
39
+ @wh_id = args.fetch(:wh_id, [])
36
40
  end
37
41
 
38
42
  def base
@@ -71,7 +75,11 @@ class ItemBuilder
71
75
  end
72
76
 
73
77
  def warehouse
74
- wh_spaces.select { |ws| ws.item_variant_id == listing.variant_id }.first
78
+ if wh_id.present?
79
+ wh_spaces.where(warehouse_id: wh_id).where(item_variant_id: listing.variant_id).first
80
+ else
81
+ wh_spaces.select { |ws| ws.item_variant_id == listing.variant_id }.first
82
+ end
75
83
  end
76
84
 
77
85
  def variant
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class ItemBuilder
4
- VERSION = '0.1.44'
4
+ VERSION = '0.1.48'
5
5
  end
@@ -58,7 +58,7 @@ class ItemBuilder
58
58
  hash = {}
59
59
  resp.dig('SuccessResponse', 'Body', 'ProductStocks', 'ProductStock').each do |sku|
60
60
  hash[sku['SellerSku']] = sku['ReservedStock']
61
- end
61
+ end if resp.dig('SuccessResponse').present?
62
62
  hash
63
63
  end
64
64
 
@@ -46,7 +46,7 @@ class ItemBuilder
46
46
  hash = {}
47
47
  resp['zilingoSKUQuantities'].each do |sku|
48
48
  hash[sku['zilingoSKUId']] = sku['quantity'] + sku['frozenQuantity']
49
- end
49
+ end if resp['zilingoSKUQuantities'].present?
50
50
  hash
51
51
  end
52
52
 
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
@@ -54,6 +55,11 @@ class ItemBuilder
54
55
  new_param = qty_simple_params(listing)
55
56
  .merge({shopify_inventory_location: shopify_inventory_location})
56
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
+
57
63
  modes[mode].new(new_param).perform
58
64
  else
59
65
  modes[mode].new(qty_simple_params(listing)).perform
@@ -67,7 +73,7 @@ class ItemBuilder
67
73
  stock_allocs: stock_allocs, variant_listings: variant_listings,
68
74
  bundles: bundles, item_bundle_variants: item_bundle_variants,
69
75
  existing_alloc_stocks: existing_alloc_stocks,
70
- reserved_stocks: reserved_stocks
76
+ reserved_stocks: reserved_stocks, wh_id: wh_id
71
77
  }
72
78
  end
73
79
 
@@ -159,6 +165,12 @@ class ItemBuilder
159
165
  ).perform
160
166
  end
161
167
 
168
+ def lazada_quantity
169
+ @lazada_quantity ||= ItemBuilder::LazadaQuantityService.new(
170
+ listings: listings, skus: skus
171
+ ).perform
172
+ end
173
+
162
174
  def modes
163
175
  {
164
176
  price: ItemBuilder::Modes::PriceService,
@@ -175,7 +187,7 @@ class ItemBuilder
175
187
  end
176
188
 
177
189
  def reserved_params
178
- "account_id=#{listings[0].profile_channel_association_id}
190
+ "account_id=#{account_id}
179
191
  &item_variant_ids=#{variant_ids.join(',')}"
180
192
  end
181
193
 
@@ -184,4 +196,18 @@ class ItemBuilder
184
196
  "#{order_host}?#{reserved_params}"
185
197
  ).body) if [3].include?(listings[0].channel_id)
186
198
  end
199
+
200
+ def wh_mapping
201
+ wh_mapping ||= WarehouseMapping.where(
202
+ profile_channel_association_id: account_id
203
+ ).first
204
+ end
205
+
206
+ def account_id
207
+ account_id ||= listings[0].profile_channel_association_id
208
+ end
209
+
210
+ def wh_id
211
+ @wh_id ||= wh_mapping&.warehouse_id
212
+ end
187
213
  end
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.44
4
+ version: 0.1.48
5
5
  platform: ruby
6
6
  authors:
7
7
  - okaaryanata
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-09-02 00:00:00.000000000 Z
11
+ date: 2021-09-20 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
@@ -165,7 +166,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
165
166
  - !ruby/object:Gem::Version
166
167
  version: '0'
167
168
  requirements: []
168
- rubygems_version: 3.2.3
169
+ rubygems_version: 3.0.9
169
170
  signing_key:
170
171
  specification_version: 4
171
172
  summary: Item builder