Skip to content

Commit

Permalink
Merge pull request #50 from solver-it-sro/GO-195/sync_folders_accordi…
Browse files Browse the repository at this point in the history
…ng_to_box

GO-195 Download messages according to box (allow same box in different tenants)
  • Loading branch information
luciajanikova authored Sep 20, 2023
2 parents 2d626be + 7b255ef commit 4dcd4bc
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 5 deletions.
2 changes: 1 addition & 1 deletion app/jobs/govbox/sync_box_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def find_or_create_folder_with_parent(folder_hash, folders, box)
parent_folder = find_or_create_folder_with_parent(folders[folder_hash['parent_id']], folders, box)
end

folder = Govbox::Folder.find_or_initialize_by(edesk_folder_id: folder_hash['id'], box_id: box.id).tap do |f|
folder = Govbox::Folder.find_or_initialize_by(edesk_folder_id: folder_hash['id'], box: box).tap do |f|
f.edesk_folder_id = folder_hash['id']
f.parent_folder = parent_folder
f.name = folder_hash['name']
Expand Down
2 changes: 1 addition & 1 deletion app/jobs/govbox/sync_folder_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def perform(folder, upvs_client: UpvsEnvironment.upvs_client, batch_size: 1000)

raise "Unable to fetch folder messages" if response_status != 200

edesk_message_ids_to_folder_ids = Govbox::Message.where(edesk_message_id: raw_messages.pluck('id')).pluck(:edesk_message_id, :folder_id).to_h
edesk_message_ids_to_folder_ids = Govbox::Message.joins(:folder).where(folder: { box_id: folder.box.id }).where(edesk_message_id: raw_messages.pluck('id')).pluck(:edesk_message_id, :folder_id).to_h
moved_edesk_message_ids = []

raw_messages.each do |raw_message|
Expand Down
2 changes: 1 addition & 1 deletion app/models/box/message_threads_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def find_or_create_by_merge_uuid!(folder:, merge_uuid:, title:, delivered_at:)
delivered_at: delivered_at,
last_message_delivered_at: delivered_at
)
thread.merge_identifiers.build(uuid: merge_uuid)
thread.merge_identifiers.build(uuid: merge_uuid, tenant: folder.tenant)
end

thread.save!
Expand Down
2 changes: 2 additions & 0 deletions app/models/message_thread_merge_identifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
#
# id :integer not null, primary key
# message_thread_id :integer not null
# tenant_id :integer not null
# uuid :uuid not null
# created_at :datetime not null
# updated_at :datetime not null

class MessageThreadMergeIdentifier < ApplicationRecord
belongs_to :message_thread
belongs_to :tenant
end
6 changes: 6 additions & 0 deletions db/migrate/20230914120010_update_govbox_folders_indexes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class UpdateGovboxFoldersIndexes < ActiveRecord::Migration[7.0]
def change
remove_index :govbox_folders, :edesk_folder_id, unique: true
add_index :govbox_folders, [:edesk_folder_id, :box_id], unique: true
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class AddTenantIdToMessageThreadMergeIdentifiers < ActiveRecord::Migration[7.0]
def up
add_reference :message_thread_merge_identifiers, :tenant, foreign_key: true

MessageThreadMergeIdentifier.find_each { |merge_identifier| merge_identifier.update(tenant: merge_identifier.message_thread.folder.tenant) }

change_column :message_thread_merge_identifiers, :tenant_id, :bigint, null: false, foreign_key: true
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class UpdateMessageThreadMergeIdentifiersUniqueConstraint < ActiveRecord::Migration[7.0]
def change
remove_index :message_thread_merge_identifiers, :uuid, unique: true
add_index :message_thread_merge_identifiers, [:uuid, :tenant_id], unique: true
end
end
7 changes: 5 additions & 2 deletions db/schema.rb

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

3 changes: 3 additions & 0 deletions test/fixtures/message_thread_merge_identifiers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
one:
message_thread: one
uuid: <%= SecureRandom.uuid %>
tenant: solver

two:
message_thread: one
uuid: <%= SecureRandom.uuid %>
tenant: solver

three:
message_thread: two
uuid: <%= SecureRandom.uuid %>
tenant: solver

0 comments on commit 4dcd4bc

Please sign in to comment.