Commit 95ac8579 authored by allison.browne's avatar allison.browne

Alert Assignement Todo: auto add a todo assignment

Add an assignment todo automatically when an alert
is assigned via the sidebar.
parent 8e87e22d
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
module AlertManagement module AlertManagement
module Alerts module Alerts
class UpdateService class UpdateService
include Gitlab::Utils::StrongMemoize
# @param alert [AlertManagement::Alert] # @param alert [AlertManagement::Alert]
# @param current_user [User] # @param current_user [User]
# @param params [Hash] Attributes of the alert # @param params [Hash] Attributes of the alert
...@@ -19,6 +21,7 @@ module AlertManagement ...@@ -19,6 +21,7 @@ module AlertManagement
filter_assignees filter_assignees
if alert.update(params) if alert.update(params)
assign_todo
success success
else else
error(alert.errors.full_messages.to_sentence) error(alert.errors.full_messages.to_sentence)
...@@ -29,6 +32,12 @@ module AlertManagement ...@@ -29,6 +32,12 @@ module AlertManagement
attr_reader :alert, :current_user, :params attr_reader :alert, :current_user, :params
def assign_todo
return unless assignee
todo_service.assign_alert(alert, assignee)
end
def allowed? def allowed?
current_user.can?(:update_alert_management_alert, alert) current_user.can?(:update_alert_management_alert, alert)
end end
...@@ -36,8 +45,20 @@ module AlertManagement ...@@ -36,8 +45,20 @@ module AlertManagement
def filter_assignees def filter_assignees
return if params[:assignees].nil? return if params[:assignees].nil?
# Take first assignee while multiple are not currently supported params[:assignees] = Array(assignee)
params[:assignees] = Array(params[:assignees].first) end
def assignee
strong_memoize(:assignee) do
# Take first assignee while multiple are not currently supported
params[:assignees]&.first
end
end
def todo_service
strong_memoize(:todo_service) do
TodoService.new
end
end end
def success def success
......
---
title: Add todo when alert is assigned to a user
merge_request: 34104
author:
type: added
...@@ -45,7 +45,7 @@ describe AlertManagement::Alerts::UpdateService do ...@@ -45,7 +45,7 @@ describe AlertManagement::Alerts::UpdateService do
end end
end end
context 'when a model attribute is included' do context 'when a model attribute is included without assignees' do
let(:params) { { title: 'This is an updated alert.' } } let(:params) { { title: 'This is an updated alert.' } }
it 'updates the attribute' do it 'updates the attribute' do
...@@ -54,6 +54,10 @@ describe AlertManagement::Alerts::UpdateService do ...@@ -54,6 +54,10 @@ describe AlertManagement::Alerts::UpdateService do
expect { response }.to change { alert.title }.from(original_title).to(params[:title]) expect { response }.to change { alert.title }.from(original_title).to(params[:title])
expect(response).to be_success expect(response).to be_success
end end
it 'skips adding a todo' do
expect { response }.not_to change(Todo, :count)
end
end end
context 'when assignees are included' do context 'when assignees are included' do
...@@ -76,6 +80,10 @@ describe AlertManagement::Alerts::UpdateService do ...@@ -76,6 +80,10 @@ describe AlertManagement::Alerts::UpdateService do
expect(response).to be_success expect(response).to be_success
end end
end end
it 'adds a todo' do
expect { response }.to change { Todo.where(user: user_with_permissions).count }.by(1)
end
end end
end end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment