Skip to content

Commit

Permalink
Fix migrations - consider box when selecting messages & add dependent…
Browse files Browse the repository at this point in the history
… option to message relations
  • Loading branch information
luciajanikova committed Oct 20, 2023
1 parent 48bc76b commit 60d610e
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 6 deletions.
3 changes: 2 additions & 1 deletion app/jobs/govbox/process_message_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ def mark_associated_delivery_notification_authorized(govbox_message)
.joins(folder: :box).where(folders: { boxes: { id: govbox_message.box.id } }).take

if delivery_notification_govbox_message
delivery_notification_message = ::Message.find_by(uuid: delivery_notification_govbox_message.message_id)
delivery_notification_message = ::Message.where(uuid: delivery_notification_govbox_message.message_id)
.joins(thread: :folder).where(folders: { box_id: govbox_message.box.id }).take
delivery_notification_message.collapsed = true
delivery_notification_message.metadata["authorized"] = true
delivery_notification_message.save!
Expand Down
3 changes: 2 additions & 1 deletion app/models/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
class Message < ApplicationRecord
belongs_to :thread, class_name: 'MessageThread', foreign_key: :message_thread_id
belongs_to :author, class_name: 'User', foreign_key: :author_id, optional: true
has_many :message_relations, class_name: 'MessageRelation'
has_many :message_relations, dependent: :destroy
has_many :message_relations_as_related_message, class_name: 'MessageRelation', foreign_key: :related_message_id, dependent: :destroy
has_many :related_messages, through: :message_relations
has_many :messages_tags, dependent: :destroy
has_many :tags, through: :messages_tags
Expand Down
5 changes: 2 additions & 3 deletions db/migrate/20231008053236_add_collapsed_to_messages.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ def change

message.update(collapsed: govbox_message.collapsed?)

delivery_notification_govbox_message = Govbox::Message.where("payload -> 'delivery_notification' -> 'consignment' ->> 'message_id' = ?", govbox_message.message_id).joins(folder: :box).where(folders: { boxes: { id: govbox_message.box.id } }).take
delivery_notification_govbox_message = Govbox::Message.where("payload -> 'delivery_notification' -> 'consignment' ->> 'message_id' = ?", govbox_message.message_id)
.joins(folder: :box).where(folders: { boxes: { id: govbox_message.box.id } }).take

if delivery_notification_govbox_message
delivery_notification_message = ::Message.find_by(uuid: delivery_notification_govbox_message.message_id)
delivery_notification_message.update(collapsed: true)
end
end

# change_column :messages, :collapsed, :boolean, null: false, default: false
end
end
26 changes: 26 additions & 0 deletions db/migrate/20231020095255_change_messages_collapsed_attribute.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class ChangeMessagesCollapsedAttribute < ActiveRecord::Migration[7.0]
def change
Message.order(:delivered_at).find_each do |message|
if message.is_a?(MessageDraft)
message.update(collapsed: false)
next
end

govbox_message = Govbox::Message.where(message_id: message.uuid)
.joins(folder: :box).where(folders: { boxes: { id: message.thread.box.id } }).take

message.update(collapsed: govbox_message.collapsed?)

delivery_notification_govbox_message = Govbox::Message.where("payload -> 'delivery_notification' -> 'consignment' ->> 'message_id' = ?", govbox_message.message_id)
.joins(folder: :box).where(folders: { boxes: { id: govbox_message.box.id } }).take

if delivery_notification_govbox_message
delivery_notification_message = ::Message.where(uuid: delivery_notification_govbox_message.message_id)
.joins(thread: :folder).where(folders: { box_id: govbox_message.box.id }).take
delivery_notification_message.update(collapsed: true)
end
end

change_column :messages, :collapsed, :boolean, null: false, default: false
end
end
13 changes: 13 additions & 0 deletions db/migrate/20231020095657_fix_message_relations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class FixMessageRelations < ActiveRecord::Migration[7.0]
def up
MessageRelation.destroy_all

Message.find_each do |message|
main_message = Message.where(uuid: message.metadata["reference_id"]).joins(thread: :folder).where(folders: { box_id: message.thread.box.id }).take

main_message.message_relations.find_or_create_by!(
related_message: message,
) if main_message
end
end
end
2 changes: 1 addition & 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 60d610e

Please sign in to comment.