2020-04-08 14:13:33 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module ResourceEvents
|
|
|
|
class ChangeMilestoneService
|
2020-05-24 23:13:21 +05:30
|
|
|
attr_reader :resource, :user, :event_created_at, :milestone, :old_milestone
|
2020-04-08 14:13:33 +05:30
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
def initialize(resource, user, created_at: Time.current, old_milestone:)
|
2020-04-08 14:13:33 +05:30
|
|
|
@resource = resource
|
|
|
|
@user = user
|
|
|
|
@event_created_at = created_at
|
|
|
|
@milestone = resource&.milestone
|
2020-05-24 23:13:21 +05:30
|
|
|
@old_milestone = old_milestone
|
2020-04-08 14:13:33 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
ResourceMilestoneEvent.create(build_resource_args)
|
|
|
|
|
|
|
|
resource.expire_note_etag_cache
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def build_resource_args
|
|
|
|
action = milestone.blank? ? :remove : :add
|
|
|
|
key = resource.class.name.foreign_key
|
|
|
|
|
|
|
|
{
|
|
|
|
user_id: user.id,
|
|
|
|
created_at: event_created_at,
|
2020-05-24 23:13:21 +05:30
|
|
|
milestone_id: action == :add ? milestone&.id : old_milestone&.id,
|
2020-04-08 14:13:33 +05:30
|
|
|
state: ResourceMilestoneEvent.states[resource.state],
|
|
|
|
action: ResourceMilestoneEvent.actions[action],
|
|
|
|
key => resource.id
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|