diff --git a/app/controllers/admin/about_controller.rb b/app/controllers/admin/about_controller.rb
index ec51dac..9405e3c 100644
--- a/app/controllers/admin/about_controller.rb
+++ b/app/controllers/admin/about_controller.rb
@@ -9,8 +9,8 @@ class Admin::AboutController < ApplicationController
# This week's reminder for the current admin, now.
def test_reminder
return redirect_to(admin_about_path, alert: "Email isn't set up yet.") unless MailSettings.configured?
- if ReminderMailer.items_for(current_user).empty?
- return redirect_to admin_about_path, notice: "Nothing needs attention on the aircraft you own, so there's no reminder to send."
+ unless ReminderMailer.anything_for?(current_user)
+ return redirect_to admin_about_path, notice: "Nothing needs attention on the aircraft you own or your certificates, so there's no reminder to send."
end
ReminderMailer.weekly(current_user).deliver_now
redirect_to admin_about_path, notice: "Reminder sent to #{current_user.email_address}."
diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb
index fca96cc..cfd7cd8 100644
--- a/app/controllers/admin/users_controller.rb
+++ b/app/controllers/admin/users_controller.rb
@@ -3,7 +3,7 @@ class Admin::UsersController < ApplicationController
before_action :set_user, only: %i[edit update destroy]
def index
- @users = User.order(:email_address).includes(aircraft_roles: :aircraft)
+ @users = User.order(:email_address).includes(:certificates, aircraft_roles: :aircraft)
end
def new
diff --git a/app/controllers/user_certificates_controller.rb b/app/controllers/user_certificates_controller.rb
new file mode 100644
index 0000000..21efa11
--- /dev/null
+++ b/app/controllers/user_certificates_controller.rb
@@ -0,0 +1,32 @@
+# The signed-in user's own certificates (Profile → Certificates).
+class UserCertificatesController < ApplicationController
+ before_action :set_certificate, only: %i[update destroy]
+
+ def create
+ certificate = current_user.certificates.new(certificate_params)
+ if certificate.save
+ redirect_to edit_profile_path(anchor: "certificates"), notice: "#{certificate.kind_label} certificate added."
+ else
+ redirect_to edit_profile_path(anchor: "certificates"), alert: "Certificate not added: #{certificate.errors.full_messages.to_sentence}."
+ end
+ end
+
+ def update
+ if @certificate.update(certificate_params)
+ redirect_to edit_profile_path(anchor: "certificates"), notice: "Certificate updated."
+ else
+ redirect_to edit_profile_path(anchor: "certificates"), alert: "Certificate not updated: #{@certificate.errors.full_messages.to_sentence}."
+ end
+ end
+
+ def destroy
+ @certificate.destroy!
+ redirect_to edit_profile_path(anchor: "certificates"), notice: "Certificate removed."
+ end
+
+ private
+
+ def set_certificate = @certificate = current_user.certificates.find(params[:id])
+
+ def certificate_params = params.require(:user_certificate).permit(:kind, :number, :expires_on)
+end
diff --git a/app/javascript/controllers/entry_template_controller.js b/app/javascript/controllers/entry_template_controller.js
index 72d9991..4b6d100 100644
--- a/app/javascript/controllers/entry_template_controller.js
+++ b/app/javascript/controllers/entry_template_controller.js
@@ -1,7 +1,8 @@
import { Controller } from "@hotwired/stimulus"
// Fills a new logbook entry from a preventive maintenance template: the
-// wording, the entry type and "pilot" as the kind of certificate.
+// wording, the entry type, and "pilot" as the kind of certificate unless one
+// is already chosen.
export default class extends Controller {
static targets = [ "template", "text", "entryType", "certificate" ]
@@ -11,7 +12,7 @@ export default class extends Controller {
if (this.textTarget.value.trim() && !confirm("Replace the entry text with this template?")) return
this.textTarget.value = option.dataset.text
if (this.hasEntryTypeTarget) this.entryTypeTarget.value = "MAINTENANCE"
- if (this.hasCertificateTarget) this.certificateTarget.value = "PILOT"
+ if (this.hasCertificateTarget && !this.certificateTarget.value) this.certificateTarget.value = "PILOT" // keep a chosen certificate
this.textTarget.focus()
}
}
diff --git a/app/javascript/controllers/signer_controller.js b/app/javascript/controllers/signer_controller.js
new file mode 100644
index 0000000..79fabd3
--- /dev/null
+++ b/app/javascript/controllers/signer_controller.js
@@ -0,0 +1,15 @@
+import { Controller } from "@hotwired/stimulus"
+
+// "Sign as": fills the signer's name, kind of certificate and number from one
+// of the current user's certificates.
+export default class extends Controller {
+ static targets = [ "choice", "name", "kind", "number" ]
+
+ fill() {
+ const option = this.choiceTarget.selectedOptions[0]
+ if (!option?.dataset.number) return
+ if (this.hasNameTarget) this.nameTarget.value = option.dataset.name
+ if (this.hasKindTarget) this.kindTarget.value = option.dataset.kind
+ if (this.hasNumberTarget) this.numberTarget.value = option.dataset.number
+ }
+}
diff --git a/app/jobs/weekly_reminder_job.rb b/app/jobs/weekly_reminder_job.rb
index b31febb..1111949 100644
--- a/app/jobs/weekly_reminder_job.rb
+++ b/app/jobs/weekly_reminder_job.rb
@@ -1,5 +1,6 @@
# Weekly email to aircraft owners listing what's overdue or coming due
-# (inspections, ADs, components, expiring documents) and deferred items.
+# (inspections, ADs, components, expiring documents), deferred items, new
+# ADs, and the user's own certificates that are about to expire.
# Nothing is sent when nothing needs attention, or when email isn't set up.
class WeeklyReminderJob < ApplicationJob
queue_as :default
@@ -7,7 +8,7 @@ class WeeklyReminderJob < ApplicationJob
def perform
return unless MailSettings.configured?
User.where(reminder_emails: true).find_each do |user|
- ReminderMailer.weekly(user).deliver_now if ReminderMailer.items_for(user).any?
+ ReminderMailer.weekly(user).deliver_now if ReminderMailer.anything_for?(user)
rescue StandardError => e
Rails.logger.warn("[reminders] #{user.id}: #{e.class}: #{e.message}")
end
diff --git a/app/mailers/reminder_mailer.rb b/app/mailers/reminder_mailer.rb
index f0cde28..09c600a 100644
--- a/app/mailers/reminder_mailer.rb
+++ b/app/mailers/reminder_mailer.rb
@@ -6,11 +6,17 @@ class ReminderMailer < ApplicationMailer
.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.anything_for?(user) = items_for(user).any? || expiring_certificates_for(user).any?
+
def weekly(user)
@user = user
@items = self.class.items_for(user)
+ @certificates = self.class.expiring_certificates_for(user)
overdue = @items.sum { |_, issues, _| issues.count { _1.severity.in?(%w[RED LIMITED]) } }
subject = overdue.positive? ? "#{overdue} #{'item'.pluralize(overdue)} overdue" : "Maintenance coming due"
- mail subject: "#{subject}: #{@items.map { _1.first.tail_number }.to_sentence}", to: user.email_address
+ subject = "Certificate expiring" if @items.empty?
+ mail subject: [ subject, @items.map { _1.first.tail_number }.to_sentence.presence ].compact.join(": "), to: user.email_address
end
end
diff --git a/app/models/user.rb b/app/models/user.rb
index aef4c17..3d6736e 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -4,6 +4,7 @@ class User < ApplicationRecord
has_many :aircraft_roles, dependent: :destroy
has_many :aircraft, through: :aircraft_roles
has_one :air_trail_connection, dependent: :destroy
+ has_many :certificates, -> { ordered }, class_name: "UserCertificate", dependent: :destroy
# Emailed sign-in links: valid 15 minutes, and spent by the next sign-in of
# any kind (or an email change), so each link works once. last_signed_in_at
diff --git a/app/models/user_certificate.rb b/app/models/user_certificate.rb
new file mode 100644
index 0000000..8b3a994
--- /dev/null
+++ b/app/models/user_certificate.rb
@@ -0,0 +1,26 @@
+# A user's FAA certificate (A&P, IA, pilot…), used to sign logbook entries.
+# Some expire (an Inspection Authorization lapses on March 31 of each
+# odd-numbered year unless renewed, 14 CFR 65.92).
+class UserCertificate < ApplicationRecord
+ KINDS = LogbookEntry::CERTIFICATE_TYPES
+ MECHANIC_KINDS = %w[AP IA A P REPAIR_STATION REPAIRMAN].freeze
+ SHORT_LABELS = { "AP" => "A&P", "IA" => "IA", "A" => "Airframe", "P" => "Powerplant", "REPAIR_STATION" => "Repair station",
+ "REPAIRMAN" => "Repairman", "PILOT" => "Pilot", "OTHER" => "Other" }.freeze
+ EXPIRY_WARNING_DAYS = 60
+
+ belongs_to :user
+
+ normalizes :number, with: ->(n) { n.strip }
+ validates :kind, inclusion: { in: KINDS.keys }
+ validates :number, presence: true, length: { maximum: 40 }
+
+ scope :ordered, -> { order(Arel.sql("CASE kind WHEN 'IA' THEN 0 WHEN 'AP' THEN 1 ELSE 2 END"), :created_at) }
+ # Expiring soon, or lapsed in the last month (so it isn't mentioned forever).
+ scope :expiring, ->(within = EXPIRY_WARNING_DAYS.days) { where(expires_on: (Date.current - 30)..(Date.current + within)) }
+
+ def kind_label = KINDS[kind]
+ def short_label = SHORT_LABELS[kind]
+ def mechanic? = MECHANIC_KINDS.include?(kind)
+ def expired?(today = Date.current) = expires_on.present? && today > expires_on
+ def to_s = "#{kind_label} #{number}"
+end
diff --git a/app/views/admin/users/index.html.erb b/app/views/admin/users/index.html.erb
index 700b394..e897fdb 100644
--- a/app/views/admin/users/index.html.erb
+++ b/app/views/admin/users/index.html.erb
@@ -4,7 +4,7 @@
Name Email Aircraft Last sign-in
A maintenance entry needs a description of the work, the date, and the name, signature, certificate number and kind of certificate of whoever approved the return to service (14 CFR 43.9). An inspection entry also needs the type and extent of the inspection, the aircraft's time in service, and the result (43.11).
+ <%= render "shared/sign_as" %>Your FAA certificates, for signing logbook entries: “Sign as” fills in your name, the kind of certificate and its number.
+ <% if @user.certificates.any? %> +An Inspection Authorization expires on March 31 of each odd-numbered year unless renewed; you'll be reminded 60 days ahead in the weekly email.
+ <% end %> +Here's what needs attention on your aircraft.
+Here's what needs attention<%= " on your aircraft" if @items.any? %>.
<% @items.each do |aircraft, issues, alerts| %>Based on the records in Simple Aircraft Manager. Turn these emails off on your <%= link_to "profile", edit_profile_url %>.
diff --git a/app/views/reminder_mailer/weekly.text.erb b/app/views/reminder_mailer/weekly.text.erb index e23a2e7..695048c 100644 --- a/app/views/reminder_mailer/weekly.text.erb +++ b/app/views/reminder_mailer/weekly.text.erb @@ -1,4 +1,4 @@ -Here's what needs attention on your aircraft. +Here's what needs attention<%= " on your aircraft" if @items.any? %>. <% @items.each do |aircraft, issues, alerts| %> <%= aircraft.tail_number %> (<%= aircraft.airworthiness.label %>): <%= aircraft_url(aircraft) %> @@ -9,5 +9,12 @@ Here's what needs attention on your aircraft. - New ADs that may apply: <%= alerts.map { _1.ad_number || _1.fr_document_number }.to_sentence %>. Review: <%= aircraft_airworthiness_directives_url(aircraft, anchor: "new-ads") %> <% end %> <% end %> +<% if @certificates.any? %> + +Your certificates: +<% @certificates.each do |c| %> +- <%= c.expired? ? "Expired" : "Expires" %> <%= c.expires_on %>: <%= c.kind_label %> <%= c.number %> +<% end %> +<% end %> Based on the records in Simple Aircraft Manager. Turn these emails off on your profile: <%= edit_profile_url %> diff --git a/app/views/shared/_sign_as.html.erb b/app/views/shared/_sign_as.html.erb new file mode 100644 index 0000000..c58c4f9 --- /dev/null +++ b/app/views/shared/_sign_as.html.erb @@ -0,0 +1,14 @@ +<%# "Sign as" picker for the current user's certificates (signer controller). %> +<% certificates = current_user.certificates.reject(&:expired?) %> +<% if certificates.any? %> +For a new entry. 14 CFR 43.9: what was done, the date, and the name, certificate number and kind of certificate of whoever approved the return to service.
+ <%= render "shared/sign_as" %> <%= fields_for :logbook_entry, @entry do |f| %>