From 5afccb84c925f02b1f15507def440b700db72d55 Mon Sep 17 00:00:00 2001 From: Tekin Suleyman Date: Tue, 31 Jul 2018 11:07:26 +0100 Subject: [PATCH] API exposes the latest submission for a task To be able to show the user the status of their task, we need to be able to get the most recent submission. This makes it available as part of the API. --- app/serializable/serializable_task.rb | 1 + spec/requests/v1/tasks_spec.rb | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/app/serializable/serializable_task.rb b/app/serializable/serializable_task.rb index 0413cce55..746f42da8 100644 --- a/app/serializable/serializable_task.rb +++ b/app/serializable/serializable_task.rb @@ -1,6 +1,7 @@ class SerializableTask < JSONAPI::Serializable::Resource type 'tasks' + has_one :latest_submission has_many :submissions belongs_to :framework diff --git a/spec/requests/v1/tasks_spec.rb b/spec/requests/v1/tasks_spec.rb index 2f28c7946..b26f2347a 100644 --- a/spec/requests/v1/tasks_spec.rb +++ b/spec/requests/v1/tasks_spec.rb @@ -61,7 +61,7 @@ expect(json['data'][2]).to have_attribute(:status).with_value('in_progress') end - it 'can optionally return included models' do + it 'can optionally include submissions' do task = FactoryBot.create(:task) submission = FactoryBot.create(:submission, task: task, aasm_state: 'pending') @@ -75,6 +75,22 @@ expect(json['included'][0]) .to have_attribute(:status).with_value('pending') end + + it 'can optionally include the latest_submission' do + task = FactoryBot.create(:task) + submission = FactoryBot.create(:submission, task: task, aasm_state: 'pending') + + get '/v1/tasks?include=latest_submission' + + expect(response).to be_successful + expect(json['data'][0]).to have_id(task.id) + expect(json['data'][0]) + .to have_relationship(:latest_submission) + .with_data('id' => submission.id, 'type' => 'submissions') + + expect(json['included'][0]) + .to have_attribute(:status).with_value('pending') + end end describe 'GET /tasks?filter[status]=' do