Skip to content

Commit

Permalink
Merge pull request #539 from richardlences/feature/dont_allow_to_dele…
Browse files Browse the repository at this point in the history
…te_message_while_being_sent

don't allow to delete message while being sent
  • Loading branch information
luciajanikova authored Feb 18, 2025
2 parents baeee7b + 2c523d4 commit fffb1e0
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
9 changes: 6 additions & 3 deletions app/controllers/message_drafts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@ def destroy
# redirect_path = @message.original_message.present? ? message_thread_path(@message.original_message.thread) : message_drafts_path
redirect_path = @message.original_message.present? ? message_thread_path(@message.original_message.thread) : message_threads_path

@message.destroy

redirect_to redirect_path, notice: "Správa bola zahodená"
if @message.not_yet_submitted?
@message.destroy
redirect_to redirect_path, notice: "Správa bola zmazaná"
else
redirect_to redirect_path, alert: "Správu nie je možné zmazať po zaradení na odoslanie"
end
end

def unlock
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def destroy
message_drafts_to_destroy_ids = message_threads.map(&:message_drafts).flatten.map(&:id)

message_threads.transaction do
MessageDraft.where(id: message_drafts_to_destroy_ids).destroy_all
MessageDraft.not_in_submission_process.where(id: message_drafts_to_destroy_ids).destroy_all
end

if message_drafts_to_destroy_ids.present?
Expand Down
25 changes: 25 additions & 0 deletions test/controllers/message_drafts_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'test_helper'

class MessageDraftsControllerTest < ActionController::TestCase
setup do
session[:login_expires_at] = Time.now + 1.day
Current.user = users(:basic)
session[:user_id] = Current.user.id
end
test "should destroy draft if it was not yet submitted" do
message_draft = messages(:ssd_main_draft)
delete :destroy, params: { id: message_draft.id }
assert_raises(ActiveRecord::RecordNotFound) do
MessageDraft.find(message_draft.id)
end
end

test "should not destroy draft that is being submitted" do
message_draft = messages(:ssd_main_draft)
message_draft.metadata[:status] = "being_submitted"
message_draft.save!
delete :destroy, params: { id: message_draft.id }
assert_equal "Správu nie je možné zmazať po zaradení na odoslanie", flash[:alert]
assert MessageDraft.exists?(message_draft.id)
end
end

0 comments on commit fffb1e0

Please sign in to comment.