item_builder_mwh 0.1.24 → 0.1.28

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 433fd54a7f90b3889e4e1ca203c4fc6212e1613718b6220fb40b7c9ca8923760
4
- data.tar.gz: e2832bdad60f6f48f6a4715f6014ec3790ee9d6adfeaa52d20f23a0cbd10db62
3
+ metadata.gz: cb7b98bbb829748842913583d5fd9b676ae548d49eccf88e2cbd39967e3dc20e
4
+ data.tar.gz: d3a91b8e34b5644bcaffd6bccac057595df3c9a0161633dd19c58da2cece9fe1
5
5
  SHA512:
6
- metadata.gz: 69f3d4575556e47967ec0dd47f0e82909c85c32abbb9a7e9fcb6082c0e514f9890d78cd9f0c806a3448d49119d1accb17b9c34e7e2f15242affdf3c7878c7d08
7
- data.tar.gz: 3a90c1e89b7945d0f21e6c84bd879ab309689504b2301a70608878929183525101a4497226b1d832c733315197b43cfb287661098153e0f396d627b74749fb23
6
+ metadata.gz: 378009edaa560ef37c91565a64c8907dc39b62873ecf61de4a174ab30b13027fea05d3c8379b1b7f9f5ead88a0b607e2b6df97e7b3b0936cf6832297cce176d3
7
+ data.tar.gz: 5f386b734f3bb639dc81dd55bf3286c4f7d58fd6b888573a0e9afdd8a944220a73b6232f89a97ea55a3bb24538d1e50719fee986b428b1b2389558135a725db5
@@ -79,10 +79,18 @@ class ItemBuilderMwh
79
79
  end
80
80
 
81
81
  def listing_warehouse_routing_quantity
82
- warehouse_spaces = WarehouseSpace.where(item_variant_id: variant.id)
82
+ warehouse_spaces = WarehouseSpace.where(item_variant_id: variant.id, warehouse_id: wh_ids)
83
83
  warehouse_spaces.sum(:quantity)
84
84
  end
85
85
 
86
+ def wh_mappings
87
+ WarehouseMapping.where(profile_channel_association_id: listing.profile_channel_association_id)
88
+ end
89
+
90
+ def wh_ids
91
+ @wh_ids ||= wh_mappings.map{|x|x.warehouse_id}.flatten.uniq
92
+ end
93
+
86
94
  def qty_bundle
87
95
  # Quantity for bundle config
88
96
  if @bundle_variants.present?
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'item_builder_mwh/modes.rb'
4
+ class ItemBuilderMwh
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
@@ -15,13 +15,16 @@ class ItemBuilderMwh
15
15
 
16
16
  QUANTITY_CHANNEL = {}.tap do |hash|
17
17
  hash[2] = :Shopify
18
+ hash[3] = :Lazada
18
19
  hash[13] = :Zalora
19
20
  hash[18] = :Zilingo
20
21
  end.freeze
21
22
 
22
23
  def perform
23
24
  if channel_name == "Shopify"
24
- base.merge!(shopify_inventory_location[listing.local_id], warehouse: warehouses )
25
+ dataSIL = shopify_inventory_location[listing.local_id]
26
+
27
+ base.merge!(dataSIL, warehouse: warehouses ) if dataSIL.present?
25
28
  else
26
29
  base.merge!(
27
30
  warehouse: warehouses
@@ -31,7 +34,7 @@ class ItemBuilderMwh
31
34
 
32
35
  def to_h(warehouse_space)
33
36
  if channel_name == "Zilingo"
34
- {
37
+ {
35
38
  quantity: qty(warehouse_space.quantity),
36
39
  warehouse_id: wh_mapping(
37
40
  warehouse_space.warehouse_id
@@ -75,7 +78,9 @@ class ItemBuilderMwh
75
78
 
76
79
  zalora_reserved_stock[listing.local_id].to_i
77
80
  else
78
- reserved_stocks.find {|rs| rs['variant_id'] == listing.variant_id }['reserved_quantity']
81
+ return 0 if lazada_quantity.blank?
82
+
83
+ lazada_quantity[listing.local_id].to_i
79
84
  end
80
85
  end
81
86
 
@@ -17,6 +17,7 @@ class ItemBuilderMwh
17
17
  attr_reader :variant_listings
18
18
  attr_reader :reserved_stocks
19
19
  attr_reader :zilingo_quantity
20
+ attr_reader :lazada_quantity
20
21
  attr_reader :zalora_reserved_stock
21
22
  attr_reader :shopify_inventory_location
22
23
  def initialize(args)
@@ -30,6 +31,7 @@ class ItemBuilderMwh
30
31
  @variant_listings = args.fetch(:variant_listings, [])
31
32
  @reserved_stocks = args.fetch(:reserved_stocks, [])
32
33
  @zilingo_quantity = args.fetch(:zilingo_quantity, [])
34
+ @lazada_quantity = args.fetch(:lazada_quantity, [])
33
35
  @zalora_reserved_stock = args.fetch(:zalora_reserved_stock, [])
34
36
  @shopify_inventory_location = args.fetch(:shopify_inventory_location, {})
35
37
  warehouse
@@ -14,9 +14,14 @@ class ItemBuilderMwh
14
14
  def perform
15
15
  datas = {}
16
16
  @listings.each do |listing|
17
- @listing = listing
18
- @sku = listing.local_id
19
- 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
20
25
  end
21
26
 
22
27
  # update variant real_local_id database icava
@@ -100,8 +105,12 @@ class ItemBuilderMwh
100
105
  def response_process
101
106
  hash = Hash.new
102
107
 
103
- hash[:inventory_item_id] = inventory_item_id.to_i
104
- hash[:location_id] = location_id.to_i
108
+ begin
109
+ hash[:inventory_item_id] = inventory_item_id.to_i
110
+ hash[:location_id] = location_id.to_i
111
+ rescue
112
+ hash
113
+ end
105
114
 
106
115
  hash
107
116
  end
@@ -109,8 +118,9 @@ class ItemBuilderMwh
109
118
  def rest_client(params, rescued_codes = 200)
110
119
  RestClient::Request.execute(params.merge(timeout: 3)) do |response|
111
120
  code = response.code
121
+ resp = response.body.to_str
112
122
  unless Array.wrap(rescued_codes).include?(code)
113
- raise "Response Code is #{code}"
123
+ raise "Response Code is #{code}" unless resp.include?('Response code = 404')
114
124
  end
115
125
 
116
126
  response
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class ItemBuilderMwh
4
- VERSION = '0.1.24'
4
+ VERSION = '0.1.28'
5
5
  end
@@ -57,7 +57,7 @@ class ItemBuilderMwh
57
57
  hash = Hash.new
58
58
  resp.dig("SuccessResponse", "Body", "ProductStocks", "ProductStock").each do |sku|
59
59
  hash[sku['SellerSku']] = sku['ReservedStock']
60
- end
60
+ end if resp.dig('SuccessResponse').present?
61
61
  hash
62
62
  end
63
63
 
@@ -46,7 +46,7 @@ class ItemBuilderMwh
46
46
  hash = Hash.new
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
 
@@ -8,6 +8,7 @@ require 'item_builder_mwh/modes/quantity_service'
8
8
  require 'item_builder_mwh/modes/simple_service'
9
9
  require 'item_models'
10
10
  require 'item_builder_mwh/zilingo_quantity_service'
11
+ require 'item_builder_mwh/lazada_quantity_service'
11
12
  require 'item_builder_mwh/zalora_quantity_service'
12
13
  require 'item_builder_mwh/shopify_quantity_service'
13
14
 
@@ -37,6 +38,8 @@ class ItemBuilderMwh
37
38
 
38
39
  def quantity_simple_mode
39
40
  listings.map do |listing|
41
+ next unless listing.local_id.present?
42
+
40
43
  if listing.channel_id == 18
41
44
  new_param = qty_simple_params(listing)
42
45
  .merge({zilingo_quantity: zilingo_quantity})
@@ -51,23 +54,30 @@ class ItemBuilderMwh
51
54
  new_param = qty_simple_params(listing)
52
55
  .merge({shopify_inventory_location: shopify_inventory_location})
53
56
 
57
+ modes[mode].new(new_param).perform
58
+ elsif listing.channel_id == 3
59
+ new_param = qty_simple_params(listing)
60
+ .merge({lazada_quantity: lazada_quantity})
61
+
54
62
  modes[mode].new(new_param).perform
55
63
  else
56
64
  modes[mode].new(qty_simple_params(listing)).perform
57
65
  end
58
- end
66
+ end.compact
59
67
  end
60
68
 
61
69
  def default
62
70
  listings.map do |listing|
63
- if listing.channel_id == 2 && mode == :active
64
- modes[mode].new(qty_simple_params(listing)).perform
65
- elsif listing.channel_id == 18 && mode == :active
66
- modes[mode].new(qty_simple_params(listing)
67
- .merge(zilingo_quantity: zilingo_quantity)
68
- ).perform
69
- else
70
- modes[mode].new(listing: listing).perform
71
+ if listing.local_id.present?
72
+ if listing.channel_id == 2 && mode == :active
73
+ modes[mode].new(qty_simple_params(listing)).perform
74
+ elsif listing.channel_id == 18 && mode == :active
75
+ modes[mode].new(qty_simple_params(listing)
76
+ .merge(zilingo_quantity: zilingo_quantity)
77
+ ).perform
78
+ else
79
+ modes[mode].new(listing: listing).perform
80
+ end
71
81
  end
72
82
  end
73
83
  end
@@ -150,6 +160,12 @@ class ItemBuilderMwh
150
160
  ).perform
151
161
  end
152
162
 
163
+ def lazada_quantity
164
+ @lazada_quantity ||= ItemBuilderMwh::LazadaQuantityService.new(
165
+ listings: listings, skus: skus
166
+ ).perform
167
+ end
168
+
153
169
  def order_host
154
170
  url = ENV['ORDERS_URL'] || 'orders.forstok.com'
155
171
  url + '/api/v3/item_line/reserved_stock'
@@ -176,6 +192,6 @@ class ItemBuilderMwh
176
192
  end
177
193
 
178
194
  def listings
179
- @listings ||= VariantListing.where(id: listing_ids)
195
+ @listings ||= VariantListing.joins(:variant).where(id: listing_ids)
180
196
  end
181
197
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: item_builder_mwh
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.24
4
+ version: 0.1.28
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin Ivander
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-08-05 00:00:00.000000000 Z
11
+ date: 2021-09-06 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_mwh.rb
119
119
  - lib/item_builder_mwh/get_quantity_service.rb
120
+ - lib/item_builder_mwh/lazada_quantity_service.rb
120
121
  - lib/item_builder_mwh/modes.rb
121
122
  - lib/item_builder_mwh/modes/active_service.rb
122
123
  - lib/item_builder_mwh/modes/base_service.rb