Skip to content
1 change: 1 addition & 0 deletions app/graphql/types/flow_type_setting_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class FlowTypeSettingType < Types::BaseObject
field :flow_type, Types::FlowTypeType, null: true, description: 'Flow type of the flow type setting'
field :identifier, String, null: false, description: 'Identifier of the flow type setting'
field :names, [Types::TranslationType], null: false, description: 'Names of the flow type setting'
field :removed_at, Types::TimeType, null: true, description: 'The timestamp when this setting was soft removed'
field :unique, Boolean, null: false, description: 'Unique status of the flow type setting'

id_field FlowTypeSetting
Expand Down
3 changes: 3 additions & 0 deletions app/models/flow_type_setting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@ class FlowTypeSetting < ApplicationRecord

has_many :names, -> { by_purpose(:name) }, class_name: 'Translation', as: :owner, inverse_of: :owner
has_many :descriptions, -> { by_purpose(:description) }, class_name: 'Translation', as: :owner, inverse_of: :owner

scope :active, -> { where(removed_at: nil) }
scope :removed, -> { where.not(removed_at: nil) }
end
32 changes: 19 additions & 13 deletions app/services/runtimes/grpc/flow_types/update_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,26 +60,32 @@ def update_flowtype(flow_type, t)
db_object.version = flow_type.version
db_object.definition_source = flow_type.definition_source
db_object.display_icon = flow_type.display_icon
db_object.flow_type_settings = update_settings(flow_type.settings, db_object.flow_type_settings)
update_settings(flow_type.settings, db_object.flow_type_settings, t)
link_data_types(db_object, flow_type.linked_data_type_identifiers, t)
db_object.save
db_object
end

def update_settings(flow_type_settings, db_setting_relation)
db_settings = db_setting_relation.first(flow_type_settings.length)
flow_type_settings.each_with_index do |setting, index|
db_settings[index] ||= db_setting_relation.build
db_settings[index].identifier = setting.identifier
db_settings[index].unique = setting.unique.to_s.downcase
db_settings[index].default_value = setting.default_value&.to_ruby
db_settings[index].descriptions = update_translations(setting.description, db_settings[index].descriptions)
db_settings[index].names = update_translations(setting.name, db_settings[index].names)
end
def update_settings(flow_type_settings, db_setting_relation, t)
# rubocop:disable Rails/SkipsModelValidations -- when marking settings as removed, we don't care about validations
db_setting_relation.update_all(removed_at: Time.zone.now)
Comment thread
Taucher2003 marked this conversation as resolved.
# rubocop:enable Rails/SkipsModelValidations

db_setting_relation.excluding(*db_settings).destroy_all
flow_type_settings.each do |setting|
db_setting = db_setting_relation.find_or_initialize_by(identifier: setting.identifier)
db_setting.unique = setting.unique&.to_s&.downcase
db_setting.default_value = setting.default_value&.to_ruby
db_setting.descriptions = update_translations(setting.description, db_setting.descriptions)
db_setting.names = update_translations(setting.name, db_setting.names)
db_setting.removed_at = nil
next if db_setting.save

db_settings
t.rollback_and_return! ServiceResponse.error(
message: 'Failed to update flow type setting',
error_code: :invalid_flow_setting,
details: db_setting.errors
)
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class AddRemovedAtToFlowTypeSettings < Code0::ZeroTrack::Database::Migration[1.0]
def change
add_column :flow_type_settings, :removed_at, :datetime_with_timezone
end
end
1 change: 1 addition & 0 deletions db/schema_migrations/20260504131123
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
230a4eedca49cadd700627c194d5f046f32435cecc60120b63a4e53fc419ef4b
3 changes: 2 additions & 1 deletion db/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,8 @@ CREATE TABLE flow_type_settings (
default_value jsonb,
created_at timestamp with time zone NOT NULL,
updated_at timestamp with time zone NOT NULL,
"unique" integer DEFAULT 0 NOT NULL
"unique" integer DEFAULT 0 NOT NULL,
removed_at timestamp with time zone
);

CREATE SEQUENCE flow_type_settings_id_seq
Expand Down
1 change: 1 addition & 0 deletions docs/graphql/object/flowtypesetting.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ Represents a flow type setting
| `id` | [`FlowTypeSettingID!`](../scalar/flowtypesettingid.md) | Global ID of this FlowTypeSetting |
| `identifier` | [`String!`](../scalar/string.md) | Identifier of the flow type setting |
| `names` | [`[Translation!]!`](../object/translation.md) | Names of the flow type setting |
| `removedAt` | [`Time`](../scalar/time.md) | The timestamp when this setting was soft removed |
| `unique` | [`Boolean!`](../scalar/boolean.md) | Unique status of the flow type setting |
| `updatedAt` | [`Time!`](../scalar/time.md) | Time when this FlowTypeSetting was last updated |
1 change: 1 addition & 0 deletions spec/graphql/types/flow_type_setting_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
flow_type
names
descriptions
removed_at
id
created_at
updated_at
Expand Down
15 changes: 15 additions & 0 deletions spec/models/flow_type_setting_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,19 @@
it { is_expected.to allow_values(:none, :project, 'none', 'project').for(:unique) }
it { is_expected.not_to allow_value(:unknown, 'unknown', 0).for(:unique) }
end

describe 'scopes' do
let!(:active_setting) { create(:flow_type_setting, removed_at: nil) }
let!(:removed_setting) { create(:flow_type_setting, removed_at: Time.zone.now) }

it 'returns active settings' do
expect(described_class.active).to include(active_setting)
expect(described_class.active).not_to include(removed_setting)
end

it 'returns removed settings' do
expect(described_class.removed).to include(removed_setting)
expect(described_class.removed).not_to include(active_setting)
end
end
end
50 changes: 48 additions & 2 deletions spec/requests/grpc/sagittarius/flow_type_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,54 @@

flow_type.reload

expect(flow_type.flow_type_settings.size).to eq(1)
expect(flow_type.flow_type_settings.first.identifier).to eq(flow_types.first[:settings].first[:identifier])
first_setting = flow_type.flow_type_settings.find_by(identifier: 'first_setting')
second_setting = flow_type.flow_type_settings.find_by(identifier: 'second_setting')
other_setting = flow_type.flow_type_settings.find_by(identifier: flow_types.first[:settings].first[:identifier])

expect(first_setting).to be_present
expect(second_setting).to be_present
expect(other_setting).to be_present
expect(first_setting.removed_at).to be_present
expect(second_setting.removed_at).to be_present
expect(other_setting.removed_at).to be_nil
end
Comment thread
raphael-goetz marked this conversation as resolved.

context 'when updating existing flow type settings' do
let(:flow_types) do
[
{
identifier: flow_type.identifier,
settings: [
{
identifier: 'second_setting',
default_value: Tucana::Shared::Value.from_ruby('something'),
unique: :PROJECT,
}
],
signature: '(input: NUMBER): NUMBER',
linked_data_type_identifiers: %w[],
editable: false,
version: '0.0.0',
definition_source: 'draco-rest',
display_icon: 'rest-icon',
}
]
end

it 'updates the flow type settings' do
expect(stub.update(message, authorization(runtime)).success).to be(true)

flow_type.reload

first_setting = flow_type.flow_type_settings.find_by(identifier: 'first_setting')
second_setting = flow_type.flow_type_settings.find_by(identifier: 'second_setting')

expect(first_setting).to be_present
expect(second_setting).to be_present
expect(first_setting.removed_at).to be_present
expect(second_setting.removed_at).to be_nil
expect(second_setting.default_value).to eq('something')
end
end
end
end
Expand Down