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 +4 -4
- data/lib/item_builder.rb +22 -7
- data/lib/item_builder/get_quantity_service.rb +1 -1
- data/lib/item_builder/modes.rb +10 -8
- data/lib/item_builder/modes/active_service.rb +3 -3
- data/lib/item_builder/modes/quantity/base.rb +3 -3
- data/lib/item_builder/modes/quantity/lazada_service.rb +1 -1
- data/lib/item_builder/modes/quantity/zalora_service.rb +1 -1
- data/lib/item_builder/modes/quantity/zilingo_service.rb +1 -1
- data/lib/item_builder/modes/quantity_service.rb +21 -10
- data/lib/item_builder/modes/simple_service.rb +2 -2
- data/lib/item_builder/shopify_quantity_service.rb +118 -0
- data/lib/item_builder/version.rb +1 -1
- data/lib/item_builder/zalora_quantity_service.rb +7 -6
- data/lib/item_builder/zilingo_quantity_service.rb +3 -3
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ff44f1879704b4fd441241688415eac4160d7ab8f9ef8fbc39d98d1f7ed7e985
|
4
|
+
data.tar.gz: cae5a688bb97a68d74e6cb55c4bc18fcfaee523f9f572eb4c5a615ab94945cfd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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(
|
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(
|
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
|
132
|
-
@
|
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,
|
data/lib/item_builder/modes.rb
CHANGED
@@ -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 :
|
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
|
-
@
|
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
|
-
|
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 :
|
9
|
+
attr_reader :local_qty
|
10
10
|
|
11
|
-
def initialize(listing, available_quantity,
|
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
|
-
@
|
16
|
+
@local_qty = local_qty
|
17
17
|
end
|
18
18
|
end
|
19
19
|
end
|
@@ -13,23 +13,31 @@ class ItemBuilder
|
|
13
13
|
include Modes
|
14
14
|
|
15
15
|
QUANTITY_CHANNEL = {}.tap do |hash|
|
16
|
-
hash[
|
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
|
-
|
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:
|
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,
|
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
|
57
|
+
def local_qty
|
50
58
|
if channel_name == 'Zilingo'
|
51
|
-
|
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
|
-
|
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
|
data/lib/item_builder/version.rb
CHANGED
@@ -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 +
|
42
|
+
url + '/zalora/product_stocks'
|
43
43
|
end
|
44
44
|
|
45
45
|
def response_process(resp)
|
46
|
-
if resp.dig(
|
47
|
-
raise "Response Code is #{resp.dig(
|
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 =
|
56
|
-
resp.dig(
|
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 +
|
42
|
+
url + '/zilingo/item_quantity_by_sku'
|
43
43
|
end
|
44
44
|
|
45
45
|
def response_process(resp)
|
46
|
-
hash =
|
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.
|
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-
|
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
|