workarea-core 3.5.21 → 3.5.22

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: 95264a0fb5c9e405852a21f6c8b590f54c97a64b9fb72bc97156dbc57ec36c01
4
- data.tar.gz: 6811a1d019494db24af2cbc60ec987f71d58c96b27677bc5260780c939619be0
3
+ metadata.gz: 48dfcf76f720a117fd8f3a16cdbef856816837692dc53f731b38729c5d37b9ef
4
+ data.tar.gz: a1ffa660f618f5d1b428ce4db9c6d768f0a12c3490ea45f02d699fa4b5ff441f
5
5
  SHA512:
6
- metadata.gz: 64268e7f2384a9812fd40613e5b0466a5e28945ad7b7f68a3cf33392ff27fb2b1030ce802b69f4d569ee08e5a8efac51cdb0c75359bec5376825018458eef126
7
- data.tar.gz: 6aa1f3a86cc08c3d2f1ab9dab6bf0087c01533d53b3fc38f157e92a834cbc6cddfc9838a8351a0d4cd1e2d754cc274081e83dfe9b62aa9b6437c47e958762f23
6
+ metadata.gz: 509b598d21369c93345837d160e2a95d271c4c0d06276192f050a2997badec0b1542a012be9c1ed10d7bb176816372825abc08370d8fb67b3cd961a96ecb6246
7
+ data.tar.gz: 0cf582e5e981a1524f7b1cb2229f542c2d99f8b152802386aa68a42a09de7c84602ee8c07659011ecc3a1bec1ca6ddcfdb85c2e7aa8ecdcf6333c42808b81027
@@ -27,7 +27,9 @@ module Workarea
27
27
  cookies.delete(:email)
28
28
  elsif email != cookies.signed[:email]
29
29
  unless impersonating?
30
- Metrics::User.find_or_initialize_by(id: email).merge!(current_visit&.metrics)
30
+ Metrics::User
31
+ .find_or_initialize_by(id: email)
32
+ .merge_views!(current_visit&.metrics)
31
33
  end
32
34
 
33
35
  cookies.permanent.signed[:email] = email
@@ -110,6 +110,8 @@ module Workarea
110
110
  end
111
111
 
112
112
  def merge!(other)
113
+ return if other.blank?
114
+
113
115
  # To recalculate average_order_value
114
116
  self.orders += other.orders
115
117
  self.revenue += other.revenue
@@ -132,14 +134,8 @@ module Workarea
132
134
  update['$max'] = { last_order_at: other.last_order_at.utc } if other.last_order_at.present?
133
135
 
134
136
  self.class.collection.update_one({ _id: id }, update, upsert: true)
137
+ other.delete
135
138
 
136
- self.class.save_affinity(
137
- id: id,
138
- action: 'viewed',
139
- product_ids: other.viewed.product_ids,
140
- category_ids: other.viewed.category_ids,
141
- search_ids: other.viewed.search_ids
142
- )
143
139
  self.class.save_affinity(
144
140
  id: id,
145
141
  action: 'purchased',
@@ -148,7 +144,21 @@ module Workarea
148
144
  search_ids: other.purchased.search_ids
149
145
  )
150
146
 
151
- reload
147
+ merge_views!(other)
148
+ end
149
+
150
+ def merge_views!(other)
151
+ return if other.blank?
152
+
153
+ self.class.save_affinity(
154
+ id: id,
155
+ action: 'viewed',
156
+ product_ids: other.viewed.product_ids,
157
+ category_ids: other.viewed.category_ids,
158
+ search_ids: other.viewed.search_ids
159
+ )
160
+
161
+ reload rescue self # save_affinity might not have actually persisted anything
152
162
  end
153
163
  end
154
164
  end
@@ -129,6 +129,14 @@ module Workarea
129
129
  scoped.sort_by { |r| [r.publish_at, r.created_at] }
130
130
  end
131
131
 
132
+ def self.schedule_affected_by_changesets(changesets)
133
+ changesets
134
+ .uniq(&:release)
135
+ .reject { |cs| cs.release.blank? }
136
+ .flat_map { |cs| [cs.release] + cs.release.scheduled_after }
137
+ .uniq
138
+ end
139
+
132
140
  def as_current
133
141
  self.class.with_current(self) { yield }
134
142
  end
@@ -83,11 +83,7 @@ module Workarea
83
83
  end
84
84
 
85
85
  def releases
86
- changesets
87
- .uniq(&:release)
88
- .reject { |cs| cs.release.blank? }
89
- .flat_map { |cs| [cs.release] + cs.release.scheduled_after }
90
- .uniq
86
+ Release.schedule_affected_by_changesets(changesets)
91
87
  end
92
88
 
93
89
  def as_document
@@ -122,21 +122,8 @@ module Workarea
122
122
  ProductPrimaryImageUrl.new(model).path
123
123
  end
124
124
 
125
- # All {Releasable}s that could affect the product's Elasticsearch document
126
- # should add their changesets to this method.
127
- #
128
- # @example Add to the changesets affecting a product in a decorator
129
- # def changesets
130
- # super.merge(SomeReleasable.for_product(product.id).changesets_with_children)
131
- # end
132
- #
133
- # @return [Mongoid::Criteria]
134
- #
135
125
  def changesets
136
- criteria = model.changesets_with_children
137
- pricing.each { |ps| criteria.merge!(ps.changesets_with_children) }
138
- criteria.merge!(FeaturedProducts.changesets(model.id))
139
- criteria.includes(:release)
126
+ ProductReleases.new(model).changesets
140
127
  end
141
128
 
142
129
  private
@@ -1,10 +1,4 @@
1
1
  module Workarea
2
- #
3
- # TODO remove in v3.6
4
- #
5
- # This is no longer used, this logic was moved into the search models to allow
6
- # it to be used for any model (not just products).
7
- #
8
2
  class ProductReleases
9
3
  attr_reader :product
10
4
 
@@ -13,11 +7,7 @@ module Workarea
13
7
  end
14
8
 
15
9
  def releases
16
- changesets
17
- .uniq(&:release)
18
- .reject { |cs| cs.release.blank? }
19
- .flat_map { |cs| [cs.release] + cs.release.scheduled_after }
20
- .uniq
10
+ Release.schedule_affected_by_changesets(changesets)
21
11
  end
22
12
 
23
13
  # All {Releasable}s that could affect the product's Elasticsearch document
@@ -0,0 +1,33 @@
1
+ module Workarea
2
+ class UpdateEmail
3
+ include Sidekiq::Worker
4
+ include Sidekiq::CallbacksWorker
5
+
6
+ sidekiq_options(
7
+ enqueue_on: { User => :update, with: -> { [id, changes] } }
8
+ )
9
+
10
+ def perform(id, changes)
11
+ return unless changes['email'].present? && changes['email'].first.present?
12
+
13
+ old_email, new_email = changes['email']
14
+ update_payment_profile(id, old_email, new_email)
15
+ update_metrics(old_email, new_email)
16
+ end
17
+
18
+ def update_payment_profile(id, old_email, new_email)
19
+ user = User.find(id)
20
+ user.email = old_email # set old email so we lookup by old email value
21
+
22
+ Payment::Profile.update_email(PaymentReference.new(user), new_email)
23
+ end
24
+
25
+ def update_metrics(old_email, new_email)
26
+ old_metrics = Metrics::User.find(old_email) rescue nil
27
+ return if old_metrics.blank?
28
+
29
+ new_metrics = Metrics::User.find_or_initialize_by(id: new_email)
30
+ new_metrics.merge!(old_metrics)
31
+ end
32
+ end
33
+ end
@@ -38,7 +38,7 @@ namespace :workarea do
38
38
  author: author
39
39
  }
40
40
 
41
- if subject.start_with?('Revert')
41
+ if subject.start_with?('Revert "')
42
42
  reverts << body.match(/[a-f0-9]{40}/)
43
43
  reverts << sha
44
44
  end
@@ -2,7 +2,7 @@ module Workarea
2
2
  module VERSION
3
3
  MAJOR = 3
4
4
  MINOR = 5
5
- PATCH = 21
5
+ PATCH = 22
6
6
  PRE = nil
7
7
  STRING = [MAJOR, MINOR, PATCH, PRE].compact.join('.')
8
8
 
@@ -134,7 +134,7 @@ module Workarea
134
134
  def test_merging_metrics
135
135
  freeze_time
136
136
 
137
- metrics = User.create!(
137
+ first = User.create!(
138
138
  first_order_at: 2.weeks.ago,
139
139
  last_order_at: 1.day.ago,
140
140
  orders: 2,
@@ -147,41 +147,43 @@ module Workarea
147
147
  purchased: { product_ids: ['qoo'], category_ids: ['quo'], search_ids: ['qux'] }
148
148
  )
149
149
 
150
- metrics.merge!(User.new)
151
- metrics.reload
152
- assert_equal(2.weeks.ago, metrics.first_order_at)
153
- assert_equal(1.day.ago, metrics.last_order_at)
154
- assert_equal(2, metrics.orders)
155
- assert_equal(100, metrics.revenue)
156
- assert_equal(-10, metrics.discounts)
157
- assert_equal(50, metrics.average_order_value)
158
- assert_equal(1, metrics.cancellations)
159
- assert_equal(-20, metrics.refund)
160
- assert_equal(['foo'], metrics.viewed.product_ids)
161
- assert_equal(['bar'], metrics.viewed.category_ids)
162
- assert_equal(['baz'], metrics.viewed.search_ids)
163
- assert_equal(['qoo'], metrics.purchased.product_ids)
164
- assert_equal(['quo'], metrics.purchased.category_ids)
165
- assert_equal(['qux'], metrics.purchased.search_ids)
166
-
167
- blank = User.create!(id: 'foo').tap { |u| u.merge!(metrics) }
168
- blank.reload
169
- assert_equal(2.weeks.ago, blank.first_order_at)
170
- assert_equal(1.day.ago, blank.last_order_at)
171
- assert_equal(2, blank.orders)
172
- assert_equal(100, blank.revenue)
173
- assert_equal(-10, blank.discounts)
174
- assert_equal(50, blank.average_order_value)
175
- assert_equal(1, blank.cancellations)
176
- assert_equal(-20, blank.refund)
177
- assert_equal(['foo'], blank.viewed.product_ids)
178
- assert_equal(['bar'], blank.viewed.category_ids)
179
- assert_equal(['baz'], blank.viewed.search_ids)
180
- assert_equal(['qoo'], blank.purchased.product_ids)
181
- assert_equal(['quo'], blank.purchased.category_ids)
182
- assert_equal(['qux'], blank.purchased.search_ids)
183
-
184
- existing = User.create!(
150
+ first.merge!(User.new)
151
+ first.reload
152
+ assert_equal(1, Metrics::User.count)
153
+ assert_equal(2.weeks.ago, first.first_order_at)
154
+ assert_equal(1.day.ago, first.last_order_at)
155
+ assert_equal(2, first.orders)
156
+ assert_equal(100, first.revenue)
157
+ assert_equal(-10, first.discounts)
158
+ assert_equal(50, first.average_order_value)
159
+ assert_equal(1, first.cancellations)
160
+ assert_equal(-20, first.refund)
161
+ assert_equal(['foo'], first.viewed.product_ids)
162
+ assert_equal(['bar'], first.viewed.category_ids)
163
+ assert_equal(['baz'], first.viewed.search_ids)
164
+ assert_equal(['qoo'], first.purchased.product_ids)
165
+ assert_equal(['quo'], first.purchased.category_ids)
166
+ assert_equal(['qux'], first.purchased.search_ids)
167
+
168
+ second = User.create!(id: 'foo').tap { |u| u.merge!(first) }
169
+ second.reload
170
+ assert_equal(1, Metrics::User.count)
171
+ assert_equal(2.weeks.ago, second.first_order_at)
172
+ assert_equal(1.day.ago, second.last_order_at)
173
+ assert_equal(2, second.orders)
174
+ assert_equal(100, second.revenue)
175
+ assert_equal(-10, second.discounts)
176
+ assert_equal(50, second.average_order_value)
177
+ assert_equal(1, second.cancellations)
178
+ assert_equal(-20, second.refund)
179
+ assert_equal(['foo'], second.viewed.product_ids)
180
+ assert_equal(['bar'], second.viewed.category_ids)
181
+ assert_equal(['baz'], second.viewed.search_ids)
182
+ assert_equal(['qoo'], second.purchased.product_ids)
183
+ assert_equal(['quo'], second.purchased.category_ids)
184
+ assert_equal(['qux'], second.purchased.search_ids)
185
+
186
+ third = User.create!(
185
187
  first_order_at: 3.weeks.ago,
186
188
  last_order_at: 3.weeks.ago,
187
189
  orders: 2,
@@ -191,22 +193,23 @@ module Workarea
191
193
  purchased: { product_ids: ['four'], category_ids: ['five'], search_ids: ['six'] }
192
194
  )
193
195
 
194
- existing.merge!(metrics)
195
- existing.reload
196
- assert_equal(3.weeks.ago, existing.first_order_at)
197
- assert_equal(1.day.ago, existing.last_order_at)
198
- assert_equal(4, existing.orders)
199
- assert_equal(220, existing.revenue)
200
- assert_equal(-10, existing.discounts)
201
- assert_equal(55, existing.average_order_value)
202
- assert_equal(1, existing.cancellations)
203
- assert_equal(-20, existing.refund)
204
- assert_equal(%w(one foo), existing.viewed.product_ids)
205
- assert_equal(%w(two bar), existing.viewed.category_ids)
206
- assert_equal(%w(three baz), existing.viewed.search_ids)
207
- assert_equal(%w(four qoo), existing.purchased.product_ids)
208
- assert_equal(%w(five quo), existing.purchased.category_ids)
209
- assert_equal(%w(six qux), existing.purchased.search_ids)
196
+ third.merge!(second)
197
+ third.reload
198
+ assert_equal(1, Metrics::User.count)
199
+ assert_equal(3.weeks.ago, third.first_order_at)
200
+ assert_equal(1.day.ago, third.last_order_at)
201
+ assert_equal(4, third.orders)
202
+ assert_equal(220, third.revenue)
203
+ assert_equal(-10, third.discounts)
204
+ assert_equal(55, third.average_order_value)
205
+ assert_equal(1, third.cancellations)
206
+ assert_equal(-20, third.refund)
207
+ assert_equal(%w(one foo), third.viewed.product_ids)
208
+ assert_equal(%w(two bar), third.viewed.category_ids)
209
+ assert_equal(%w(three baz), third.viewed.search_ids)
210
+ assert_equal(%w(four qoo), third.purchased.product_ids)
211
+ assert_equal(%w(five quo), third.purchased.category_ids)
212
+ assert_equal(%w(six qux), third.purchased.search_ids)
210
213
  end
211
214
  end
212
215
  end
@@ -0,0 +1,39 @@
1
+ require 'test_helper'
2
+
3
+ module Workarea
4
+ class UpdateEmailTest < TestCase
5
+ def test_updating_payment_profile
6
+ user = create_user(email: 'user@workarea.com')
7
+ profile = Payment::Profile.lookup(PaymentReference.new(user))
8
+
9
+ UpdateEmail.new.perform(user.id.to_s, 'email' => [nil, 'user@workarea.com'])
10
+ assert_equal(profile.reload.email, 'user@workarea.com')
11
+
12
+ UpdateEmail.new.perform(
13
+ user.id.to_s,
14
+ 'email' => ['user@workarea.com', 'test@workarea.com']
15
+ )
16
+ assert_equal(profile.reload.email, 'test@workarea.com')
17
+ end
18
+
19
+ def test_updating_metrics
20
+ user = create_user(email: 'user@workarea.com')
21
+ old_metrics = Metrics::User.find_or_initialize_by(id: 'user@workarea.com')
22
+ old_metrics.update!(orders: 3)
23
+
24
+ UpdateEmail.new.perform(user.id.to_s, 'email' => [nil, 'user@workarea.com'])
25
+ assert_equal(1, Metrics::User.count)
26
+ assert_equal(3, old_metrics.reload.orders)
27
+
28
+ new_metrics = Metrics::User.create!(id: 'test@workarea.com', orders: 1)
29
+
30
+ UpdateEmail.new.perform(
31
+ user.id.to_s,
32
+ 'email' => ['user@workarea.com', 'test@workarea.com']
33
+ )
34
+ assert_equal(1, Metrics::User.count)
35
+ assert_raises(Mongoid::Errors::DocumentNotFound) { old_metrics.reload }
36
+ assert_equal(4, new_metrics.reload.orders)
37
+ end
38
+ end
39
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: workarea-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.5.21
4
+ version: 3.5.22
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Crouse
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-10-14 00:00:00.000000000 Z
11
+ date: 2020-11-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -1795,7 +1795,7 @@ files:
1795
1795
  - app/workers/workarea/status_reporter.rb
1796
1796
  - app/workers/workarea/synchronize_user_metrics.rb
1797
1797
  - app/workers/workarea/update_elasticsearch_settings.rb
1798
- - app/workers/workarea/update_payment_profile_email.rb
1798
+ - app/workers/workarea/update_email.rb
1799
1799
  - app/workers/workarea/verify_scheduled_releases.rb
1800
1800
  - bin/rails
1801
1801
  - config/i18n-js.yml
@@ -2499,7 +2499,7 @@ files:
2499
2499
  - test/workers/workarea/send_refund_email_test.rb
2500
2500
  - test/workers/workarea/status_reporter_test.rb
2501
2501
  - test/workers/workarea/synchronize_user_metrics_test.rb
2502
- - test/workers/workarea/update_payment_profile_email_test.rb
2502
+ - test/workers/workarea/update_email_test.rb
2503
2503
  - test/workers/workarea/verify_scheduled_releases_test.rb
2504
2504
  - vendor/active_shipping/.gitignore
2505
2505
  - vendor/active_shipping/.travis.yml
@@ -1,22 +0,0 @@
1
- module Workarea
2
- class UpdatePaymentProfileEmail
3
- include Sidekiq::Worker
4
- include Sidekiq::CallbacksWorker
5
-
6
- sidekiq_options(
7
- enqueue_on: { User => :update, with: -> { [id, changes] } }
8
- )
9
-
10
- def perform(id, changes)
11
- if changes['email'].present? && changes['email'].first.present?
12
- old_email = changes['email'].first
13
- new_email = changes['email'].last
14
-
15
- user = User.find(id)
16
- user.email = old_email # set old email so we lookup by old email value
17
-
18
- Payment::Profile.update_email(PaymentReference.new(user), new_email)
19
- end
20
- end
21
- end
22
- end
@@ -1,27 +0,0 @@
1
- require 'test_helper'
2
-
3
- module Workarea
4
- class UpdatePaymentProfileEmailTest < Workarea::TestCase
5
- setup do
6
- @user = create_user(email: 'user@workarea.com')
7
- @profile = Payment::Profile.lookup(PaymentReference.new(@user))
8
- @worker = UpdatePaymentProfileEmail.new
9
- end
10
-
11
- def test_updating_payment_profile_email_address
12
- @worker.perform(
13
- @user.id.to_s,
14
- 'email' => ['user@workarea.com', 'test@workarea.com']
15
- )
16
-
17
- @profile.reload
18
- assert_equal(@profile.email, 'test@workarea.com')
19
- end
20
-
21
- def test_skipping_update_if_email_change_is_nil
22
- @worker.perform(@user.id.to_s, 'email' => [nil, 'user@workarea.com'])
23
- @profile.reload
24
- assert_equal(@profile.email, 'user@workarea.com')
25
- end
26
- end
27
- end