Files
simple-aircraft-manager-rails/app/mailers/reminder_mailer.rb
T
rickyandClaude Opus 5.5 9662c7ae16 Time zones: each person's own, and TZ for the installation
- Requests run in the viewer's zone: Profile -> Time zone, else their
  device's (a tz cookie set from the browser), else the installation's;
  times show in it and "today" is the local date (a new flight's date)
- Docker's TZ sets the installation zone (default UTC): scheduled jobs run
  on its clock (recurring.yml), and it's used before a device's zone is known
- The weekly reminder email is worked out in the recipient's zone
- Signatures, record history and exports keep exact UTC timestamps
- users.time_zone; README, .env.example and guide updated

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 07:49:22 -05:00

45 lines
2.0 KiB
Ruby

class ReminderMailer < ApplicationMailer
# [[aircraft, issues, new AD alerts], …] for the aircraft the user owns
# that need attention.
def self.items_for(user)
Aircraft.joins(:roles).where(aircraft_roles: { user_id: user.id, role: "owner" }).ordered
.map { [ _1, _1.airworthiness.issues, _1.ad_alerts.pending.to_a ] }.reject { _2.empty? && _3.empty? }
end
def self.expiring_certificates_for(user) = user.certificates.expiring.to_a
def self.audit_due_for?(user) = user.admin? && AuditRun.due_soon?
# Owned aircraft whose AD baseline needs a (new) DRS import: none yet, a
# year old, or a model never covered.
def self.drs_due_for(user)
Aircraft.joins(:roles).where(aircraft_roles: { user_id: user.id, role: "owner" }).ordered
.select { DrsImport.renewal_due?(_1) || DrsImport.missing_models(_1).any? }
end
# The DRS reminder rides along with any other news; on its own it's sent
# on the first Monday of the month only, not every week.
def self.anything_for?(user, today: Date.current)
items_for(user).any? || expiring_certificates_for(user).any? || audit_due_for?(user) ||
(today.day <= 7 && drs_due_for(user).any?)
end
def weekly(user)
Time.use_zone(user.time_zone.presence || Time.zone_default) { weekly_in_zone(user) }
end
private
def weekly_in_zone(user)
@user = user
@items = self.class.items_for(user)
@certificates = self.class.expiring_certificates_for(user)
@audit_due = self.class.audit_due_for?(user) && AuditRun.next_due
@drs_due = self.class.drs_due_for(user)
overdue = @items.sum { |_, issues, _| issues.count { _1.severity.in?(%w[RED LIMITED]) } }
subject = overdue.positive? ? t(".subject_overdue", count: overdue) : t(".subject_due")
subject = t(@certificates.any? ? ".subject_certificate" : (@audit_due ? ".subject_audit" : ".subject_drs")) if @items.empty?
mail subject: [ subject, @items.map { _1.first.tail_number }.to_sentence.presence ].compact.join(": "), to: user.email_address
end
end