diff --git a/app/models/govbox/message.rb b/app/models/govbox/message.rb index ccf41f77..b8f73ce2 100644 --- a/app/models/govbox/message.rb +++ b/app/models/govbox/message.rb @@ -28,29 +28,28 @@ class Govbox::Message < ApplicationRecord def self.create_message_with_thread!(govbox_message) message = nil message_draft = Upvs::MessageDraft.where(uuid: govbox_message.message_id).joins(:thread).where(thread: { box_id: govbox_message.box.id }).take - tags_to_migrate = message_draft&.tags_to_migrate_to_message MessageThread.with_advisory_lock!(govbox_message.correlation_id, transaction: true, timeout_seconds: 10) do message = create_message(govbox_message) - message.thread = govbox_message.box.message_threads.find_or_create_by_merge_uuid!( + message.thread = message_draft.thread if message_draft || govbox_message.box.message_threads.find_or_create_by_merge_uuid!( box: govbox_message.box, merge_uuid: govbox_message.correlation_id, title: message.metadata.dig("delivery_notification", "consignment", "subject").presence || message.title, delivered_at: govbox_message.delivered_at ) - message_draft&.destroy message.save! - add_upvs_related_tags(message, govbox_message) - end + create_message_objects(message, govbox_message.payload) - create_message_objects(message, govbox_message.payload) + add_upvs_related_tags(message, govbox_message) + migrate_tags_from_draft(message, message_draft) if message_draft - migrate_tags_from_draft(message, tags_to_migrate) if tags_to_migrate + MessageObject.mark_message_objects_externally_signed(message.objects) - MessageObject.mark_message_objects_externally_signed(message.objects) + message_draft.destroy if message_draft + end EventBus.publish(:message_thread_created, message.thread) if message.thread.previously_new_record? EventBus.publish(:message_created, message) @@ -146,21 +145,13 @@ def self.add_upvs_related_tags(message, govbox_message) add_delivery_notification_tag(message) if message.can_be_authorized? end - def self.migrate_tags_from_draft(message, tags_to_migrate) - tags_to_migrate[:objects].each do |object_data| - object = message.objects.find_by(uuid: object_data[:uuid]) - object_data[:tags].each do |object_tag| - object&.assign_tag(object_tag) - end - end - - tags_to_migrate[:message].each do |message_tag| - message.tags.add_cascading_tag(message_tag) + def self.migrate_tags_from_draft(message, message_draft) + message_draft.objects.map do |message_draft_object| + message_object = message.objects.find_by(uuid: message_draft_object.uuid) + message_draft_object.tags.signed.each { |tag| message_object.assign_tag(tag) } end - tags_to_migrate[:thread].each do |thread_tag| - message.thread.assign_tag(thread_tag) - end + (message_draft.tags.simple + message_draft.tags.signed).each { |tag| message.assign_tag(tag) } end def self.add_delivery_notification_tag(message) diff --git a/app/models/message_draft.rb b/app/models/message_draft.rb index cfa63f95..2e03797a 100644 --- a/app/models/message_draft.rb +++ b/app/models/message_draft.rb @@ -79,21 +79,6 @@ def update_content(parameters) def submit raise NotImplementedError end - - def tags_to_migrate_to_message - migration_list = {} - - migration_list[:objects] = objects.map do |object| { - uuid: object.uuid, - tags: object.tags.signed.to_a - } if object.tags.signed.any? - end.compact - migration_list[:message] = tags.simple.or(tags.signed).to_a - migration_list[:thread] = (thread.tags.simple + thread.tags.signed).to_a - - migration_list - end - def draft? true end diff --git a/db/migrate/20241025151903_update_index_on_uuid_message_thread_id_to_messages.rb b/db/migrate/20241025151903_update_index_on_uuid_message_thread_id_to_messages.rb new file mode 100644 index 00000000..92362e8c --- /dev/null +++ b/db/migrate/20241025151903_update_index_on_uuid_message_thread_id_to_messages.rb @@ -0,0 +1,8 @@ +class UpdateIndexOnUuidMessageThreadIdToMessages < ActiveRecord::Migration[7.1] + def change + remove_index :messages, name: 'index_messages_on_uuid_and_message_thread_id' + + add_unique_constraint :messages, [:uuid, :message_thread_id], deferrable: :deferred + end +end + diff --git a/db/schema.rb b/db/schema.rb index 5e01f385..a42c507f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2024_09_27_104543) do +ActiveRecord::Schema[7.1].define(version: 2024_10_25_151903) do # These are extensions that must be enabled in order to support this database enable_extension "pgcrypto" enable_extension "plpgsql" @@ -487,6 +487,7 @@ t.index ["author_id"], name: "index_messages_on_author_id" t.index ["import_id"], name: "index_messages_on_import_id" t.index ["message_thread_id"], name: "index_messages_on_message_thread_id" + t.unique_constraint ["uuid", "message_thread_id"], deferrable: :deferred end create_table "messages_tags", force: :cascade do |t|