Add step reordering and step element reordering service endpoints to API [SCI-6891][SCI-6892] (#4179)

* Add step reordering service endpoint to API [SCI-6891]

* Generalize reorder validation [SCI-6891]

* Add endpoint for reordering step elements, fix issues [SCI-6892]

* Add appropriate serializers [SCI-6891][SCI-6892]

* Add step elements to step serializer [SCI-6891]

* Simplify routes, add locking [SCI-6891]
This commit is contained in:
artoscinote 2022-07-12 10:13:47 +02:00 committed by GitHub
parent 82132c865c
commit dd27fadd98
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 390 additions and 1 deletions

View file

@ -0,0 +1,44 @@
# frozen_string_literal: true
module Api
module Service
class ProtocolsController < BaseController
include Api::Service::ReorderValidation
before_action :load_protocol
before_action :validate_step_order, only: :reorder_steps
def reorder_steps
@protocol.with_lock do
step_reorder_params.each do |order|
# rubocop:disable Rails/SkipsModelValidations
@protocol.steps.find(order['id']).update_column(:position, order['position'])
# rubocop:enable Rails/SkipsModelValidations
end
rescue StandardError
head :bad_request
end
render json: @protocol.steps, each_serializer: Api::V1::StepSerializer
end
private
def load_protocol
@protocol = Protocol.find(params.require(:protocol_id))
raise PermissionError.new(Protocol, :manage) unless can_manage_protocol_in_module?(@protocol)
end
def step_reorder_params
params.require(:step_order).map { |o| o.permit(:id, :position).to_h }
end
def validate_step_order
unless reorder_params_valid?(@protocol.steps, step_reorder_params)
render json: { error: I18n.t('activerecord.errors.models.protocol.attributes.step_order.invalid') },
status: :bad_request
end
end
end
end
end

View file

@ -0,0 +1,48 @@
# frozen_string_literal: true
module Api
module Service
class StepsController < BaseController
include Api::Service::ReorderValidation
before_action :load_step
before_action :validate_element_order, only: :reorder_elements
def reorder_elements
@step.with_lock do
step_element_reorder_params.each do |order|
# rubocop:disable Rails/SkipsModelValidations
@step.step_orderable_elements.find(order['id']).update_column(:position, order['position'])
# rubocop:enable Rails/SkipsModelValidations
end
rescue StandardError
head :bad_request
end
render json: @step.step_orderable_elements, each_serializer: Api::V1::StepOrderableElementSerializer
end
private
def load_step
@step = Step.find(params.require(:step_id))
raise PermissionError.new(Protocol, :manage) unless can_manage_protocol_in_module?(@step.protocol)
end
def step_element_reorder_params
params.require(:step_element_order).map { |o| o.permit(:id, :position).to_h }
end
def validate_element_order
unless reorder_params_valid?(@step.step_orderable_elements, step_element_reorder_params)
render(
json:
{
error: I18n.t('activerecord.errors.models.step.attributes.step_orderable_elements_order.invalid')
},
status: :bad_request
)
end
end
end
end
end

View file

@ -0,0 +1,16 @@
# frozen_string_literal: true
module Api
module Service
module ReorderValidation
extend ActiveSupport::Concern
def reorder_params_valid?(collection, reorder_params)
# contains all collection ids, positions have values from 0 to number of items in collection - 1
collection.order(:id).pluck(:id) == reorder_params.pluck(:id).sort &&
reorder_params.pluck(:position).sort == (0...reorder_params.length).to_a
end
end
end
end

View file

@ -0,0 +1,20 @@
# frozen_string_literal: true
module Api
module V1
class StepOrderableElementSerializer < ActiveModel::Serializer
attributes :position, :element
def element
case object.orderable_type
when 'Checklist'
ChecklistSerializer.new(object.orderable).as_json
when 'StepTable'
TableSerializer.new(object.orderable.table).as_json
when 'StepText'
StepTextSerializer.new(object.orderable).as_json
end
end
end
end
end

View file

@ -17,6 +17,7 @@ module Api
has_many :tables, serializer: TableSerializer
has_many :step_texts, serializer: StepTextSerializer
has_many :step_comments, key: :comments, serializer: CommentSerializer
has_many :step_orderable_elements, key: :step_elements, serializer: StepOrderableElementSerializer
include TimestampableModel

View file

@ -0,0 +1,16 @@
# frozen_string_literal: true
module Api
module V1
class StepTextSerializer < ActiveModel::Serializer
type :tables
attributes :id, :text
include TimestampableModel
def contents
object.text&.force_encoding(Encoding::UTF_8)
end
end
end
end

View file

@ -129,6 +129,14 @@ en:
attributes:
team_id:
same_team: "Inventory can't be shared to the same team as it belongs to"
protocol:
attributes:
step_order:
invalid: "Invalid step order."
step:
attributes:
step_orderable_element_order:
invalid: "Invalid step element order"
my_module:
attributes:
my_module_status_id:

View file

@ -715,8 +715,16 @@ Rails.application.routes.draw do
get 'status', to: 'api#status'
namespace :service do
post 'projects_json_export', to: 'projects_json_export#projects_json_export'
resources :teams, except: %i(index new create show edit update destroy) do
resources :teams, only: [] do
post 'clone_experiment' => 'experiments#clone'
resources :protocols, only: [] do
post 'reorder_steps' => 'protocols#reorder_steps'
end
resources :steps, only: [] do
post 'reorder_elements' => 'steps#reorder_elements'
end
end
end
if Rails.configuration.x.core_api_v1_enabled

View file

@ -0,0 +1,9 @@
# frozen_string_literal: true
FactoryBot.define do
factory :step_orderable_element do
orderable { create :step_text, step: step }
step
position { step ? step.step_orderable_elements.count : Faker::Number.between(from: 1, to: 10) }
end
end

View file

@ -0,0 +1,109 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe "Api::Service::ProtocolsController", type: :request do
before :all do
@user = create(:user)
@team = create(:team, created_by: @user)
create(:user_team, user: @user, team: @team, role: 2)
@project = create(:project, name: Faker::Name.unique.name, created_by: @user, team: @team)
@experiment = create(:experiment, created_by: @user, last_modified_by: @user, project: @project, created_by: @user)
@my_module = create(
:my_module,
:with_due_date,
created_by: @user,
last_modified_by: @user,
experiment: @experiment
)
@protocol = create(:protocol, team: @team, my_module: @my_module, name: "Test protocol")
create_list(:step, 3, protocol: @protocol)
@valid_headers =
{
'Authorization'=> 'Bearer ' + generate_token(@user.id),
'Content-Type' => 'application/json'
}
end
describe 'POST reorder steps, #reorder_steps' do
let(:action) do
post(
api_service_team_protocol_reorder_steps_path(
team_id: @team.id,
protocol_id: @protocol.id
),
params: request_body.to_json,
headers: @valid_headers
)
end
context 'when has valid params' do
let(:request_body) do
{ step_order:
@protocol.steps.pluck(:id).each_with_index.map do |id, i|
{ id: id, position: @protocol.steps.length - 1 - i }
end
}
end
it 'returns status 200 and reorderes steps' do
action
expect(response).to have_http_status 200
new_step_order = @protocol.steps.order(position: :asc).pluck(:id, :position)
expect(new_step_order).to(
eq(
request_body[:step_order].map(&:values).sort { |a, b| a[1] <=> b[1] }
)
)
end
end
context "when step order doesn't include all step ids" do
let(:request_body) do
{ step_order:
@protocol.steps.last(2).pluck(:id).each_with_index.map do |id, i|
{ id: id, position: @protocol.steps.length - 1 - i }
end
}
end
it 'returns status 400' do
action
expect(response).to have_http_status 400
end
end
context "when step order doesn't have the correct positions" do
let(:request_body) do
{ step_order:
@protocol.steps.last(2).pluck(:id).each_with_index.map do |id, i|
{ id: id, position: i + 1 }
end
}
end
it 'returns status 400' do
action
expect(response).to have_http_status 400
end
end
context 'when has missing param' do
let(:request_body) do
{}
end
it 'renders 400' do
action
expect(response).to have_http_status(400)
end
end
end
end

View file

@ -0,0 +1,110 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe "Api::Service::StepsController", type: :request do
before :all do
@user = create(:user)
@team = create(:team, created_by: @user)
create(:user_team, user: @user, team: @team, role: 2)
@project = create(:project, name: Faker::Name.unique.name, created_by: @user, team: @team)
@experiment = create(:experiment, created_by: @user, last_modified_by: @user, project: @project, created_by: @user)
@my_module = create(
:my_module,
:with_due_date,
created_by: @user,
last_modified_by: @user,
experiment: @experiment
)
@protocol = create(:protocol, team: @team, my_module: @my_module, name: "Test protocol")
@step = create(:step, protocol: @protocol)
create_list(:step_orderable_element, 3, step: @step)
@valid_headers =
{
'Authorization'=> 'Bearer ' + generate_token(@user.id),
'Content-Type' => 'application/json'
}
end
describe 'POST reorder steps, #reorder_steps' do
let(:action) do
post(
api_service_team_step_reorder_elements_path(
team_id: @team.id,
step_id: @step.id
),
params: request_body.to_json,
headers: @valid_headers
)
end
context 'when has valid params' do
let(:request_body) do
{ step_element_order:
@step.step_orderable_elements.pluck(:id).each_with_index.map do |id, i|
{ id: id, position: @step.step_orderable_elements.length - 1 - i }
end
}
end
it 'returns status 200 and reorderes step elements' do
action
expect(response).to have_http_status 200
new_step_element_order = @step.step_orderable_elements.order(position: :asc).pluck(:id, :position)
expect(new_step_element_order).to(
eq(
request_body[:step_element_order].map(&:values).sort { |a, b| a[1] <=> b[1] }
)
)
end
end
context "when step order doesn't include all step ids" do
let(:request_body) do
{ step_element_order:
@step.step_orderable_elements.last(2).pluck(:id).each_with_index.map do |id, i|
{ id: id, position: @step.step_orderable_elements.length - 1 - i }
end
}
end
it 'returns status 400' do
action
expect(response).to have_http_status 400
end
end
context "when step order doesn't have the correct positions" do
let(:request_body) do
{ step_element_order:
@step.step_orderable_elements.last(2).pluck(:id).each_with_index.map do |id, i|
{ id: id, position: i + 1 }
end
}
end
it 'returns status 400' do
action
expect(response).to have_http_status 400
end
end
context 'when has missing param' do
let(:request_body) do
{}
end
it 'renders 400' do
action
expect(response).to have_http_status(400)
end
end
end
end