2019-06-11 21:32:23 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module ProtocolImporters
|
|
|
|
class ImportProtocolService
|
|
|
|
extend Service
|
|
|
|
|
|
|
|
attr_reader :errors, :protocol
|
|
|
|
|
2019-06-27 19:10:28 +08:00
|
|
|
def initialize(protocol_params:, steps_params_json:, team_id:, user_id:)
|
|
|
|
@user = User.find_by_id user_id
|
|
|
|
@team = Team.find_by_id team_id
|
2019-06-11 21:32:23 +08:00
|
|
|
@protocol_params = protocol_params
|
2019-07-09 21:32:43 +08:00
|
|
|
@steps_params = JSON.parse(steps_params_json)
|
2019-06-11 21:32:23 +08:00
|
|
|
@errors = {}
|
|
|
|
end
|
|
|
|
|
|
|
|
def call
|
|
|
|
return self unless valid?
|
|
|
|
|
|
|
|
@protocol = Protocol.new(@protocol_params.merge!(added_by: @user, team: @team))
|
2019-06-26 15:41:24 +08:00
|
|
|
|
2019-06-27 19:10:28 +08:00
|
|
|
@protocol.steps << @steps_params.map do |step_params|
|
|
|
|
# Create step with nested attributes for tables
|
|
|
|
s = Step.new(step_params
|
|
|
|
.symbolize_keys
|
|
|
|
.slice(:name, :position, :description, :tables_attributes)
|
|
|
|
.merge(user: @user, completed: false))
|
|
|
|
|
2019-07-09 21:32:43 +08:00
|
|
|
# 'Manually' create assets here. "Accept nasted attributes" won't work for assets
|
2019-06-27 19:10:28 +08:00
|
|
|
s.assets << AttachmentsBuilder.generate(step_params.deep_symbolize_keys, user: @user, team: @team)
|
|
|
|
s
|
2019-06-11 21:32:23 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
@errors[:protocol] = @protocol.errors.messages unless @protocol.save
|
|
|
|
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
def succeed?
|
|
|
|
@errors.none?
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def valid?
|
|
|
|
unless [@protocol_params, @user, @team].all?
|
|
|
|
@errors[:invalid_arguments] = {
|
|
|
|
'user': @user,
|
|
|
|
'team': @team,
|
|
|
|
'@protocol_params': @protocol_params
|
|
|
|
}.map { |key, value| "Can't find #{key.capitalize}" if value.nil? }.compact
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|