scinote-web/app/controllers/external_protocols_controller.rb

134 lines
4.3 KiB
Ruby
Raw Normal View History

2019-06-13 14:49:03 +08:00
# frozen_string_literal: true
class ExternalProtocolsController < ApplicationController
before_action :load_vars
before_action :check_import_permissions, only: [:create]
2019-06-26 15:41:24 +08:00
# GET list_external_protocols
def index
2019-06-14 16:41:51 +08:00
service_call = ProtocolImporters::SearchProtocolsService
.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',
locals: { protocols: service_call.protocols_list, show_import_button: show_import_button }
),
page_id: service_call.protocols_list[:pagination][:current_page]
2019-06-13 14:49:03 +08:00
}
else
2019-06-14 16:41:51 +08:00
render json: { errors: service_call.errors }, status: 400
end
end
# GET show_external_protocol
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-27 15:42:21 +08:00
html_preview = api_client.protocol_html_preview(show_params[:protocol_id])
base_uri = URI.parse(html_preview.request.last_uri.to_s)
base_uri = "#{base_uri.scheme}://#{base_uri.host}"
2019-06-13 17:17:08 +08:00
render json: {
protocol_source: show_params[:protocol_source],
protocol_id: show_params[:protocol_id],
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
end
# GET build_online_sources_protocol
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,
team_id: @team.id,
build_with_assets: false
)
2019-06-26 15:41:24 +08:00
if service_call.succeed?
@protocol = service_call.built_protocol
@protocol&.valid? # Get validations errors here
render json: {
html: render_to_string(
partial: 'protocol_importers/import_form.html.erb',
locals: { protocol: @protocol,
steps_json: service_call.serialized_steps,
steps_assets: service_call.steps_assets }
2019-06-26 15:41:24 +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'
),
validation_errors: { protocol: @protocol.errors.messages }
}
else
render json: { errors: service_call.errors }, status: 400
end
end
# POST import_external_protocol
def create
service_call = ProtocolImporters::ImportProtocolService.call(
2019-06-26 15:41:24 +08:00
protocol_params: create_protocol_params,
steps_params_json: create_steps_params[:steps],
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'
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,
message: message }
else
render json: { errors: service_call.errors }, status: 400
end
end
private
def load_vars
@team = Team.find_by_id(params[:team_id])
render_404 unless @team
end
def index_params
params.permit(:team_id, :protocol_source, :key, :page_id, :page_size, :sort_by)
end
2019-06-13 14:49:03 +08:00
def show_params
params.permit(:team_id, :protocol_source, :protocol_id)
2019-06-13 14:49:03 +08:00
end
def new_params
params.permit(:team_id, :protocol_source, :protocol_client_id)
end
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)
end
def check_import_permissions
render_403 unless can_create_protocols_in_repository?(@team)
end
end