workarea-core 3.4.42 → 3.4.43
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/models/workarea/metrics/user.rb +25 -0
- data/app/workers/workarea/update_email.rb +33 -0
- data/lib/workarea/changelog.rake +1 -1
- data/lib/workarea/version.rb +1 -1
- data/test/models/workarea/metrics/user_test.rb +51 -0
- data/test/workers/workarea/update_email_test.rb +39 -0
- metadata +4 -4
- data/app/workers/workarea/update_payment_profile_email.rb +0 -22
- data/test/workers/workarea/update_payment_profile_email_test.rb +0 -27
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 30d5b2273f510412bf81387ac4d229e05ef322219dba42166dffd16c2a30d458
|
4
|
+
data.tar.gz: 0163e5cd44474ad11c119debb3dd116a0637be8f514b8d920cab4e093f3f4578
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 00d52ab3b83bb7057218e4b70d668a98e85b1c953f2607b15826599d75d6970fdd681eea595b1270533889f90e75be3db8a539f4a16016578acd6c285fffb4aa
|
7
|
+
data.tar.gz: d73e489b510f361b0a75cbd705c1731ae6bea3693fd25ed3ee76d502ff4802724ba744442bade25b99171d444c0ec0f805591650a852fe74f8f7a5bfef83d5df
|
@@ -123,6 +123,31 @@ module Workarea
|
|
123
123
|
return nil if orders.zero?
|
124
124
|
revenue / orders.to_f
|
125
125
|
end
|
126
|
+
|
127
|
+
def merge!(other)
|
128
|
+
# To recalculate average_order_value
|
129
|
+
self.orders += other.orders
|
130
|
+
self.revenue += other.revenue
|
131
|
+
|
132
|
+
update = {
|
133
|
+
'$set' => {
|
134
|
+
average_order_value: average_order_value,
|
135
|
+
updated_at: Time.current.utc
|
136
|
+
},
|
137
|
+
'$inc' => {
|
138
|
+
orders: other.orders,
|
139
|
+
revenue: other.revenue,
|
140
|
+
discounts: other.discounts
|
141
|
+
}
|
142
|
+
}
|
143
|
+
|
144
|
+
update['$min'] = { first_order_at: other.first_order_at.utc } if other.first_order_at.present?
|
145
|
+
update['$max'] = { last_order_at: other.last_order_at.utc } if other.last_order_at.present?
|
146
|
+
|
147
|
+
self.class.collection.update_one({ _id: id }, update, upsert: true)
|
148
|
+
other.delete
|
149
|
+
reload
|
150
|
+
end
|
126
151
|
end
|
127
152
|
end
|
128
153
|
end
|
@@ -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
|
data/lib/workarea/changelog.rake
CHANGED
data/lib/workarea/version.rb
CHANGED
@@ -74,6 +74,57 @@ module Workarea
|
|
74
74
|
assert_equal(50, two.revenue_percentile)
|
75
75
|
assert_equal(50, two.average_order_value_percentile)
|
76
76
|
end
|
77
|
+
|
78
|
+
def test_merging_metrics
|
79
|
+
freeze_time
|
80
|
+
|
81
|
+
first = User.create!(
|
82
|
+
first_order_at: 2.weeks.ago,
|
83
|
+
last_order_at: 1.day.ago,
|
84
|
+
orders: 2,
|
85
|
+
revenue: 100,
|
86
|
+
discounts: -10,
|
87
|
+
average_order_value: 50,
|
88
|
+
)
|
89
|
+
|
90
|
+
first.merge!(User.new)
|
91
|
+
first.reload
|
92
|
+
assert_equal(1, Metrics::User.count)
|
93
|
+
assert_equal(2.weeks.ago, first.first_order_at)
|
94
|
+
assert_equal(1.day.ago, first.last_order_at)
|
95
|
+
assert_equal(2, first.orders)
|
96
|
+
assert_equal(100, first.revenue)
|
97
|
+
assert_equal(-10, first.discounts)
|
98
|
+
assert_equal(50, first.average_order_value)
|
99
|
+
|
100
|
+
second = User.create!(id: 'foo').tap { |u| u.merge!(first) }
|
101
|
+
second.reload
|
102
|
+
assert_equal(1, Metrics::User.count)
|
103
|
+
assert_equal(2.weeks.ago, second.first_order_at)
|
104
|
+
assert_equal(1.day.ago, second.last_order_at)
|
105
|
+
assert_equal(2, second.orders)
|
106
|
+
assert_equal(100, second.revenue)
|
107
|
+
assert_equal(-10, second.discounts)
|
108
|
+
assert_equal(50, second.average_order_value)
|
109
|
+
|
110
|
+
third = User.create!(
|
111
|
+
first_order_at: 3.weeks.ago,
|
112
|
+
last_order_at: 3.weeks.ago,
|
113
|
+
orders: 2,
|
114
|
+
revenue: 120,
|
115
|
+
average_order_value: 60,
|
116
|
+
)
|
117
|
+
|
118
|
+
third.merge!(second)
|
119
|
+
third.reload
|
120
|
+
assert_equal(1, Metrics::User.count)
|
121
|
+
assert_equal(3.weeks.ago, third.first_order_at)
|
122
|
+
assert_equal(1.day.ago, third.last_order_at)
|
123
|
+
assert_equal(4, third.orders)
|
124
|
+
assert_equal(220, third.revenue)
|
125
|
+
assert_equal(-10, third.discounts)
|
126
|
+
assert_equal(55, third.average_order_value)
|
127
|
+
end
|
77
128
|
end
|
78
129
|
end
|
79
130
|
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.4.
|
4
|
+
version: 3.4.43
|
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-
|
11
|
+
date: 2020-11-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -1711,7 +1711,7 @@ files:
|
|
1711
1711
|
- app/workers/workarea/status_reporter.rb
|
1712
1712
|
- app/workers/workarea/undo_release.rb
|
1713
1713
|
- app/workers/workarea/update_elasticsearch_settings.rb
|
1714
|
-
- app/workers/workarea/
|
1714
|
+
- app/workers/workarea/update_email.rb
|
1715
1715
|
- app/workers/workarea/verify_scheduled_releases.rb
|
1716
1716
|
- bin/rails
|
1717
1717
|
- config/i18n-js.yml
|
@@ -2368,7 +2368,7 @@ files:
|
|
2368
2368
|
- test/workers/workarea/send_refund_email_test.rb
|
2369
2369
|
- test/workers/workarea/status_reporter_test.rb
|
2370
2370
|
- test/workers/workarea/undo_release_test.rb
|
2371
|
-
- test/workers/workarea/
|
2371
|
+
- test/workers/workarea/update_email_test.rb
|
2372
2372
|
- test/workers/workarea/verify_scheduled_releases_test.rb
|
2373
2373
|
- vendor/active_shipping/.gitignore
|
2374
2374
|
- 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
|