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(
|
2023-06-21 20:13:20 +08:00
|
|
|
partial: 'protocol_importers/list_of_protocol_cards',
|
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
|
2023-02-06 20:49:34 +08:00
|
|
|
render json: { errors: service_call.errors }, status: :bad_request
|
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
|
|
|
|
2021-02-17 20:30:10 +08:00
|
|
|
html_preview_request = api_client.protocol_html_preview(show_params[:protocol_id])
|
|
|
|
base_uri = URI.parse(html_preview_request.request.last_uri.to_s)
|
2019-07-09 19:21:06 +08:00
|
|
|
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,
|
2021-02-17 20:30:10 +08:00
|
|
|
html: html_preview_request.body
|
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]
|
2023-02-06 20:49:34 +08:00
|
|
|
}, status: :bad_request
|
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(
|
2023-06-21 20:13:20 +08:00
|
|
|
partial: 'protocol_importers/import_form',
|
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(
|
2023-06-21 20:13:20 +08:00
|
|
|
partial: 'protocol_importers/preview_modal_footer'
|
2019-06-26 15:41:24 +08:00
|
|
|
),
|
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
|
2023-02-06 20:49:34 +08:00
|
|
|
render json: { errors: service_call.errors }, status: :bad_request
|
2019-06-11 23:04:59 +08:00
|
|
|
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],
|
2023-02-27 17:37:51 +08:00
|
|
|
user: current_user,
|
|
|
|
team: @team
|
2019-06-13 07:02:44 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
if service_call.succeed?
|
2023-02-27 17:37:51 +08:00
|
|
|
message = t('protocols.index.protocolsio.import.success_flash', name: service_call.protocol.name)
|
|
|
|
render json: { protocol: service_call.protocol, message: message }
|
2019-06-13 07:02:44 +08:00
|
|
|
else
|
2023-02-06 20:49:34 +08:00
|
|
|
render json: { validation_errors: service_call.errors }, status: :bad_request
|
2019-06-13 07:02:44 +08:00
|
|
|
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
|
2023-02-27 17:37:51 +08:00
|
|
|
params
|
|
|
|
.require(:protocol)
|
|
|
|
.permit(:name, :authors, :published_on, :protocol_type, :description, :visibility, :default_public_user_role_id)
|
|
|
|
.except(:steps)
|
2019-06-26 15:41:24 +08:00
|
|
|
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
|