item_builder 0.1.36 → 0.1.41

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: 950910a70e41e602df25095660a238c5ee33d9c8b21169686e45e60a4fa20c4e
4
- data.tar.gz: 57ae5913a690907f317a01a8d37d740165687249d53e4ded6eb0cd19e1665c58
3
+ metadata.gz: ff44f1879704b4fd441241688415eac4160d7ab8f9ef8fbc39d98d1f7ed7e985
4
+ data.tar.gz: cae5a688bb97a68d74e6cb55c4bc18fcfaee523f9f572eb4c5a615ab94945cfd
5
5
  SHA512:
6
- metadata.gz: 88c6c7f55bd6a10aeec1c21073883b6f9afbaf94c9992d0be75af4a23d53bf5deff63c60afdc60b73e7a991f1c69768a7753252f9374a4e59450ab7c1755d426
7
- data.tar.gz: cc0ff00ce0cb0e56c04c1f3ec489ebcb53fd50de7ca726343747dd265425fd388f4eb420bdf9419323cd98f5e42f2dddaf61b098338ab9459a468ef861e47fd5
6
+ metadata.gz: bfa8ec3f6af0a9046c4d60a150789cf075f85060f00f6bc0b46106780b7d341dee8a1fe791c23404439a0b42b22801d52802c5277ae785327b9cbd2eea9ba6eb
7
+ data.tar.gz: cd0129f8b41c40fb291057eb76afc377594025431b8f44662871cac3a94a422c6370720fba3b558ff1e1100b2b63b3086a173e0e07998d64ee201de531ff300d
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
@@ -13,23 +13,31 @@ class ItemBuilder
13
13
  include Modes
14
14
 
15
15
  QUANTITY_CHANNEL = {}.tap do |hash|
16
- hash[3] = :Lazada
16
+ hash[2] = :Shopify
17
17
  hash[13] = :Zalora
18
18
  hash[18] = :Zilingo
19
19
  end.freeze
20
20
 
21
21
  def perform
22
- to_h.merge(base)
22
+ if channel_name == "Shopify"
23
+ base.merge(to_h, shopify_inventory_location[listing.local_id])
24
+ else
25
+ to_h.merge(base)
26
+ end
23
27
  end
24
28
 
25
29
  def to_h
26
- {
27
- quantity: [qty, 0].sort[1]
28
- }
30
+ if channel_name == "Zilingo"
31
+ { quantity: qty }
32
+ else
33
+ {
34
+ quantity: [qty, 0].sort[1]
35
+ }
36
+ end
29
37
  end
30
38
 
31
39
  def qty
32
- if channel_name.empty?
40
+ if channel_name.empty? || channel_name == "Shopify"
33
41
  available_quantity
34
42
  else
35
43
  qty_channel
@@ -39,21 +47,24 @@ class ItemBuilder
39
47
  def qty_channel
40
48
  class_name = "ItemBuilder::Modes::Quantity::#{channel_name}Service"
41
49
  qty_channel_service = class_name.constantize
42
- qty_channel_service.new(listing, available_quantity, reserved_stock).perform
50
+ qty_channel_service.new(listing, available_quantity, local_qty.to_i).perform
43
51
  end
44
52
 
45
53
  def channel_name
46
54
  QUANTITY_CHANNEL[listing.channel_id].to_s
47
55
  end
48
56
 
49
- def reserved_stock
57
+ def local_qty
50
58
  if channel_name == 'Zilingo'
51
- zilingo_delta_quantity[listing.local_id].to_i
59
+ return 0 if zilingo_quantity.blank?
60
+
61
+ zilingo_quantity[listing.local_id].to_i
52
62
  elsif channel_name == 'Zalora'
53
63
  return 0 if zalora_reserved_stock.blank?
64
+
54
65
  zalora_reserved_stock[listing.local_id].to_i
55
66
  else
56
- reserved_stocks.find {|rs| rs['variant_id'] == listing.variant_id }['reserved_quantity']
67
+ reserved_stocks.find { |rs| rs['variant_id'] == listing.variant_id }['reserved_quantity']
57
68
  end
58
69
  end
59
70
 
@@ -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.36'
4
+ VERSION = '0.1.41'
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,21 +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")}"
46
+ if resp.dig('response_code') && resp.dig('response_code') != 200
47
+ raise "Response Code is #{resp.dig('response_code')}"
48
48
  elsif resp.dig('ErrorResponse', 'Head', 'ErrorMessage') == 'E009: Access Denied'
49
49
  return nil
50
50
  end
51
+
51
52
  success_handle(resp)
52
53
  end
53
54
 
54
55
  def success_handle(resp)
55
- hash = Hash.new
56
- resp.dig("SuccessResponse", "Body", "ProductStocks", "ProductStock").each do |sku|
56
+ hash = {}
57
+ resp.dig('SuccessResponse', 'Body', 'ProductStocks', 'ProductStock').each do |sku|
57
58
  hash[sku['SellerSku']] = sku['ReservedStock']
58
59
  end
59
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.36
4
+ version: 0.1.41
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-30 00:00:00.000000000 Z
11
+ date: 2021-07-09 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