Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

use advisory lock in AddWorkPackageNoteService

Ivan Kuchin7d892335b617
diff --git a/app/services/add_work_package_note_service.rb b/app/services/add_work_package_note_service.rb
index d0bf6ff79b6..69bb8054f41 100644
--- a/app/services/add_work_package_note_service.rb
+++ b/app/services/add_work_package_note_service.rb
@@ -32,6 +32,8 @@
class AddWorkPackageNoteService
include Contracted
+ include Shared::ServiceContext
+
attr_accessor :user, :work_package
def initialize(user:, work_package:)
@@ -41,7 +43,7 @@ class AddWorkPackageNoteService
end
def call(notes, send_notifications: nil)
- Journal::NotificationConfiguration.with send_notifications do
+ in_context(work_package, send_notifications:) do
work_package.add_journal(user:, notes:)
success, errors = validate_and_yield(work_package, user) do
diff --git a/spec/services/add_work_package_note_service_spec.rb b/spec/services/add_work_package_note_service_spec.rb
index 7bc8d81788e..ed6156c640b 100644
--- a/spec/services/add_work_package_note_service_spec.rb
+++ b/spec/services/add_work_package_note_service_spec.rb
@@ -60,11 +60,6 @@ RSpec.describe AddWorkPackageNoteService, type: :model do
let(:send_notifications) { false }
before do
- expect(Journal::NotificationConfiguration)
- .to receive(:with)
- .with(send_notifications)
- .and_yield
-
allow(instance).to receive(:contract_class).and_return(mock_contract)
allow(work_package).to receive(:save_journals).and_return true
end
@@ -81,6 +76,30 @@ RSpec.describe AddWorkPackageNoteService, type: :model do
subject
end
+ it "creates an advisory lock" do
+ allow(OpenProject::Mutex)
+ .to receive(:with_advisory_lock_transaction)
+ .with(work_package)
+ .and_call_original
+
+ subject
+
+ expect(OpenProject::Mutex)
+ .to have_received(:with_advisory_lock_transaction)
+ end
+
+ it "sends notifications" do
+ allow(Journal::NotificationConfiguration)
+ .to receive(:with)
+ .with(send_notifications)
+ .and_yield
+
+ subject
+
+ expect(Journal::NotificationConfiguration)
+ .to have_received(:with)
+ end
+
it "has no errors" do
expect(subject.errors).to be_empty
end