Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/solver-it-sro/govbox-pro in…
Browse files Browse the repository at this point in the history
…to GO-195/sync_folders_according_to_box

# Conflicts:
#	app/jobs/govbox/sync_box_job.rb
#	db/schema.rb
  • Loading branch information
luciajanikova committed Sep 20, 2023
2 parents f620e94 + 2d626be commit 7b255ef
Show file tree
Hide file tree
Showing 18 changed files with 1,694 additions and 35 deletions.
2 changes: 1 addition & 1 deletion app/components/message_threads_table_component.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class="flex flex-col justify-stretch items-stretch gap-4 p-4">
<div class="flex flex-col justify-stretch items-stretch rounded-md bg-white border border-gray-200">
<%= form_with(url:merge_message_threads_path, method: :get, local: true) do |form|%>
<%= form_with(url: merge_message_threads_path, local: true) do |form|%>
<div class="flex justify-stretch items-center gap-4 p-4 border-t-0 border-r-0 border-b border-l-0 border-gray-200">
<div class="todo flex justify-start items-start pr-[100px]">
<div class=" w-4 h-4 relative">
Expand Down
10 changes: 10 additions & 0 deletions app/jobs/govbox/destroy_box_data_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module Govbox
class DestroyBoxDataJob < ApplicationJob
queue_as :default

def perform(box_id)
Govbox::ApiConnection.find_by(box_id: box_id).destroy
Govbox::Folder.where(box_id: box_id).destroy_all
end
end
end
41 changes: 22 additions & 19 deletions app/jobs/govbox/submit_message_draft_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,24 @@ def perform(message_draft, upvs_client: UpvsEnvironment.upvs_client)
}.compact

sktalk_api = upvs_client.api(message_draft.thread.folder.box).sktalk
success, response_status, response_body = sktalk_api.receive_and_save_to_outbox(message_draft_data)

begin
success, response_status = sktalk_api.receive_and_save_to_outbox(message_draft_data)
if success
message_draft.metadata["status"] = "submitted"
Govbox::SyncBoxJob.set(wait: 3.minutes).perform_later(message_draft.thread.folder.box)
else
handle_submit_fail(message_draft, response_status)
end

message_draft.save!
rescue Error => error
message_draft.metadata["status"] = "submit_failed_temporary"
if success
message_draft.metadata["status"] = "submitted"
message_draft.save!

raise error
Govbox::SyncBoxJob.set(wait: 3.minutes).perform_later(message_draft.thread.folder.box)
else
handle_submit_fail(message_draft, response_status, response_body.dig("message"))
end
end

class SubmissionError < StandardError
end

class TemporarySubmissionError < SubmissionError
end

private

def build_objects(message_draft)
Expand All @@ -52,14 +51,18 @@ def build_objects(message_draft)
objects
end

def handle_submit_fail(message_draft, response_status)
def handle_submit_fail(message_draft, response_status, response_message)
case response_status
when 408
# TODO
when 422
message_draft.metadata["status"] = "submit_failed_unprocessable"
when 408, 503
message_draft.metadata["status"] = "temporary_submit_fail"
message_draft.save

raise TemporarySubmissionError, "#{response_status}, #{response_message}"
else
message_draft.metadata["status"] = "submit_failed_temporary"
message_draft.metadata["status"] = "submit_fail"
message_draft.save

raise SubmissionError, "#{response_status}, #{response_message}"
end
end
end
1 change: 1 addition & 0 deletions app/lib/event_bus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ def self.reset!
->(message_thread_tag) { Searchable::ReindexMessageThreadJob.perform_later(message_thread_tag.message_thread) }
EventBus.subscribe :tag_renamed, ->(tag) { Searchable::ReindexMessageThreadsWithTagIdJob.perform_later(tag.id) }
EventBus.subscribe :tag_removed, ->(tag) { Searchable::ReindexMessageThreadsWithTagIdJob.perform_later(tag.id) }
EventBus.subscribe :box_destroyed, ->(box_id) { Govbox::DestroyBoxDataJob.perform_later(box_id) }
2 changes: 1 addition & 1 deletion app/lib/upvs/govbox_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def authorization_successful?(response_status, authorization_code)
class SkTalk < Namespace
def receive_and_save_to_outbox(data)
response_status, response_body = @api.request(:post, "#{@api.url}/api/sktalk/receive_and_save_to_outbox", data.to_json, header)
[submit_successful?(response_status, response_body['receive_result'], response_body['save_to_outbox_result']), response_status]
[submit_successful?(response_status, response_body['receive_result'], response_body['save_to_outbox_result']), response_status, response_body]
end

private
Expand Down
5 changes: 1 addition & 4 deletions app/models/box.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ class Box < ApplicationRecord
has_many :message_threads, through: :folders, extend: MessageThreadsExtensions, dependent: :destroy
has_many :message_drafts_imports, dependent: :destroy

before_destroy do
Govbox::ApiConnection.find_by(box_id: id).destroy
Govbox::Folder.where(box_id: id).destroy_all
end
before_destroy ->(box) { EventBus.publish(:box_destroyed, box.id) }
end

4 changes: 2 additions & 2 deletions app/models/govbox/folder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ def full_name
end

def inbox?
name == "Inbox"
name.start_with? 'Inbox'
end

def bin?
name == 'Bin'
end

def drafts?
name == 'Drafts'
name.start_with? == 'Drafts'
end
end
3 changes: 3 additions & 0 deletions app/models/tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ class Tag < ApplicationRecord
has_many :groups, through: :tag_groups
belongs_to :owner, class_name: 'User', optional: true

validates :name, presence: true
validates :name, uniqueness: { scope: :tenant_id, case_sensitive: false }

after_update_commit ->(tag) { EventBus.publish(:tag_renamed, tag) if previous_changes.key?("name") }
after_destroy ->(tag) { EventBus.publish(:tag_removed, tag) }
end
2 changes: 1 addition & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class User < ApplicationRecord
has_many :automation_rules, class_name: 'Automation::Rule'

validates_presence_of :name, :email
validates_uniqueness_of :name, :email, scope: :tenant_id
validates_uniqueness_of :name, :email, scope: :tenant_id, case_sensitive: false

before_destroy :delete_user_group, prepend: true
after_create :handle_default_groups
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

resources :message_threads do
collection do
get 'merge'
post 'merge'
end
resources :messages
end
Expand Down
43 changes: 43 additions & 0 deletions db/migrate/20230914130304_add_unique_name_index_to_tags.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class AddUniqueNameIndexToTags < ActiveRecord::Migration[7.0]
def up
Tag.connection.select_all(
"select tenant_id, ARRAY_AGG(id) AS ids from tags GROUP BY (tenant_id, LOWER(name)) HAVING COUNT(*) > 1;"
).cast_values.each do |row|
tenant_id, ids = row

tags = Tag.where(tenant_id: tenant_id).find(ids)

main_tag = tags[0]

tags[1..-1].each do |other_tag|
MessagesTag.where(tag_id: other_tag).map do |mt|
mt.tag_id = main_tag.id
mt.save!
end

MessageThreadsTag.where(tag_id: other_tag).map do |mtt|
mtt.tag_id = main_tag.id
mtt.save!
end

TagGroup.where(tag_id: other_tag).map do |tg|
tg.tag_id = main_tag.id
tg.save!
end

TagUser.where(tag_id: other_tag).map do |tu|
tu.tag_id = main_tag.id
tu.save!
end

other_tag.destroy
end
end

execute "CREATE UNIQUE INDEX index_tags_on_tenant_id_and_lowercase_name ON tags USING btree (tenant_id, lower(name));"
end

def down
remove_index :tags, name: "index_tags_on_tenant_id_and_lowercase_name"
end
end
11 changes: 11 additions & 0 deletions db/migrate/20230914140353_change_email_index_in_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class ChangeEmailIndexInUsers < ActiveRecord::Migration[7.0]
def up
remove_index :users, name: "index_users_on_tenant_id_and_email"
execute "CREATE UNIQUE INDEX index_users_on_tenant_id_and_lowercase_email ON users USING btree (tenant_id, lower(email));"
end

def down
remove_index :users, name: "index_users_on_tenant_id_and_lowercase_email"
add_index :users, [:tenant_id, :email], unique: true, name: "index_users_on_tenant_id_and_email"
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class UpdateReplyableAttributesOnMessages < ActiveRecord::Migration[7.0]
def change
Message.find_each do |message|
govbox_message = Govbox::Message.find_by(message_id: message.uuid)
message.update(replyable: govbox_message&.replyable? || false)
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class UpdateBoxReferenceInGovboxFolders < ActiveRecord::Migration[7.0]
def change
remove_foreign_key :govbox_folders, :boxes
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class UpdateBoxReferenceInGovboxApiConnections < ActiveRecord::Migration[7.0]
def change
remove_foreign_key :govbox_api_connections, :boxes
end
end
7 changes: 3 additions & 4 deletions db/schema.rb

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

Loading

0 comments on commit 7b255ef

Please sign in to comment.