item_builder_mwh 0.1.19 → 0.1.23
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_mwh.rb +12 -0
- data/lib/item_builder_mwh/get_quantity_service.rb +11 -1
- data/lib/item_builder_mwh/modes.rb +26 -2
- data/lib/item_builder_mwh/modes/quantity_service.rb +37 -12
- data/lib/item_builder_mwh/shopify_quantity_service.rb +120 -0
- data/lib/item_builder_mwh/version.rb +1 -1
- data/lib/item_builder_mwh/zalora_quantity_service.rb +2 -0
- 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: b3a0e01c128197865e478b27fe4c9de5fbb46a020a160474c94306eb9a733a26
|
4
|
+
data.tar.gz: 1b970a5f68404642e15ab1595d67509b6e2fe231f8d27832491088bb75babe0f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0cf9c7b481de437b1e68deafd22510a391c55cb3181d19e303aa71c1d22b1ad929929fa9c8ce5ccafe7eae81fa9f54f97da3ec1abff269ce72bc3fabb0a9d188
|
7
|
+
data.tar.gz: 91c7701900ca7a4f5fe5a0b386ee3130c5ef2f5a882a7f353ca5a2a779346d0dad2e8c9b2325e83711f391ad8f96a9fdda6f8430cc0b61565fed32bde73a0355
|
data/lib/item_builder_mwh.rb
CHANGED
@@ -9,6 +9,7 @@ require 'item_builder_mwh/modes/simple_service'
|
|
9
9
|
require 'item_models'
|
10
10
|
require 'item_builder_mwh/zilingo_quantity_service'
|
11
11
|
require 'item_builder_mwh/zalora_quantity_service'
|
12
|
+
require 'item_builder_mwh/shopify_quantity_service'
|
12
13
|
|
13
14
|
class ItemBuilderMwh
|
14
15
|
attr_reader :listing_ids
|
@@ -45,6 +46,11 @@ class ItemBuilderMwh
|
|
45
46
|
new_param = qty_simple_params(listing)
|
46
47
|
.merge({zalora_reserved_stock: zalora_reserved_stock})
|
47
48
|
|
49
|
+
modes[mode].new(new_param).perform
|
50
|
+
elsif listing.channel_id == 2
|
51
|
+
new_param = qty_simple_params(listing)
|
52
|
+
.merge({shopify_inventory_location: shopify_inventory_location})
|
53
|
+
|
48
54
|
modes[mode].new(new_param).perform
|
49
55
|
else
|
50
56
|
modes[mode].new(qty_simple_params(listing)).perform
|
@@ -138,6 +144,12 @@ class ItemBuilderMwh
|
|
138
144
|
).perform
|
139
145
|
end
|
140
146
|
|
147
|
+
def shopify_inventory_location
|
148
|
+
@shopify_inventory_location ||= ItemBuilderMwh::ShopifyQuantityService.new(
|
149
|
+
listings: listings, skus: skus
|
150
|
+
).perform
|
151
|
+
end
|
152
|
+
|
141
153
|
def order_host
|
142
154
|
url = ENV['ORDERS_URL'] || 'orders.forstok.com'
|
143
155
|
url + '/api/v3/item_line/reserved_stock'
|
@@ -12,6 +12,7 @@ class ItemBuilderMwh
|
|
12
12
|
@bundle_variants = args.fetch(:bundle_variants)
|
13
13
|
@existing_allocated_stock = args.fetch(:existing_allocated_stock)
|
14
14
|
@listing_wh_sp_quantity = args.fetch(:listing_wh_sp_quantity)
|
15
|
+
@wh_routing = args.fetch(:wh_routing, 0)
|
15
16
|
end
|
16
17
|
|
17
18
|
def perform
|
@@ -70,7 +71,16 @@ class ItemBuilderMwh
|
|
70
71
|
|
71
72
|
def qty_default
|
72
73
|
# set lower limit for quantity
|
73
|
-
|
74
|
+
if @wh_routing == 1
|
75
|
+
[listing_warehouse_routing_quantity, 0].sort[1]
|
76
|
+
else
|
77
|
+
[@listing_wh_sp_quantity, 0].sort[1]
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def listing_warehouse_routing_quantity
|
82
|
+
warehouse_spaces = WarehouseSpace.where(item_variant_id: variant.id)
|
83
|
+
warehouse_spaces.sum(:quantity)
|
74
84
|
end
|
75
85
|
|
76
86
|
def qty_bundle
|
@@ -18,6 +18,7 @@ class ItemBuilderMwh
|
|
18
18
|
attr_reader :reserved_stocks
|
19
19
|
attr_reader :zilingo_quantity
|
20
20
|
attr_reader :zalora_reserved_stock
|
21
|
+
attr_reader :shopify_inventory_location
|
21
22
|
def initialize(args)
|
22
23
|
@listing = args.fetch(:listing)
|
23
24
|
@wh_spaces = args.fetch(:wh_spaces, [])
|
@@ -30,6 +31,7 @@ class ItemBuilderMwh
|
|
30
31
|
@reserved_stocks = args.fetch(:reserved_stocks, [])
|
31
32
|
@zilingo_quantity = args.fetch(:zilingo_quantity, [])
|
32
33
|
@zalora_reserved_stock = args.fetch(:zalora_reserved_stock, [])
|
34
|
+
@shopify_inventory_location = args.fetch(:shopify_inventory_location, {})
|
33
35
|
warehouse
|
34
36
|
end
|
35
37
|
|
@@ -73,8 +75,12 @@ class ItemBuilderMwh
|
|
73
75
|
end
|
74
76
|
|
75
77
|
def warehouse
|
76
|
-
|
77
|
-
warehouses << to_h(
|
78
|
+
if multiwarehouse && wh_routing == 1
|
79
|
+
warehouses << to_h(warehouse_spaces.first)
|
80
|
+
else
|
81
|
+
warehouse_spaces.each do |warehouse_space|
|
82
|
+
warehouses << to_h(warehouse_space)
|
83
|
+
end
|
78
84
|
end
|
79
85
|
end
|
80
86
|
|
@@ -104,6 +110,24 @@ class ItemBuilderMwh
|
|
104
110
|
warehouse_mapping(warehouse_id)[0].channel_warehouse_id
|
105
111
|
end
|
106
112
|
|
113
|
+
def credential
|
114
|
+
return @shopify_inventory_location['credential'] if @shopify_inventory_location['credential'].present?
|
115
|
+
|
116
|
+
nil
|
117
|
+
end
|
118
|
+
|
119
|
+
def multiwarehouse
|
120
|
+
return credential['multiwarehouse'] if credential.present?
|
121
|
+
|
122
|
+
false
|
123
|
+
end
|
124
|
+
|
125
|
+
def wh_routing
|
126
|
+
return credential['warehouse_routing'] if credential.present? && credential['warehouse_routing'].present?
|
127
|
+
|
128
|
+
0
|
129
|
+
end
|
130
|
+
|
107
131
|
def warehouses
|
108
132
|
@warehouses ||= []
|
109
133
|
end
|
@@ -14,28 +14,41 @@ class ItemBuilderMwh
|
|
14
14
|
include Modes
|
15
15
|
|
16
16
|
QUANTITY_CHANNEL = {}.tap do |hash|
|
17
|
-
hash[
|
17
|
+
hash[2] = :Shopify
|
18
18
|
hash[13] = :Zalora
|
19
19
|
hash[18] = :Zilingo
|
20
20
|
end.freeze
|
21
21
|
|
22
22
|
def perform
|
23
|
-
|
24
|
-
warehouse: warehouses
|
25
|
-
|
23
|
+
if channel_name == "Shopify"
|
24
|
+
base.merge!(shopify_inventory_location[listing.local_id], warehouse: warehouses )
|
25
|
+
else
|
26
|
+
base.merge!(
|
27
|
+
warehouse: warehouses
|
28
|
+
)
|
29
|
+
end
|
26
30
|
end
|
27
31
|
|
28
32
|
def to_h(warehouse_space)
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
33
|
+
if channel_name == "Zilingo"
|
34
|
+
{
|
35
|
+
quantity: qty(warehouse_space.quantity),
|
36
|
+
warehouse_id: wh_mapping(
|
37
|
+
warehouse_space.warehouse_id
|
38
|
+
)
|
39
|
+
}
|
40
|
+
else
|
41
|
+
{
|
42
|
+
quantity: [qty(warehouse_space.quantity), 0].sort[1],
|
43
|
+
warehouse_id: wh_mapping(
|
44
|
+
warehouse_space.warehouse_id
|
45
|
+
)
|
46
|
+
}
|
47
|
+
end
|
35
48
|
end
|
36
49
|
|
37
50
|
def qty(qty)
|
38
|
-
if channel_name.empty?
|
51
|
+
if channel_name.empty? || channel_name == "Shopify"
|
39
52
|
available_quantity(qty)
|
40
53
|
else
|
41
54
|
qty_channel(qty)
|
@@ -66,12 +79,24 @@ class ItemBuilderMwh
|
|
66
79
|
end
|
67
80
|
end
|
68
81
|
|
82
|
+
def credential
|
83
|
+
return shopify_inventory_location['credential'] if shopify_inventory_location['credential'].present?
|
84
|
+
|
85
|
+
nil
|
86
|
+
end
|
87
|
+
|
88
|
+
def wh_routing
|
89
|
+
return credential['warehouse_routing'] if credential.present? && credential['warehouse_routing'].present?
|
90
|
+
|
91
|
+
0
|
92
|
+
end
|
93
|
+
|
69
94
|
def available_quantity(qty)
|
70
95
|
ItemBuilderMwh::GetQuantityService.new(
|
71
96
|
listing: listing, stock_alloc: stock_alloc,
|
72
97
|
variant: variant, bundle_variants: bundle_variants,
|
73
98
|
existing_allocated_stock: existing_allocated_stock,
|
74
|
-
listing_wh_sp_quantity: qty
|
99
|
+
listing_wh_sp_quantity: qty, wh_routing: wh_routing
|
75
100
|
).perform
|
76
101
|
end
|
77
102
|
end
|
@@ -0,0 +1,120 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'item_builder_mwh/modes.rb'
|
4
|
+
class ItemBuilderMwh
|
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
|
+
@listings.each do |listing|
|
17
|
+
@listing = listing
|
18
|
+
@sku = listing.local_id
|
19
|
+
datas[@sku] = response_process
|
20
|
+
end
|
21
|
+
|
22
|
+
# update variant real_local_id database icava
|
23
|
+
VariantListing.update(@variant_listings.keys, @variant_listings.values)
|
24
|
+
datas['credential'] = JSON.parse(credential)['credential']
|
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 = {
|
35
|
+
method: :post, url: url_variant, payload: data_variant, headers: headers
|
36
|
+
}
|
37
|
+
|
38
|
+
resp = JSON.parse(rest_client(args, [200, 500, 406]))
|
39
|
+
|
40
|
+
hash = { listing_id => { "real_local_id" => resp['inventory_item_id'] } }
|
41
|
+
@variant_listings = @variant_listings.merge(hash)
|
42
|
+
|
43
|
+
resp['inventory_item_id']
|
44
|
+
else
|
45
|
+
# real_local_id exists
|
46
|
+
real_local_id
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def location_id
|
51
|
+
cred = JSON.parse(credential)['credential']
|
52
|
+
if cred['primary_warehouse_id'].present?
|
53
|
+
cred['primary_warehouse_id']
|
54
|
+
else
|
55
|
+
# get data from shopify
|
56
|
+
args = {
|
57
|
+
method: :post, url: url_location, payload: data_location, headers: headers
|
58
|
+
}
|
59
|
+
|
60
|
+
resp = JSON.parse(rest_client(args, [200, 500, 406]))
|
61
|
+
resp['id']
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def headers
|
66
|
+
{ content_type: :json, accept: :json }
|
67
|
+
end
|
68
|
+
|
69
|
+
def data_location
|
70
|
+
{
|
71
|
+
"credential": JSON.parse(credential)['credential']
|
72
|
+
}.to_json
|
73
|
+
end
|
74
|
+
|
75
|
+
def data_variant
|
76
|
+
{
|
77
|
+
"credential": JSON.parse(credential)['credential'],
|
78
|
+
"data": { "local_id": @sku }
|
79
|
+
}.to_json
|
80
|
+
end
|
81
|
+
|
82
|
+
def credential
|
83
|
+
return @credential if @credential
|
84
|
+
|
85
|
+
host = ENV['CREDENTIAL_URL'] || raise('credential url is not set')
|
86
|
+
url = "#{host}/credential?account_id=#{@account_id}"
|
87
|
+
@credential = rest_client(method: :get, url: url)
|
88
|
+
end
|
89
|
+
|
90
|
+
def url_variant
|
91
|
+
url = ENV['API_GATEWAY_URL'] || raise('api gateway is not set')
|
92
|
+
url + "/shopify/data_variant"
|
93
|
+
end
|
94
|
+
|
95
|
+
def url_location
|
96
|
+
url = ENV['API_GATEWAY_URL'] || raise('api gateway is not set')
|
97
|
+
url + "/shopify/data_location"
|
98
|
+
end
|
99
|
+
|
100
|
+
def response_process
|
101
|
+
hash = Hash.new
|
102
|
+
|
103
|
+
hash[:inventory_item_id] = inventory_item_id.to_i
|
104
|
+
hash[:location_id] = location_id.to_i
|
105
|
+
|
106
|
+
hash
|
107
|
+
end
|
108
|
+
|
109
|
+
def rest_client(params, rescued_codes = 200)
|
110
|
+
RestClient::Request.execute(params.merge(timeout: 3)) do |response|
|
111
|
+
code = response.code
|
112
|
+
unless Array.wrap(rescued_codes).include?(code)
|
113
|
+
raise "Response Code is #{code}"
|
114
|
+
end
|
115
|
+
|
116
|
+
response
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
@@ -47,6 +47,8 @@ class ItemBuilderMwh
|
|
47
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
|
+
elsif resp.dig('ErrorResponse').present?
|
51
|
+
return nil
|
50
52
|
end
|
51
53
|
success_handle(resp)
|
52
54
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: item_builder_mwh
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.23
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kevin Ivander
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-08-02 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_mwh/modes/simple/shopify_service.rb
|
144
144
|
- lib/item_builder_mwh/modes/simple/zalora_service.rb
|
145
145
|
- lib/item_builder_mwh/modes/simple_service.rb
|
146
|
+
- lib/item_builder_mwh/shopify_quantity_service.rb
|
146
147
|
- lib/item_builder_mwh/version.rb
|
147
148
|
- lib/item_builder_mwh/zalora_quantity_service.rb
|
148
149
|
- lib/item_builder_mwh/zilingo_quantity_service.rb
|