item_builder 0.1.42 → 0.1.46

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: 5e9332c89e3fb06f1d60cbc7b01f9247491900d73e5d7a36a1103cf3bf37f25a
4
- data.tar.gz: 3add3dd0a79aa29e537c617fe54388dc81cdf5d07439e8b6b6bbca1ac6c3afba
3
+ metadata.gz: 67a534f744daba68c789abfd5d5d610ae94d858eea53f5ca82a37a49a73472ff
4
+ data.tar.gz: d968255f6bb5badf8095be52d0592e9668903fa5257b6d9c1ea3a611f0e172a6
5
5
  SHA512:
6
- metadata.gz: 8219fd536e925ec65bfdeda6eab8d79a953f1a038e3820e44f9c405186dbc83b560c187792fdf4f73856a6fe77fdb431d14bf48d696c5dcd17818258279c9a82
7
- data.tar.gz: 41555eb31879b5691467407e95dd88cfbd1b3c9125f2f3e7c955aca932d64a26cbab899216b249b59e66a3e2650bb70e5f19fe7ff7a1fcaca4e7db0a7cf29d09
6
+ metadata.gz: 2aeaabf7da717c6e7031dbdccf3c91dce6461f73a862a5a273ac45bce40f8b59c1a137b7b3eee0bad1129efe767c0081381507e354c8cd58b810bce6536bbfc6
7
+ data.tar.gz: 296c74c78199a39cb9ae7624c74f5fa1c81504c33dad1627c04ac77515c6e7c24a61de4b7fc7195f845626d60bd524a54cb59768148853e3f0138a4f48a93642
@@ -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 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,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.42'
4
+ VERSION = '0.1.46'
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)
@@ -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.42
4
+ version: 0.1.46
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-02 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.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