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