Add missing files :(
This commit is contained in:
parent
b69cd9df2a
commit
4182608f39
3 changed files with 69 additions and 0 deletions
45
app/models/bulk_email_session.rb
Normal file
45
app/models/bulk_email_session.rb
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'securerandom'
|
||||
|
||||
class BulkEmailSession < ApplicationRecord
|
||||
before_validation :generate_token, on: :create
|
||||
before_validation :set_expiration, on: :create
|
||||
|
||||
validates :token, presence: true, uniqueness: true
|
||||
validates :recipient_emails, presence: true
|
||||
|
||||
serialize :recipient_emails, type: Array
|
||||
|
||||
# Sessions expire after 1 hour to prevent database bloat
|
||||
EXPIRATION_TIME = 1.hour
|
||||
|
||||
scope :active, -> { where('expires_at > ?', Time.current) }
|
||||
scope :expired, -> { where('expires_at <= ?', Time.current) }
|
||||
|
||||
def self.cleanup_expired!
|
||||
expired.delete_all
|
||||
end
|
||||
|
||||
def expired?
|
||||
expires_at <= Time.current
|
||||
end
|
||||
|
||||
def recipient_count
|
||||
recipient_emails&.size || 0
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def generate_token
|
||||
return if token.present?
|
||||
|
||||
self.token = SecureRandom.hex(16)
|
||||
end
|
||||
|
||||
def set_expiration
|
||||
return if expires_at.present?
|
||||
|
||||
self.expires_at = EXPIRATION_TIME.from_now
|
||||
end
|
||||
end
|
||||
15
db/migrate/20251013095048_create_bulk_email_sessions.rb
Normal file
15
db/migrate/20251013095048_create_bulk_email_sessions.rb
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
class CreateBulkEmailSessions < ActiveRecord::Migration[7.2]
|
||||
def change
|
||||
create_table :bulk_email_sessions do |t|
|
||||
t.string :token, null: false
|
||||
t.text :recipient_emails
|
||||
t.string :filter_type
|
||||
t.string :search_term
|
||||
t.datetime :expires_at
|
||||
|
||||
t.timestamps
|
||||
end
|
||||
add_index :bulk_email_sessions, :token, unique: true
|
||||
add_index :bulk_email_sessions, :expires_at
|
||||
end
|
||||
end
|
||||
9
lib/tasks/bulk_email_cleanup.rake
Normal file
9
lib/tasks/bulk_email_cleanup.rake
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
namespace :bulk_email do
|
||||
desc 'Clean up expired bulk email sessions'
|
||||
task cleanup: :environment do
|
||||
count = BulkEmailSession.cleanup_expired!
|
||||
puts "Cleaned up #{count} expired bulk email sessions"
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue