Merge pull request #2798 from aignatov-bio/ai-sci-4831-update-api-for-new-statuses

Add API endpoints for workflows [SCI-4831]
This commit is contained in:
Alex Kriuchykhin 2020-09-08 16:23:05 +02:00 committed by GitHub
commit c413ac21a1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 182 additions and 4 deletions

View file

@ -201,6 +201,10 @@ module Api
@checklist_item = @checklist.checklist_items.find(params.require(key))
raise PermissionError.new(Protocol, :read) unless can_read_protocol_in_module?(@step.protocol)
end
def load_workflow(key = :workflow_id)
@workflow = MyModuleStatusFlow.find(params.require(key))
end
end
end
end

View file

@ -16,6 +16,7 @@ module Api
def index
tasks = @experiment.my_modules
.includes(:my_module_status, :my_modules, :my_module_antecessors)
.page(params.dig(:page, :number))
.per(params.dig(:page, :size))
@ -29,7 +30,7 @@ module Api
def create
raise PermissionError.new(MyModule, :create) unless can_manage_experiment?(@experiment)
my_module = @experiment.my_modules.create!(task_params)
my_module = @experiment.my_modules.create!(task_params_create)
render jsonapi: my_module, serializer: TaskSerializer,
rte_rendering: render_rte?,
@ -37,7 +38,7 @@ module Api
end
def update
@task.assign_attributes(task_params)
@task.assign_attributes(task_params_update)
if @task.changed? && @task.save!
render jsonapi: @task, serializer: TaskSerializer, status: :ok
@ -56,12 +57,18 @@ module Api
private
def task_params
def task_params_create
raise TypeError unless params.require(:data).require(:type) == 'tasks'
params.require(:data).require(:attributes).permit(%i(name x y description state))
end
def task_params_update
raise TypeError unless params.require(:data).require(:type) == 'tasks'
params.require(:data).require(:attributes).permit(%i(name x y description state my_module_status_id))
end
def load_task_for_managing
@task = @experiment.my_modules.find(params.require(:id))
raise PermissionError.new(MyModule, :manage) unless can_manage_module?(@task)

View file

@ -0,0 +1,16 @@
# frozen_string_literal: true
module Api
module V1
class WorkflowStatusesController < BaseController
before_action only: :index do
load_workflow(:workflow_id)
end
def index
statuses = @workflow.my_module_statuses
render jsonapi: statuses, each_serializer: WorkflowStatusSerializer
end
end
end
end

View file

@ -0,0 +1,20 @@
# frozen_string_literal: true
module Api
module V1
class WorkflowsController < BaseController
before_action only: :show do
load_workflow(:id)
end
def index
workflows = MyModuleStatusFlow.all
render jsonapi: workflows, each_serializer: WorkflowSerializer
end
def show
render jsonapi: @workflow, serializer: WorkflowSerializer
end
end
end
end

View file

@ -8,7 +8,8 @@ module Api
include InputSanitizeHelper
type :tasks
attributes :id, :name, :started_on, :due_date, :description, :state, :archived
attributes :id, :name, :started_on, :due_date, :description, :state, :archived, :status_id, :status_name,
:prev_status_id, :prev_status_name, :next_status_id, :next_status_name
has_many :output_tasks, key: :outputs,
serializer: TaskSerializer,
class_name: 'MyModule'
@ -16,6 +17,30 @@ module Api
serializer: TaskSerializer,
class_name: 'MyModule'
def status_id
object.my_module_status_id
end
def status_name
object.my_module_status.name
end
def prev_status_id
object.my_module_status.previous_status&.id
end
def prev_status_name
object.my_module_status.previous_status&.name
end
def next_status_id
object.my_module_status.next_status&.id
end
def next_status_name
object.my_module_status.next_status&.name
end
def output_tasks
object.my_modules
end

View file

@ -0,0 +1,11 @@
# frozen_string_literal: true
module Api
module V1
class WorkflowSerializer < ActiveModel::Serializer
type :workflows
attributes :id, :name, :description, :visibility, :team_id
end
end
end

View file

@ -0,0 +1,11 @@
# frozen_string_literal: true
module Api
module V1
class WorkflowStatusSerializer < ActiveModel::Serializer
type :workflow_statuses
attributes :id, :name, :description, :color, :previous_status_id
end
end
end

View file

@ -707,6 +707,10 @@ Rails.application.routes.draw do
path: 'identities',
as: :identities
end
resources :workflows, only: %i(index show) do
resources :workflow_statuses, path: :statuses, only: %i(index)
end
end
end
end

View file

@ -4,6 +4,8 @@ require 'rails_helper'
RSpec.describe 'Api::V1::TasksController', type: :request do
before :all do
MyModuleStatusFlow.ensure_default
@user = create(:user)
@teams = create_list(:team, 2, created_by: @user)
create(:user_team, user: @user, team: @teams.first, role: 2)

View file

@ -0,0 +1,28 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe 'Api::V1::WrokflowsController', type: :request do
before :all do
@user = create(:user)
@teams = create_list(:team, 2, created_by: @user)
create(:user_team, user: @user, team: @teams.first, role: 2)
@valid_headers =
{ 'Authorization': 'Bearer ' + generate_token(@user.id) }
MyModuleStatusFlow.ensure_default
end
describe 'GET workflow statuses, #index' do
it 'Response with correct workflow statuses' do
hash_body = nil
get api_v1_workflow_workflow_statuses_path(workflow_id: MyModuleStatusFlow.first.id), headers: @valid_headers
expect { hash_body = json }.not_to raise_exception
expect(hash_body[:data]).to match(
ActiveModelSerializers::SerializableResource
.new(MyModuleStatusFlow.first.my_module_statuses,
each_serializer: Api::V1::WorkflowStatusSerializer)
.as_json[:data]
)
end
end
end

View file

@ -0,0 +1,50 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe 'Api::V1::WrokflowsController', type: :request do
before :all do
@user = create(:user)
@teams = create_list(:team, 2, created_by: @user)
create(:user_team, user: @user, team: @teams.first, role: 2)
@valid_headers =
{ 'Authorization': 'Bearer ' + generate_token(@user.id) }
MyModuleStatusFlow.ensure_default
end
describe 'GET workflows, #index' do
it 'Response with correct workflows' do
hash_body = nil
get api_v1_workflows_path, headers: @valid_headers
expect { hash_body = json }.not_to raise_exception
expect(hash_body[:data]).to match(
ActiveModelSerializers::SerializableResource
.new(MyModuleStatusFlow.all,
each_serializer: Api::V1::WorkflowSerializer)
.as_json[:data]
)
end
end
describe 'GET worflow, #show' do
it 'When valid request' do
hash_body = nil
get api_v1_workflow_path(id: MyModuleStatusFlow.all.first), headers: @valid_headers
expect { hash_body = json }.not_to raise_exception
expect(hash_body[:data]).to match(
ActiveModelSerializers::SerializableResource
.new(MyModuleStatusFlow.all.first,
serializer: Api::V1::WorkflowSerializer)
.as_json[:data]
)
end
it 'When invalid request, non existing workflow' do
hash_body = nil
get api_v1_workflow_path(id: -1), headers: @valid_headers
expect(response).to have_http_status(404)
expect { hash_body = json }.not_to raise_exception
expect(hash_body['errors'][0]).to include('status': 404)
end
end
end