item_builder 0.1.35 → 0.1.40

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: 943f08798aa049f27fb26ea0770050ebee5c79e99938024ef0d5aaf154d084c9
4
- data.tar.gz: 3318ecd4b4f81f91ab4c9c46e9919ffd55a34dadd3c8954864acbaa9573dd00c
3
+ metadata.gz: 9105f19ab628cbee42342b843bb2900ecb3d99d80036b2412c094d0a7386f2a1
4
+ data.tar.gz: 52910c9fc720ea5932874ea1ea4fbb7d20eab29ccb3deb4b1f2a79f70e971725
5
5
  SHA512:
6
- metadata.gz: df2e735b1590b7ad053d8c436c6dfacf4ec1c6493e4d50118392f40c61cb2d5eaa095734ea7b9b21b663a40c10aa4ba21a33061975d8c046a134d3553d2d0f69
7
- data.tar.gz: b93c28e2d41870f21d51cefa3a7c2afff1f52c3badb9fdbb4ffc709de8c930da12e9a0f96c4e4a0fcdcfb99b7a302482626465a9337fd971bbb96c8148dff70f
6
+ metadata.gz: 664f4c500f1b02523cec53db670303b0222d7fac1f160fac419a0f44dfa96c8cbcf386879ce6cdab42a8a41540a5339c651242e719684ed661ca21e2391e2499
7
+ data.tar.gz: 0fd76898fc7610770498cbb58fa7d62d078189e6ed75f81df83db07e48c92c32af717bbf7dd0328b62b718628a0202e8fc13349b8802ba33ce75852c16a3300d
data/lib/item_builder.rb CHANGED
@@ -10,7 +10,7 @@ require 'item_builder/modes/active_service'
10
10
  require 'item_models'
11
11
  require 'item_builder/zilingo_quantity_service'
12
12
  require 'item_builder/zalora_quantity_service'
13
-
13
+ require 'item_builder/shopify_quantity_service'
14
14
  class ItemBuilder
15
15
  def self.build(listing_ids, mode)
16
16
  new(listing_ids, mode).mode_check
@@ -29,7 +29,7 @@ class ItemBuilder
29
29
  end
30
30
 
31
31
  def mode_check
32
- if mode == :quantity || mode == :simple
32
+ if mode == :quantity || mode == :simple
33
33
  quantity_simple_mode
34
34
  else
35
35
  default
@@ -40,12 +40,17 @@ class ItemBuilder
40
40
  listings.map do |listing|
41
41
  if listing.channel_id == 18
42
42
  new_param = qty_simple_params(listing)
43
- .merge({zilingo_delta_quantity: zilingo_delta_quantity})
43
+ .merge(zilingo_quantity: zilingo_quantity)
44
44
 
45
45
  modes[mode].new(new_param).perform
46
46
  elsif listing.channel_id == 13
47
47
  new_param = qty_simple_params(listing)
48
- .merge({zalora_reserved_stock: zalora_reserved_stock})
48
+ .merge(zalora_reserved_stock: zalora_reserved_stock)
49
+
50
+ modes[mode].new(new_param).perform
51
+ elsif listing.channel_id == 2
52
+ new_param = qty_simple_params(listing)
53
+ .merge({shopify_inventory_location: shopify_inventory_location})
49
54
 
50
55
  modes[mode].new(new_param).perform
51
56
  else
@@ -60,7 +65,7 @@ class ItemBuilder
60
65
  stock_allocs: stock_allocs, variant_listings: variant_listings,
61
66
  bundles: bundles, item_bundle_variants: item_bundle_variants,
62
67
  existing_alloc_stocks: existing_alloc_stocks,
63
- reserved_stocks: reserved_stocks
68
+ reserved_stocks: reserved_stocks
64
69
  }
65
70
  end
66
71
 
@@ -68,6 +73,10 @@ class ItemBuilder
68
73
  listings.map do |listing|
69
74
  if listing.channel_id == 2 && mode == :active
70
75
  modes[mode].new(qty_simple_params(listing)).perform
76
+ elsif listing.channel_id == 18 && mode == :active
77
+ modes[mode].new(qty_simple_params(listing)
78
+ .merge(zilingo_quantity: zilingo_quantity)
79
+ ).perform
71
80
  else
72
81
  modes[mode].new(listing: listing).perform
73
82
  end
@@ -128,8 +137,8 @@ class ItemBuilder
128
137
  @skus ||= listings.map(&:local_id).uniq
129
138
  end
130
139
 
131
- def zilingo_delta_quantity
132
- @zilingo_delta_quantity ||= ItemBuilder::ZilingoQuantityService.new(
140
+ def zilingo_quantity
141
+ @zilingo_quantity ||= ItemBuilder::ZilingoQuantityService.new(
133
142
  listings: listings, skus: skus
134
143
  ).perform
135
144
  end
@@ -140,6 +149,12 @@ class ItemBuilder
140
149
  ).perform
141
150
  end
142
151
 
152
+ def shopify_inventory_location
153
+ @shopify_inventory_location ||= ItemBuilder::ShopifyQuantityService.new(
154
+ listings: listings, skus: skus
155
+ ).perform
156
+ end
157
+
143
158
  def modes
144
159
  {
145
160
  price: ItemBuilder::Modes::PriceService,
@@ -38,7 +38,7 @@ class ItemBuilder
38
38
  def check_consignment_variant?
39
39
  listing.consignment? ||
40
40
  (
41
- !listing.active? && [11, 12, 15, 19].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,8 +17,9 @@ 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
21
  attr_reader :zalora_reserved_stock
22
+ attr_reader :shopify_inventory_location
22
23
  def initialize(args)
23
24
  @listing = args.fetch(:listing)
24
25
  @wh_spaces = args.fetch(:wh_spaces, [])
@@ -29,8 +30,9 @@ class ItemBuilder
29
30
  @existing_alloc_stocks = args.fetch(:existing_alloc_stocks, [])
30
31
  @variant_listings = args.fetch(:variant_listings, [])
31
32
  @reserved_stocks = args.fetch(:reserved_stocks, [])
32
- @zilingo_delta_quantity = args.fetch(:zilingo_delta_quantity, [])
33
+ @zilingo_quantity = args.fetch(:zilingo_quantity, [])
33
34
  @zalora_reserved_stock = args.fetch(:zalora_reserved_stock, [])
35
+ @shopify_inventory_location = args.fetch(:shopify_inventory_location, [])
34
36
  end
35
37
 
36
38
  def base
@@ -51,29 +53,29 @@ class ItemBuilder
51
53
  end
52
54
 
53
55
  def listings
54
- variant_listings.select {|vl| vl.variant_id == listing.variant_id}
56
+ variant_listings.select { |vl| vl.variant_id == listing.variant_id}
55
57
  end
56
58
 
57
59
  def bundle_variants
58
60
  if bundle.present?
59
- item_bundle_variants.select {|ibv| ibv.bundle_id == bundle.id }
61
+ item_bundle_variants.select { |ibv| ibv.bundle_id == bundle.id }
60
62
  end
61
63
  end
62
64
 
63
65
  def bundle
64
- bundles.select {|b| b.variant_id == listing.variant_id }.first
66
+ bundles.select { |b| b.variant_id == listing.variant_id }.first
65
67
  end
66
68
 
67
69
  def stock_alloc
68
- stock_allocs.select {|sa| sa.variant_association_id == listing.id }.first
70
+ stock_allocs.select { |sa| sa.variant_association_id == listing.id }.first
69
71
  end
70
72
 
71
73
  def warehouse
72
- wh_spaces.select {|ws| ws.item_variant_id == listing.variant_id }.first
74
+ wh_spaces.select { |ws| ws.item_variant_id == listing.variant_id }.first
73
75
  end
74
76
 
75
77
  def variant
76
- variants.select {|v| v.id == listing.variant_id }.first
78
+ variants.select { |v| v.id == listing.variant_id }.first
77
79
  end
78
80
  end
79
81
  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,8 @@ 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
33
  zalora_reserved_stock: zalora_reserved_stock
34
34
  ).to_h
35
35
  end
@@ -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
@@ -14,22 +14,31 @@ class ItemBuilder
14
14
 
15
15
  QUANTITY_CHANNEL = {}.tap do |hash|
16
16
  hash[3] = :Lazada
17
+ hash[2] = :Shopify
17
18
  hash[13] = :Zalora
18
19
  hash[18] = :Zilingo
19
20
  end.freeze
20
21
 
21
22
  def perform
22
- to_h.merge(base)
23
+ if channel_name == "Shopify"
24
+ base.merge(to_h, shopify_inventory_location[listing.local_id])
25
+ else
26
+ to_h.merge(base)
27
+ end
23
28
  end
24
29
 
25
30
  def to_h
26
- {
27
- quantity: [qty, 0].sort[1]
28
- }
31
+ if channel_name == "Zilingo"
32
+ { quantity: qty }
33
+ else
34
+ {
35
+ quantity: [qty, 0].sort[1]
36
+ }
37
+ end
29
38
  end
30
39
 
31
40
  def qty
32
- if channel_name.empty?
41
+ if channel_name.empty? || channel_name == "Shopify"
33
42
  available_quantity
34
43
  else
35
44
  qty_channel
@@ -39,20 +48,24 @@ class ItemBuilder
39
48
  def qty_channel
40
49
  class_name = "ItemBuilder::Modes::Quantity::#{channel_name}Service"
41
50
  qty_channel_service = class_name.constantize
42
- qty_channel_service.new(listing, available_quantity, reserved_stock).perform
51
+ qty_channel_service.new(listing, available_quantity, local_qty.to_i).perform
43
52
  end
44
53
 
45
54
  def channel_name
46
55
  QUANTITY_CHANNEL[listing.channel_id].to_s
47
56
  end
48
57
 
49
- def reserved_stock
58
+ def local_qty
50
59
  if channel_name == 'Zilingo'
51
- zilingo_delta_quantity[listing.local_id].to_i
60
+ return 0 if zilingo_quantity.blank?
61
+
62
+ zilingo_quantity[listing.local_id].to_i
52
63
  elsif channel_name == 'Zalora'
64
+ return 0 if zalora_reserved_stock.blank?
65
+
53
66
  zalora_reserved_stock[listing.local_id].to_i
54
67
  else
55
- reserved_stocks.find {|rs| rs['variant_id'] == listing.variant_id }['reserved_quantity']
68
+ reserved_stocks.find { |rs| rs['variant_id'] == listing.variant_id }['reserved_quantity']
56
69
  end
57
70
  end
58
71
 
@@ -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
 
@@ -0,0 +1,118 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'item_builder/modes.rb'
4
+ class ItemBuilder
5
+ class ShopifyQuantityService
6
+ attr_reader :listings, :skus
7
+ def initialize(args)
8
+ @listings = args.fetch(:listings)
9
+ @skus = args.fetch(:skus)
10
+ @account_id = listings[0].profile_channel_association_id
11
+ @variant_listings = {}
12
+ end
13
+
14
+ def perform
15
+ datas = {}
16
+
17
+ @listings.each do |listing|
18
+ @listing = listing
19
+ @sku = listing.local_id
20
+ datas[@sku] = response_process
21
+ end
22
+
23
+ # update variant real_local_id database icava
24
+ VariantListing.update(@variant_listings.keys, @variant_listings.values)
25
+ datas
26
+ end
27
+
28
+ def inventory_item_id
29
+ real_local_id = @listing.real_local_id
30
+ listing_id = @listing.id
31
+
32
+ if real_local_id.nil? || real_local_id.blank?
33
+ # get data from shopify
34
+ args = {method: :post, url: url_variant, payload: data_variant, headers: headers}
35
+
36
+ resp = JSON.parse(rest_client(args, [200, 500, 406]))
37
+
38
+ hash = { listing_id => { "real_local_id" => resp['inventory_item_id'] } }
39
+ @variant_listings = @variant_listings.merge(hash)
40
+
41
+ resp['inventory_item_id']
42
+ else
43
+ # real_local_id exists
44
+ real_local_id
45
+ end
46
+ end
47
+
48
+ def location_id
49
+ cred = JSON.parse(credential)['credential']
50
+ if cred['primary_warehouse_id'].present?
51
+ cred['primary_warehouse_id']
52
+ else
53
+ # get data from shopify
54
+ args = {
55
+ method: :post, url: url_location, payload: data_location, headers: headers
56
+ }
57
+
58
+ resp = JSON.parse(rest_client(args, [200, 500, 406]))
59
+ resp['id']
60
+ end
61
+ end
62
+
63
+ def headers
64
+ { content_type: :json, accept: :json }
65
+ end
66
+
67
+ def data_location
68
+ {
69
+ "credential": JSON.parse(credential)['credential']
70
+ }.to_json
71
+ end
72
+
73
+ def data_variant
74
+ {
75
+ "credential": JSON.parse(credential)['credential'],
76
+ "data": { "local_id": @sku }
77
+ }.to_json
78
+ end
79
+
80
+ def credential
81
+ return @credential if @credential
82
+
83
+ host = ENV['CREDENTIAL_URL'] || raise('credential url is not set')
84
+ url = "#{host}/credential?account_id=#{@account_id}"
85
+ @credential = rest_client(method: :get, url: url)
86
+ end
87
+
88
+ def url_variant
89
+ url = ENV['API_GATEWAY_URL'] || raise('api gateway is not set')
90
+ url + "/shopify/data_variant"
91
+ end
92
+
93
+ def url_location
94
+ url = ENV['API_GATEWAY_URL'] || raise('api gateway is not set')
95
+ url + "/shopify/data_location"
96
+ end
97
+
98
+ def response_process
99
+ hash = Hash.new
100
+
101
+ hash[:inventory_item_id] = inventory_item_id.to_i
102
+ hash[:location_id] = location_id.to_i
103
+
104
+ hash
105
+ end
106
+
107
+ def rest_client(params, rescued_codes = 200)
108
+ RestClient::Request.execute(params.merge(timeout: 3)) do |response|
109
+ code = response.code
110
+ unless Array.wrap(rescued_codes).include?(code)
111
+ raise "Response Code is #{code}"
112
+ end
113
+
114
+ response
115
+ end
116
+ end
117
+ end
118
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class ItemBuilder
4
- VERSION = '0.1.35'
4
+ VERSION = '0.1.40'
5
5
  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,16 +39,22 @@ class ItemBuilder
39
39
 
40
40
  def url
41
41
  url = ENV['API_GATEWAY_URL'] || raise('api gateway is not set')
42
- url + "/zalora/product_stocks"
42
+ url + '/zalora/product_stocks'
43
43
  end
44
44
 
45
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
- end
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
49
54
 
50
- hash = Hash.new
51
- resp.dig("SuccessResponse", "Body", "ProductStocks", "ProductStock").each do |sku|
55
+ def success_handle(resp)
56
+ hash = {}
57
+ resp.dig('SuccessResponse', 'Body', 'ProductStocks', 'ProductStock').each do |sku|
52
58
  hash[sku['SellerSku']] = sku['ReservedStock']
53
59
  end
54
60
  hash
@@ -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.35
4
+ version: 0.1.40
5
5
  platform: ruby
6
6
  authors:
7
7
  - okaaryanata
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-04-27 00:00:00.000000000 Z
11
+ date: 2021-07-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -143,6 +143,7 @@ files:
143
143
  - lib/item_builder/modes/update/tokopedia_service.rb
144
144
  - lib/item_builder/modes/update/zalora_service.rb
145
145
  - lib/item_builder/modes/update_service.rb
146
+ - lib/item_builder/shopify_quantity_service.rb
146
147
  - lib/item_builder/version.rb
147
148
  - lib/item_builder/zalora_quantity_service.rb
148
149
  - lib/item_builder/zilingo_quantity_service.rb