2019-06-13 14:49:03 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-06-13 06:17:32 +08:00
|
|
|
class ExternalProtocolsController < ApplicationController
|
2019-06-13 07:02:44 +08:00
|
|
|
before_action :load_vars
|
|
|
|
before_action :check_import_permissions, only: [:create]
|
|
|
|
|
2019-06-13 19:28:08 +08:00
|
|
|
# GET list_external_protocol
|
2019-06-11 23:04:59 +08:00
|
|
|
def index
|
|
|
|
# list_protocols = SearchService.call(index_params)
|
2019-06-13 14:49:03 +08:00
|
|
|
succeed = true
|
|
|
|
protocols = [
|
2019-06-11 23:04:59 +08:00
|
|
|
{ name: 'Protocol1' },
|
|
|
|
{ name: 'Protocol2' },
|
|
|
|
{ name: 'Protocol3' }
|
|
|
|
]
|
|
|
|
|
|
|
|
if succeed
|
2019-06-13 14:49:03 +08:00
|
|
|
render json: {
|
|
|
|
html: render_to_string(
|
|
|
|
partial: 'protocol_importers/list_of_protocol_cards.html.erb',
|
|
|
|
locals: { protocols: protocols }
|
|
|
|
)
|
|
|
|
}
|
2019-06-11 23:04:59 +08:00
|
|
|
else
|
|
|
|
render json: {
|
|
|
|
errors: { protocol: 'error_placeholder' }
|
|
|
|
}, status: 400
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-13 19:28:08 +08:00
|
|
|
# GET show_external_protocol
|
2019-06-11 23:04:59 +08:00
|
|
|
def show
|
2019-06-13 17:17:08 +08:00
|
|
|
# TODO: this should be refactored, it's only for placeholding
|
|
|
|
endpoint_name = Constants::PROTOCOLS_ENDPOINTS.dig(*show_params[:protocol_source]
|
|
|
|
.split('/').map(&:to_sym))
|
|
|
|
api_client = "ProtocolImporters::#{endpoint_name}::ApiClient".constantize.new
|
|
|
|
html_preview = api_client.protocol_html_preview(show_params[:protocol_id])
|
2019-06-11 23:04:59 +08:00
|
|
|
|
2019-06-13 17:17:08 +08:00
|
|
|
render json: {
|
|
|
|
protocol_source: show_params[:protocol_source],
|
|
|
|
protocol_id: show_params[:protocol_id],
|
|
|
|
html: html_preview
|
|
|
|
} and return
|
|
|
|
rescue StandardError => e
|
|
|
|
render json: {
|
|
|
|
errors: [show_protocol: e.message]
|
|
|
|
}, status: 400
|
2019-06-11 23:04:59 +08:00
|
|
|
end
|
|
|
|
|
2019-06-13 19:28:08 +08:00
|
|
|
# GET build_online_sources_protocol
|
2019-06-11 23:04:59 +08:00
|
|
|
def new
|
|
|
|
service_call = ProtocolImporters::BuildProtocolFromClientService.call(
|
|
|
|
protocol_source: new_params[:protocol_source],
|
|
|
|
protocol_client_id: new_params[:protocol_client_id],
|
|
|
|
user_id: current_user.id,
|
2019-06-13 07:02:44 +08:00
|
|
|
team_id: @team.id
|
2019-06-11 23:04:59 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
if service_call.succeed?
|
2019-06-13 19:28:08 +08:00
|
|
|
render json: {
|
|
|
|
html: render_to_string(
|
|
|
|
partial: 'protocol_importers/import_form.html.erb',
|
|
|
|
locals: { protocol: service_call.built_protocol }
|
|
|
|
)
|
|
|
|
}
|
2019-06-11 23:04:59 +08:00
|
|
|
else
|
|
|
|
render json: { errors: service_call.errors }, status: 400
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-13 07:02:44 +08:00
|
|
|
# POST import_external_protocol
|
2019-06-11 23:04:59 +08:00
|
|
|
def create
|
2019-06-13 07:02:44 +08:00
|
|
|
service_call = ProtocolImporters::ImportProtocolService.call(
|
|
|
|
protocol_params: create_params[:protocol_params],
|
|
|
|
steps_params: create_params[:steps_paramas],
|
|
|
|
user_id: current_user.id,
|
|
|
|
team_id: @team.id
|
|
|
|
)
|
|
|
|
|
|
|
|
if service_call.succeed?
|
|
|
|
render json: service_call.protocol
|
|
|
|
else
|
|
|
|
render json: { errors: service_call.errors }, status: 400
|
|
|
|
end
|
2019-06-11 23:04:59 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-06-13 07:02:44 +08:00
|
|
|
def load_vars
|
|
|
|
@team = Team.find_by_id(params[:team_id])
|
|
|
|
|
|
|
|
render_404 unless @team
|
|
|
|
end
|
|
|
|
|
2019-06-11 23:04:59 +08:00
|
|
|
def index_params
|
|
|
|
params.permit(:protocol_source, :key, :page_id, :page_size, :order_field, :order_dir)
|
|
|
|
end
|
|
|
|
|
2019-06-13 14:49:03 +08:00
|
|
|
def show_params
|
|
|
|
params.permit(:protocol_source, :protocol_id)
|
|
|
|
end
|
|
|
|
|
2019-06-11 23:04:59 +08:00
|
|
|
def new_params
|
|
|
|
params.permit(:protocol_source, :protocol_client_id)
|
|
|
|
end
|
2019-06-13 07:02:44 +08:00
|
|
|
|
|
|
|
def create_params
|
|
|
|
params.permit(:protocol_params, :steps_params)
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_import_permissions
|
|
|
|
render_403 unless can_create_protocols_in_repository?(@team)
|
|
|
|
end
|
2019-06-11 23:04:59 +08:00
|
|
|
end
|