Skip to content

Commit

Permalink
Merge branch 'main' into GO-49/do_not_submit_drafts_without_requested…
Browse files Browse the repository at this point in the history
…_signatures

# Conflicts:
#	app/models/message_object.rb
#	app/models/tag.rb
#	db/schema.rb
  • Loading branch information
luciajanikova committed Oct 29, 2024
2 parents 270661c + 2f187cf commit 868aa26
Show file tree
Hide file tree
Showing 17 changed files with 154 additions and 26 deletions.
10 changes: 8 additions & 2 deletions app/components/message_state_component.html.erb
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
<% if @message.form_object&.is_signed? || @message.form_object.present? || @message.authorized? %>
<div class="w-fit flex flex-col gap-1 <%= @classes %>">
<% if @message.form_object&.is_signed? %>
<div class="w-fit flex flex-row gap-1 <%= @classes %>">
<% if @message.form_object&.tags&.signed_by&.any? %>
<% @message.form_object.tags.signed_by.each do |tag| %>
<%= render Common::BadgeComponent.new(tag.name, "green", "fingerprint") %>
<% end %>
<% elsif @message.form_object&.is_signed? %>
<%= render Common::BadgeComponent.new("Podpísané", "green", "fingerprint") %>
<% end %>

<% if @message.authorized? %>
<%= render Common::BadgeComponent.new("Prevzatá doručenka", "purple") %>
<% end %>

<% if @message.thread.archived? %>
<%= render ArchivedObjectTagComponent.new(@message.form_object&.archived_object) %>
<% end %>
Expand Down
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: 23 additions & 6 deletions app/models/govbox/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ 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

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
message.thread ||= 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,
Expand All @@ -40,10 +42,17 @@ def self.create_message_with_thread!(govbox_message)

message.save!

create_message_objects(message, govbox_message.payload)

add_upvs_related_tags(message, govbox_message)
end

create_message_objects(message, govbox_message.payload)
if message_draft
copy_tags_from_draft(message, message_draft)
message_draft.destroy
end

MessageObject.mark_message_objects_externally_signed(message.objects)
end

EventBus.publish(:message_thread_created, message.thread) if message.thread.previously_new_record?
EventBus.publish(:message_created, message)
Expand Down Expand Up @@ -103,15 +112,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 +148,15 @@ def self.add_upvs_related_tags(message, govbox_message)
add_delivery_notification_tag(message) if message.can_be_authorized?
end

def self.copy_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

(message_draft.tags.simple + message_draft.tags.signed).each { |tag| message.assign_tag(tag) }
end

def self.add_delivery_notification_tag(message)
message.add_cascading_tag(delivery_notification_tag(message))
end
Expand Down
1 change: 0 additions & 1 deletion app/models/message_draft.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ def update_content(parameters)
def submit
raise NotImplementedError
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 @@ -7,6 +7,7 @@
# mimetype :string
# name :string
# object_type :string not null
# uuid :uuid
# visualizable :boolean
# created_at :datetime not null
# updated_at :datetime not null
Expand Down Expand Up @@ -54,6 +55,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 @@ -114,6 +123,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
3 changes: 3 additions & 0 deletions app/models/tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ 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_by, -> { where(type: "SignedByTag") }
scope :signature_requesting, -> { where(type: "SignatureRequestedFromTag") }
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
Original file line number Diff line number Diff line change
@@ -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

2 changes: 2 additions & 0 deletions db/schema.rb

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

33 changes: 33 additions & 0 deletions test/fixtures/govbox/messages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,39 @@ ssd_referring_to_outbox_message:
class: MyClass
content: MyContent

ssd_general_created_from_draft:
message_id: 9b1b718a-c06c-487c-86c2-b68b8606aa5c
correlation_id: 7a364355-882c-41d2-b1b3-e215644f805b
edesk_message_id: 7
delivered_at: <%= DateTime.current %>
edesk_class: TEST
folder: ssd_sent
payload:
message_id: 9b1b718a-c06c-487c-86c2-b68b8606aa5c
subject: Title is not shown
sender_name: MySender
sender_uri: MySenderURIq
recipient_name: MyRecipient
delivered_at: <%= DateTime.current.to_s %>
original_html: Reply to something
objects:
- id: 6a0f716a-c284-4680-ad7e-ed2bde769dd2
name: MyString
mime_type: MyString
class: FORM
content: MyContent
- id: af0e1c11-d226-45b7-8816-a5c24e139d35
name: Attachment
mime_type: MyString
class: ATTACHMENT
content: MyContent
- id: 57c9954c-93e2-470c-833c-fd2bc6d8c70f
name: Attachment2
mime_type: MyString
signed: true
class: ATTACHMENT
content: MyContent

ssd_without_recipient_name:
message_id: <%= SecureRandom.uuid %>
correlation_id: <%= SecureRandom.uuid %>
Expand Down
10 changes: 10 additions & 0 deletions test/fixtures/message_objects.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,27 @@ ssd_main_fs_one_form:
object_type: FORM

ssd_main_draft_to_be_signed_draft_one_form:
uuid: 6a0f716a-c284-4680-ad7e-ed2bde769dd2
message: ssd_main_draft_to_be_signed_draft_one
name: MyString
mimetype: MyString
object_type: FORM

ssd_main_draft_to_be_signed_draft_one_attachment:
uuid: af0e1c11-d226-45b7-8816-a5c24e139d35
message: ssd_main_draft_to_be_signed_draft_one
name: Attachment
mimetype: MyString
object_type: ATTACHMENT

ssd_main_draft_to_be_signed_draft_one_attachment2:
uuid: 57c9954c-93e2-470c-833c-fd2bc6d8c70f
message: ssd_main_draft_to_be_signed_draft_one
name: Attachment2
mimetype: MyString
is_signed: true
object_type: ATTACHMENT

ssd_main_draft_to_be_signed_draft_two_form:
message: ssd_main_draft_to_be_signed_draft_two
name: MyString
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/message_objects_tags.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ ssd_main_draft_to_be_signed_draft_one_form_signature_requested:
message_object: ssd_main_draft_to_be_signed_draft_one_form
tag: ssd_signer_user_signature_requested

ssd_main_draft_to_be_signed_draft_one_attachment2_signed_ssd_signer:
message_object: ssd_main_draft_to_be_signed_draft_one_attachment2
tag: ssd_signer_user_signed

ssd_main_draft_to_be_signed_draft_two_form_signature_requested:
message_object: ssd_main_draft_to_be_signed_draft_two_form
tag: ssd_signer_user_signature_requested
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/message_thread_merge_identifiers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,8 @@ ssd_main_issue_one:
message_thread: ssd_main_issue
uuid: <%= SecureRandom.uuid %>
box: ssd_main

ssd_main_draft_to_be_signed:
message_thread: ssd_main_draft_to_be_signed
uuid: 7a364355-882c-41d2-b1b3-e215644f805b
box: ssd_main
12 changes: 12 additions & 0 deletions test/fixtures/message_threads_tags.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,18 @@ ssd_main_draft_to_be_signed_ssd_signer_user_signature_requested:
message_thread: ssd_main_draft_to_be_signed
tag: ssd_signer_user_signature_requested

ssd_main_draft_to_be_signed_ssd_signer_user_signed:
message_thread: ssd_main_draft_to_be_signed
tag: ssd_signer_user_signed

ssd_main_draft_to_be_signed_ssd_signed:
message_thread: ssd_main_draft_to_be_signed
tag: ssd_signed

ssd_main_draft_to_be_signed_ssd_submitted:
message_thread: ssd_main_draft_to_be_signed
tag: ssd_submitted

ssd_main_draft_to_be_signed2_finance:
message_thread: ssd_main_draft_to_be_signed2
tag: ssd_finance
Expand Down
7 changes: 4 additions & 3 deletions test/fixtures/messages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -186,15 +186,16 @@ ssd_main_collapsed_two:
collapsed: true

ssd_main_draft_to_be_signed_draft_one:
type: MessageDraft
uuid: <%= SecureRandom.uuid %>
type: Upvs::MessageDraft
uuid: 9b1b718a-c06c-487c-86c2-b68b8606aa5c
title: Title is not shown
html_visualization: Reply to something
delivered_at: 2023-05-18 16:18:26
thread: ssd_main_draft_to_be_signed
replyable: false
metadata:
status: created
correlation_id: 7a364355-882c-41d2-b1b3-e215644f805b
author: basic

ssd_main_draft_to_be_signed_draft_two:
Expand All @@ -203,7 +204,7 @@ ssd_main_draft_to_be_signed_draft_two:
title: MyStringDraft2
html_visualization: MyString
delivered_at: 2023-05-18 16:18:26
thread: ssd_main_draft_to_be_signed
thread: ssd_main_draft_to_be_signed2
replyable: false
metadata:
status: created
Expand Down
Loading

0 comments on commit 868aa26

Please sign in to comment.