From b83423f35fde86de51f632089ede0ad3cd7c62a4 Mon Sep 17 00:00:00 2001
From: Miroslav Hettes
Date: Thu, 17 Aug 2023 15:16:10 +0200
Subject: [PATCH 13/34] Convert joins to subselect
---
app/policies/message_thread_policy.rb | 15 ++++++++++-----
app/policies/tag_policy.rb | 7 ++++++-
2 files changed, 16 insertions(+), 6 deletions(-)
diff --git a/app/policies/message_thread_policy.rb b/app/policies/message_thread_policy.rb
index cb7607b69..6d6d5ec48 100644
--- a/app/policies/message_thread_policy.rb
+++ b/app/policies/message_thread_policy.rb
@@ -10,11 +10,16 @@ def initialize(user, message_thread)
class Scope < Scope
def resolve
- if @user.site_admin?
- scope.all
- else
- scope.joins(tags: { groups: :group_memberships }).where(group_memberships: { user_id: @user.id })
- end
+ return scope.all if @user.site_admin?
+
+ scope.where(
+ 'id IN (
+ SELECT message_thread_id FROM message_threads_tags mt_tags
+ JOIN tag_groups tg on mt_tags.tag_id = tg.tag_id
+ JOIN group_memberships gm on tg.group_id = gm.group_id
+ WHERE gm.user_id = ?)',
+ @user.id
+ )
end
end
diff --git a/app/policies/tag_policy.rb b/app/policies/tag_policy.rb
index 416b8c6fa..eea53689e 100644
--- a/app/policies/tag_policy.rb
+++ b/app/policies/tag_policy.rb
@@ -12,7 +12,12 @@ class Scope < Scope
def resolve
return scope.where(tenant_id: Current.tenant.id) if @user.site_admin?
return scope.where(tenant_id: @user.tenant_id) if @user.admin?
- joins(groups: :group_memberships).where(group_memberships: {user_id: @user.id})
+
+ scope.where('id IN (
+ SELECT tag_id FROM tag_groups tg
+ JOIN group_memberships gm on tg.group_id = gm.group_id
+ WHERE gm.user_id = ?
+ )', @user.id)
end
end
From 87832bf96456e26419ff873b18d0dfea982bd943 Mon Sep 17 00:00:00 2001
From: Miroslav Hettes
Date: Fri, 18 Aug 2023 14:06:25 +0200
Subject: [PATCH 14/34] White subselect with exists and user active record
methods
---
app/models/message_threads_tag.rb | 3 +++
app/models/tag_group.rb | 3 +++
app/policies/message_draft_policy.rb | 24 +++++++----------
app/policies/message_object_policy.rb | 25 +++++++----------
app/policies/message_policy.rb | 24 +++++++----------
app/policies/message_thread_policy.rb | 12 ++++-----
app/policies/message_threads_tag_policy.rb | 31 +++++++---------------
app/policies/tag_policy.rb | 13 +++++----
8 files changed, 60 insertions(+), 75 deletions(-)
diff --git a/app/models/message_threads_tag.rb b/app/models/message_threads_tag.rb
index 6a3054d2a..b6b9270f4 100644
--- a/app/models/message_threads_tag.rb
+++ b/app/models/message_threads_tag.rb
@@ -11,4 +11,7 @@
class MessageThreadsTag < ApplicationRecord
belongs_to :message_thread
belongs_to :tag
+
+ # used for joins only
+ has_many :tag_groups, primary_key: :tag_id, foreign_key: :tag_id
end
diff --git a/app/models/tag_group.rb b/app/models/tag_group.rb
index ad3de2192..e6d077459 100644
--- a/app/models/tag_group.rb
+++ b/app/models/tag_group.rb
@@ -1,4 +1,7 @@
class TagGroup < ApplicationRecord
belongs_to :group
belongs_to :tag
+
+ # used for joins only
+ has_many :group_memberships, primary_key: :group_id, foreign_key: :group_id
end
diff --git a/app/policies/message_draft_policy.rb b/app/policies/message_draft_policy.rb
index 0ce4e71bf..425554454 100644
--- a/app/policies/message_draft_policy.rb
+++ b/app/policies/message_draft_policy.rb
@@ -10,20 +10,16 @@ def initialize(user, message)
class Scope < Scope
def resolve
- if @user.site_admin?
- scope.all
- else
- scope.where(
- 'message_thread_id in (
- select mt.id
- from message_threads mt
- join message_threads_tags mt_tags on mt.id = mt_tags.message_thread_id
- join tag_groups tg on mt_tags.tag_id = tg.tag_id
- join group_memberships gm on tg.group_id = gm.group_id
- where user_id = ?)',
- @user.id
- )
- end
+ scope.all if @user.site_admin?
+
+ scope.where(
+ MessageThreadsTag
+ .select("1")
+ .joins(tag_groups: :group_memberships)
+ .where("message_threads_tags.message_thread_id = messages.message_thread_id")
+ .where(group_memberships: { user_id: @user.id })
+ .arel.exists
+ )
end
end
diff --git a/app/policies/message_object_policy.rb b/app/policies/message_object_policy.rb
index 3761719f3..94b37dd78 100644
--- a/app/policies/message_object_policy.rb
+++ b/app/policies/message_object_policy.rb
@@ -10,21 +10,16 @@ def initialize(user, message_object)
class Scope < Scope
def resolve
- if @user.site_admin?
- scope.all
- else
- scope.where(
- 'message_id in (
- select m.id
- from messages m
- join message_threads mt on mt.id = m.message_thread_id
- join message_threads_tags mt_tags on mt.id = mt_tags.message_thread_id
- join tag_groups tg on mt_tags.tag_id = tg.tag_id
- join group_memberships gm on tg.group_id = gm.group_id
- where user_id = ?)',
- @user.id
- )
- end
+ scope.all if @user.site_admin?
+
+ scope.where(
+ Message
+ .select("1")
+ .joins(message_threads_tags: { tag_groups: :group_memberships })
+ .where("message_objects.message_id = messages.id")
+ .where(group_memberships: { user_id: @user.id })
+ .arel.exists
+ )
end
end
diff --git a/app/policies/message_policy.rb b/app/policies/message_policy.rb
index fc6e8aa9b..5deb2e085 100644
--- a/app/policies/message_policy.rb
+++ b/app/policies/message_policy.rb
@@ -10,20 +10,16 @@ def initialize(user, message)
class Scope < Scope
def resolve
- if @user.site_admin?
- scope.all
- else
- scope.where(
- 'message_thread_id in (
- select mt.id
- from message_threads mt
- join message_threads_tags mt_tags on mt.id = mt_tags.message_thread_id
- join tag_groups tg on mt_tags.tag_id = tg.tag_id
- join group_memberships gm on tg.group_id = gm.group_id
- where user_id = ?)',
- @user.id
- )
- end
+ scope.all if @user.site_admin?
+
+ scope.where(
+ MessageThreadsTag
+ .select("1")
+ .joins(tag_groups: :group_memberships)
+ .where("message_threads_tags.message_thread_id = messages.message_thread_id")
+ .where(group_memberships: { user_id: @user.id })
+ .arel.exists
+ )
end
end
diff --git a/app/policies/message_thread_policy.rb b/app/policies/message_thread_policy.rb
index 6d6d5ec48..fa660d561 100644
--- a/app/policies/message_thread_policy.rb
+++ b/app/policies/message_thread_policy.rb
@@ -13,12 +13,12 @@ def resolve
return scope.all if @user.site_admin?
scope.where(
- 'id IN (
- SELECT message_thread_id FROM message_threads_tags mt_tags
- JOIN tag_groups tg on mt_tags.tag_id = tg.tag_id
- JOIN group_memberships gm on tg.group_id = gm.group_id
- WHERE gm.user_id = ?)',
- @user.id
+ MessageThreadsTag
+ .select("1")
+ .joins(:tag_groups => :group_memberships)
+ .where("message_threads_tags.message_thread_id = message_threads.id")
+ .where(group_memberships: { user_id: @user.id })
+ .arel.exists
)
end
end
diff --git a/app/policies/message_threads_tag_policy.rb b/app/policies/message_threads_tag_policy.rb
index 03bea0be6..61efd292f 100644
--- a/app/policies/message_threads_tag_policy.rb
+++ b/app/policies/message_threads_tag_policy.rb
@@ -10,27 +10,16 @@ def initialize(user, message_threads_tag)
class Scope < Scope
def resolve
- if @user.site_admin?
- scope.all
- else
- scope.where(
- 'message_thread_id in (
- select mt.id
- from message_threads mt
- join message_threads_tags mt_tags on mt.id = mt_tags.message_thread_id
- join tag_groups tg on mt_tags.tag_id = tg.tag_id
- join group_memberships gm on tg.group_id = gm.group_id
- where user_id = ?)',
- @user.id
- ).where(
- "tag_id in (
- select tag_id
- from tag_groups tg
- join group_memberships gm on tg.group_id = gm.group_id
- where user_id = ?)",
- @user.id
- )
- end
+ scope.all if @user.site_admin?
+
+ scope.where(
+ TagGroup
+ .select("1")
+ .joins(:group_memberships)
+ .where("tag_groups.tag_id = message_threads_tags.tag_id")
+ .where(group_memberships: { user_id: @user.id })
+ .arel.exists
+ )
end
end
diff --git a/app/policies/tag_policy.rb b/app/policies/tag_policy.rb
index eea53689e..db6579bfd 100644
--- a/app/policies/tag_policy.rb
+++ b/app/policies/tag_policy.rb
@@ -13,11 +13,14 @@ def resolve
return scope.where(tenant_id: Current.tenant.id) if @user.site_admin?
return scope.where(tenant_id: @user.tenant_id) if @user.admin?
- scope.where('id IN (
- SELECT tag_id FROM tag_groups tg
- JOIN group_memberships gm on tg.group_id = gm.group_id
- WHERE gm.user_id = ?
- )', @user.id)
+ scope.where(
+ TagGroup
+ .select("1")
+ .joins(:group_memberships)
+ .where("tag_groups.tag_id = tags.id")
+ .where(group_memberships: { user_id: @user.id })
+ .arel.exists
+ )
end
end
From 6c9480c30db035eee3edee01438f926d8bc9beda Mon Sep 17 00:00:00 2001
From: Miroslav Hettes
Date: Mon, 21 Aug 2023 11:27:08 +0200
Subject: [PATCH 15/34] Change to select 1
---
app/policies/message_draft_policy.rb | 2 +-
app/policies/message_object_policy.rb | 2 +-
app/policies/message_policy.rb | 2 +-
app/policies/message_thread_policy.rb | 2 +-
app/policies/message_threads_tag_policy.rb | 2 +-
app/policies/tag_policy.rb | 2 +-
6 files changed, 6 insertions(+), 6 deletions(-)
diff --git a/app/policies/message_draft_policy.rb b/app/policies/message_draft_policy.rb
index 425554454..6e4d6e2e3 100644
--- a/app/policies/message_draft_policy.rb
+++ b/app/policies/message_draft_policy.rb
@@ -14,7 +14,7 @@ def resolve
scope.where(
MessageThreadsTag
- .select("1")
+ .select(1)
.joins(tag_groups: :group_memberships)
.where("message_threads_tags.message_thread_id = messages.message_thread_id")
.where(group_memberships: { user_id: @user.id })
diff --git a/app/policies/message_object_policy.rb b/app/policies/message_object_policy.rb
index 94b37dd78..6fa5e82de 100644
--- a/app/policies/message_object_policy.rb
+++ b/app/policies/message_object_policy.rb
@@ -14,7 +14,7 @@ def resolve
scope.where(
Message
- .select("1")
+ .select(1)
.joins(message_threads_tags: { tag_groups: :group_memberships })
.where("message_objects.message_id = messages.id")
.where(group_memberships: { user_id: @user.id })
diff --git a/app/policies/message_policy.rb b/app/policies/message_policy.rb
index 5deb2e085..e020a5c05 100644
--- a/app/policies/message_policy.rb
+++ b/app/policies/message_policy.rb
@@ -14,7 +14,7 @@ def resolve
scope.where(
MessageThreadsTag
- .select("1")
+ .select(1)
.joins(tag_groups: :group_memberships)
.where("message_threads_tags.message_thread_id = messages.message_thread_id")
.where(group_memberships: { user_id: @user.id })
diff --git a/app/policies/message_thread_policy.rb b/app/policies/message_thread_policy.rb
index fa660d561..6ba588a6a 100644
--- a/app/policies/message_thread_policy.rb
+++ b/app/policies/message_thread_policy.rb
@@ -14,7 +14,7 @@ def resolve
scope.where(
MessageThreadsTag
- .select("1")
+ .select(1)
.joins(:tag_groups => :group_memberships)
.where("message_threads_tags.message_thread_id = message_threads.id")
.where(group_memberships: { user_id: @user.id })
diff --git a/app/policies/message_threads_tag_policy.rb b/app/policies/message_threads_tag_policy.rb
index 61efd292f..214dce59e 100644
--- a/app/policies/message_threads_tag_policy.rb
+++ b/app/policies/message_threads_tag_policy.rb
@@ -14,7 +14,7 @@ def resolve
scope.where(
TagGroup
- .select("1")
+ .select(1)
.joins(:group_memberships)
.where("tag_groups.tag_id = message_threads_tags.tag_id")
.where(group_memberships: { user_id: @user.id })
diff --git a/app/policies/tag_policy.rb b/app/policies/tag_policy.rb
index db6579bfd..fad809012 100644
--- a/app/policies/tag_policy.rb
+++ b/app/policies/tag_policy.rb
@@ -15,7 +15,7 @@ def resolve
scope.where(
TagGroup
- .select("1")
+ .select(1)
.joins(:group_memberships)
.where("tag_groups.tag_id = tags.id")
.where(group_memberships: { user_id: @user.id })
From 570b0dee12dbbf23734b3e954a6606cdeb94d1a8 Mon Sep 17 00:00:00 2001
From: Miroslav Hettes
Date: Tue, 22 Aug 2023 09:00:45 +0200
Subject: [PATCH 16/34] Fix admin policy
---
app/policies/message_draft_policy.rb | 2 +-
app/policies/message_object_policy.rb | 2 +-
app/policies/message_policy.rb | 2 +-
app/policies/message_threads_tag_policy.rb | 2 +-
4 files changed, 4 insertions(+), 4 deletions(-)
diff --git a/app/policies/message_draft_policy.rb b/app/policies/message_draft_policy.rb
index 6e4d6e2e3..326af2cd4 100644
--- a/app/policies/message_draft_policy.rb
+++ b/app/policies/message_draft_policy.rb
@@ -10,7 +10,7 @@ def initialize(user, message)
class Scope < Scope
def resolve
- scope.all if @user.site_admin?
+ return scope.all if @user.site_admin?
scope.where(
MessageThreadsTag
diff --git a/app/policies/message_object_policy.rb b/app/policies/message_object_policy.rb
index 6fa5e82de..260e53f80 100644
--- a/app/policies/message_object_policy.rb
+++ b/app/policies/message_object_policy.rb
@@ -10,7 +10,7 @@ def initialize(user, message_object)
class Scope < Scope
def resolve
- scope.all if @user.site_admin?
+ return scope.all if @user.site_admin?
scope.where(
Message
diff --git a/app/policies/message_policy.rb b/app/policies/message_policy.rb
index e020a5c05..72f665dfd 100644
--- a/app/policies/message_policy.rb
+++ b/app/policies/message_policy.rb
@@ -10,7 +10,7 @@ def initialize(user, message)
class Scope < Scope
def resolve
- scope.all if @user.site_admin?
+ return scope.all if @user.site_admin?
scope.where(
MessageThreadsTag
diff --git a/app/policies/message_threads_tag_policy.rb b/app/policies/message_threads_tag_policy.rb
index 214dce59e..d913a5ba1 100644
--- a/app/policies/message_threads_tag_policy.rb
+++ b/app/policies/message_threads_tag_policy.rb
@@ -10,7 +10,7 @@ def initialize(user, message_threads_tag)
class Scope < Scope
def resolve
- scope.all if @user.site_admin?
+ return scope.all if @user.site_admin?
scope.where(
TagGroup
From e1de5cb63cb449a3d24270e2cf4a2fb85ff928f7 Mon Sep 17 00:00:00 2001
From: luciajanikova <19lucia99@gmail.com>
Date: Wed, 23 Aug 2023 19:42:26 +0200
Subject: [PATCH 17/34] Update models, Add GeneralAgendaBuilder
---
.../message_drafts_component.html.erb | 2 +-
app/controllers/drafts_controller.rb | 44 -----------
app/controllers/message_drafts_controller.rb | 26 ++++---
...b => message_drafts_imports_controller.rb} | 8 +-
app/jobs/drafts/finish_import_job.rb | 4 +-
app/jobs/drafts/parse_import_job.rb | 2 +
app/lib/upvs/GeneralAgendaBuilder.rb | 12 +++
app/models/box.rb | 4 +-
app/models/draft.rb | 69 -----------------
app/models/drafts.rb | 5 --
app/models/drafts/object.rb | 36 ---------
app/models/message_draft.rb | 77 ++++++++++++++++++-
.../import.rb => message_drafts_import.rb} | 6 +-
....rb => message_drafts_import_validator.rb} | 2 +-
app/policies/draft_policy.rb | 36 ---------
...icy.rb => message_drafts_import_policy.rb} | 2 +-
app/views/drafts/index.html.erb | 33 --------
app/views/drafts/show.html.erb | 31 --------
app/views/message_drafts/index.html.erb | 11 ++-
.../upload_new.html.erb | 2 +-
config/routes.rb | 14 +---
db/migrate/20230809141544_drop_drafts.rb | 6 ++
...rafts_imports_to_message_drafts_imports.rb | 5 ++
db/schema.rb | 61 ++++-----------
24 files changed, 151 insertions(+), 347 deletions(-)
delete mode 100644 app/controllers/drafts_controller.rb
rename app/controllers/{drafts/imports_controller.rb => message_drafts_imports_controller.rb} (69%)
create mode 100644 app/lib/upvs/GeneralAgendaBuilder.rb
delete mode 100644 app/models/draft.rb
delete mode 100644 app/models/drafts.rb
delete mode 100644 app/models/drafts/object.rb
rename app/models/{drafts/import.rb => message_drafts_import.rb} (85%)
rename app/models/validators/{drafts_import_validator.rb => message_drafts_import_validator.rb} (96%)
delete mode 100644 app/policies/draft_policy.rb
rename app/policies/{drafts/import_policy.rb => message_drafts_import_policy.rb} (80%)
delete mode 100644 app/views/drafts/index.html.erb
delete mode 100644 app/views/drafts/show.html.erb
rename app/views/{drafts/imports => message_drafts_imports}/upload_new.html.erb (69%)
create mode 100644 db/migrate/20230809141544_drop_drafts.rb
create mode 100644 db/migrate/20230809145031_rename_drafts_imports_to_message_drafts_imports.rb
diff --git a/app/components/message_drafts_component.html.erb b/app/components/message_drafts_component.html.erb
index a0fc58fbc..f2f426bac 100644
--- a/app/components/message_drafts_component.html.erb
+++ b/app/components/message_drafts_component.html.erb
@@ -80,7 +80,7 @@
<% end %>
- <%= button_to message_draft_path(@message), method: :delete, class: "flex justify-strech items-start self-stretch flex-grow-0 flex-shrink-0 gap-2 p-6 rounded-lg border border-gray-400 px-3.5 py-2.5 hover:bg-gray-100 focus:ring-4 focus:ring-gray-100 rounded-lg", title: "Zahodiť draft" do %>
+ <%= button_to message_draft_path(@message), method: :delete, params: { redirect_url: message_path(@message.original_message) }, class: "flex justify-strech items-start self-stretch flex-grow-0 flex-shrink-0 gap-2 p-6 rounded-lg border border-gray-400 px-3.5 py-2.5 hover:bg-gray-100 focus:ring-4 focus:ring-gray-100 rounded-lg", title: "Zahodiť draft" do %>
diff --git a/app/controllers/drafts_controller.rb b/app/controllers/drafts_controller.rb
deleted file mode 100644
index 02773228d..000000000
--- a/app/controllers/drafts_controller.rb
+++ /dev/null
@@ -1,44 +0,0 @@
-class DraftsController < ApplicationController
- before_action :load_draft, only: [:show, :submit]
- before_action :load_drafts, only: [:index, :destroy, :submit_all]
-
- def index
- @drafts = @drafts.order(created_at: :desc)
- end
-
- def show
- end
-
- def destroy
- @drafts.find(params[:id]).destroy
-
- redirect_to drafts_path
- end
-
- def submit
- submit_draft(@draft)
- end
-
- def submit_all
- @drafts.each do |draft|
- submit_draft(draft) if draft.submittable?
- end
- end
-
- private
-
- def submit_draft(draft)
- draft.being_submitted!
- Drafts::SubmitJob.perform_later(draft)
- end
-
- def load_draft
- @draft = policy_scope(Draft).find(params[:id] || params[:draft_id])
- authorize @draft
- end
-
- def load_drafts
- authorize Draft
- @drafts = policy_scope(Draft)
- end
-end
diff --git a/app/controllers/message_drafts_controller.rb b/app/controllers/message_drafts_controller.rb
index 94bff2b17..2187f1fd3 100644
--- a/app/controllers/message_drafts_controller.rb
+++ b/app/controllers/message_drafts_controller.rb
@@ -1,15 +1,16 @@
class MessageDraftsController < ApplicationController
before_action :load_message_drafts, only: :index
before_action :set_message, only: :create
- before_action :set_message_draft, except: [:index, :create]
+ before_action :load_draft, except: [:index, :create]
def index
+ @message_drafts = @message_drafts.order(created_at: :desc)
end
def create
authorize @message
- @message_draft = MessageDraft.create_from_message(@message)
+ @message_draft = MessageDraft.create_message_reply(@message)
redirect_to message_draft_path(@message_draft)
end
@@ -24,31 +25,32 @@ def update
permitted_params = message_params
- @message_draft.title = permitted_params["message_title"]
- @message_draft.metadata["message_body"] = permitted_params["message_text"]
- @message_draft.save!
+ @message_draft.update_content(title: permitted_params["message_title"], body: permitted_params["message_text"])
end
def submit
authorize @message_draft
if @message_draft.submittable?
- Govbox::SubmitMessageDraftJob.perform_later(@message_draft)
-
- @message_draft.metadata["status"] = "being_submitted"
- @message_draft.save!
-
+ MessageDraft.submit(@message_draft)
redirect_to message_path(@message_draft.original_message), notice: "Správa bola zaradená na odoslanie."
else
+ # TODO prisposobit importovanym draftom
redirect_to message_draft_path(@message_draft), notice: "Vyplňte predmet a text odpovede."
end
end
+
+ def submit_all
+ @message_drafts.each do |message_draft|
+ MessageDraft.submit(@message_draft) if message_draft.submittable?
+ end
+ end
def destroy
authorize @message_draft
@message_draft.destroy
- redirect_to message_path(@message_draft.original_message)
+ redirect_to (params[:redirect_url] || message_drafts_path)
end
private
@@ -62,7 +64,7 @@ def set_message
@message = policy_scope(Message).find(params[:original_message_id])
end
- def set_message_draft
+ def load_draft
@message_draft = policy_scope(MessageDraft).find(params[:id])
@menu = SidebarMenu.new(controller_name, action_name, { message: @message_draft })
end
diff --git a/app/controllers/drafts/imports_controller.rb b/app/controllers/message_drafts_imports_controller.rb
similarity index 69%
rename from app/controllers/drafts/imports_controller.rb
rename to app/controllers/message_drafts_imports_controller.rb
index 185c23d85..1e0979c66 100644
--- a/app/controllers/drafts/imports_controller.rb
+++ b/app/controllers/message_drafts_imports_controller.rb
@@ -1,11 +1,11 @@
-class Drafts::ImportsController < ApplicationController
+class MessageDraftsImportsController < ApplicationController
def create
- authorize Drafts::Import, policy_class: Drafts::ImportPolicy
+ authorize MessageDraftsImport
file_storage = FileStorage.new
zip_content = params[:content]
- import = Drafts::Import.create!(
+ import = MessageDraftsImport.create!(
name: "#{Time.now.to_i}_#{zip_content.original_filename}",
box: Current.box
)
@@ -17,7 +17,7 @@ def create
end
def upload_new
- authorize Drafts::Import, policy_class: Drafts::ImportPolicy
+ authorize MessageDraftsImport
end
private
diff --git a/app/jobs/drafts/finish_import_job.rb b/app/jobs/drafts/finish_import_job.rb
index 876eb863c..5c62d4bba 100644
--- a/app/jobs/drafts/finish_import_job.rb
+++ b/app/jobs/drafts/finish_import_job.rb
@@ -2,9 +2,9 @@ class Drafts::FinishImportJob < ApplicationJob
def perform(batch, params)
batch.properties[:import].message_drafts.find_each do |message_draft|
if message_draft.valid?(:validate_data)
- message_draft.metadata["status"] = "loading_done"
+ message_draft.metadata["status"] = "created"
else
- message_draft.metadata["status"] = "invalid_data"
+ message_draft.metadata["status"] = "invalid"
end
message_draft.save
diff --git a/app/jobs/drafts/parse_import_job.rb b/app/jobs/drafts/parse_import_job.rb
index 76ccff098..c4ebb1b96 100644
--- a/app/jobs/drafts/parse_import_job.rb
+++ b/app/jobs/drafts/parse_import_job.rb
@@ -29,6 +29,7 @@ def perform(import, import_zip_path, jobs_batch: GoodJob::Batch.new, load_conten
thread: thread,
title: File.basename(entry_name),
replyable: false,
+ read: true,
delivered_at: Time.now,
metadata: {
"import_id": import.id,
@@ -81,6 +82,7 @@ def load_import_csv(import, csv_path)
thread: message_thread,
title: row['message_subject'],
replyable: false,
+ read: true,
delivered_at: Time.now,
metadata: {
"recipient_uri": row['recipient_uri'],
diff --git a/app/lib/upvs/GeneralAgendaBuilder.rb b/app/lib/upvs/GeneralAgendaBuilder.rb
new file mode 100644
index 000000000..8e5877c5d
--- /dev/null
+++ b/app/lib/upvs/GeneralAgendaBuilder.rb
@@ -0,0 +1,12 @@
+class GeneralAgendaBuilder
+ GENERAL_AGENDA_SCHEMA ||= 'http://schemas.gov.sk/form/App.GeneralAgenda/1.9'
+
+ def self.build_xml(subject:, body:)
+ <<~GENERAL_AGENDA
+
+ #{subject}
+ #{body}
+
+ GENERAL_AGENDA
+ end
+end
\ No newline at end of file
diff --git a/app/models/box.rb b/app/models/box.rb
index 789e57e08..8f017d420 100644
--- a/app/models/box.rb
+++ b/app/models/box.rb
@@ -14,8 +14,6 @@ class Box < ApplicationRecord
has_many :folders
has_many :message_threads, through: :folders, extend: MessageThreadsExtensions
-
- has_many :drafts_imports, class_name: 'Drafts::Import'
- has_many :drafts
+ has_many :message_drafts_imports
end
diff --git a/app/models/draft.rb b/app/models/draft.rb
deleted file mode 100644
index 0c746b4a2..000000000
--- a/app/models/draft.rb
+++ /dev/null
@@ -1,69 +0,0 @@
-# == Schema Information
-#
-# Table name: drafts
-#
-# id :integer not null, primary key
-# box_id :integer
-# import_id :integer
-# status :integer default("created")
-# recipient_uri :string
-# posp_id :string
-# posp_version :string
-# message_type :string
-# message_subject :string
-# sender_business_reference :string
-# recipient_business_reference :string
-# import_subfolder :string
-# message_id :uuid
-# correlation_id :uuid
-# created_at :datetime not null
-# updated_at :datetime not null
-
-class Draft < ApplicationRecord
- belongs_to :box
- belongs_to :import, class_name: 'Drafts::Import', optional: true
-
- has_many :objects, class_name: 'Drafts::Object', :dependent => :destroy
-
- delegate :tenant, to: :box
-
- with_options on: :validate_data do |loaded_draft|
- loaded_draft.validates :recipient_uri, :posp_id, :posp_version, :message_type, :message_subject, :message_id, :correlation_id, presence: true
- loaded_draft.validates :message_id, :correlation_id, format: { with: Utils::UUID_PATTERN }, allow_blank: true
- loaded_draft.validate :validate_form
- loaded_draft.validate :validate_objects
- end
-
- enum status: { created: 0, being_loaded: 1, loading_done: 2, invalid_data: 3, being_submitted: 4, submitted: 5, submit_failed_unprocessable: 6, submit_failed_temporary: 7 }
-
- def title
- message_subject || import_subfolder
- end
-
- def submittable?
- (loading_done? || submit_failed_temporary?) && valid?
- end
-
- def form
- objects.select { |o| o.form? }&.first
- end
-
- private
-
- def validate_form
- forms = objects.select { |o| o.form? }
-
- if objects.size == 0
- errors.add(:objects, "No objects found for draft")
- elsif forms.count != 1
- errors.add(:objects, "Draft has to contain exactly one form")
- end
- end
-
- def validate_objects
- objects.each do |object|
- object.valid?(:validate_data)
- errors.merge!(object.errors)
- end
- end
-end
diff --git a/app/models/drafts.rb b/app/models/drafts.rb
deleted file mode 100644
index 8cebffe19..000000000
--- a/app/models/drafts.rb
+++ /dev/null
@@ -1,5 +0,0 @@
-module Drafts
- def self.table_name_prefix
- 'drafts_'
- end
-end
diff --git a/app/models/drafts/object.rb b/app/models/drafts/object.rb
deleted file mode 100644
index 80c98451e..000000000
--- a/app/models/drafts/object.rb
+++ /dev/null
@@ -1,36 +0,0 @@
-# == Schema Information
-#
-# Table name: drafts_objects
-#
-# id :integer not null, primary key
-# draft_id :string not null
-# uuid :uuid not null
-# name :string not null
-# signed :boolean
-# to_be_signed :boolean
-# form :boolean
-# created_at :datetime not null
-# updated_at :datetime not null
-
-class Drafts::Object < ApplicationRecord
- belongs_to :draft, class_name: 'Draft'
-
- has_one_attached :content
-
- validates :name, :uuid, presence: true, on: :validate_data
- validates :uuid, format: { with: Utils::UUID_PATTERN }, on: :validate_data
- validate :content_attached?, on: :validate_data
- validate :allowed_mime_type?, on: :validate_data
-
- private
-
- def content_attached?
- content.attached?
- end
-
- def allowed_mime_type?
- Utils.detect_mime_type(entry_name: self.name, is_form: self.form?)
- rescue StandardError
- errors.add(:mime_type, "of #{name} object is disallowed", allowed_mime_types: Utils::EXTENSIONS_ALLOW_LIST.join(', '))
- end
-end
diff --git a/app/models/message_draft.rb b/app/models/message_draft.rb
index 912a5fb95..6e83f1450 100644
--- a/app/models/message_draft.rb
+++ b/app/models/message_draft.rb
@@ -1,5 +1,16 @@
class MessageDraft < Message
- def self.create_from_message(message)
+ MESSAGE_REPLY_POSP_ID ||= "App.GeneralAgenda"
+ MESSAGE_REPLY_POSP_VERSION ||= "1.9"
+ MESSAGE_REPLY_MESSAGE_TYPE ||= "App.GeneralAgenda"
+
+ with_options on: :validate_data do |message_draft|
+ message_draft.validates :uuid, format: { with: Utils::UUID_PATTERN }, allow_blank: false
+ message_draft.validate :validate_metadata
+ message_draft.validate :validate_form
+ message_draft.validate :validate_objects
+ end
+
+ def self.create_message_reply(message)
MessageDraft.create!(
uuid: SecureRandom.uuid,
thread: message.thread,
@@ -8,17 +19,47 @@ def self.create_from_message(message)
read: true,
delivered_at: Time.now,
metadata: {
- "status": "created",
+ "recipient_uri": message.metadata["sender_uri"],
+ "posp_id": MESSAGE_REPLY_POSP_ID,
+ "posp_version": MESSAGE_REPLY_POSP_VERSION,
+ "message_type": MESSAGE_REPLY_MESSAGE_TYPE,
"correlation_id": message.metadata["correlation_id"],
"reference_id": message.uuid,
"original_message_id": message.id,
- "recipient_uri": message.metadata["sender_uri"],
+ "status": "created"
}
)
end
+ def self.submit(message_draft)
+ Govbox::SubmitMessageDraftJob.perform_later(message_draft)
+
+ message_draft.metadata["status"] = "being_submitted"
+ message_draft.save!
+ end
+
+ def update_content(title:, body:)
+ form = objects.select { |o| o.form? }&.first
+
+ unless form
+ form = MessageObject.create(
+ message_id: id,
+ name: "form.xml",
+ mimetype: "application/x-eform-xml",
+ object_type: "FORM",
+ is_signed: false
+ )
+
+ form.message_object_datum.create
+ end
+
+ form.message_object_datum.update(
+ blob: GeneralAgendaBuilder.build_xml(subject: title, body: body)
+ )
+ end
+
def import
- Drafts::Import.find(metadata["import_id"]) if metadata["import_id"]
+ MessageDraftsImport.find(metadata["import_id"]) if metadata["import_id"]
end
def submittable?
@@ -40,4 +81,32 @@ def submitted?
def original_message
Message.find(metadata["original_message_id"])
end
+
+ private
+
+ def validate_metadata
+ errors.add(:metadata, "No recipient URI") unless metadata["recipient_uri"].present?
+ errors.add(:metadata, "No posp ID") unless metadata["posp_id"].present?
+ errors.add(:metadata, "No posp version") unless metadata["posp_version"].present?
+ errors.add(:metadata, "No message type") unless metadata["message_type"].present?
+ errors.add(:metadata, "No correlation ID") unless metadata["correlation_id"].present?
+ errors.add(:metadata, "Correlation ID must be UUID") unless metadata["correlation_id"]&.match?(Utils::UUID_PATTERN)
+ end
+
+ def validate_form
+ forms = objects.select { |o| o.form? }
+
+ if objects.size == 0
+ errors.add(:objects, "No objects found for draft")
+ elsif forms.count != 1
+ errors.add(:objects, "Draft has to contain exactly one form")
+ end
+ end
+
+ def validate_objects
+ objects.each do |object|
+ object.valid?(:validate_data)
+ errors.merge!(object.errors)
+ end
+ end
end
diff --git a/app/models/drafts/import.rb b/app/models/message_drafts_import.rb
similarity index 85%
rename from app/models/drafts/import.rb
rename to app/models/message_drafts_import.rb
index d4a2a3522..641d975c6 100644
--- a/app/models/drafts/import.rb
+++ b/app/models/message_drafts_import.rb
@@ -1,6 +1,6 @@
# == Schema Information
#
-# Table name: drafts_imports
+# Table name: message_drafts_imports
#
# id :integer not null, primary key
# name :string not null
@@ -9,12 +9,12 @@
# created_at :datetime not null
# updated_at :datetime not null
-class Drafts::Import < ApplicationRecord
+class MessageDraftsImport < ApplicationRecord
belongs_to :box, class_name: 'Box'
after_destroy_commit { MessageDraft.where("metadata ->> 'import_subfolder' = ?", id.to_s).destroy_all }
- validates_with DraftsImportValidator, if: :content_path
+ validates_with MessageDraftsImportValidator, if: :content_path
enum status: { uploaded: 0, parsed: 1, parsing_failed: 2 }
diff --git a/app/models/validators/drafts_import_validator.rb b/app/models/validators/message_drafts_import_validator.rb
similarity index 96%
rename from app/models/validators/drafts_import_validator.rb
rename to app/models/validators/message_drafts_import_validator.rb
index 40efed2b0..50d492a58 100644
--- a/app/models/validators/drafts_import_validator.rb
+++ b/app/models/validators/message_drafts_import_validator.rb
@@ -1,4 +1,4 @@
-class DraftsImportValidator < ActiveModel::Validator
+class MessageDraftsImportValidator < ActiveModel::Validator
def validate(record)
record.errors.add(:drafts, "No drafts found") if Utils.sub_folders(record.content_path).empty?
diff --git a/app/policies/draft_policy.rb b/app/policies/draft_policy.rb
deleted file mode 100644
index 78d8bfe48..000000000
--- a/app/policies/draft_policy.rb
+++ /dev/null
@@ -1,36 +0,0 @@
-# frozen_string_literal: true
-
-class DraftPolicy < ApplicationPolicy
- attr_reader :user, :draft
-
- def initialize(user, draft)
- @user = user
- @draft = draft
- end
-
- class Scope < Scope
- def resolve
- @user.site_admin? ? scope.all : scope.where(box: Current.box)
- end
- end
-
- def index?
- true
- end
-
- def show?
- @user.tenant == @draft.box.tenant && @draft.box == Current.box
- end
-
- def destroy?
- @user.tenant == @draft.box.tenant && @draft.box == Current.box
- end
-
- def submit?
- @user.tenant == @draft.box.tenant && @draft.box == Current.box
- end
-
- def submit_all?
- @user.tenant == @draft.box.tenant && @draft.box == Current.box
- end
-end
diff --git a/app/policies/drafts/import_policy.rb b/app/policies/message_drafts_import_policy.rb
similarity index 80%
rename from app/policies/drafts/import_policy.rb
rename to app/policies/message_drafts_import_policy.rb
index 2d4402048..9a20d638c 100644
--- a/app/policies/drafts/import_policy.rb
+++ b/app/policies/message_drafts_import_policy.rb
@@ -1,6 +1,6 @@
# frozen_string_literal: true
-class Drafts::ImportPolicy < ApplicationPolicy
+class MessageDraftsImportPolicy < ApplicationPolicy
attr_reader :user
def initialize(user, _)
diff --git a/app/views/drafts/index.html.erb b/app/views/drafts/index.html.erb
deleted file mode 100644
index c68422dac..000000000
--- a/app/views/drafts/index.html.erb
+++ /dev/null
@@ -1,33 +0,0 @@
-
Drafty
-
-
- <%= link_to "Vytvoriť nový draft", drafts_path %>
-
- <%= link_to "Nahrať import", upload_new_drafts_imports_path %>
- <% if @drafts.any? %>
-
- <%= button_to "Podať všetky", submit_all_drafts_path, method: :post %>
- <% end %>
-
-
-<% if @drafts.any? %>
-
-
Vytvorené podania
-
-
-
- Podanie |
- Balík |
- Stav |
-
-
- <% @drafts.each do |draft| %>
-
- <%= link_to draft.title, draft_path(draft.id) %> |
- <%= " #{draft.import&.base_name}" %> |
- <%= " #{draft.status}" %> |
-
- <% end %>
-
-
-<% end %>
diff --git a/app/views/drafts/show.html.erb b/app/views/drafts/show.html.erb
deleted file mode 100644
index 8d2418b27..000000000
--- a/app/views/drafts/show.html.erb
+++ /dev/null
@@ -1,31 +0,0 @@
-
Draft <%= @draft.message_subject %>
-
-
Stav: <%= @draft.status %>
-
-
-<% if @draft.import %>
-
Z importu: <%= @draft.import.base_name %>
-
-<% end %>
-
-<%= form_tag draft_path(@draft), method: :delete do %>
- <%= submit_tag 'Zmazať' %>
-<% end %>
-
-
-
-<% if @draft.submittable? %>
- <%= form_tag draft_submit_path(@draft), method: :post do %>
- <%= submit_tag 'Podať' %>
- <% end %>
-<% end %>
-
-<% unless @draft.valid?(:validate_data) %>
-
Errors:
-
- <% @draft.errors.each do |error| %>
- - <%= error.full_message %>
- <% end %>
-
-<% end %>
-
diff --git a/app/views/message_drafts/index.html.erb b/app/views/message_drafts/index.html.erb
index 68ecfee98..599e92066 100644
--- a/app/views/message_drafts/index.html.erb
+++ b/app/views/message_drafts/index.html.erb
@@ -1,12 +1,12 @@
Drafty
- <%= link_to "Nahrať import", upload_new_drafts_imports_path %>
+ <%= link_to "Nahrať import", upload_new_message_drafts_imports_path %>
<% if @message_drafts.any? %>
-
Vytvorené podania
+
Podania
@@ -18,10 +18,15 @@
<% @message_drafts.each do |message_draft| %>
- <%= " #{message_draft.import&.base_name}" %> |
+ <%= " #{message_draft.title}" %> |
<%= " #{message_draft.metadata["status"]}" %> |
<% end %>
+
+
+
+
+
<% end %>
diff --git a/app/views/drafts/imports/upload_new.html.erb b/app/views/message_drafts_imports/upload_new.html.erb
similarity index 69%
rename from app/views/drafts/imports/upload_new.html.erb
rename to app/views/message_drafts_imports/upload_new.html.erb
index 5045ed07f..66a4a03d4 100644
--- a/app/views/drafts/imports/upload_new.html.erb
+++ b/app/views/message_drafts_imports/upload_new.html.erb
@@ -1,6 +1,6 @@
Nový import draftov
-<%= form_tag drafts_imports_path, method: :post, multipart: true do %>
+<%= form_tag message_drafts_imports_path, method: :post, multipart: true do %>
<%= label_tag :content, 'Import' %>
<%= file_field_tag :content, accept: "application/zip" %>
diff --git a/config/routes.rb b/config/routes.rb
index f2b12a5f8..fd0d8ecbe 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -26,7 +26,7 @@
resources :tags do
resources :message_threads do
- end
+ end
end
resources :message_threads do
@@ -45,6 +45,7 @@
resources :message_objects do
member do
get 'download'
+ get 'signing_data'
end
end
end
@@ -59,15 +60,8 @@
resource :settings
- namespace :drafts, path: 'drafty' do
- resources :imports, path: 'importy', only: :create do
- get :upload_new, path: 'novy', on: :collection
- end
- end
-
- resources :drafts, path: 'drafty', only: %i[index show destroy] do
- post :submit
- post :submit_all, on: :collection
+ resources :message_drafts_imports, only: :create do
+ get :upload_new, path: 'novy', on: :collection
end
resources :sessions do
diff --git a/db/migrate/20230809141544_drop_drafts.rb b/db/migrate/20230809141544_drop_drafts.rb
new file mode 100644
index 000000000..19ebcd4ca
--- /dev/null
+++ b/db/migrate/20230809141544_drop_drafts.rb
@@ -0,0 +1,6 @@
+class DropDrafts < ActiveRecord::Migration[7.0]
+ def up
+ drop_table :drafts_objects
+ drop_table :drafts
+ end
+end
diff --git a/db/migrate/20230809145031_rename_drafts_imports_to_message_drafts_imports.rb b/db/migrate/20230809145031_rename_drafts_imports_to_message_drafts_imports.rb
new file mode 100644
index 000000000..8d33f6908
--- /dev/null
+++ b/db/migrate/20230809145031_rename_drafts_imports_to_message_drafts_imports.rb
@@ -0,0 +1,5 @@
+class RenameDraftsImportsToMessageDraftsImports < ActiveRecord::Migration[7.0]
+ def up
+ rename_table :drafts_imports, :message_drafts_imports
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 8b3d9f16f..eb4a629ba 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema[7.0].define(version: 2023_08_04_122837) do
+ActiveRecord::Schema[7.0].define(version: 2023_08_09_145031) do
# These are extensions that must be enabled in order to support this database
enable_extension "pgcrypto"
enable_extension "plpgsql"
@@ -86,48 +86,6 @@
t.index ["tenant_id"], name: "index_boxes_on_tenant_id"
end
- create_table "drafts", force: :cascade do |t|
- t.bigint "import_id"
- t.integer "status", default: 0
- t.string "recipient_uri"
- t.string "posp_id"
- t.string "posp_version"
- t.string "message_type"
- t.string "message_subject"
- t.string "import_subfolder"
- t.string "sender_business_reference"
- t.string "recipient_business_reference"
- t.uuid "message_id"
- t.uuid "correlation_id"
- t.datetime "created_at", null: false
- t.datetime "updated_at", null: false
- t.bigint "box_id", null: false
- t.index ["box_id"], name: "index_drafts_on_box_id"
- t.index ["import_id"], name: "index_drafts_on_import_id"
- end
-
- create_table "drafts_imports", force: :cascade do |t|
- t.string "name", null: false
- t.integer "status", default: 0
- t.string "content_path"
- t.datetime "created_at", null: false
- t.datetime "updated_at", null: false
- t.bigint "box_id", null: false
- t.index ["box_id"], name: "index_drafts_imports_on_box_id"
- end
-
- create_table "drafts_objects", force: :cascade do |t|
- t.bigint "draft_id", null: false
- t.uuid "uuid", null: false
- t.string "name", null: false
- t.boolean "signed"
- t.boolean "to_be_signed"
- t.boolean "form"
- t.datetime "created_at", null: false
- t.datetime "updated_at", null: false
- t.index ["draft_id"], name: "index_drafts_objects_on_draft_id"
- end
-
create_table "folders", force: :cascade do |t|
t.bigint "box_id", null: false
t.string "name", null: false
@@ -266,6 +224,16 @@
t.index ["tenant_id"], name: "index_groups_on_tenant_id"
end
+ create_table "message_drafts_imports", force: :cascade do |t|
+ t.string "name", null: false
+ t.integer "status", default: 0
+ t.string "content_path"
+ t.datetime "created_at", null: false
+ t.datetime "updated_at", null: false
+ t.bigint "box_id", null: false
+ t.index ["box_id"], name: "index_message_drafts_imports_on_box_id"
+ end
+
create_table "message_object_data", force: :cascade do |t|
t.bigint "message_object_id", null: false
t.binary "blob", null: false
@@ -329,8 +297,8 @@
t.text "html_visualization"
t.boolean "read", default: false, null: false
t.json "metadata"
- t.boolean "replyable", default: true, null: false
t.string "type"
+ t.boolean "replyable", default: true, null: false
t.index ["message_thread_id"], name: "index_messages_on_message_thread_id"
end
@@ -385,10 +353,6 @@
add_foreign_key "automation_rules", "tenants"
add_foreign_key "automation_rules", "users"
add_foreign_key "boxes", "tenants"
- add_foreign_key "drafts", "boxes"
- add_foreign_key "drafts", "drafts_imports", column: "import_id"
- add_foreign_key "drafts_imports", "boxes"
- add_foreign_key "drafts_objects", "drafts"
add_foreign_key "folders", "boxes"
add_foreign_key "govbox_api_connections", "boxes"
add_foreign_key "govbox_folders", "boxes"
@@ -397,6 +361,7 @@
add_foreign_key "group_memberships", "groups"
add_foreign_key "group_memberships", "users"
add_foreign_key "groups", "tenants"
+ add_foreign_key "message_drafts_imports", "boxes"
add_foreign_key "message_object_data", "message_objects"
add_foreign_key "message_objects", "messages"
add_foreign_key "message_thread_merge_identifiers", "message_threads"
From be91d6bdfe53dad18e3afc609e760ba8f3002733 Mon Sep 17 00:00:00 2001
From: luciajanikova <19lucia99@gmail.com>
Date: Wed, 23 Aug 2023 20:59:58 +0200
Subject: [PATCH 18/34] Make one job for submitting messages
---
app/controllers/message_drafts_controller.rb | 4 +-
app/jobs/drafts/submit_job.rb | 64 -----------------
app/jobs/govbox/submit_message_draft_job.rb | 72 ++++++++++++-------
...daBuilder.rb => general_agenda_builder.rb} | 2 +-
app/models/message_draft.rb | 45 +++++++-----
app/models/message_object.rb | 2 +-
app/views/message_drafts/index.html.erb | 8 +--
7 files changed, 78 insertions(+), 119 deletions(-)
delete mode 100644 app/jobs/drafts/submit_job.rb
rename app/lib/upvs/{GeneralAgendaBuilder.rb => general_agenda_builder.rb} (90%)
diff --git a/app/controllers/message_drafts_controller.rb b/app/controllers/message_drafts_controller.rb
index 2187f1fd3..7633442fe 100644
--- a/app/controllers/message_drafts_controller.rb
+++ b/app/controllers/message_drafts_controller.rb
@@ -32,7 +32,7 @@ def submit
authorize @message_draft
if @message_draft.submittable?
- MessageDraft.submit(@message_draft)
+ @message_draft.submit
redirect_to message_path(@message_draft.original_message), notice: "Správa bola zaradená na odoslanie."
else
# TODO prisposobit importovanym draftom
@@ -42,7 +42,7 @@ def submit
def submit_all
@message_drafts.each do |message_draft|
- MessageDraft.submit(@message_draft) if message_draft.submittable?
+ message_draft.submit if message_draft.submittable?
end
end
diff --git a/app/jobs/drafts/submit_job.rb b/app/jobs/drafts/submit_job.rb
deleted file mode 100644
index c98d579c0..000000000
--- a/app/jobs/drafts/submit_job.rb
+++ /dev/null
@@ -1,64 +0,0 @@
-class Drafts::SubmitJob < ApplicationJob
- def perform(message_draft, upvs_client: UpvsEnvironment.upvs_client)
- message_draft_data = {
- posp_id: message_draft.metadata["posp_id"],
- posp_version: message_draft.metadata["posp_version"],
- message_type: message_draft.metadata["message_type"],
- message_id: message_draft.uuid,
- correlation_id: message_draft.metadata["correlation_id"],
- recipient_uri: message_draft.metadata["recipient_uri"],
- message_subject: message_draft.title,
- sender_business_reference: message_draft.metadata["sender_business_reference"],
- recipient_business_reference: message_draft.metadata["recipient_business_reference"],
- objects: build_objects(message_draft)
- }.compact
-
- sktalk_api = upvs_client.api(message_draft.thread.folder.box).sktalk
-
- begin
- success, response_status = sktalk_api.receive_and_save_to_outbox(message_draft_data)
- if success
- message_draft.metadata["status"] = "submitted"
- else
- handle_submit_fail(message_draft, response_status)
- end
-
- message_draft.save!
- rescue Error => error
- message_draft.metadata["status"] = "submit_failed_temporary"
- message_draft.save!
-
- raise error
- end
- end
-
- private
-
- def build_objects(message_draft)
- objects = []
- message_draft.objects.each do |object|
- objects << {
- id: SecureRandom.uuid,
- name: object.name,
- encoding: "Base64",
- signed: object.is_signed,
- mime_type: object.mimetype,
- form: object.form?,
- content: Base64.strict_encode64(object.message_object_datum.blob)
- }.compact
- end
-
- objects
- end
-
- def handle_submit_fail(message_draft, response_status)
- case response_status
- when 408
- # TODO
- when 422
- message_draft.metadata["status"] = "submit_failed_unprocessable"
- else
- message_draft.metadata["status"] = "submit_failed_temporary"
- end
- end
-end
diff --git a/app/jobs/govbox/submit_message_draft_job.rb b/app/jobs/govbox/submit_message_draft_job.rb
index ae71bb5a5..59b388aaa 100644
--- a/app/jobs/govbox/submit_message_draft_job.rb
+++ b/app/jobs/govbox/submit_message_draft_job.rb
@@ -1,47 +1,65 @@
class Govbox::SubmitMessageDraftJob < ApplicationJob
- class << self
- delegate :uuid, to: SecureRandom
- end
-
def perform(message_draft, upvs_client: UpvsEnvironment.upvs_client)
- reply_data = {
+ message_draft_data = {
+ posp_id: message_draft.metadata["posp_id"],
+ posp_version: message_draft.metadata["posp_version"],
+ message_type: message_draft.metadata["message_type"],
message_id: message_draft.uuid,
correlation_id: message_draft.metadata["correlation_id"],
- reference_id: message_draft.metadata["reference_id"],
recipient_uri: message_draft.metadata["recipient_uri"],
- general_agenda: {
- subject: message_draft.title,
- body: message_draft.metadata["message_body"]
- },
- attachments: (message_attachments_data(message_draft) if message_draft.objects.any?)
+ message_subject: message_draft.title,
+ sender_business_reference: message_draft.metadata["sender_business_reference"],
+ recipient_business_reference: message_draft.metadata["recipient_business_reference"],
+ objects: build_objects(message_draft)
}.compact
sktalk_api = upvs_client.api(message_draft.thread.folder.box).sktalk
- success, response_status = sktalk_api.receive_and_save_to_outbox(reply_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
- raise StandardError, "Message reply submission failed!" unless success
+ message_draft.save!
+ rescue Error => error
+ message_draft.metadata["status"] = "submit_failed_temporary"
+ message_draft.save!
- message_draft.metadata["status"] = "submitted"
- message_draft.save!
-
- Govbox::SyncBoxJob.set(wait: 3.minutes).perform_later(message_draft.thread.folder.box)
+ raise error
+ end
end
private
- def message_attachments_data(message_draft)
- message_draft.objects.map do |message_attachment|
- {
- id: uuid,
- name: message_attachment.name,
- signed: message_attachment.is_signed,
+ def build_objects(message_draft)
+ objects = []
+ message_draft.objects.each do |object|
+ objects << {
+ id: SecureRandom.uuid,
+ name: object.name,
encoding: "Base64",
- mime_type: Utils.detect_mime_type(entry_name: message_attachment.name),
- content: Base64.strict_encode64(message_attachment.message_object_datum.blob)
- }
+ signed: object.is_signed,
+ mime_type: object.mimetype,
+ form: object.form?,
+ content: Base64.strict_encode64(object.message_object_datum.blob)
+ }.compact
end
+
+ objects
end
- delegate :uuid, to: self
+ def handle_submit_fail(message_draft, response_status)
+ case response_status
+ when 408
+ # TODO
+ when 422
+ message_draft.metadata["status"] = "submit_failed_unprocessable"
+ else
+ message_draft.metadata["status"] = "submit_failed_temporary"
+ end
+ end
end
diff --git a/app/lib/upvs/GeneralAgendaBuilder.rb b/app/lib/upvs/general_agenda_builder.rb
similarity index 90%
rename from app/lib/upvs/GeneralAgendaBuilder.rb
rename to app/lib/upvs/general_agenda_builder.rb
index 8e5877c5d..db69ece58 100644
--- a/app/lib/upvs/GeneralAgendaBuilder.rb
+++ b/app/lib/upvs/general_agenda_builder.rb
@@ -1,4 +1,4 @@
-class GeneralAgendaBuilder
+class Upvs::GeneralAgendaBuilder
GENERAL_AGENDA_SCHEMA ||= 'http://schemas.gov.sk/form/App.GeneralAgenda/1.9'
def self.build_xml(subject:, body:)
diff --git a/app/models/message_draft.rb b/app/models/message_draft.rb
index 6e83f1450..7ea474241 100644
--- a/app/models/message_draft.rb
+++ b/app/models/message_draft.rb
@@ -1,7 +1,7 @@
class MessageDraft < Message
- MESSAGE_REPLY_POSP_ID ||= "App.GeneralAgenda"
- MESSAGE_REPLY_POSP_VERSION ||= "1.9"
- MESSAGE_REPLY_MESSAGE_TYPE ||= "App.GeneralAgenda"
+ GENERAL_AGENDA_POSP_ID ||= "App.GeneralAgenda"
+ GENERAL_AGENDA_POSP_VERSION ||= "1.9"
+ GENERAL_AGENDA_MESSAGE_TYPE ||= "App.GeneralAgenda"
with_options on: :validate_data do |message_draft|
message_draft.validates :uuid, format: { with: Utils::UUID_PATTERN }, allow_blank: false
@@ -20,9 +20,9 @@ def self.create_message_reply(message)
delivered_at: Time.now,
metadata: {
"recipient_uri": message.metadata["sender_uri"],
- "posp_id": MESSAGE_REPLY_POSP_ID,
- "posp_version": MESSAGE_REPLY_POSP_VERSION,
- "message_type": MESSAGE_REPLY_MESSAGE_TYPE,
+ "posp_id": GENERAL_AGENDA_POSP_ID,
+ "posp_version": GENERAL_AGENDA_POSP_VERSION,
+ "message_type": GENERAL_AGENDA_MESSAGE_TYPE,
"correlation_id": message.metadata["correlation_id"],
"reference_id": message.uuid,
"original_message_id": message.id,
@@ -31,17 +31,23 @@ def self.create_message_reply(message)
)
end
- def self.submit(message_draft)
- Govbox::SubmitMessageDraftJob.perform_later(message_draft)
+ def submit
+ Govbox::SubmitMessageDraftJob.perform_later(self)
- message_draft.metadata["status"] = "being_submitted"
- message_draft.save!
+ metadata["status"] = "being_submitted"
+ save!
end
def update_content(title:, body:)
- form = objects.select { |o| o.form? }&.first
+ self.title = title
+ metadata["message_body"] = body
+ save!
- unless form
+ if form
+ form.message_object_datum.update(
+ blob: Upvs::GeneralAgendaBuilder.build_xml(subject: title, body: body)
+ )
+ else
form = MessageObject.create(
message_id: id,
name: "form.xml",
@@ -50,18 +56,21 @@ def update_content(title:, body:)
is_signed: false
)
- form.message_object_datum.create
+ form.message_object_datum = MessageObjectDatum.create(
+ message_object: form,
+ blob: Upvs::GeneralAgendaBuilder.build_xml(subject: title, body: body)
+ )
end
-
- form.message_object_datum.update(
- blob: GeneralAgendaBuilder.build_xml(subject: title, body: body)
- )
+ end
+
+ def form
+ objects.select { |o| o.form? }&.first
end
def import
MessageDraftsImport.find(metadata["import_id"]) if metadata["import_id"]
end
-
+
def submittable?
title.present? && metadata["message_body"].present? && not_yet_submitted?
end
diff --git a/app/models/message_object.rb b/app/models/message_object.rb
index 1383f773f..8275fb7f7 100644
--- a/app/models/message_object.rb
+++ b/app/models/message_object.rb
@@ -38,6 +38,6 @@ def form?
end
def destroyable?
- message.is_a?(MessageDraft)
+ message.is_a?(MessageDraft) && !form?
end
end
diff --git a/app/views/message_drafts/index.html.erb b/app/views/message_drafts/index.html.erb
index 599e92066..780c702fd 100644
--- a/app/views/message_drafts/index.html.erb
+++ b/app/views/message_drafts/index.html.erb
@@ -18,15 +18,11 @@
<% @message_drafts.each do |message_draft| %>
- <%= " #{message_draft.title}" %> |
+ <%= message_draft.title %> |
+ <%= " #{message_draft&.import&.base_name || '-'}" %> |
<%= " #{message_draft.metadata["status"]}" %> |
<% end %>
-
-
-
-
-
<% end %>
From 5a957f3b5e7186d03d54a6106ba8d65f05cd5af5 Mon Sep 17 00:00:00 2001
From: luciajanikova <19lucia99@gmail.com>
Date: Wed, 23 Aug 2023 22:07:33 +0200
Subject: [PATCH 19/34] Add editable?, custom_visualization? methods to
MessageDraft
---
.../message_drafts_component.html.erb | 20 +++++++++++--------
app/jobs/drafts/parse_import_job.rb | 6 ++++++
app/models/message_draft.rb | 10 +++++++++-
3 files changed, 27 insertions(+), 9 deletions(-)
diff --git a/app/components/message_drafts_component.html.erb b/app/components/message_drafts_component.html.erb
index f2f426bac..9d43858d5 100644
--- a/app/components/message_drafts_component.html.erb
+++ b/app/components/message_drafts_component.html.erb
@@ -31,14 +31,18 @@