Skip to content

Commit 92178cd

Browse files
authored
Merge pull request #21 from dxw/187-no-business-endpoint
187 Add task endpoint for submitting no business
2 parents 8575e23 + f46dd46 commit 92178cd

6 files changed

Lines changed: 70 additions & 2 deletions

File tree

app/controllers/v1/tasks_controller.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ def update
3737
end
3838
end
3939

40+
def no_business
41+
task = Task.find(params[:id])
42+
task.file_no_business!
43+
submission = task.latest_submission
44+
render jsonapi: submission, status: :created
45+
end
46+
4047
def complete
4148
task = Task.find(params[:id])
4249
task.status = 'completed'

app/models/task.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class Task < ApplicationRecord
99
state :cancelled
1010

1111
event :completed do
12-
transitions from: %i[in_progress], to: :completed
12+
transitions from: %i[unstarted in_progress], to: :completed
1313
end
1414
end
1515

@@ -19,6 +19,7 @@ class Task < ApplicationRecord
1919
belongs_to :supplier
2020

2121
has_many :submissions, dependent: :nullify
22+
has_one :latest_submission, -> { order(created_at: :desc) }, inverse_of: :task, class_name: 'Submission'
2223

2324
def self.for_user_id(user_id)
2425
supplier_ids = Membership
@@ -27,4 +28,11 @@ def self.for_user_id(user_id)
2728

2829
where(supplier_id: supplier_ids)
2930
end
31+
32+
def file_no_business!
33+
transaction do
34+
completed!
35+
submissions.create!(framework: framework, supplier: supplier, aasm_state: :completed)
36+
end
37+
end
3038
end

config/routes.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
end
1919
resources :tasks, only: %i[index show create update] do
2020
member do
21-
post 'complete', to: 'tasks#complete'
21+
post :complete
22+
post :no_business
2223
end
2324
end
2425

spec/models/task_spec.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,36 @@
3434
expect(tasks).to_not include(task3)
3535
end
3636
end
37+
38+
describe '#latest_submission' do
39+
let(:task) { FactoryBot.create(:task) }
40+
41+
it 'returns the most recent submission' do
42+
_old_submission = FactoryBot.create(:submission, task: task)
43+
44+
travel 1.day do
45+
latest_submission = FactoryBot.create(:submission, task: task)
46+
47+
expect(task.latest_submission).to eq latest_submission
48+
end
49+
end
50+
end
51+
52+
describe '#file_no_business!' do
53+
let(:task) { FactoryBot.create(:task) }
54+
55+
it 'creates an empty completed submission' do
56+
expect { task.file_no_business! }.to change { task.submissions.count }.by 1
57+
58+
submission = task.latest_submission
59+
60+
expect(submission).to be_completed
61+
expect(submission.entries).to be_empty
62+
end
63+
64+
it 'transitions the task to "completed"' do
65+
task.file_no_business!
66+
expect(task.reload).to be_completed
67+
end
68+
end
3769
end

spec/rails_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
RSpec.configure do |config|
3131
config.include JSONAPI::RSpec
3232
config.include RequestHelpers, type: :request
33+
config.include ActiveSupport::Testing::TimeHelpers
3334
Aws.config[:stub_responses] = true
3435

3536
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures

spec/requests/v1/tasks_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,25 @@
170170
end
171171
end
172172

173+
describe 'POST /v1/tasks/:task_id/no_business' do
174+
let(:task) { FactoryBot.create(:task) }
175+
176+
before { post "/v1/tasks/#{task.id}/no_business" }
177+
178+
it 'marks the task as completed' do
179+
expect(task.reload).to be_completed
180+
end
181+
182+
it 'creates and returns a completed submissions' do
183+
expect(response).to have_http_status(:created)
184+
185+
submission = task.submissions.last
186+
187+
expect(json['data']).to have_id submission.id
188+
expect(json['data']['attributes']['status']).to eq 'completed'
189+
end
190+
end
191+
173192
describe 'POST /v1/tasks/:task_id/complete' do
174193
it "changes a task's status to completed" do
175194
task = FactoryBot.create(:task)

0 commit comments

Comments
 (0)