item_builder_mwh 0.1.14 → 0.1.19

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: 5a9f73afc814a44d6e54abb4e1b2fa988ed233ec7e6858be9aa964dc1e98c124
4
- data.tar.gz: 7faa7a9622d86db9130acbcaa14febed1f35cf2f93e0603c832b87c92f9e6e4e
3
+ metadata.gz: '08f98253d8c700cbb5849d299ce485fc07ec96d5de0df1077e711dff490055b9'
4
+ data.tar.gz: 56f60c4b7f0f5356fb22b58eab61b63bed29e603d5ad44155d55cc6b231d077a
5
5
  SHA512:
6
- metadata.gz: 0d78506e0099bb9e9326ef25ddc00db9d482728777d15e8cb37f750fb18d031767942e5233f23c28a236a9cfa2c92531b34abd3a08b5e7b69c9812b0ad72cc2b
7
- data.tar.gz: 02d0fc58a57ca7fb34429c59bb651f582412d8cb2abf70aa0de5b85e6fd11b6fbfe3e4410f897f46dae7989425f2bb03c12828194f5d754f073b2e0fc1df0de3
6
+ metadata.gz: 1937a3e7a8853bd0a13d554951ba4f32ff8579237ffe05e0c0510a6cbb689bcdae5bd0ff8320ac9d0b9754d043a4b35ca4670accf1e0928289921658e79605f1
7
+ data.tar.gz: fafb92c7b40c166bd96265618935e1e8aba6aa47977ee13b0727b29326a5c29044ec286d9f64d9ba5cfbb2e7624b246144f8cbe83bb080cf47b55d1a818053d7
@@ -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/zalora_quantity_service'
11
12
 
12
13
  class ItemBuilderMwh
13
14
  attr_reader :listing_ids
@@ -37,7 +38,12 @@ class ItemBuilderMwh
37
38
  listings.map do |listing|
38
39
  if listing.channel_id == 18
39
40
  new_param = qty_simple_params(listing)
40
- .merge({zilingo_delta_quantity: zilingo_delta_quantity})
41
+ .merge({zilingo_quantity: zilingo_quantity})
42
+
43
+ modes[mode].new(new_param).perform
44
+ elsif listing.channel_id == 13
45
+ new_param = qty_simple_params(listing)
46
+ .merge({zalora_reserved_stock: zalora_reserved_stock})
41
47
 
42
48
  modes[mode].new(new_param).perform
43
49
  else
@@ -48,7 +54,15 @@ class ItemBuilderMwh
48
54
 
49
55
  def default
50
56
  listings.map do |listing|
51
- modes[mode].new(listing: listing).perform
57
+ if listing.channel_id == 2 && mode == :active
58
+ modes[mode].new(qty_simple_params(listing)).perform
59
+ elsif listing.channel_id == 18 && mode == :active
60
+ modes[mode].new(qty_simple_params(listing)
61
+ .merge(zilingo_quantity: zilingo_quantity)
62
+ ).perform
63
+ else
64
+ modes[mode].new(listing: listing).perform
65
+ end
52
66
  end
53
67
  end
54
68
 
@@ -112,8 +126,14 @@ class ItemBuilderMwh
112
126
  @skus ||= listings.map(&:local_id).uniq
113
127
  end
114
128
 
115
- def zilingo_delta_quantity
116
- @zilingo_delta_quantity ||= ItemBuilderMwh::ZilingoQuantityService.new(
129
+ def zilingo_quantity
130
+ @zilingo_quantity ||= ItemBuilderMwh::ZilingoQuantityService.new(
131
+ listings: listings, skus: skus
132
+ ).perform
133
+ end
134
+
135
+ def zalora_reserved_stock
136
+ @zalora_reserved_stock ||= ItemBuilderMwh::ZaloraQuantityService.new(
117
137
  listings: listings, skus: skus
118
138
  ).perform
119
139
  end
@@ -38,7 +38,7 @@ class ItemBuilderMwh
38
38
  def check_consignment_variant?
39
39
  listing.consignment? ||
40
40
  (
41
- !listing.active? && [11, 12, 15].include?(listing.channel_id)
41
+ !listing.active? && [11, 12, 15, 19, 2, 18].include?(listing.channel_id)
42
42
  )
43
43
  end
44
44
 
@@ -16,7 +16,8 @@ class ItemBuilderMwh
16
16
  attr_reader :existing_alloc_stocks
17
17
  attr_reader :variant_listings
18
18
  attr_reader :reserved_stocks
19
- attr_reader :zilingo_delta_quantity
19
+ attr_reader :zilingo_quantity
20
+ attr_reader :zalora_reserved_stock
20
21
  def initialize(args)
21
22
  @listing = args.fetch(:listing)
22
23
  @wh_spaces = args.fetch(:wh_spaces, [])
@@ -27,7 +28,8 @@ class ItemBuilderMwh
27
28
  @existing_alloc_stocks = args.fetch(:existing_alloc_stocks, [])
28
29
  @variant_listings = args.fetch(:variant_listings, [])
29
30
  @reserved_stocks = args.fetch(:reserved_stocks, [])
30
- @zilingo_delta_quantity = args.fetch(:zilingo_delta_quantity, [])
31
+ @zilingo_quantity = args.fetch(:zilingo_quantity, [])
32
+ @zalora_reserved_stock = args.fetch(:zalora_reserved_stock, [])
31
33
  warehouse
32
34
  end
33
35
 
@@ -6,14 +6,14 @@ class ItemBuilderMwh
6
6
  class Base
7
7
  attr_reader :listing
8
8
  attr_reader :available_quantity
9
- attr_reader :reserved_stock
9
+ attr_reader :local_qty
10
10
 
11
- def initialize(listing, available_quantity, reserved_stock)
11
+ def initialize(listing, available_quantity, local_qty)
12
12
  raise 'listing is not set' if listing.nil?
13
13
 
14
14
  @listing = listing
15
15
  @available_quantity = available_quantity
16
- @reserved_stock = reserved_stock
16
+ @local_qty = local_qty
17
17
  end
18
18
  end
19
19
  end
@@ -6,7 +6,7 @@ class ItemBuilderMwh
6
6
  module Quantity
7
7
  class LazadaService < Base
8
8
  def perform
9
- available_quantity + reserved_stock.to_i
9
+ available_quantity + local_qty
10
10
  end
11
11
  end
12
12
  end
@@ -6,7 +6,7 @@ class ItemBuilderMwh
6
6
  module Quantity
7
7
  class ZaloraService < Base
8
8
  def perform
9
- available_quantity + reserved_stock.to_i
9
+ available_quantity + local_qty
10
10
  end
11
11
  end
12
12
  end
@@ -6,7 +6,7 @@ class ItemBuilderMwh
6
6
  module Quantity
7
7
  class ZilingoService < Base
8
8
  def perform
9
- available_quantity - reserved_stock.to_i
9
+ available_quantity - local_qty
10
10
  end
11
11
  end
12
12
  end
@@ -45,16 +45,22 @@ class ItemBuilderMwh
45
45
  def qty_channel(qty)
46
46
  class_name = "ItemBuilderMwh::Modes::Quantity::#{channel_name}Service"
47
47
  qty_channel_service = class_name.constantize
48
- qty_channel_service.new(listing, available_quantity(qty), reserved_stock).perform
48
+ qty_channel_service.new(listing, available_quantity(qty), local_qty.to_i).perform
49
49
  end
50
50
 
51
51
  def channel_name
52
52
  QUANTITY_CHANNEL[listing.channel_id].to_s
53
53
  end
54
54
 
55
- def reserved_stock
55
+ def local_qty
56
56
  if channel_name == 'Zilingo'
57
- zilingo_delta_quantity[listing.local_id].to_i
57
+ return 0 if zilingo_quantity.blank?
58
+
59
+ zilingo_quantity[listing.local_id].to_i
60
+ elsif channel_name == 'Zalora'
61
+ return 0 if zalora_reserved_stock.blank?
62
+
63
+ zalora_reserved_stock[listing.local_id].to_i
58
64
  else
59
65
  reserved_stocks.find {|rs| rs['variant_id'] == listing.variant_id }['reserved_quantity']
60
66
  end
@@ -88,11 +88,21 @@ class ItemBuilderMwh
88
88
  def qty_channel(qty)
89
89
  class_name = "ItemBuilderMwh::Modes::Quantity::#{quantity_name}Service"
90
90
  qty_channel_service = class_name.constantize
91
- qty_channel_service.new(listing, available_quantity(qty), reserved_stock).perform
91
+ qty_channel_service.new(listing, available_quantity(qty), local_qty.to_i).perform
92
92
  end
93
93
 
94
- def reserved_stock
95
- reserved_stocks.find {|rs| rs['variant_id'] == listing.variant_id }['reserved_quantity']
94
+ def local_qty
95
+ if channel_name == 'Zilingo'
96
+ return 0 if zilingo_quantity.blank?
97
+
98
+ zilingo_quantity[listing.local_id].to_i
99
+ elsif channel_name == 'Zalora'
100
+ return 0 if zalora_reserved_stock.blank?
101
+
102
+ zalora_reserved_stock[listing.local_id].to_i
103
+ else
104
+ reserved_stocks.find {|rs| rs['variant_id'] == listing.variant_id }['reserved_quantity']
105
+ end
96
106
  end
97
107
 
98
108
  def available_quantity(qty)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class ItemBuilderMwh
4
- VERSION = '0.1.14'
4
+ VERSION = '0.1.19'
5
5
  end
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'item_builder_mwh/modes.rb'
4
+ class ItemBuilderMwh
5
+ class ZaloraQuantityService
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 }
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 + "/zalora/product_stocks"
43
+ end
44
+
45
+ def response_process(resp)
46
+ if resp.dig("response_code") && resp.dig("response_code") != 200
47
+ raise "Response Code is #{resp.dig("response_code")}"
48
+ elsif resp.dig('ErrorResponse', 'Head', 'ErrorMessage') == 'E009: Access Denied'
49
+ return nil
50
+ end
51
+ success_handle(resp)
52
+ end
53
+
54
+ def success_handle(resp)
55
+ hash = Hash.new
56
+ resp.dig("SuccessResponse", "Body", "ProductStocks", "ProductStock").each do |sku|
57
+ hash[sku['SellerSku']] = sku['ReservedStock']
58
+ end
59
+ hash
60
+ end
61
+
62
+ def rest_client(params, rescued_codes = 200)
63
+ RestClient::Request.execute(params.merge(timeout: 3)) do |response|
64
+ code = response.code
65
+ unless Array.wrap(rescued_codes).include?(code)
66
+ raise "Response Code is #{code}"
67
+ end
68
+
69
+ response
70
+ end
71
+ end
72
+ end
73
+ 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.14
4
+ version: 0.1.19
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-03-03 00:00:00.000000000 Z
11
+ date: 2021-06-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -144,6 +144,7 @@ files:
144
144
  - lib/item_builder_mwh/modes/simple/zalora_service.rb
145
145
  - lib/item_builder_mwh/modes/simple_service.rb
146
146
  - lib/item_builder_mwh/version.rb
147
+ - lib/item_builder_mwh/zalora_quantity_service.rb
147
148
  - lib/item_builder_mwh/zilingo_quantity_service.rb
148
149
  homepage:
149
150
  licenses: []