Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CTSKF-832_vinceTest1004 #7616

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions app/controllers/message_documents_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class MessageDocumentsController < ApplicationController

def create
binding.pry
@document = MessageDocument.new(document_params.merge(creator_id: current_user.id))

if @document.save_and_verify
render json: { document: { id: @document.id, filename: @document.document.filename } }, status: :created
else
render json: { error: @document.errors[:document].join(', ') }, status: :unprocessable_entity
end
end

def destroy
if document.destroy
respond_to do |format|
format.json { render json: { message: 'Document removed', document: } }
format.js
end
else
respond_to do |format|
format.json { render json: { message: document.errors.full_messages.join(', '), document: } }
format.js
end
end
end

end
6 changes: 3 additions & 3 deletions app/controllers/messages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ def create
end

def download_attachment
raise 'No attachment present on this message' unless message.attachment.attached?
raise 'No attachment present on this message' unless message.attachments.attached?

redirect_to message.attachment.blob.url(disposition: 'attachment'), allow_other_host: true
redirect_to message.attachments.first.blob.url(disposition: 'attachment'), allow_other_host: true
end

private
Expand All @@ -59,7 +59,7 @@ def message_params
params.require(:message).permit(
:sender_id,
:claim_id,
:attachment,
:attachments,
:body,
:claim_action,
:written_reasons_submitted
Expand Down
10 changes: 5 additions & 5 deletions app/interfaces/api/entities/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ class Document < BaseEntity

private

def attachment
object.is_a?(ActiveStorage::Attachment) ? object : object.attachment
def attachments
object.is_a?(ActiveStorage::Attachment) ? object : object.attachments
end

def url
attachment.blob.url(disposition: 'attachment') if attachment.attached?
attachments.first.blob.url(disposition: 'attachment') if attachments.attached?
end

def file_name
attachment.filename if attachment.attached?
attachments.first.filename if attachments.attached?
end

def size
attachment.byte_size if attachment.attached?
attachments.first.byte_size if attachments.attached?
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions app/interfaces/api/entities/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ class Message < BaseEntity
expose :created_at, format_with: :utc
expose :sender_uuid
expose :body
expose :attachment,
expose :attachments,
as: :document,
using: API::Entities::Document,
if: ->(instance, _opts) { instance.attachment.present? }
if: ->(instance, _opts) { instance.attachments.present? }

private

Expand Down
7 changes: 4 additions & 3 deletions app/models/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ class Message < ApplicationRecord

attr_accessor :claim_action, :written_reasons_submitted

has_one_attached :attachment
# has_one_attached :attachment
has_many_attached :attachments

validates :attachment,
validates :attachments,
size: { less_than: 20.megabytes },
content_type: %w[
application/pdf
Expand Down Expand Up @@ -51,7 +52,7 @@ class Message < ApplicationRecord
scope :most_recent_last, -> { includes(:user_message_statuses).order(created_at: :asc) }

after_create :generate_statuses, :process_claim_action, :process_written_reasons, :send_email_if_required
before_destroy -> { attachment.purge }
before_destroy -> { attachments.purge }

class << self
def for(object)
Expand Down
27 changes: 27 additions & 0 deletions app/models/message_document.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class MessageDocument < ApplicationRecord
belongs_to :message, class_name: 'Message'

has_one_attached :message_document
# has_many_attached :attachments

validates :message_document,
size: { less_than: 20.megabytes },
content_type: %w[
application/pdf
application/msword
application/vnd.openxmlformats-officedocument.wordprocessingml.document
application/vnd.oasis.opendocument.text
application/vnd.ms-excel
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
application/vnd.oasis.opendocument.spreadsheet
text/rtf
application/rtf
image/jpeg
image/png
image/tiff
image/x-bmp
image/x-bitmap
]


end
6 changes: 3 additions & 3 deletions app/presenters/message_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def sender_is_a?(klass)
def body
h.tag.div do
h.concat(simple_format(message.body))
attachment_field if message.attachment.present?
attachment_field if message.attachments.present?
end
end

Expand Down Expand Up @@ -39,11 +39,11 @@ def download_file_link
end

def attachment_file_name
message.attachment.filename.to_s
message.attachments.first.filename.to_s
end

def attachment_file_size
h.number_to_human_size(message.attachment.byte_size)
h.number_to_human_size(message.attachments.first.byte_size)
end

def hide_author?
Expand Down
31 changes: 24 additions & 7 deletions app/views/shared/_message_controls.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,29 @@
link_errors: true,
label: { text: t('.written_reasons') }

= f.govuk_file_field :attachment,
label: { text: t('.attachment_label') },
hint: { text: t('.accepted_files_help_text') }
.moj-multi-file-upload
.moj-multi-file__uploaded-files
%h2.govuk-heading-m Files added
.govuk-summary-list.moj-multi-file-upload__list
.moj-multi-file-upload__upload

.file-to-be-uploaded.govuk-form-group
%span.filename
= govuk_link_to t('.remove_file_html'), '#'
.govuk-form-group
%label.govuk-label.govuk-label--m{for: "attachments"}
Upload a file
%input#attachments.govuk-file-upload.moj-multi-file-upload__input{multiple: "multiple", name: "attachments", type: "file"}/

= f.govuk_submit t('.send'), class: 'govuk-button--secondary'
%button.govuk-button.govuk-button--secondary.moj-multi-file-upload__button{"data-module" => "govuk-button", type: "submit"}
Upload file

%button.govuk-button{"data-module" => "govuk-button", type: "submit"}
Continue

-# = f.govuk_file_field :attachments,
-# label: { text: t('.attachment_label') },
-# hint: { text: t('.accepted_files_help_text') }

-# .file-to-be-uploaded.govuk-form-group
-# %span.filename
-# = govuk_link_to t('.remove_file_html'), '#'

-# = f.govuk_submit t('.send'), class: 'govuk-button--secondary'
6 changes: 3 additions & 3 deletions app/webpack/javascripts/modules/Modules.Messaging.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ moj.Modules.Messaging = {
adpMsg.clearUserMessageBody()
$('.no-messages').hide()
$('.file-to-be-uploaded').hide()
$('#message-attachment-field').val('')
$('#message-attachments-field').val('')
this.messagesList.html(rorData.sentMessage).scrollTop(this.messagesList.prop('scrollHeight'))
// If there was an error
} else {
Expand Down Expand Up @@ -65,7 +65,7 @@ moj.Modules.Messaging = {
selectedFileUpload: function () {
const self = this

self.messageControls.on('change', '#message-attachment-field', function () {
self.messageControls.on('change', '#message-attachments-field', function () {
const $element = $(this)
const filename = $element.val().replace(/C:\\fakepath\\/i, '')
const $controls = self.messageControls
Expand All @@ -84,7 +84,7 @@ moj.Modules.Messaging = {

event.preventDefault()
$element.closest('.file-to-be-uploaded').hide()
$('#message-attachment-field').val('')
$('#message-attachments-field').val('')
})
},

Expand Down
9 changes: 9 additions & 0 deletions app/webpack/javascripts/modules/Modules.MultiFileUpload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
moj.Modules.MultiFileUpload = {
init: function () {
new MOJFrontend.MultiFileUpload({
container: document.querySelector('.moj-multi-file-upload'),
uploadUrl: '/message_documents',
deleteUrl: '/message_documents'
});
}
}
1 change: 1 addition & 0 deletions app/webpack/packs/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ import '../javascripts/modules/Helpers.Autocomplete.js'
import '../javascripts/modules/Modules.TableRowClick.js'
import '../javascripts/modules/Modules.AllocationScheme.js'
import '../javascripts/modules/Modules.AddEditAdvocate.js'
import '../javascripts/modules/Modules.MultiFileUpload.js'

import '../javascripts/plugins/jquery.numbered.elements.js'

Expand Down
2 changes: 2 additions & 0 deletions app/webpack/stylesheets/application.scss
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ $govuk-fonts-path: "~govuk-frontend/dist/govuk/assets/fonts/";
@import "~govuk-frontend/dist/govuk/base";
@import "~govuk-frontend/dist/govuk/core/typography";

@import "@ministryofjustice/frontend/moj/components/multi-file-upload/_multi-file-upload.scss";

// Project specific
// Everything below this comment should be project specific.
// If you absolutely need to override gov.uk styles, don't,
Expand Down
1 change: 1 addition & 0 deletions config/initializers/assets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@
Rails.application.config.assets.paths << Rails.root.join('node_modules', 'govuk-frontend', 'dist', 'govuk', 'assets')
Rails.application.config.assets.paths << Rails.root.join('node_modules', 'govuk-frontend', 'dist', 'govuk', 'assets', 'images')
Rails.application.config.assets.paths << Rails.root.join('app', 'webpack', 'packs')
Rails.application.config.assets.paths << Rails.root.join('node_modules', '@ministryofjustice', 'frontend', 'moj', 'assets')
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@
get 'download_attachment', on: :member
end

resources :message_documents, only: [:create, :destroy]

resources :establishments, only: %i[index], format: :js
resources :offences, only: [:index], format: :js
resources :case_types, only: [:show], format: :js
Expand Down
3 changes: 2 additions & 1 deletion config/webpack/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ module.exports = {
jQuery: require.resolve('jquery'),
jquery: require.resolve('jquery'),
Dropzone: require.resolve('dropzone/dist/dropzone.js'),
Stickyfill: require.resolve('stickyfilljs')
Stickyfill: require.resolve('stickyfilljs'),
MOJFrontend: require.resolve('@ministryofjustice/frontend/moj/all.js')
})
]
}
19 changes: 19 additions & 0 deletions db/migrate/20241203142017_create_message_documents.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class CreateMessageDocuments < ActiveRecord::Migration[7.0]
def change
create_table :message_documents do |t|
t.integer :message_id
t.integer :external_user_id
t.string :uuid
t.string :form_id
t.integer :creator_id
t.integer :verified_file_size
t.string :file_path
t.boolean :verified

t.timestamps
end
add_index :message_documents, :message_id
add_index :message_documents, :external_user_id
add_index :message_documents, :creator_id
end
end
Loading