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-26 15:41:24 +08:00
|
|
|
# GET list_external_protocols
|
2019-06-11 23:04:59 +08:00
|
|
|
def index
|
2019-06-14 16:41:51 +08:00
|
|
|
service_call = ProtocolImporters::SearchProtocolsService
|
2019-06-28 08:26:31 +08:00
|
|
|
.call(protocol_source: index_params[:protocol_source],
|
|
|
|
query_params: index_params)
|
2019-06-14 16:41:51 +08:00
|
|
|
if service_call.succeed?
|
2019-07-03 16:22:59 +08:00
|
|
|
show_import_button = can_create_protocols_in_repository?(@team)
|
2019-06-13 14:49:03 +08:00
|
|
|
render json: {
|
|
|
|
html: render_to_string(
|
|
|
|
partial: 'protocol_importers/list_of_protocol_cards.html.erb',
|
2019-07-03 00:24:07 +08:00
|
|
|
locals: { protocols: service_call.protocols_list, show_import_button: show_import_button }
|
2019-07-04 21:35:18 +08:00
|
|
|
),
|
|
|
|
page_id: service_call.protocols_list[:pagination][:current_page]
|
2019-06-13 14:49:03 +08:00
|
|
|
}
|
2019-06-11 23:04:59 +08:00
|
|
|
else
|
2019-06-14 16:41:51 +08:00
|
|
|
render json: { errors: service_call.errors }, status: 400
|
2019-06-11 23:04:59 +08:00
|
|
|
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
|
2019-06-18 20:01:57 +08:00
|
|
|
|
2019-06-27 15:42:21 +08:00
|
|
|
html_preview = api_client.protocol_html_preview(show_params[:protocol_id])
|
2019-07-09 19:21:06 +08:00
|
|
|
base_uri = URI.parse(html_preview.request.last_uri.to_s)
|
|
|
|
base_uri = "#{base_uri.scheme}://#{base_uri.host}"
|
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],
|
2019-07-09 19:21:06 +08:00
|
|
|
base_uri: base_uri,
|
2019-06-13 17:17:08 +08:00
|
|
|
html: html_preview
|
2019-06-27 15:42:21 +08:00
|
|
|
}
|
2019-06-13 17:17:08 +08:00
|
|
|
rescue StandardError => e
|
|
|
|
render json: {
|
2019-06-27 15:42:21 +08:00
|
|
|
errors: [protocol_html_preview: e.message]
|
2019-06-13 17:17:08 +08:00
|
|
|
}, 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-27 19:10:28 +08:00
|
|
|
team_id: @team.id,
|
|
|
|
build_with_assets: false
|
2019-06-11 23:04:59 +08:00
|
|
|
)
|
2019-06-26 15:41:24 +08:00
|
|
|
|
2019-06-11 23:04:59 +08:00
|
|
|
if service_call.succeed?
|
2019-06-27 19:10:28 +08:00
|
|
|
@protocol = service_call.built_protocol
|
|
|
|
@protocol&.valid? # Get validations errors here
|
|
|
|
|
2019-06-13 19:28:08 +08:00
|
|
|
render json: {
|
|
|
|
html: render_to_string(
|
|
|
|
partial: 'protocol_importers/import_form.html.erb',
|
2019-06-27 19:10:28 +08:00
|
|
|
locals: { protocol: @protocol,
|
|
|
|
steps_json: service_call.serialized_steps,
|
|
|
|
steps_assets: service_call.steps_assets }
|
2019-06-26 15:41:24 +08:00
|
|
|
),
|
2019-06-27 19:10:28 +08:00
|
|
|
title: t('protocol_importers.new.modal_title', protocol_name: @protocol.name),
|
2019-06-26 15:41:24 +08:00
|
|
|
footer: render_to_string(
|
|
|
|
partial: 'protocol_importers/preview_modal_footer.html.erb'
|
|
|
|
),
|
2019-06-27 19:10:28 +08:00
|
|
|
validation_errors: { protocol: @protocol.errors.messages }
|
2019-06-13 19:28:08 +08:00
|
|
|
}
|
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(
|
2019-06-26 15:41:24 +08:00
|
|
|
protocol_params: create_protocol_params,
|
2019-06-27 19:10:28 +08:00
|
|
|
steps_params_json: create_steps_params[:steps],
|
2019-06-13 07:02:44 +08:00
|
|
|
user_id: current_user.id,
|
|
|
|
team_id: @team.id
|
|
|
|
)
|
|
|
|
|
|
|
|
if service_call.succeed?
|
2019-06-26 15:41:24 +08:00
|
|
|
protocol_type = service_call.protocol.in_repository_public? ? 'public' : 'private'
|
2019-07-11 15:50:39 +08:00
|
|
|
message = t('protocols.index.external_protocols.import.success_flash',
|
|
|
|
name: service_call.protocol.name,
|
|
|
|
type: t("protocols.index.external_protocols.import.#{protocol_type}"))
|
2019-06-26 15:41:24 +08:00
|
|
|
render json: { protocol: service_call.protocol,
|
2019-07-11 15:50:39 +08:00
|
|
|
message: message }
|
2019-06-13 07:02:44 +08:00
|
|
|
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
|
2019-07-09 19:21:06 +08:00
|
|
|
params.permit(:team_id, :protocol_source, :key, :page_id, :page_size, :sort_by)
|
2019-06-11 23:04:59 +08:00
|
|
|
end
|
|
|
|
|
2019-06-13 14:49:03 +08:00
|
|
|
def show_params
|
2019-07-09 19:21:06 +08:00
|
|
|
params.permit(:team_id, :protocol_source, :protocol_id)
|
2019-06-13 14:49:03 +08:00
|
|
|
end
|
|
|
|
|
2019-06-11 23:04:59 +08:00
|
|
|
def new_params
|
2019-07-09 19:21:06 +08:00
|
|
|
params.permit(:team_id, :protocol_source, :protocol_client_id)
|
2019-06-11 23:04:59 +08:00
|
|
|
end
|
2019-06-13 07:02:44 +08:00
|
|
|
|
2019-06-26 15:41:24 +08:00
|
|
|
def create_protocol_params
|
|
|
|
params.require(:protocol).permit(:name, :authors, :published_on, :protocol_type, :description).except(:steps)
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_steps_params
|
|
|
|
params.require(:protocol).permit(:steps)
|
2019-06-13 07:02:44 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def check_import_permissions
|
|
|
|
render_403 unless can_create_protocols_in_repository?(@team)
|
|
|
|
end
|
2019-06-11 23:04:59 +08:00
|
|
|
end
|