item_builder 0.1.24 → 0.1.29
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/item_builder.rb +24 -8
- data/lib/item_builder/modes.rb +36 -0
- data/lib/item_builder/modes/quantity/base.rb +3 -19
- data/lib/item_builder/modes/quantity/zilingo_service.rb +71 -0
- data/lib/item_builder/modes/quantity_service.rb +7 -35
- data/lib/item_builder/modes/simple_service.rb +7 -1
- data/lib/item_builder/version.rb +1 -1
- 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: 99df6cad1a31d94df70b1deb5aa40b57d48eb6fab7a27c36a2843a0d81710aa9
|
4
|
+
data.tar.gz: e07088f915c42ce126e8ce943113d272312c65dd1961b703b9210ef4b6459da8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 996034fe165910db1cc1d7431492329383068effe64f8f014a98b333b1b6a94c8a166b19a9a113c84afe2eb4d7a95c4992a93e5104a3f3f59efb82b322619e4e
|
7
|
+
data.tar.gz: 20ffc61ea320c64af957763e8c69bc6aea2aebe0c3b62b99f38029d8d5f9df904b210125b0430965e0de4d9051ad5f2b78d7e67107d10590970a3709b8f0d74b
|
data/lib/item_builder.rb
CHANGED
@@ -26,25 +26,26 @@ class ItemBuilder
|
|
26
26
|
end
|
27
27
|
|
28
28
|
def mode_check
|
29
|
-
if mode == :quantity
|
30
|
-
|
29
|
+
if mode == :quantity || mode == :simple
|
30
|
+
quantity_simple_mode
|
31
31
|
else
|
32
32
|
default
|
33
33
|
end
|
34
34
|
end
|
35
35
|
|
36
|
-
def
|
36
|
+
def quantity_simple_mode
|
37
37
|
listings.map do |listing|
|
38
|
-
modes[mode].new(
|
38
|
+
modes[mode].new(qty_simple_params(listing)).perform
|
39
39
|
end
|
40
40
|
end
|
41
41
|
|
42
|
-
def
|
42
|
+
def qty_simple_params(listing)
|
43
43
|
{
|
44
44
|
listing: listing, wh_spaces: wh_spaces, variants: variants,
|
45
45
|
stock_allocs: stock_allocs, variant_listings: variant_listings,
|
46
46
|
bundles: bundles, item_bundle_variants: item_bundle_variants,
|
47
|
-
existing_alloc_stocks: existing_alloc_stocks
|
47
|
+
existing_alloc_stocks: existing_alloc_stocks,
|
48
|
+
reserved_stocks: reserved_stocks
|
48
49
|
}
|
49
50
|
end
|
50
51
|
|
@@ -70,8 +71,7 @@ class ItemBuilder
|
|
70
71
|
|
71
72
|
def item_bundle_variants
|
72
73
|
@item_bundle_variants ||= BundleVariant.where(
|
73
|
-
bundle_id: bundle_ids
|
74
|
-
channel_id: listings[0].channel_id
|
74
|
+
bundle_id: bundle_ids
|
75
75
|
)
|
76
76
|
end
|
77
77
|
|
@@ -114,4 +114,20 @@ class ItemBuilder
|
|
114
114
|
update: ItemBuilder::Modes::UpdateService
|
115
115
|
}
|
116
116
|
end
|
117
|
+
|
118
|
+
def order_host
|
119
|
+
url = ENV['ORDERS_URL'] || 'orders.forstok.com'
|
120
|
+
url + '/api/v3/item_line/reserved_stock'
|
121
|
+
end
|
122
|
+
|
123
|
+
def reserved_params
|
124
|
+
"account_id=#{listings[0].profile_channel_association_id}
|
125
|
+
&item_variant_ids=#{variant_ids.join(',')}"
|
126
|
+
end
|
127
|
+
|
128
|
+
def reserved_stocks
|
129
|
+
@reserved_stocks ||= JSON.parse(RestClient.get(
|
130
|
+
"#{order_host}?#{reserved_params}"
|
131
|
+
).body) if [3,13,18].include?(listings[0].channel_id)
|
132
|
+
end
|
117
133
|
end
|
data/lib/item_builder/modes.rb
CHANGED
@@ -16,6 +16,7 @@ class ItemBuilder
|
|
16
16
|
attr_reader :item_bundle_variants
|
17
17
|
attr_reader :existing_alloc_stocks
|
18
18
|
attr_reader :variant_listings
|
19
|
+
attr_reader :reserved_stocks
|
19
20
|
def initialize(args)
|
20
21
|
@listing = args.fetch(:listing)
|
21
22
|
@wh_spaces = args.fetch(:wh_spaces, [])
|
@@ -25,6 +26,7 @@ class ItemBuilder
|
|
25
26
|
@item_bundle_variants = args.fetch(:item_bundle_variants, [])
|
26
27
|
@existing_alloc_stocks = args.fetch(:existing_alloc_stocks, [])
|
27
28
|
@variant_listings = args.fetch(:variant_listings, [])
|
29
|
+
@reserved_stocks = args.fetch(:reserved_stocks, [])
|
28
30
|
end
|
29
31
|
|
30
32
|
def base
|
@@ -35,5 +37,39 @@ class ItemBuilder
|
|
35
37
|
sku: listing.sku
|
36
38
|
}
|
37
39
|
end
|
40
|
+
|
41
|
+
def existing_allocated_stock
|
42
|
+
existing_alloc_stocks.map do |els|
|
43
|
+
if listings.pluck(:id).include?(els.variant_association_id)
|
44
|
+
els
|
45
|
+
end
|
46
|
+
end.compact
|
47
|
+
end
|
48
|
+
|
49
|
+
def listings
|
50
|
+
variant_listings.select {|vl| vl.variant_id == listing.variant_id}
|
51
|
+
end
|
52
|
+
|
53
|
+
def bundle_variants
|
54
|
+
if bundle.present?
|
55
|
+
item_bundle_variants.select {|ibv| ibv.bundle_id == bundle.id }
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def bundle
|
60
|
+
bundles.select {|b| b.variant_id == listing.variant_id }.first
|
61
|
+
end
|
62
|
+
|
63
|
+
def stock_alloc
|
64
|
+
stock_allocs.select {|sa| sa.variant_association_id == listing.id }.first
|
65
|
+
end
|
66
|
+
|
67
|
+
def warehouse
|
68
|
+
wh_spaces.select {|ws| ws.item_variant_id == listing.variant_id }.first
|
69
|
+
end
|
70
|
+
|
71
|
+
def variant
|
72
|
+
variants.select {|v| v.id == listing.variant_id }.first
|
73
|
+
end
|
38
74
|
end
|
39
75
|
end
|
@@ -6,30 +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
10
|
|
10
|
-
def initialize(listing, available_quantity)
|
11
|
+
def initialize(listing, available_quantity, reserved_stock)
|
11
12
|
raise 'listing is not set' if listing.nil?
|
12
13
|
|
13
14
|
@listing = listing
|
14
15
|
@available_quantity = available_quantity
|
15
|
-
|
16
|
-
|
17
|
-
def order_host
|
18
|
-
url = ENV['ORDERS_URL'] || 'orders.forstok.com'
|
19
|
-
url + '/api/v2/item_line/reserved_stock'
|
20
|
-
end
|
21
|
-
|
22
|
-
def reserved_params
|
23
|
-
"account_id=#{listing.profile_channel_association_id}
|
24
|
-
&item_variant_id=#{listing.variant_id}"
|
25
|
-
end
|
26
|
-
|
27
|
-
def reserved_stock
|
28
|
-
RestClient.get("#{order_host}?#{reserved_params}")
|
29
|
-
end
|
30
|
-
|
31
|
-
def apigateway_get
|
32
|
-
RestClient.get("#{host}?#{params}")
|
16
|
+
@reserved_stock = reserved_stock
|
33
17
|
end
|
34
18
|
end
|
35
19
|
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'item_builder/modes/quantity/base'
|
4
|
+
class ItemBuilder
|
5
|
+
module Modes
|
6
|
+
module Quantity
|
7
|
+
class ZilingoService < Base
|
8
|
+
def perform
|
9
|
+
available_quantity - local_quantity.to_i
|
10
|
+
end
|
11
|
+
|
12
|
+
def local_quantity
|
13
|
+
quantities = sku_quantities(listing.local_item_id)
|
14
|
+
quantities[listing.local_id]
|
15
|
+
end
|
16
|
+
|
17
|
+
def headers
|
18
|
+
{ content_type: :json, accept: :json }
|
19
|
+
end
|
20
|
+
|
21
|
+
def data(item_id)
|
22
|
+
{
|
23
|
+
"credential": JSON.parse(credential)['credential'],
|
24
|
+
"data": { "item_id": item_id }
|
25
|
+
}.to_json
|
26
|
+
end
|
27
|
+
|
28
|
+
def credential
|
29
|
+
return @credential if @credential
|
30
|
+
|
31
|
+
account_id = listing.profile_channel_association_id
|
32
|
+
host = ENV['CREDENTIAL_URL'] || raise('credential url is not set')
|
33
|
+
url = "#{host}/credential?account_id=#{account_id}"
|
34
|
+
@credential = rest_client(method: :get, url: url)
|
35
|
+
end
|
36
|
+
|
37
|
+
def url
|
38
|
+
url = ENV['API_GATEWAY_URL'] || raise('api gateway is not set')
|
39
|
+
url + "/zilingo/item_quantities"
|
40
|
+
end
|
41
|
+
|
42
|
+
def sku_quantities(item_id)
|
43
|
+
args = {
|
44
|
+
method: :post, url: url, payload: data(item_id), headers: headers
|
45
|
+
}
|
46
|
+
resp = JSON.parse(rest_client(args, [200, 500, 406]))
|
47
|
+
response_process(resp)
|
48
|
+
end
|
49
|
+
|
50
|
+
def response_process(resp)
|
51
|
+
hash = Hash.new
|
52
|
+
resp['zilingoSKUQuantities'].each do |sku|
|
53
|
+
hash[sku['zilingoSKUId']] = sku['quantity'] + sku['frozenQuantity']
|
54
|
+
end
|
55
|
+
hash
|
56
|
+
end
|
57
|
+
|
58
|
+
def rest_client(params, rescued_codes = 200)
|
59
|
+
RestClient::Request.execute(params.merge(timeout: 3)) do |response|
|
60
|
+
code = response.code
|
61
|
+
unless Array.wrap(rescued_codes).include?(code)
|
62
|
+
raise "Response Code is #{code}"
|
63
|
+
end
|
64
|
+
|
65
|
+
response
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
@@ -6,6 +6,7 @@ require 'item_builder/get_quantity_service'
|
|
6
6
|
require 'item_builder/modes/quantity/base'
|
7
7
|
require 'item_builder/modes/quantity/lazada_service'
|
8
8
|
require 'item_builder/modes/quantity/zalora_service'
|
9
|
+
require 'item_builder/modes/quantity/zilingo_service'
|
9
10
|
class ItemBuilder
|
10
11
|
module Modes
|
11
12
|
class QuantityService
|
@@ -14,6 +15,7 @@ class ItemBuilder
|
|
14
15
|
QUANTITY_CHANNEL = {}.tap do |hash|
|
15
16
|
hash[3] = :Lazada
|
16
17
|
hash[13] = :Zalora
|
18
|
+
hash[18] = :Zilingo
|
17
19
|
end.freeze
|
18
20
|
|
19
21
|
def perform
|
@@ -37,13 +39,17 @@ class ItemBuilder
|
|
37
39
|
def qty_channel
|
38
40
|
class_name = "ItemBuilder::Modes::Quantity::#{channel_name}Service"
|
39
41
|
qty_channel_service = class_name.constantize
|
40
|
-
qty_channel_service.new(listing, available_quantity).perform
|
42
|
+
qty_channel_service.new(listing, available_quantity, reserved_stock).perform
|
41
43
|
end
|
42
44
|
|
43
45
|
def channel_name
|
44
46
|
QUANTITY_CHANNEL[listing.channel_id].to_s
|
45
47
|
end
|
46
48
|
|
49
|
+
def reserved_stock
|
50
|
+
reserved_stocks.find {|rs| rs['variant_id'] == listing.variant_id }['reserved_quantity']
|
51
|
+
end
|
52
|
+
|
47
53
|
def available_quantity
|
48
54
|
ItemBuilder::GetQuantityService.new(
|
49
55
|
listing: listing, variant: variant,
|
@@ -52,40 +58,6 @@ class ItemBuilder
|
|
52
58
|
existing_allocated_stock: existing_allocated_stock
|
53
59
|
).perform
|
54
60
|
end
|
55
|
-
|
56
|
-
def existing_allocated_stock
|
57
|
-
existing_alloc_stocks.map do |els|
|
58
|
-
if listings.pluck(:id).include?(els.variant_association_id)
|
59
|
-
els
|
60
|
-
end
|
61
|
-
end.compact
|
62
|
-
end
|
63
|
-
|
64
|
-
def listings
|
65
|
-
variant_listings.select {|vl| vl.variant_id == listing.variant_id}
|
66
|
-
end
|
67
|
-
|
68
|
-
def bundle_variants
|
69
|
-
if bundle.present?
|
70
|
-
item_bundle_variants.select {|ibv| ibv.bundle_id == bundle.id }
|
71
|
-
end
|
72
|
-
end
|
73
|
-
|
74
|
-
def bundle
|
75
|
-
bundles.select {|b| b.variant_id == listing.variant_id }.first
|
76
|
-
end
|
77
|
-
|
78
|
-
def stock_alloc
|
79
|
-
stock_allocs.select {|sa| sa.variant_association_id == listing.id }.first
|
80
|
-
end
|
81
|
-
|
82
|
-
def warehouse
|
83
|
-
wh_spaces.select {|ws| ws.item_variant_id == listing.variant_id }.first
|
84
|
-
end
|
85
|
-
|
86
|
-
def variant
|
87
|
-
variants.select {|v| v.id == listing.variant_id }.first
|
88
|
-
end
|
89
61
|
end
|
90
62
|
end
|
91
63
|
end
|
@@ -17,7 +17,13 @@ class ItemBuilder
|
|
17
17
|
end
|
18
18
|
|
19
19
|
def quantity
|
20
|
-
QuantityService.new(
|
20
|
+
QuantityService.new(
|
21
|
+
listing: listing, wh_spaces: wh_spaces, variants: variants,
|
22
|
+
stock_allocs: stock_allocs, variant_listings: variant_listings,
|
23
|
+
bundles: bundles, item_bundle_variants: item_bundle_variants,
|
24
|
+
existing_alloc_stocks: existing_alloc_stocks,
|
25
|
+
reserved_stocks: reserved_stocks
|
26
|
+
).to_h
|
21
27
|
end
|
22
28
|
|
23
29
|
def price
|
data/lib/item_builder/version.rb
CHANGED
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.29
|
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-02-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -131,6 +131,7 @@ files:
|
|
131
131
|
- lib/item_builder/modes/quantity/base.rb
|
132
132
|
- lib/item_builder/modes/quantity/lazada_service.rb
|
133
133
|
- lib/item_builder/modes/quantity/zalora_service.rb
|
134
|
+
- lib/item_builder/modes/quantity/zilingo_service.rb
|
134
135
|
- lib/item_builder/modes/quantity_service.rb
|
135
136
|
- lib/item_builder/modes/simple_service.rb
|
136
137
|
- lib/item_builder/modes/update/base.rb
|