Skip to content

Commit

Permalink
Migrate tags from MessageDraft to associated Message
Browse files Browse the repository at this point in the history
  • Loading branch information
luciajanikova committed Sep 30, 2024
1 parent abfe888 commit 2ae51ba
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 12 deletions.
7 changes: 0 additions & 7 deletions app/jobs/govbox/process_message_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ def perform(govbox_message)
processed_message = ::Message.where(type: [nil, 'Message']).where(uuid: govbox_message.message_id).joins(:thread).where(thread: { box_id: govbox_message.box.id }).take

ActiveRecord::Base.transaction do
destroy_associated_message_draft(govbox_message)

message = Govbox::Message.create_message_with_thread!(govbox_message)

mark_delivery_notification_authorized(govbox_message)
Expand All @@ -24,11 +22,6 @@ def perform(govbox_message)

private

def destroy_associated_message_draft(govbox_message)
message_draft = Upvs::MessageDraft.where(uuid: govbox_message.message_id).joins(:thread).where(thread: { box_id: govbox_message.box.id }).take
message_draft&.destroy
end

def mark_delivery_notification_authorized(govbox_message)
return unless govbox_message.delivery_notification

Expand Down
2 changes: 1 addition & 1 deletion app/jobs/govbox/submit_message_draft_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def build_objects(message_draft)
objects = []
message_draft.objects.each do |object|
objects << {
id: SecureRandom.uuid,
id: object.uuid,
name: object.name,
encoding: "Base64",
signed: object.is_signed,
Expand Down
29 changes: 26 additions & 3 deletions app/models/govbox/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ 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)
Expand All @@ -38,13 +40,18 @@ def self.create_message_with_thread!(govbox_message)
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)

migrate_tags_from_draft(message, tags_to_migrate) if tags_to_migrate

MessageObject.mark_message_objects_externally_signed(message.objects)

EventBus.publish(:message_thread_created, message.thread) if message.thread.previously_new_record?
EventBus.publish(:message_created, message)

Expand Down Expand Up @@ -103,15 +110,14 @@ def self.create_message_objects(message, raw_message)
raw_message["objects"].each do |raw_object|
message_object_type = raw_object["class"]
visualizable = (message_object_type == "FORM" && message.html_visualization.present?) ? true : nil
tags = raw_object["signed"] ? [message.thread.box.tenant.signed_externally_tag!] : []

message_object = message.objects.create!(
uuid: raw_object["id"],
name: raw_object["name"],
mimetype: raw_object["mime_type"],
is_signed: raw_object["signed"],
object_type: message_object_type,
visualizable: visualizable,
tags: tags
visualizable: visualizable
)

if raw_object["encoding"] == "Base64"
Expand Down Expand Up @@ -140,6 +146,23 @@ 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_id|
object.assign_tag(Tag.find(object_tag_id))
end
end

tags_to_migrate[:message].each do |message_tag_id|
message.tags.add_cascading_tag(Tag.find(message_tag_id))
end

tags_to_migrate[:thread].each do |thread_tag_id|
message.thread.assign_tag(Tag.find(thread_tag_id))
end
end

def self.add_delivery_notification_tag(message)
message.add_cascading_tag(delivery_notification_tag(message))
end
Expand Down
15 changes: 15 additions & 0 deletions app/models/message_draft.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,21 @@ 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.map(&:id)
} if object.tags.signed.any?
end.compact
migration_list[:message] = tags.simple.or(tags.signed).map(&:id)
migration_list[:thread] = (thread.tags.simple + thread.tags.signed).map(&:id)

migration_list
end

def draft?
true
end
Expand Down
10 changes: 10 additions & 0 deletions app/models/message_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
# name :string
# object_type :string not null
# to_be_signed :boolean default(FALSE), not null
# uuid :uuid
# visualizable :boolean
# created_at :datetime not null
# updated_at :datetime not null
Expand Down Expand Up @@ -57,6 +58,14 @@ def self.create_message_objects(message, objects)
end
end

def self.mark_message_objects_externally_signed(objects)
objects.find_each do |object|
next unless object.is_signed?

object.assign_tag(object.message.tenant.signed_externally_tag!) unless object.tags.signed_internally.present?
end
end

def mark_signed_by_user(user)
assign_tag(user.signed_by_tag)
unassign_tag(user.signature_requested_from_tag)
Expand Down Expand Up @@ -117,6 +126,7 @@ def assign_tag(tag)
end

def fill_missing_info
update(uuid: SecureRandom.uuid) unless uuid.present?
update(name: name + Utils.file_extension_by_mimetype(mimetype).to_s) if Utils.file_name_without_extension?(self)
update(mimetype: Utils.file_mimetype_by_name(entry_name: name)) if mimetype == Utils::OCTET_STREAM_MIMETYPE
end
Expand Down
2 changes: 2 additions & 0 deletions app/models/tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class Tag < ApplicationRecord
scope :simple, -> { where(type: SimpleTag.to_s) }
scope :visible, -> { where(visible: true) }
scope :signing_tags, -> { where(type: ["SignedTag", "SignedByTag", "SignatureRequestedTag", "SignatureRequestedFromTag"]) }
scope :signed, -> { where(type: ["SignedTag", "SignedByTag", "SignedExternallyTag"]) }
scope :signed_internally, -> { where(type: ["SignedTag", "SignedByTag"]) }
scope :archived, -> { where(type: ArchivedTag.to_s) }

after_update_commit ->(tag) { EventBus.publish(:tag_renamed, tag) if previous_changes.key?("name") }
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20240927104543_add_uuid_to_message_objects.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddUuidToMessageObjects < ActiveRecord::Migration[7.1]
def change
add_column :message_objects, :uuid, :uuid
end
end
3 changes: 2 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2ae51ba

Please sign in to comment.