|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module Namespaces |
| 4 | + module Projects |
| 5 | + module Flows |
| 6 | + class UpdateService |
| 7 | + include Sagittarius::Database::Transactional |
| 8 | + |
| 9 | + attr_reader :current_authentication, :flow, :flow_input |
| 10 | + |
| 11 | + def initialize(current_authentication, flow, flow_input) |
| 12 | + @current_authentication = current_authentication |
| 13 | + @flow = flow |
| 14 | + @flow_input = flow_input |
| 15 | + end |
| 16 | + |
| 17 | + def execute |
| 18 | + unless Ability.allowed?(current_authentication, :update_flow, flow) |
| 19 | + return ServiceResponse.error(message: 'Missing permission', payload: :missing_permission) |
| 20 | + end |
| 21 | + |
| 22 | + transactional do |t| |
| 23 | + update_settings(t) |
| 24 | + update_nodes(t) |
| 25 | + |
| 26 | + validate_flow(t) |
| 27 | + |
| 28 | + create_audit_event |
| 29 | + |
| 30 | + ServiceResponse.success(message: 'Flow updated', payload: flow) |
| 31 | + end |
| 32 | + end |
| 33 | + |
| 34 | + private |
| 35 | + |
| 36 | + def update_settings(t) |
| 37 | + flow_input.settings.each do |setting| |
| 38 | + flow_setting = flow.flow_settings.find_or_initialize_by(flow_setting_id: setting.flow_setting_id) |
| 39 | + flow_setting.object = setting.object |
| 40 | + |
| 41 | + next if flow_setting.valid? |
| 42 | + |
| 43 | + t.rollback_and_return! ServiceResponse.error( |
| 44 | + message: 'Invalid flow settings', |
| 45 | + payload: flow_setting.errors |
| 46 | + ) |
| 47 | + end |
| 48 | + |
| 49 | + flow.flow_settings.where.not(flow_setting_id: flow_input.settings.map(&:flow_setting_id)).destroy_all |
| 50 | + end |
| 51 | + |
| 52 | + def update_nodes(t) |
| 53 | + all_nodes = flow.collect_node_functions |
| 54 | + |
| 55 | + current_node_input = flow_input.starting_node |
| 56 | + |
| 57 | + node_index = 0 |
| 58 | + until current_node_input.nil? |
| 59 | + current_node = all_nodes[node_index] |
| 60 | + |
| 61 | + update_node(t, current_node, current_node_input) |
| 62 | + |
| 63 | + current_node_input = current_node_input.next_node |
| 64 | + node_index += 1 |
| 65 | + end |
| 66 | + end |
| 67 | + |
| 68 | + def update_node(t, current_node, current_node_input) |
| 69 | + |
| 70 | + end |
| 71 | + |
| 72 | + def update_node_parameters(t, current_node, current_node_input) |
| 73 | + current_node_input.parameters.each do |parameter| |
| 74 | + node_parameter = current_node.parameters.find_or_initialize_by(runtime_parameter_definition_id: parameter.runtime_parameter_definition_id) |
| 75 | + node_parameter.value = parameter.value |
| 76 | + |
| 77 | + next if node_parameter.valid? |
| 78 | + |
| 79 | + t.rollback_and_return! ServiceResponse.error( |
| 80 | + message: 'Invalid node parameter', |
| 81 | + payload: node_parameter.errors |
| 82 | + ) |
| 83 | + end |
| 84 | + |
| 85 | + current_node.node_parameters.where.not(runtime_parameter_id: current_node_input.parameters.map(&:runtime_parameter_definition_id)).destroy_all |
| 86 | + end |
| 87 | + |
| 88 | + def validate_flow(t) |
| 89 | + res = Validation::ValidationService.new(current_authentication, flow).execute |
| 90 | + |
| 91 | + return unless res.error? |
| 92 | + |
| 93 | + t.rollback_and_return! ServiceResponse.error( |
| 94 | + message: 'Flow validation failed', |
| 95 | + payload: res.payload |
| 96 | + ) |
| 97 | + end |
| 98 | + |
| 99 | + def create_audit_event |
| 100 | + AuditService.audit( |
| 101 | + :flow_updated, |
| 102 | + author_id: current_authentication.user.id, |
| 103 | + entity: flow, |
| 104 | + target: flow.project, |
| 105 | + details: { |
| 106 | + **flow_input.attributes.except('created_at', 'updated_at'), |
| 107 | + } |
| 108 | + ) |
| 109 | + end |
| 110 | + end |
| 111 | + end |
| 112 | + end |
| 113 | +end |
0 commit comments