item_builder 0.1.25 → 0.1.30

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: f7fe5aee9e5ad53c176f35a5f67d68dedcc66bb5475861c432865a1fe762252f
4
- data.tar.gz: d1c4b8a261c1a9839d273b3a9c2fb4aad7a4a5ac62cfec359e9c7b5f1cbc38ed
3
+ metadata.gz: 4b05a2126b02a1494542d1dfa5c7889dde9df101efb6cc2d36a2b639483deaeb
4
+ data.tar.gz: c5a27afae1de6da5ee31ced96d1924df675d7b0ae38c8765e2f0b770d00a9c58
5
5
  SHA512:
6
- metadata.gz: 4293e1f028ae58b3a9b1925adad9f24a3e9aeb1262254691c6bda8138cce5dbddcba15011259bdfaadc473ef92b1cd9fa7bc5373f9de2608ee63f60401151bdd
7
- data.tar.gz: b6a48cb493ad29427f51a8d85dc6abf529b2ad09ad8e6498aa606c8d940146f21a3c05d4c63e8d9d732f50171d9f08ed578aab8990ce43bf3243ba241eb7343c
6
+ metadata.gz: 79681dbd1b388370204afc5b517ec7ac1cf8943ca1ac3f798f7f4667fc215597f68b888bcc31a42b48b98e285b60c5ebb0ed1a3d3961221e84f4ca3b5f2c41d3
7
+ data.tar.gz: 36e3a579e47f0533dca84a9cf8482320d6bf4607af85aac3c360de8d50830fd061010323bd9a2686897790a87c927ad5f2b308e84397c812cd605816ee7f6991
data/lib/item_builder.rb CHANGED
@@ -7,6 +7,7 @@ require 'item_builder/modes/price_service'
7
7
  require 'item_builder/modes/quantity_service'
8
8
  require 'item_builder/modes/simple_service'
9
9
  require 'item_models'
10
+ require 'item_builder/zilingo_quantity_service'
10
11
 
11
12
  class ItemBuilder
12
13
  def self.build(listing_ids, mode)
@@ -26,26 +27,27 @@ class ItemBuilder
26
27
  end
27
28
 
28
29
  def mode_check
29
- if mode == :quantity
30
- quantity_mode
30
+ if mode == :quantity || mode == :simple
31
+ quantity_simple_mode
31
32
  else
32
33
  default
33
34
  end
34
35
  end
35
36
 
36
- def quantity_mode
37
+ def quantity_simple_mode
37
38
  listings.map do |listing|
38
- modes[mode].new(qty_params(listing)).perform
39
+ modes[mode].new(qty_simple_params(listing)).perform
39
40
  end
40
41
  end
41
42
 
42
- def qty_params(listing)
43
+ def qty_simple_params(listing)
43
44
  {
44
45
  listing: listing, wh_spaces: wh_spaces, variants: variants,
45
46
  stock_allocs: stock_allocs, variant_listings: variant_listings,
46
47
  bundles: bundles, item_bundle_variants: item_bundle_variants,
47
48
  existing_alloc_stocks: existing_alloc_stocks,
48
- reserved_stocks: reserved_stocks
49
+ reserved_stocks: reserved_stocks,
50
+ zilingo_delta_quantity: zilingo_delta_quantity
49
51
  }
50
52
  end
51
53
 
@@ -71,8 +73,7 @@ class ItemBuilder
71
73
 
72
74
  def item_bundle_variants
73
75
  @item_bundle_variants ||= BundleVariant.where(
74
- bundle_id: bundle_ids,
75
- channel_id: listings[0].channel_id
76
+ bundle_id: bundle_ids
76
77
  )
77
78
  end
78
79
 
@@ -106,6 +107,16 @@ class ItemBuilder
106
107
  @listings ||= VariantListing.where(id: listing_ids)
107
108
  end
108
109
 
110
+ def skus
111
+ @skus ||= listings.map(&:local_id).uniq
112
+ end
113
+
114
+ def zilingo_delta_quantity
115
+ @zilingo_delta_quantity ||= ItemBuilder::ZilingoQuantityService.new(
116
+ listings: listings, skus: skus
117
+ ).perform
118
+ end
119
+
109
120
  def modes
110
121
  {
111
122
  price: ItemBuilder::Modes::PriceService,
@@ -17,6 +17,7 @@ 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
21
  def initialize(args)
21
22
  @listing = args.fetch(:listing)
22
23
  @wh_spaces = args.fetch(:wh_spaces, [])
@@ -27,6 +28,7 @@ class ItemBuilder
27
28
  @existing_alloc_stocks = args.fetch(:existing_alloc_stocks, [])
28
29
  @variant_listings = args.fetch(:variant_listings, [])
29
30
  @reserved_stocks = args.fetch(:reserved_stocks, [])
31
+ @zilingo_delta_quantity = args.fetch(:zilingo_delta_quantity)
30
32
  end
31
33
 
32
34
  def base
@@ -37,5 +39,39 @@ class ItemBuilder
37
39
  sku: listing.sku
38
40
  }
39
41
  end
42
+
43
+ def existing_allocated_stock
44
+ existing_alloc_stocks.map do |els|
45
+ if listings.pluck(:id).include?(els.variant_association_id)
46
+ els
47
+ end
48
+ end.compact
49
+ end
50
+
51
+ def listings
52
+ variant_listings.select {|vl| vl.variant_id == listing.variant_id}
53
+ end
54
+
55
+ def bundle_variants
56
+ if bundle.present?
57
+ item_bundle_variants.select {|ibv| ibv.bundle_id == bundle.id }
58
+ end
59
+ end
60
+
61
+ def bundle
62
+ bundles.select {|b| b.variant_id == listing.variant_id }.first
63
+ end
64
+
65
+ def stock_alloc
66
+ stock_allocs.select {|sa| sa.variant_association_id == listing.id }.first
67
+ end
68
+
69
+ def warehouse
70
+ wh_spaces.select {|ws| ws.item_variant_id == listing.variant_id }.first
71
+ end
72
+
73
+ def variant
74
+ variants.select {|v| v.id == listing.variant_id }.first
75
+ end
40
76
  end
41
77
  end
@@ -0,0 +1,14 @@
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 - reserved_stock.to_i
10
+ end
11
+ end
12
+ end
13
+ end
14
+ 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
@@ -45,7 +47,11 @@ class ItemBuilder
45
47
  end
46
48
 
47
49
  def reserved_stock
48
- reserved_stocks.find {|rs| rs['variant_id'] == listing.variant_id }['reserved_quantity']
50
+ if channel_name == 'Zilingo'
51
+ zilingo_delta_quantity[listing.local_id]
52
+ else
53
+ reserved_stocks.find {|rs| rs['variant_id'] == listing.variant_id }['reserved_quantity']
54
+ end
49
55
  end
50
56
 
51
57
  def available_quantity
@@ -56,40 +62,6 @@ class ItemBuilder
56
62
  existing_allocated_stock: existing_allocated_stock
57
63
  ).perform
58
64
  end
59
-
60
- def existing_allocated_stock
61
- existing_alloc_stocks.map do |els|
62
- if listings.pluck(:id).include?(els.variant_association_id)
63
- els
64
- end
65
- end.compact
66
- end
67
-
68
- def listings
69
- variant_listings.select {|vl| vl.variant_id == listing.variant_id}
70
- end
71
-
72
- def bundle_variants
73
- if bundle.present?
74
- item_bundle_variants.select {|ibv| ibv.bundle_id == bundle.id }
75
- end
76
- end
77
-
78
- def bundle
79
- bundles.select {|b| b.variant_id == listing.variant_id }.first
80
- end
81
-
82
- def stock_alloc
83
- stock_allocs.select {|sa| sa.variant_association_id == listing.id }.first
84
- end
85
-
86
- def warehouse
87
- wh_spaces.select {|ws| ws.item_variant_id == listing.variant_id }.first
88
- end
89
-
90
- def variant
91
- variants.select {|v| v.id == listing.variant_id }.first
92
- end
93
65
  end
94
66
  end
95
67
  end
@@ -17,7 +17,13 @@ class ItemBuilder
17
17
  end
18
18
 
19
19
  def quantity
20
- QuantityService.new(listing: listing).to_h
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class ItemBuilder
4
- VERSION = '0.1.25'
4
+ VERSION = '0.1.30'
5
5
  end
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'item_builder/modes.rb'
4
+ class ItemBuilder
5
+ class ZilingoQuantityService
6
+ attr_reader :listings, :skus
7
+ def initialize(args)
8
+ @listings = args.fetch(:listings)
9
+ @skus = args.fetch(:skus)
10
+ end
11
+
12
+ def perform
13
+ args = {
14
+ method: :post, url: url, payload: data, headers: headers
15
+ }
16
+ resp = JSON.parse(rest_client(args, [200, 500, 406]))
17
+ response_process(resp)
18
+ end
19
+
20
+ def headers
21
+ { content_type: :json, accept: :json }
22
+ end
23
+
24
+ def data
25
+ {
26
+ "credential": JSON.parse(credential)['credential'],
27
+ "data": { "skus": skus }
28
+ }.to_json
29
+ end
30
+
31
+ def credential
32
+ return @credential if @credential
33
+
34
+ account_id = listings[0].profile_channel_association_id
35
+ host = ENV['CREDENTIAL_URL'] || raise('credential url is not set')
36
+ url = "#{host}/credential?account_id=#{account_id}"
37
+ @credential = rest_client(method: :get, url: url)
38
+ end
39
+
40
+ def url
41
+ url = ENV['API_GATEWAY_URL'] || raise('api gateway is not set')
42
+ url + "/zilingo/item_quantity_by_sku"
43
+ end
44
+
45
+ def response_process(resp)
46
+ hash = Hash.new
47
+ resp['zilingoSKUQuantities'].each do |sku|
48
+ hash[sku['zilingoSKUId']] = sku['quantity'] + sku['frozenQuantity']
49
+ end
50
+ hash
51
+ end
52
+
53
+ def rest_client(params, rescued_codes = 200)
54
+ RestClient::Request.execute(params.merge(timeout: 3)) do |response|
55
+ code = response.code
56
+ unless Array.wrap(rescued_codes).include?(code)
57
+ raise "Response Code is #{code}"
58
+ end
59
+
60
+ response
61
+ end
62
+ end
63
+ end
64
+ 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.25
4
+ version: 0.1.30
5
5
  platform: ruby
6
6
  authors:
7
7
  - okaaryanata
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-01-29 00:00:00.000000000 Z
11
+ date: 2021-02-16 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
@@ -143,6 +144,7 @@ files:
143
144
  - lib/item_builder/modes/update/zalora_service.rb
144
145
  - lib/item_builder/modes/update_service.rb
145
146
  - lib/item_builder/version.rb
147
+ - lib/item_builder/zilingo_quantity_service.rb
146
148
  homepage:
147
149
  licenses: []
148
150
  metadata: {}