item_builder 0.1.43 → 0.1.47

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: 49e8a0ba80c8a73c29b9d53439a55575ae6faed6f5a7d5ae19b5b8e4101e8b3f
4
- data.tar.gz: 1bf2017d6d0c2afdf9d83a60ab0f5ad4a6c3000aa9b97a57946d5c80f3ee94d1
3
+ metadata.gz: 98c440176871d006bbd1ab07292696d7e779cffa64bdf097afcdcbb7fc9cb986
4
+ data.tar.gz: 1f3a2607695baa0e5d8767c06c6a007554d02c9390de6a597267871789dafcf9
5
5
  SHA512:
6
- metadata.gz: 893107b4c5642f15c5d05a500afb5eb6b1e87a53a749c75d73b6efb24b0fb78b6095790b8b0f2aa4cf677f223fe5c9a965e7f59ae2e6b94c4ab7918ec8fc4197
7
- data.tar.gz: f826724734d4abe3427c06a2a9618aa2bae96e6f61a58e3adb1de3c9553629c19b05ea3911c7b44e1fe8cf7779424cb7c6166759b41e8c1287dfdf0a167a03bb
6
+ metadata.gz: e7ad9a187205d4b5b8dab861d4b6e90261fc2348c90eabb8eb058bb944ab18b62508873b381effcb969c6600bc3657742ac615070a39e85be0cb2b33f2be8271
7
+ data.tar.gz: 35c72c9bbe5b5636c5494b0d72ff7fa54337383206c5f8b9b870ec647737164bcfcb4609f17bbfa9ce4fd73f3b259288f2df91958900f0c6dc0ba5d05ed9dd30
@@ -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,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,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).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
@@ -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.43'
4
+ VERSION = '0.1.47'
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
@@ -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)
@@ -65,12 +73,14 @@ class ItemBuilder
65
73
  stock_allocs: stock_allocs, variant_listings: variant_listings,
66
74
  bundles: bundles, item_bundle_variants: item_bundle_variants,
67
75
  existing_alloc_stocks: existing_alloc_stocks,
68
- reserved_stocks: reserved_stocks
76
+ reserved_stocks: reserved_stocks, wh_id: wh_id
69
77
  }
70
78
  end
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,
@@ -171,7 +187,7 @@ class ItemBuilder
171
187
  end
172
188
 
173
189
  def reserved_params
174
- "account_id=#{listings[0].profile_channel_association_id}
190
+ "account_id=#{account_id}
175
191
  &item_variant_ids=#{variant_ids.join(',')}"
176
192
  end
177
193
 
@@ -180,4 +196,18 @@ class ItemBuilder
180
196
  "#{order_host}?#{reserved_params}"
181
197
  ).body) if [3].include?(listings[0].channel_id)
182
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
183
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.43
4
+ version: 0.1.47
5
5
  platform: ruby
6
6
  authors:
7
7
  - okaaryanata
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-08 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