iachat/enterprise/app/services/captain/lifecycle/guards/min_interval.rb
Rodribm10 6d84a7586b feat(lifecycle): add MinInterval and CustomerReplied guards
Implement guards following the same pass/reschedule/too_stale pattern as QuietHours.
Also fix belongs_to :conversation on Delivery to use class_name: '::Conversation' to avoid namespace resolution failure inside Captain::Lifecycle module.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-04-15 01:49:22 -03:00

33 lines
859 B
Ruby

# frozen_string_literal: true
class Captain::Lifecycle::Guards::MinInterval < Captain::Lifecycle::Guards::Base
MAX_DELAY = 2.hours
def check
config = Captain::Lifecycle::Config.for_account(@account)
interval = config.min_interval_minutes.to_i
return pass if interval <= 0
last_sent_at = last_sent_delivery_at
return pass if last_sent_at.blank?
earliest_allowed = last_sent_at + interval.minutes
return pass if @delivery.fire_at >= earliest_allowed
delay = earliest_allowed - @delivery.fire_at
return skip('too_stale') if delay > MAX_DELAY
reschedule(earliest_allowed)
end
private
def last_sent_delivery_at
Captain::Lifecycle::Delivery
.where(captain_reservation_id: @reservation.id,
status: 'sent',
origin: 'scheduled_lifecycle')
.maximum(:sent_at)
end
end