item_builder 0.1.34 → 0.1.39

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: 8e9895ea021aab0e2ffe939409176b92e8478b8c31a1645480e55ed07f14e86d
4
- data.tar.gz: a35617cca876d0d21c28b5bd11c6d8a209111b9ef0b96540908b177f38755cec
3
+ metadata.gz: 33a795a8c5bc377cb851c71b81db06c9e7e56002c46dedbd55fbddb7fe6f2d0b
4
+ data.tar.gz: 0af4f467706e87b849455dd0a0a45ee21785f82820fbf2631aeee31540a44a2e
5
5
  SHA512:
6
- metadata.gz: 9fd151f3dc3afba437effd13e5d2cb199fc8590615bca4c696e0d5eb291eddeb23a331cc12569bedea8eb50a5a3979fc3cd959625b18c41b166c22fa5f19e22c
7
- data.tar.gz: 6ce4e2f994f07c8bd0074a23d1fd7764e86c43a323ff588506af7ffa4aaf53f0bcaca3589f9710d23168041cdc5715a5a8b23269c076a0ddd9cbad3667b91d6e
6
+ metadata.gz: 2855fd51aef9dcd3c36c8c0776b83149d65598b661a6e1c87fe815e4e32bb2a7f2ffe74f1009ef6c492e0574787088c72b15e7408bfb4b2f4d70b7bd9a10eecc
7
+ data.tar.gz: 2f6e91d85c03dc6df7abfe7af2db1b833c8e1a5f82772a96b40db135a02bcd2bd3f913b91afb0fb19c493f73ed99de337452f94e832c647dae7ee8d1c97a08d4
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].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,12 +14,17 @@ 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
@@ -29,7 +34,7 @@ class ItemBuilder
29
34
  end
30
35
 
31
36
  def qty
32
- if channel_name.empty?
37
+ if channel_name.empty? || channel_name == "Shopify"
33
38
  available_quantity
34
39
  else
35
40
  qty_channel
@@ -39,20 +44,24 @@ class ItemBuilder
39
44
  def qty_channel
40
45
  class_name = "ItemBuilder::Modes::Quantity::#{channel_name}Service"
41
46
  qty_channel_service = class_name.constantize
42
- qty_channel_service.new(listing, available_quantity, reserved_stock).perform
47
+ qty_channel_service.new(listing, available_quantity, local_qty.to_i).perform
43
48
  end
44
49
 
45
50
  def channel_name
46
51
  QUANTITY_CHANNEL[listing.channel_id].to_s
47
52
  end
48
53
 
49
- def reserved_stock
54
+ def local_qty
50
55
  if channel_name == 'Zilingo'
51
- zilingo_delta_quantity[listing.local_id].to_i
56
+ return 0 if zilingo_quantity.blank?
57
+
58
+ zilingo_quantity[listing.local_id].to_i
52
59
  elsif channel_name == 'Zalora'
60
+ return 0 if zalora_reserved_stock.blank?
61
+
53
62
  zalora_reserved_stock[listing.local_id].to_i
54
63
  else
55
- reserved_stocks.find {|rs| rs['variant_id'] == listing.variant_id }['reserved_quantity']
64
+ reserved_stocks.find { |rs| rs['variant_id'] == listing.variant_id }['reserved_quantity']
56
65
  end
57
66
  end
58
67
 
@@ -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.34'
4
+ VERSION = '0.1.39'
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,12 +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
- hash = Hash.new
47
- resp.dig("SuccessResponse", "Body", "ProductStocks", "ProductStock").each do |sku|
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|
48
58
  hash[sku['SellerSku']] = sku['ReservedStock']
49
59
  end
50
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.34
4
+ version: 0.1.39
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-12 00:00:00.000000000 Z
11
+ date: 2021-07-05 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