Skip to content

Commit 5fbbf4d

Browse files
committed
(feature) Unfinished submissions cannot be completed
Only valid submissions that are 'in review' may be completed. Once this happens, we also mark the associated task as complete too. If either of these status changes fail, we return a 'bad request' HTTP status, else we return HTTP 204 No Content.
1 parent 2805e07 commit 5fbbf4d

4 files changed

Lines changed: 49 additions & 26 deletions

File tree

app/controllers/v1/submissions_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def complete
4949
submission = Submission.find(params[:id])
5050
complete_submission!(submission)
5151

52-
if submission.save
52+
if submission.errors.empty?
5353
head :no_content
5454
else
5555
render jsonapi_errors: submission.errors, status: :bad_request

app/models/submission.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,15 @@ class Submission < ApplicationRecord
2020
end
2121

2222
event :reviewed_and_accepted do
23-
transitions from: :in_review, to: :completed
23+
transitions from: :in_review, to: :completed, guard: :all_entries_valid?
24+
25+
error do |e|
26+
errors.add(:aasm_state, message: e.message)
27+
end
2428
end
2529
end
30+
31+
def all_entries_valid?
32+
entries.validated.count == entries.count
33+
end
2634
end

app/models/submission_completion.rb

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,10 @@ def initialize(submission)
66
end
77

88
def perform!
9-
return unless all_entries_valid?
10-
11-
Task.transaction do
12-
Submission.transaction do
13-
submission.reviewed_and_accepted!
14-
submission.task.completed!
15-
end
9+
submission.reviewed_and_accepted! do
10+
submission.task.completed!
1611
end
17-
end
18-
19-
private
2012

21-
def all_entries_valid?
22-
submission.entries.validated.count == submission.entries.count
13+
submission
2314
end
2415
end

spec/requests/v1/submissions_spec.rb

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -243,23 +243,47 @@
243243
end
244244

245245
describe 'POST /submissions/:submission_id/complete' do
246-
it 'marks the submission as complete' do
247-
task = FactoryBot.create(:task, status: :in_progress)
246+
context 'given a valid submission' do
247+
it 'marks the submission as complete' do
248+
task = FactoryBot.create(:task, status: :in_progress)
248249

249-
submission = FactoryBot.create(
250-
:submission_with_validated_entries,
251-
aasm_state: :in_review,
252-
task: task
253-
)
250+
submission = FactoryBot.create(
251+
:submission_with_validated_entries,
252+
aasm_state: :in_review,
253+
task: task
254+
)
254255

255-
post "/v1/submissions/#{submission.id}/complete"
256+
post "/v1/submissions/#{submission.id}/complete"
256257

257-
expect(response).to be_successful
258+
expect(response).to be_successful
258259

259-
submission.reload
260+
submission.reload
261+
262+
expect(submission).to be_completed
263+
expect(submission.task).to be_completed
264+
end
265+
end
266+
267+
context 'given an invalid submission' do
268+
it 'returns an error' do
269+
task = FactoryBot.create(:task, status: :in_progress)
270+
271+
submission = FactoryBot.create(
272+
:submission_with_invalid_entries,
273+
aasm_state: :in_review,
274+
task: task
275+
)
276+
277+
post "/v1/submissions/#{submission.id}/complete"
278+
279+
expect(response).to have_http_status(:bad_request)
280+
expect(json['errors'][0]['title']).to eql 'Invalid aasm_state'
281+
282+
submission.reload
260283

261-
expect(submission).to be_completed
262-
expect(submission.task).to be_completed
284+
expect(submission).to be_in_review
285+
expect(task).to be_in_progress
286+
end
263287
end
264288
end
265289
end

0 commit comments

Comments
 (0)