Skip to content

Commit f7c005b

Browse files
committed
First start of the flow update mutation
1 parent d42a043 commit f7c005b

File tree

2 files changed

+155
-0
lines changed

2 files changed

+155
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# frozen_string_literal: true
2+
3+
module Mutations
4+
module Namespaces
5+
module Projects
6+
module Flows
7+
class Update < BaseMutation
8+
description 'Update an existing flow.'
9+
10+
field :flow, Types::FlowType, null: true, description: 'The updated flow.'
11+
12+
argument :flow_id, Types::GlobalIdType[Flow],
13+
required: true, description: 'The ID of the flow to update'
14+
15+
argument :flow_input, Types::Input::FlowInputType, description: 'The updated flow', required: true
16+
17+
def resolve(flow_id:, flow_input:, **_params)
18+
flow = SagittariusSchema.object_from_id(flow_id)
19+
20+
return error('Invalid flow id') if flow.nil?
21+
22+
flow_type = SagittariusSchema.object_from_id(flow.type)
23+
return error('Invalid flow type id') if flow_type.nil?
24+
25+
::Namespaces::Projects::Flows::UpdateService.new(
26+
current_authentication,
27+
flow,
28+
flow_input
29+
).execute.to_mutation_response(success_key: :flow)
30+
end
31+
32+
def error(message)
33+
{
34+
flow: nil,
35+
errors: [create_message_error(message)],
36+
}
37+
end
38+
end
39+
end
40+
end
41+
end
42+
end
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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

Comments
 (0)