iachat/app/models/recurring_scheduled_message.rb
Cayo P. R. Oliveira c6bfd1eed3
feat: schedule messages recurrence (#240)
* feat(scheduled-messages): add recurring scheduled messages

Implements the recurring scheduled messages feature allowing agents to
configure recurrence rules when scheduling messages, with automatic
creation of subsequent scheduled messages after each send.

Backend:
- RecurringScheduledMessage model with JSONB recurrence_rule validation
- RecurrenceCalculatorService for next occurrence date calculation
- RecurrenceDescriptionService for human-readable rule descriptions
- CreateNextOccurrenceService for auto-creating child ScheduledMessages
- RecurringScheduledMessagesController with CRUD operations
- RecurringScheduledMessagePolicy for authorization
- Modified SendScheduledMessageJob to handle recurrence after send
- Updated due_for_sending scope to exclude resolved conversations
- ActionCable events for real-time updates
- Activity message i18n (en + pt-BR)

Frontend:
- RecurrenceDropdown.vue with contextual shortcut options
- RecurrenceCustomModal.vue for custom recurrence configuration
- RecurringScheduledMessageItem.vue for sidebar display
- Integration into ScheduledMessageModal.vue
- Updated ScheduledMessages.vue with recurrence section and filtering
- Vuex store module for recurring scheduled messages
- API client for CRUD operations
- WebSocket handlers in actionCable.js
- recurrenceHelpers.js utility functions
- i18n keys for en and pt-BR

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* fix(recurring-messages): fix creation and edit visibility

- Fix API payload key mismatch (snake_case vs camelCase) in modal submit
- Add status: 'active' to recurring creation payload
- Fix strong params to permit recurrence_rule array fields (week_days)
- Cast string values from strong params to integers for JSONB validation
- Show RecurrenceDropdown when editing (remove isEditing gate)
- Populate recurrenceRule from scheduled message's recurring parent
- Include recurring_scheduled_message_id and recurrence_rule in
  scheduled message jbuilder response

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* fix(recurring-messages): fix dropdown toggle and locale tag errors

- Use DropdownItem :click prop instead of @click to use the injected
  closeMenu from DropdownContainer context (default slot doesn't
  expose toggle)
- Normalize locale from pt_BR to pt-BR for Intl.DateTimeFormat
  compatibility in RecurringScheduledMessageItem

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* feat(recurring-messages): add separator line and expandable history

- Add border separator between recurring messages section and
  pending/draft messages, matching the history section separator
- Replace static 'N enviadas' counter with clickable toggle that
  expands to show individual sent/failed child messages with
  status badges and formatted timestamps

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* feat(recurring-messages): add click-to-navigate on sent children

Make sent child messages in recurring message history clickable.
Clicking navigates to the actual message in the conversation using
the messageId query param, same pattern as ScheduledMessageItem.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* feat(recurring-messages): allow editing recurring messages

- Add edit button to RecurringScheduledMessageItem (active only)
- Transform recurring message into scheduledMessage-compatible shape
  with recurring_scheduled_message_id set, so the modal reuses
  the existing update path
- Handle edge case of removing recurrence from a recurring message
  (cancels series without trying to update a non-existent standalone)
- Sent history is preserved by the backend update action

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* fix(recurring-messages): show recurrence field without date selection

Remove v-if="scheduledDate" gate so the recurrence dropdown is
always visible in the modal. Falls back to today's date for
contextual shortcut labels when no date is selected yet.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* fix(recurring-messages): ensure recurrence visible on edit and add pending_scheduled_message to API

- Add pending_scheduled_message to recurring_scheduled_message jbuilder
  so REST API data matches WebSocket push_event_data
- Add fallback in openEditRecurringModal to find pending child from
  scheduled_messages array when pending_scheduled_message is absent
- Add same fallback in nextSendLabel computed

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* fix(scheduled-messages): add sections for Drafts and Pending messages in the UI

* refactor(scheduled-messages): merge RecurringScheduledMessageItem into ScheduledMessageItem

Consolidate the recurring message card into the existing
ScheduledMessageItem component instead of maintaining a separate
component. The unified component detects recurring messages via
recurrence_rule and conditionally shows:
- Recurrence description header with repeat icon
- Next send time label
- Expandable sent/failed children history with click-to-navigate
- Stop button (replaces delete) with confirmation modal
- Active/completed/cancelled status badges

Delete the now-unused RecurringScheduledMessageItem.vue.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* fix(scheduled-messages): use blue badge for active, keep green for sent

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* fix(scheduled-messages): remove draft and pending sections from UI and update recurrence title

* fix(recurring-messages): use Teleport for recurrence dropdown

Replace DropdownContainer with Teleport-based floating dropdown so
options render outside the modal. Fixes:
- Dropdown no longer enlarges the modal or causes scrolling
- Dropdown closes before Custom modal opens (no overlap)
- Auto-detects available space and opens above/below trigger

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* fix(recurring-messages): recalculate next date when recurrence rule changes

When editing a recurring message and changing the recurrence rule,
the pending occurrence date is now validated against the new rule.
If the user-provided date doesn't match (e.g. Thursday removed from
weekly days), the system computes the next valid date using
RecurrenceCalculatorService instead of blindly using the old date.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* fix(scheduled-messages): resolve deprecated onClose and disabled type warnings

- Replace :on-close prop with @close event on woot-modal components
- Cast hasTemplate computed to boolean to fix disabled prop type check

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* fix(scheduled-messages): replace remaining deprecated on-close props

- ScheduledMessages.vue delete confirm modal
- RecurrenceCustomModal.vue modal

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* fix(recurring-messages): address review feedback on PR #240

- Fix v-if/v-else bug hiding message list behind resolved warning
- Fix occurrences_sent incrementing on failed sends (skip_increment flag)
- Fix compute_next_valid_date using .min instead of .max
- Fix Vuex delete action to update state on cancel (not remove)
- Use atomic update_counters for occurrences_sent increment
- Add safe Date.iso8601 parsing with rescue in should_complete?
- Add null: false to occurrences_sent migration column
- Fix pt-BR accent: Recorrencia → Recorrências
- Use I18n.with_locale(account.locale) for all activity messages
- Fix N+1 in jbuilder partials (Ruby filtering + eager loading)
- Add interval >= 1 validation to RecurrenceCustomModal isValid
- Validate recurrence_rule presence when status is active
- Add ISO8601 date format validation for end_date
- Add unknown_agent i18n key for fallback

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* fix(recurring-messages): wrap create/update in transactions, clean pending on deactivation

- Wrap create and update flows in ActiveRecord transactions
- Move attachment purge after save! to prevent data loss on validation failure
- Destroy pending children when status transitions to non-active
- Fixes critical bug where stopping recurrence could leave armed pending messages

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* fix(recurring-messages): prevent monthly/yearly day-of-month drift

Store the original intended day in recurrence_rule JSONB as month_day
(for monthly) and year_day/year_month (for yearly). The calculator
now uses these stored values instead of @last_date.day, preventing
drift after short months cap the day (e.g., Jan 31 → Feb 28 → all
subsequent months stuck on 28).

Backend:
- RecurrenceCalculatorService: use rule[:month_day] for monthly and
  rule[:year_day]/rule[:year_month] for yearly calculations
- Controller: permit and cast the new integer keys

Frontend:
- recurrenceHelpers: yearly shortcuts include year_day/year_month
- RecurrenceCustomModal: emit month_day for monthly day_of_month
  rules and year_day/year_month for yearly rules

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* fix(recurring-messages): i18n description services, align due_for_sending scope

Backend:
- RecurrenceDescriptionService: replace hardcoded English with I18n.t()
  calls; accept locale parameter and use I18n.with_locale
- RecurringScheduledMessage model: pass account locale to description service
- ScheduledMessage: align due_for_sending? instance method with scope by
  checking conversation status (open/pending)

Frontend:
- buildRecurrenceDescription: use t() i18n function instead of manual
  isPt locale branching
- Add DESCRIPTION i18n keys to en/conversation.json and pt_BR/conversation.json
- Update RecurrenceDropdown and ScheduledMessageItem callers to pass t

i18n:
- Add recurring_scheduled_messages.description.* keys to en.yml and pt_BR.yml

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* fix(recurring-messages): keep pending child when deactivating recurrence

When editing a recurring message to disable recurrence while setting a
future send date, the pending child message is now preserved instead of
being destroyed. This allows a 'final send' without creating new
recurrences (the send job already guards with recurring&.active?).

Backend:
- Add update_pending_on_deactivation: updates pending child's
  scheduled_at or creates a final pending occurrence
- Replace destroy_all in update's non-active branch

Frontend:
- activeRecurringMessages now includes non-active recurring messages
  that still have a pending child (pending_scheduled_message)
- Stop button hidden for already-cancelled recurring messages
- inactiveRecurringMessages excludes messages with pending children

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* fix(recurring-messages): prevent removing recurrence from existing recurring message

Once a scheduled message has recurrence, the 'Does not repeat' option is
hidden from the RecurrenceDropdown when editing. This avoids edge cases
where deactivating recurrence leaves the message in an ambiguous display
state.

- RecurrenceDropdown: add hideNoRepeat prop, filter NO_REPEAT from shortcuts
- ScheduledMessageModal: pass hideNoRepeat when isEditingRecurring
- Revert update_pending_on_deactivation (no longer reachable from UI)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* fix(recurring-messages): address CodeRabbit review feedback

- ScheduledMessage#push_event_data: expose recurring_scheduled_message_id
  in ActionCable payloads so frontend correctly classifies children
- RecurringScheduledMessagePolicy: add agent_bot? check for parity with
  ScheduledMessagePolicy
- RecurrenceCalculatorService: guard against nil/empty week_days
- Factory: bind inbox and account to conversation to prevent cross-account
  flakiness in specs

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

* fix(recurring-messages): update schema to enforce non-null constraint on occurrences_sent

---------

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: gabrieljablonski <contact@gabrieljablonski.com>
2026-03-19 22:51:14 -03:00

175 lines
6.3 KiB
Ruby

# == Schema Information
#
# Table name: recurring_scheduled_messages
#
# id :bigint not null, primary key
# author_type :string not null
# content :text
# occurrences_sent :integer default(0), not null
# recurrence_rule :jsonb not null
# status :integer default("draft"), not null
# template_params :jsonb
# created_at :datetime not null
# updated_at :datetime not null
# account_id :bigint not null
# author_id :bigint not null
# conversation_id :bigint not null
# inbox_id :bigint not null
#
# Indexes
#
# idx_recurring_sched_msgs_on_account_status (account_id,status)
# idx_recurring_sched_msgs_on_conversation_status (conversation_id,status)
# idx_recurring_sched_msgs_on_status (status)
# index_recurring_scheduled_messages_on_account_id (account_id)
# index_recurring_scheduled_messages_on_author (author_type,author_id)
# index_recurring_scheduled_messages_on_conversation_id (conversation_id)
# index_recurring_scheduled_messages_on_inbox_id (inbox_id)
#
# Foreign Keys
#
# fk_rails_... (account_id => accounts.id)
# fk_rails_... (conversation_id => conversations.id)
# fk_rails_... (inbox_id => inboxes.id)
#
class RecurringScheduledMessage < ApplicationRecord
include Rails.application.routes.url_helpers
FREQUENCIES = %w[daily weekly monthly yearly].freeze
END_TYPES = %w[never on_date after_count].freeze
MONTHLY_TYPES = %w[day_of_month day_of_week].freeze
belongs_to :account
belongs_to :conversation
belongs_to :inbox
belongs_to :author, polymorphic: true
has_many :scheduled_messages, dependent: :destroy
has_one_attached :attachment
enum status: { draft: 0, active: 1, completed: 2, cancelled: 3 }
validates :author_type, inclusion: { in: ['User'] }
validates :content, presence: true, unless: :content_optional?
validate :validate_recurrence_rule
scope :for_conversation, ->(conversation_id) { where(conversation_id: conversation_id) }
def push_event_data
base_event_data.tap do |data|
data[:author] = author.push_event_data if author.present?
data[:attachment] = attachment_data if attachment.attached?
data[:pending_scheduled_message] = pending_scheduled_message_data
data[:scheduled_messages] = recent_scheduled_messages_data
end
end
def recurrence_description
RecurringScheduledMessages::RecurrenceDescriptionService.new(recurrence_rule, locale: account&.locale || :en).generate
end
def attachment_data
return unless attachment.attached?
{
id: attachment.id,
recurring_scheduled_message_id: id,
file_type: attachment.content_type,
account_id: account_id,
file_url: url_for(attachment),
blob_id: attachment.blob.signed_id,
filename: attachment.filename.to_s
}
end
private
def base_event_data
{
id: id, content: content, inbox_id: inbox_id,
conversation_id: conversation.display_id, account_id: account_id,
status: status, template_params: template_params,
recurrence_rule: recurrence_rule, recurrence_description: recurrence_description,
occurrences_sent: occurrences_sent, author_id: author_id, author_type: author_type,
created_at: created_at.to_i, updated_at: updated_at.to_i
}
end
def pending_scheduled_message_data
sm = scheduled_messages.where(status: :pending).order(scheduled_at: :asc).first
return unless sm
{ id: sm.id, scheduled_at: sm.scheduled_at&.to_i }
end
def recent_scheduled_messages_data
scheduled_messages.order(scheduled_at: :desc).limit(50).map do |sm|
{ id: sm.id, status: sm.status, scheduled_at: sm.scheduled_at&.to_i, message_id: sm.message_id }
end
end
def content_optional?
template_params.present? || attachment.attached?
end
def validate_recurrence_rule
if recurrence_rule.blank? || recurrence_rule == {}
errors.add(:recurrence_rule, 'must have a valid frequency') if active?
return
end
rule = recurrence_rule.with_indifferent_access
validate_frequency(rule)
validate_interval(rule)
validate_weekly_fields(rule) if rule[:frequency] == 'weekly'
validate_monthly_fields(rule) if rule[:frequency] == 'monthly'
validate_end_type(rule)
end
def validate_frequency(rule)
errors.add(:recurrence_rule, 'must have a valid frequency') unless rule[:frequency].in?(FREQUENCIES)
end
def validate_interval(rule)
interval = rule[:interval]
errors.add(:recurrence_rule, 'must have an interval >= 1') unless interval.is_a?(Integer) && interval >= 1
end
def validate_weekly_fields(rule)
week_days = rule[:week_days]
return errors.add(:recurrence_rule, 'must have week_days for weekly frequency') if week_days.blank? || !week_days.is_a?(Array)
errors.add(:recurrence_rule, 'week_days must contain values between 0-6') unless week_days.all? { |d| d.is_a?(Integer) && d.between?(0, 6) }
end
def validate_monthly_fields(rule)
errors.add(:recurrence_rule, 'must have a valid monthly_type') unless rule[:monthly_type].in?(MONTHLY_TYPES)
return unless rule[:monthly_type] == 'day_of_week'
errors.add(:recurrence_rule, 'must have monthly_week for day_of_week type') unless rule[:monthly_week].is_a?(Integer)
errors.add(:recurrence_rule, 'must have monthly_weekday (0-6) for day_of_week type') unless rule[:monthly_weekday].is_a?(Integer) &&
rule[:monthly_weekday].between?(0, 6)
end
def validate_end_type(rule)
end_type = rule[:end_type]
errors.add(:recurrence_rule, 'must have a valid end_type') unless end_type.in?(END_TYPES)
case end_type
when 'on_date'
validate_end_date(rule[:end_date])
when 'after_count'
end_count = rule[:end_count]
errors.add(:recurrence_rule, 'must have end_count >= 1 for after_count end_type') unless end_count.is_a?(Integer) && end_count >= 1
end
end
def validate_end_date(end_date)
return errors.add(:recurrence_rule, 'must have an end_date for on_date end_type') if end_date.blank?
Date.iso8601(end_date)
rescue ArgumentError
errors.add(:recurrence_rule, 'end_date must be a valid ISO8601 date (YYYY-MM-DD)')
end
end