Skip to content

Commit

Permalink
Merge branch 'GO-1/drafts_fixes' of https://github.com/solver-it-sro/…
Browse files Browse the repository at this point in the history
…govbox-pro into GO-211/update_sync_rules
  • Loading branch information
luciajanikova committed Sep 27, 2023
2 parents c753028 + 9446023 commit f5db640
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 43 deletions.
2 changes: 1 addition & 1 deletion app/components/message_drafts_component.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="flex flex-col justify-stretch items-stretch">
<%= render MessageThreadHeaderComponent.new(@message.thread) %>
<%= render MessageThreadHeaderComponent.new(@message.thread, @available_tags) %>
<div class="flex flex-col justify-stretch items-stretch gap-2 p-4">
<% if @notice %>
<div class="bg-blue-100 border-t border-b border-blue-500 text-blue-700 px-4 py-3 w-full" role="alert">
Expand Down
27 changes: 18 additions & 9 deletions app/controllers/message_drafts_imports_controller.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
class MessageDraftsImportsController < ApplicationController
before_action :load_box, only: :create

def create
authorize MessageDraftsImport

file_storage = FileStorage.new

zip_content = params[:content]
import = MessageDraftsImport.create!(
name: "#{Time.now.to_i}_#{zip_content.original_filename}",
box: Current.box
import_name = "#{Time.now.to_i}_#{zip_content.original_filename}"
import_path = FileStorage.new.store("imports", import_path(import_name), zip_content.read.force_encoding("UTF-8"))

import = @box.message_drafts_imports.create!(
name: import_name,
content_path: import_path,
box: @box
)

import_path = file_storage.store("imports", import_path(import), zip_content.read.force_encoding("UTF-8"))
Drafts::ParseImportJob.perform_later(import, import_path)
Drafts::ParseImportJob.perform_later(import, author: Current.user)

redirect_to message_drafts_path
end
Expand All @@ -24,7 +27,13 @@ def upload_new

private

def import_path(import)
File.join(String(Current.box.id), import.name)
def import_path(import_name)
File.join(String(@box.id), import_name)
end

def load_box
return unless params[:box_id].present?

@box = Current.tenant.boxes.find(params[:box_id])
end
end
8 changes: 5 additions & 3 deletions app/jobs/drafts/finish_import_job.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
class Drafts::FinishImportJob < ApplicationJob
def perform(batch, params)
batch.properties[:import].message_drafts.find_each do |message_draft|
import = batch.properties[:import]

import.message_drafts.find_each do |message_draft|
if message_draft.valid?(:validate_data)
message_draft.metadata["status"] = "created"
else
Expand All @@ -10,7 +12,7 @@ def perform(batch, params)
message_draft.save
end

Utils.delete_file(batch.properties[:zip_path])
Utils.delete_file(batch.properties[:extracted_data_path])
Utils.delete_file(import.content_path)
import.update(content_path: nil)
end
end
22 changes: 12 additions & 10 deletions app/jobs/drafts/load_content_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,20 @@ class << self
end

def perform(message_draft, message_draft_path)
Dir.each_child(message_draft_path) do |subdirectory_name|
case subdirectory_name
when "podpisane"
load_message_draft_objects(message_draft, File.join(message_draft_path, subdirectory_name), signed: true, to_be_signed: false)
when "podpisat"
load_message_draft_objects(message_draft, File.join(message_draft_path, subdirectory_name), signed: false, to_be_signed: true)
when "nepodpisovat"
load_message_draft_objects(message_draft, File.join(message_draft_path, subdirectory_name), signed: false, to_be_signed: false)
ActiveRecord::Base.transaction do
Dir.each_child(message_draft_path) do |subdirectory_name|
case subdirectory_name
when "podpisane"
load_message_draft_objects(message_draft, File.join(message_draft_path, subdirectory_name), signed: true, to_be_signed: false)
when "podpisat"
load_message_draft_objects(message_draft, File.join(message_draft_path, subdirectory_name), signed: false, to_be_signed: true)
when "nepodpisovat"
load_message_draft_objects(message_draft, File.join(message_draft_path, subdirectory_name), signed: false, to_be_signed: false)
end
end
end

save_form_visualisation(message_draft)
save_form_visualisation(message_draft)
end
end

private
Expand Down
31 changes: 22 additions & 9 deletions app/jobs/drafts/parse_import_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,15 @@ class << self
delegate :uuid, to: SecureRandom
end

def perform(import, import_zip_path, jobs_batch: GoodJob::Batch.new, load_content_job: Drafts::LoadContentJob, on_success_job: Drafts::FinishImportJob)
extracted_import_path = File.join(Utils.file_directory(import_zip_path), File.basename(import_zip_path, ".*"))
system("unzip", import_zip_path, '-d', extracted_import_path)

import.update(content_path: extracted_import_path)
def perform(import, author: , jobs_batch: GoodJob::Batch.new, load_content_job: Drafts::LoadContentJob, on_success_job: Drafts::FinishImportJob)
extracted_import_path = unzip_import(import)

raise "Invalid import" unless import.valid?

csv_paths = Dir[extracted_import_path + "/*.csv"]

ActiveRecord::Base.transaction do
load_import_csv(import, csv_paths.first)
load_import_csv(import, csv_paths.first, author: author)

Dir.each_child(extracted_import_path) do |entry_name|
if File.directory?(File.join(extracted_import_path, entry_name))
Expand All @@ -32,6 +29,7 @@ def perform(import, import_zip_path, jobs_batch: GoodJob::Batch.new, load_conten
read: true,
delivered_at: Time.now,
import: import,
author: author,
metadata: {
"import_subfolder": File.basename(entry_name),
"status": "being_loaded"
Expand All @@ -45,18 +43,32 @@ def perform(import, import_zip_path, jobs_batch: GoodJob::Batch.new, load_conten
end
end

jobs_batch.enqueue(on_success: on_success_job, import: import, zip_path: import_zip_path, extracted_data_path: extracted_import_path)
jobs_batch.enqueue(on_success: on_success_job, import: import)

import.parsed!
end
rescue
# TODO Send notification
Utils.delete_file(import.content_path)
import.destroy!
end

private

def load_import_csv(import, csv_path)
def unzip_import(import)
import_zip_path = import.content_path
extracted_import_path = File.join(Utils.file_directory(import_zip_path), File.basename(import_zip_path, ".*"))

system("unzip", import_zip_path, '-d', extracted_import_path)
Utils.delete_file(import_zip_path)

import.update(content_path: extracted_import_path)
import.unzipped!

extracted_import_path
end

def load_import_csv(import, csv_path, author:)
csv_options = {
encoding: 'UTF-8',
col_sep: File.open(csv_path) { |f| f.readline }.include?(';') ? ';' : ',',
Expand All @@ -77,7 +89,7 @@ def load_import_csv(import, csv_path)
last_message_delivered_at: Time.now
)

message_thread.tags << Tag.find_by!(name: "Drafts", tenant: import.box.tenant)
message_thread.tags << Tag.find_or_create_by!(name: "Drafts", tenant: import.box.tenant)

MessageDraft.create!(
uuid: uuid,
Expand All @@ -87,6 +99,7 @@ def load_import_csv(import, csv_path)
read: true,
delivered_at: Time.now,
import: import,
author: author,
metadata: {
"recipient_uri": row['recipient_uri'],
"posp_id": row['posp_id'],
Expand Down
6 changes: 3 additions & 3 deletions app/models/message_drafts_import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
# id :integer not null, primary key
# name :string not null
# box_id :integer not null
# content_path :string not null
# content_path :string
# created_at :datetime not null
# updated_at :datetime not null

class MessageDraftsImport < ApplicationRecord
belongs_to :box, class_name: 'Box'
has_many :message_drafts, foreign_key: :import_id, dependent: :destroy

validates_with MessageDraftsImportValidator, if: :content_path
validates_with MessageDraftsImportValidator, if: :unzipped?

enum status: { uploaded: 0, parsed: 1, parsing_failed: 2 }
enum status: { uploaded: 0, unzipped: 1, parsed: 2 }

def base_name
name.split('_', 2).last
Expand Down
16 changes: 8 additions & 8 deletions app/models/validators/message_drafts_import_validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ class MessageDraftsImportValidator < ActiveModel::Validator
def validate(record)
record.errors.add(:drafts, "No drafts found") if Utils.sub_folders(record.content_path).empty?

Dir.each_child(record.content_path) do |package_entry_name|
package_entry_path = File.join(record.content_path, package_entry_name)
Dir.each_child(record.content_path) do |import_entry_name|
import_entry_path = File.join(record.content_path, import_entry_name)

if File.directory?(package_entry_path)
if File.directory?(import_entry_path)
# check each draft directory structure
Dir.each_child(package_entry_path) do |draft_subfolder_name|
draft_subfolder_path = File.join(package_entry_path, draft_subfolder_name)
Dir.each_child(import_entry_path) do |draft_subfolder_name|
draft_subfolder_path = File.join(import_entry_path, draft_subfolder_name)

if File.directory?(draft_subfolder_path)
case(draft_subfolder_name)
Expand All @@ -21,14 +21,14 @@ def validate(record)
record.errors.add(:drafts, "Unknown signature status, files must be inside a folder.")
end
end
elsif Utils.csv?(package_entry_name)
elsif Utils.csv?(import_entry_name)
# noop
else
record.errors.add(:drafts, "Package contains extra file")
record.errors.add(:drafts, "Import contains extra file")
end

csv_paths = Dir[record.content_path + "/*.csv"]
record.errors.add(:drafts, "Package must contain 1 CSV file") if csv_paths.size != 1
record.errors.add(:drafts, "Import must contain 1 CSV file") if csv_paths.size != 1
end
end

Expand Down
1 change: 1 addition & 0 deletions app/views/message_drafts_imports/upload_new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<%= @box.name %>
</div>
<%= form_tag message_drafts_imports_path, method: :post, multipart: true do %>
<%= text_field_tag :box_id, @box.id, hidden: true %>
<%= label_tag :content, 'Import' %>
<%= file_field_tag :content, accept: "application/zip" %>
<br/>
Expand Down

0 comments on commit f5db640

Please sign in to comment.