# frozen_string_literal: true

class RemoveTransientData
  # This model lets us operate on the sessions DB table using ActiveRecord's
  # methods within the scope of this service. This relies on the AR's
  # convention where a Session model maps to a sessions table.
  class Session < ActiveRecord::Base
  end

  def call
    Rails.logger.info("#{self.class.name}: processing")

    Spree::StateChange.delete_all("created_at < '#{retention_period}'")
    Spree::LogEntry.delete_all("created_at < '#{retention_period}'")
    Session.delete_all("updated_at < '#{retention_period}'")
  end

  private

  def retention_period
    2.months.ago.to_date
  end
end

# frozen_string_literal: true

class RemoveTransientData
  # This model lets us operate on the sessions DB table using ActiveRecord's
  # methods within the scope of this service. This relies on the AR's
  # convention where a Session model maps to a sessions table.
  class Session < ActiveRecord::Base
  end

  def call
    Rails.logger.info("#{self.class.name}: processing")

    Spree::StateChange.where("created_at < ?", retention_period).delete_all
    Spree::LogEntry.delete_all("created_at < '#{retention_period}'")
    Session.delete_all("updated_at < '#{retention_period}'")
  end

  private

  def retention_period
    2.months.ago.to_date
  end
end

Use where + delete_all to increase readability
Use where + delete_all to increase readability

Co-authored-by: Maikel <3db1433c7298ef0d91dd12baeaaf90dc122deafc@email.org.au>