item_builder 0.1.33 → 0.1.38

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: e5163bc9aeaf2a76f547dc19cb482de6f50fe456e9f9a62860493e9a4d2c4a7c
4
- data.tar.gz: fb2f1120b1518a26fbf7c36892afe8bfb2e7dd38fc4797e8864cf7b7c1a9fb8a
3
+ metadata.gz: 1e5978f4671ec45e2e71e9e2656b7b40e13a48a7beca282c25149963066958e3
4
+ data.tar.gz: b89bb4ccea8a89fd7d456c8b45283bf3917c0d290fad4eeb773235456a031764
5
5
  SHA512:
6
- metadata.gz: 5cd6ffa5b4add1d53f21dad2fb40475dffecc8950116c047c90d6802d56262d1fec562565ee02fae17740b660b4c6ec073f807ea68890949506f9d34fe197ec1
7
- data.tar.gz: f5c222851018274f3b03d768d0c7e78273061ef7e9c4bcf3e8c032cb533db62284291beadedccf6a83b6000ac54ca40220de3a6a1a2dbc661c064ca5316ae0e1
6
+ metadata.gz: 4462f8fa85ec85e6b9960b6114431b30372eb0df207e57257a42f7d7fcbc55c76b38d92e2c978bf202ec389491d819b5ae8eacc12898a3130a14b81929d6143e
7
+ data.tar.gz: 3ef0ce28611e19beb65a84540d732ed3034f7ec59310a52975dc75add648a6e584945e61dc6c32f9ac2f53ce13456bfddde9118d9af075d1ea65c98fdacd2815
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/zalora_quantity_service'
12
13
 
13
14
  class ItemBuilder
14
15
  def self.build(listing_ids, mode)
@@ -28,7 +29,7 @@ class ItemBuilder
28
29
  end
29
30
 
30
31
  def mode_check
31
- if mode == :quantity || mode == :simple
32
+ if mode == :quantity || mode == :simple
32
33
  quantity_simple_mode
33
34
  else
34
35
  default
@@ -39,7 +40,12 @@ class ItemBuilder
39
40
  listings.map do |listing|
40
41
  if listing.channel_id == 18
41
42
  new_param = qty_simple_params(listing)
42
- .merge({zilingo_delta_quantity: zilingo_delta_quantity})
43
+ .merge(zilingo_quantity: zilingo_quantity)
44
+
45
+ modes[mode].new(new_param).perform
46
+ elsif listing.channel_id == 13
47
+ new_param = qty_simple_params(listing)
48
+ .merge(zalora_reserved_stock: zalora_reserved_stock)
43
49
 
44
50
  modes[mode].new(new_param).perform
45
51
  else
@@ -54,7 +60,7 @@ class ItemBuilder
54
60
  stock_allocs: stock_allocs, variant_listings: variant_listings,
55
61
  bundles: bundles, item_bundle_variants: item_bundle_variants,
56
62
  existing_alloc_stocks: existing_alloc_stocks,
57
- reserved_stocks: reserved_stocks
63
+ reserved_stocks: reserved_stocks
58
64
  }
59
65
  end
60
66
 
@@ -62,6 +68,10 @@ class ItemBuilder
62
68
  listings.map do |listing|
63
69
  if listing.channel_id == 2 && mode == :active
64
70
  modes[mode].new(qty_simple_params(listing)).perform
71
+ elsif listing.channel_id == 18 && mode == :active
72
+ modes[mode].new(qty_simple_params(listing)
73
+ .merge(zilingo_quantity: zilingo_quantity)
74
+ ).perform
65
75
  else
66
76
  modes[mode].new(listing: listing).perform
67
77
  end
@@ -122,8 +132,14 @@ class ItemBuilder
122
132
  @skus ||= listings.map(&:local_id).uniq
123
133
  end
124
134
 
125
- def zilingo_delta_quantity
126
- @zilingo_delta_quantity ||= ItemBuilder::ZilingoQuantityService.new(
135
+ def zilingo_quantity
136
+ @zilingo_quantity ||= ItemBuilder::ZilingoQuantityService.new(
137
+ listings: listings, skus: skus
138
+ ).perform
139
+ end
140
+
141
+ def zalora_reserved_stock
142
+ @zalora_reserved_stock ||= ItemBuilder::ZaloraQuantityService.new(
127
143
  listings: listings, skus: skus
128
144
  ).perform
129
145
  end
@@ -151,6 +167,6 @@ class ItemBuilder
151
167
  def reserved_stocks
152
168
  @reserved_stocks ||= JSON.parse(RestClient.get(
153
169
  "#{order_host}?#{reserved_params}"
154
- ).body) if [3,13].include?(listings[0].channel_id)
170
+ ).body) if [3].include?(listings[0].channel_id)
155
171
  end
156
172
  end
@@ -38,7 +38,7 @@ class ItemBuilder
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
 
@@ -17,7 +17,8 @@ class ItemBuilder
17
17
  attr_reader :existing_alloc_stocks
18
18
  attr_reader :variant_listings
19
19
  attr_reader :reserved_stocks
20
- attr_reader :zilingo_delta_quantity
20
+ attr_reader :zilingo_quantity
21
+ attr_reader :zalora_reserved_stock
21
22
  def initialize(args)
22
23
  @listing = args.fetch(:listing)
23
24
  @wh_spaces = args.fetch(:wh_spaces, [])
@@ -28,7 +29,8 @@ class ItemBuilder
28
29
  @existing_alloc_stocks = args.fetch(:existing_alloc_stocks, [])
29
30
  @variant_listings = args.fetch(:variant_listings, [])
30
31
  @reserved_stocks = args.fetch(:reserved_stocks, [])
31
- @zilingo_delta_quantity = args.fetch(:zilingo_delta_quantity, [])
32
+ @zilingo_quantity = args.fetch(:zilingo_quantity, [])
33
+ @zalora_reserved_stock = args.fetch(:zalora_reserved_stock, [])
32
34
  end
33
35
 
34
36
  def base
@@ -49,29 +51,29 @@ class ItemBuilder
49
51
  end
50
52
 
51
53
  def listings
52
- variant_listings.select {|vl| vl.variant_id == listing.variant_id}
54
+ variant_listings.select { |vl| vl.variant_id == listing.variant_id}
53
55
  end
54
56
 
55
57
  def bundle_variants
56
58
  if bundle.present?
57
- item_bundle_variants.select {|ibv| ibv.bundle_id == bundle.id }
59
+ item_bundle_variants.select { |ibv| ibv.bundle_id == bundle.id }
58
60
  end
59
61
  end
60
62
 
61
63
  def bundle
62
- bundles.select {|b| b.variant_id == listing.variant_id }.first
64
+ bundles.select { |b| b.variant_id == listing.variant_id }.first
63
65
  end
64
66
 
65
67
  def stock_alloc
66
- stock_allocs.select {|sa| sa.variant_association_id == listing.id }.first
68
+ stock_allocs.select { |sa| sa.variant_association_id == listing.id }.first
67
69
  end
68
70
 
69
71
  def warehouse
70
- wh_spaces.select {|ws| ws.item_variant_id == listing.variant_id }.first
72
+ wh_spaces.select { |ws| ws.item_variant_id == listing.variant_id }.first
71
73
  end
72
74
 
73
75
  def variant
74
- variants.select {|v| v.id == listing.variant_id }.first
76
+ variants.select { |v| v.id == listing.variant_id }.first
75
77
  end
76
78
  end
77
79
  end
@@ -8,7 +8,7 @@ class ItemBuilder
8
8
  include Modes
9
9
 
10
10
  def perform
11
- if listing.channel_id == 2
11
+ if listing.channel_id == 2 || listing.channel_id == 18
12
12
  to_h.merge(base).merge(quantity)
13
13
  else
14
14
  to_h.merge(base)
@@ -28,8 +28,9 @@ class ItemBuilder
28
28
  stock_allocs: stock_allocs, variant_listings: variant_listings,
29
29
  bundles: bundles, item_bundle_variants: item_bundle_variants,
30
30
  existing_alloc_stocks: existing_alloc_stocks,
31
- reserved_stocks: reserved_stocks,
32
- zilingo_delta_quantity: zilingo_delta_quantity
31
+ reserved_stocks: reserved_stocks,
32
+ zilingo_quantity: zilingo_quantity,
33
+ zalora_reserved_stock: zalora_reserved_stock
33
34
  ).to_h
34
35
  end
35
36
 
@@ -6,14 +6,14 @@ class ItemBuilder
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 ItemBuilder
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 ItemBuilder
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 ItemBuilder
6
6
  module Quantity
7
7
  class ZilingoService < Base
8
8
  def perform
9
- available_quantity - reserved_stock
9
+ available_quantity - local_qty
10
10
  end
11
11
  end
12
12
  end
@@ -39,18 +39,24 @@ class ItemBuilder
39
39
  def qty_channel
40
40
  class_name = "ItemBuilder::Modes::Quantity::#{channel_name}Service"
41
41
  qty_channel_service = class_name.constantize
42
- qty_channel_service.new(listing, available_quantity, reserved_stock).perform
42
+ qty_channel_service.new(listing, available_quantity, local_qty.to_i).perform
43
43
  end
44
44
 
45
45
  def channel_name
46
46
  QUANTITY_CHANNEL[listing.channel_id].to_s
47
47
  end
48
48
 
49
- def reserved_stock
49
+ def local_qty
50
50
  if channel_name == 'Zilingo'
51
- zilingo_delta_quantity[listing.local_id].to_i
51
+ return 0 if zilingo_quantity.blank?
52
+
53
+ zilingo_quantity[listing.local_id].to_i
54
+ elsif channel_name == 'Zalora'
55
+ return 0 if zalora_reserved_stock.blank?
56
+
57
+ zalora_reserved_stock[listing.local_id].to_i
52
58
  else
53
- reserved_stocks.find {|rs| rs['variant_id'] == listing.variant_id }['reserved_quantity']
59
+ reserved_stocks.find { |rs| rs['variant_id'] == listing.variant_id }['reserved_quantity']
54
60
  end
55
61
  end
56
62
 
@@ -22,8 +22,8 @@ class ItemBuilder
22
22
  stock_allocs: stock_allocs, variant_listings: variant_listings,
23
23
  bundles: bundles, item_bundle_variants: item_bundle_variants,
24
24
  existing_alloc_stocks: existing_alloc_stocks,
25
- reserved_stocks: reserved_stocks,
26
- zilingo_delta_quantity: zilingo_delta_quantity
25
+ reserved_stocks: reserved_stocks,
26
+ zilingo_quantity: zilingo_quantity
27
27
  ).to_h
28
28
  end
29
29
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class ItemBuilder
4
- VERSION = '0.1.33'
4
+ VERSION = '0.1.38'
5
5
  end
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'item_builder/modes.rb'
4
+ class ItemBuilder
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
+
52
+ success_handle(resp)
53
+ end
54
+
55
+ def success_handle(resp)
56
+ hash = {}
57
+ resp.dig('SuccessResponse', 'Body', 'ProductStocks', 'ProductStock').each do |sku|
58
+ hash[sku['SellerSku']] = sku['ReservedStock']
59
+ end
60
+ hash
61
+ end
62
+
63
+ def rest_client(params, rescued_codes = 200)
64
+ RestClient::Request.execute(params.merge(timeout: 3)) do |response|
65
+ code = response.code
66
+ unless Array.wrap(rescued_codes).include?(code)
67
+ raise "Response Code is #{code}"
68
+ end
69
+
70
+ response
71
+ end
72
+ end
73
+ end
74
+ end
@@ -6,7 +6,7 @@ class ItemBuilder
6
6
  attr_reader :listings, :skus
7
7
  def initialize(args)
8
8
  @listings = args.fetch(:listings)
9
- @skus = args.fetch(:skus)
9
+ @skus = args.fetch(:skus)
10
10
  end
11
11
 
12
12
  def perform
@@ -39,11 +39,11 @@ class ItemBuilder
39
39
 
40
40
  def url
41
41
  url = ENV['API_GATEWAY_URL'] || raise('api gateway is not set')
42
- url + "/zilingo/item_quantity_by_sku"
42
+ url + '/zilingo/item_quantity_by_sku'
43
43
  end
44
44
 
45
45
  def response_process(resp)
46
- hash = Hash.new
46
+ hash = {}
47
47
  resp['zilingoSKUQuantities'].each do |sku|
48
48
  hash[sku['zilingoSKUId']] = sku['quantity'] + sku['frozenQuantity']
49
49
  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.33
4
+ version: 0.1.38
5
5
  platform: ruby
6
6
  authors:
7
7
  - okaaryanata
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/modes/update/zalora_service.rb
145
145
  - lib/item_builder/modes/update_service.rb
146
146
  - lib/item_builder/version.rb
147
+ - lib/item_builder/zalora_quantity_service.rb
147
148
  - lib/item_builder/zilingo_quantity_service.rb
148
149
  homepage:
149
150
  licenses: []