2022-07-16 23:28:13 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module IncidentManagement
|
|
|
|
module TimelineEvents
|
|
|
|
# @param timeline_event [IncidentManagement::TimelineEvent]
|
|
|
|
# @param user [User]
|
|
|
|
# @param params [Hash]
|
|
|
|
# @option params [string] note
|
|
|
|
# @option params [datetime] occurred_at
|
|
|
|
class UpdateService < TimelineEvents::BaseService
|
2023-01-13 00:05:48 +05:30
|
|
|
VALIDATION_CONTEXT = :user_input
|
|
|
|
|
2022-07-16 23:28:13 +05:30
|
|
|
def initialize(timeline_event, user, params)
|
|
|
|
@timeline_event = timeline_event
|
|
|
|
@incident = timeline_event.incident
|
|
|
|
@user = user
|
|
|
|
@note = params[:note]
|
|
|
|
@occurred_at = params[:occurred_at]
|
2023-01-13 00:05:48 +05:30
|
|
|
@validation_context = VALIDATION_CONTEXT
|
2022-07-16 23:28:13 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
return error_no_permissions unless allowed?
|
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
timeline_event.assign_attributes(update_params)
|
|
|
|
|
|
|
|
if timeline_event.save(context: validation_context)
|
2022-07-16 23:28:13 +05:30
|
|
|
add_system_note(timeline_event)
|
|
|
|
|
2022-07-23 23:45:48 +05:30
|
|
|
track_usage_event(:incident_management_timeline_event_edited, user.id)
|
2022-07-16 23:28:13 +05:30
|
|
|
success(timeline_event)
|
|
|
|
else
|
|
|
|
error_in_save(timeline_event)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
attr_reader :timeline_event, :incident, :user, :note, :occurred_at, :validation_context
|
2022-07-16 23:28:13 +05:30
|
|
|
|
|
|
|
def update_params
|
2022-08-27 11:52:29 +05:30
|
|
|
{ updated_by_user: user, note: note, occurred_at: occurred_at }.compact
|
2022-07-16 23:28:13 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def add_system_note(timeline_event)
|
|
|
|
changes = was_changed(timeline_event)
|
|
|
|
return if changes == :none
|
|
|
|
|
|
|
|
SystemNoteService.edit_timeline_event(timeline_event, user, was_changed: changes)
|
|
|
|
end
|
|
|
|
|
|
|
|
def was_changed(timeline_event)
|
|
|
|
changes = timeline_event.previous_changes
|
|
|
|
occurred_at_changed = changes.key?('occurred_at')
|
|
|
|
note_changed = changes.key?('note')
|
|
|
|
|
|
|
|
return :occurred_at_and_note if occurred_at_changed && note_changed
|
|
|
|
return :occurred_at if occurred_at_changed
|
|
|
|
return :note if note_changed
|
|
|
|
|
|
|
|
:none
|
|
|
|
end
|
2022-07-23 23:45:48 +05:30
|
|
|
|
2022-08-13 15:12:31 +05:30
|
|
|
def allowed?
|
|
|
|
user&.can?(:edit_incident_management_timeline_event, timeline_event)
|
2022-07-23 23:45:48 +05:30
|
|
|
end
|
2022-07-16 23:28:13 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|