Skip to content

Commit 8623ef1

Browse files
committed
Merge pull request #198 from openstax/fix_sync
Fix 500 errors on sync API calls
2 parents 8655590 + 5281c7c commit 8623ef1

9 files changed

+71
-68
lines changed

app/controllers/api/v1/application_groups_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ def updates
5454
5555
After you called the API and received your response, the group had a member added, setting unread_updates to 2.
5656
57-
`application_groups = {id: 13, read_updates: 1}` – will decrease unread_updates by 1, setting it to 1. The group will be sent again the next time you call `updates`, so you won't miss the updated information.
57+
`application_groups = {id: 13, read_updates: 1}` – will not affect the record. The group will be sent again the next time you call `updates`, so you won't miss the updated information.
5858
EOS
5959
def updated
6060
OSU::AccessPolicy.require_action_allowed!(:updated, current_api_user, ApplicationGroup)
61-
errors = MarkApplicationGroupsUpdatesAsRead.call(current_application,
61+
errors = MarkApplicationGroupUpdatesAsRead.call(current_application,
6262
ActiveSupport::JSON.decode(request.body)).errors
6363
head (errors.any? ? :internal_server_error : :no_content)
6464
end

app/controllers/api/v1/application_users_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,11 @@ def updates
196196
197197
After you called the API and received your response, the user updated their profile in Accounts, setting unread_updates to 2.
198198
199-
`application_users = {id: 13, read_updates: 1}` – will decrease unread_updates by 1, setting it to 1. The user will be sent again the next time you call `updates`, so you won't miss the updated information.
199+
`application_users = {id: 13, read_updates: 1}` – will not affect the record. The user will be sent again the next time you call `updates`, so you won't miss the updated information.
200200
EOS
201201
def updated
202202
OSU::AccessPolicy.require_action_allowed!(:updated, current_api_user, ApplicationUser)
203-
errors = MarkApplicationUsersUpdatesAsRead.call(current_application,
203+
errors = MarkApplicationUserUpdatesAsRead.call(current_application,
204204
ActiveSupport::JSON.decode(request.body)).errors
205205
head (errors.any? ? :internal_server_error : :no_content)
206206
end

app/controllers/users_controller.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ class UsersController < ApplicationController
22

33
skip_before_filter :registration, only: [:register]
44

5-
fine_print_skip :general_terms_of_use, :privacy_policy,
6-
only: [:register]
5+
fine_print_skip :general_terms_of_use, :privacy_policy, only: [:register]
76

87
def edit
98
OSU::AccessPolicy.require_action_allowed!(:update, current_user, current_user)
@@ -12,7 +11,7 @@ def edit
1211
def update
1312
OSU::AccessPolicy.require_action_allowed!(:update, current_user, current_user)
1413
if current_user.update_attributes(user_params)
15-
redirect_to profile_path, notice: 'Profile updated'
14+
redirect_to profile_path, notice: 'Your profile has been updated. These changes may take a few minutes to propagate to the entire site.'
1615
else
1716
flash.now[:alert] ||= []
1817
current_user.errors.full_messages.each do |msg|
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Routine for marking all given application groups' updates as read
2+
#
3+
# Caller provides the current application, plus an array of hashes with the following
4+
# 2 required keys: 'id' contains the ApplicationGroup's ID and
5+
# read_updates contains the last received value of unread_updates.
6+
7+
class MarkApplicationGroupUpdatesAsRead
8+
9+
# This transaction needs :repeatable_read to prevent missed updates
10+
lev_routine transaction: :repeatable_read
11+
12+
protected
13+
14+
def exec(application, application_group_hashes)
15+
return if application.blank? || application_group_hashes.blank?
16+
17+
application.application_groups.where do
18+
cumulative_query = nil
19+
application_group_hashes.each do |hash|
20+
query = (group_id == hash['group_id']) & (unread_updates == hash['read_updates'])
21+
cumulative_query = cumulative_query.nil? ? query : cumulative_query | query
22+
end
23+
cumulative_query
24+
end.update_all("unread_updates = 0")
25+
end
26+
27+
end

app/routines/mark_application_groups_updates_as_read.rb

Lines changed: 0 additions & 25 deletions
This file was deleted.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Routine for marking all given application users' updates as read
2+
#
3+
# Caller provides the current application, plus an array of hashes with the following
4+
# 2 required keys: 'id' contains the ApplicationUser's ID and
5+
# read_updates contains the last received value of unread_updates.
6+
7+
class MarkApplicationUserUpdatesAsRead
8+
9+
# This transaction needs :repeatable_read to prevent missed updates
10+
lev_routine transaction: :repeatable_read
11+
12+
protected
13+
14+
def exec(application, application_user_hashes)
15+
return if application.blank? || application_user_hashes.blank?
16+
17+
application.application_users.where do
18+
cumulative_query = nil
19+
application_user_hashes.each do |hash|
20+
query = (user_id == hash['user_id']) & (unread_updates == hash['read_updates'])
21+
cumulative_query = cumulative_query.nil? ? query : cumulative_query | query
22+
end
23+
cumulative_query
24+
end.update_all("unread_updates = 0")
25+
end
26+
27+
end

app/routines/mark_application_users_updates_as_read.rb

Lines changed: 0 additions & 25 deletions
This file was deleted.

spec/controllers/api/v1/application_groups_controller_spec.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -203,21 +203,21 @@
203203
expect(application_group_1.reload.unread_updates).to eq 4
204204

205205
api_put :updated, untrusted_application_token, raw_post_data: [
206-
{id: application_group_1.id, read_updates: 2}].to_json
206+
{group_id: group_1.id, read_updates: 2}].to_json
207207

208208
expect(response.status).to eq(204)
209209

210-
expect(application_group_1.reload.unread_updates).to eq 2
210+
expect(application_group_1.reload.unread_updates).to eq 4
211211

212212
api_put :updated, untrusted_application_token, raw_post_data: [
213-
{id: application_group_1.id, read_updates: 1}].to_json
213+
{group_id: group_1.id, read_updates: 1}].to_json
214214

215215
expect(response.status).to eq(204)
216216

217-
expect(application_group_1.reload.unread_updates).to eq 1
217+
expect(application_group_1.reload.unread_updates).to eq 4
218218

219219
api_put :updated, untrusted_application_token, raw_post_data: [
220-
{id: application_group_1.id, read_updates: 2}].to_json
220+
{group_id: group_1.id, read_updates: 4}].to_json
221221

222222
expect(response.status).to eq(204)
223223

spec/controllers/api/v1/application_users_controller_spec.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
expect(response.body).to eq(expected_response)
6262
end
6363

64-
it "raises not found when when not found" do
64+
it "raises not found when not found" do
6565
expect {
6666
api_get :find_by_username, untrusted_application_token, parameters: { username: 'foo' }
6767
}.to raise_error(ActiveRecord::RecordNotFound)
@@ -296,21 +296,21 @@
296296
expect(app_user.reload.unread_updates).to eq 4
297297

298298
api_put :updated, untrusted_application_token, raw_post_data: [
299-
{id: app_user.id, read_updates: 2}].to_json
299+
{user_id: user_2.id, read_updates: 2}].to_json
300300

301301
expect(response.status).to eq(204)
302302

303-
expect(app_user.reload.unread_updates).to eq 2
303+
expect(app_user.reload.unread_updates).to eq 4
304304

305305
api_put :updated, untrusted_application_token, raw_post_data: [
306-
{id: app_user.id, read_updates: 1}].to_json
306+
{user_id: user_2.id, read_updates: 1}].to_json
307307

308308
expect(response.status).to eq(204)
309309

310-
expect(app_user.reload.unread_updates).to eq 1
310+
expect(app_user.reload.unread_updates).to eq 4
311311

312312
api_put :updated, untrusted_application_token, raw_post_data: [
313-
{id: app_user.id, read_updates: 2}].to_json
313+
{user_id: user_2.id, read_updates: 4}].to_json
314314

315315
expect(response.status).to eq(204)
316316

0 commit comments

Comments
 (0)