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 @@ NameEmailAircraftLast sign-in <% @users.each do |u| %> <%= u.name %> <% if u.admin? %>Admin<% end %><% if u.sso_uid? %> <%= Sso.label %><% end %> - <%= u.email_address %> + <%= u.email_address %><% if u.certificates.any? %>
<%= u.certificates.map { "#{_1.short_label} #{_1.number}" }.join(", ") %>
<% end %> <%= u.aircraft_roles.map { "#{_1.aircraft.tail_number} (#{_1.role})" }.join(", ").presence || "—" %> <%= u.last_signed_in_at ? "#{time_ago_in_words(u.last_signed_in_at)} ago" : "never" %> <%= link_to "Edit", edit_admin_user_path(u), class: "btn-secondary btn-sm" %> diff --git a/app/views/logbook_entries/_form.html.erb b/app/views/logbook_entries/_form.html.erb index 249750e..f28e073 100644 --- a/app/views/logbook_entries/_form.html.erb +++ b/app/views/logbook_entries/_form.html.erb @@ -1,4 +1,4 @@ -<%= form_with model: [aircraft, entry], class: "card card-body space-y-4 max-w-3xl", data: { controller: "entry-template" } do |f| %> +<%= form_with model: [aircraft, entry], class: "card card-body space-y-4 max-w-3xl", data: { controller: "entry-template signer" } do |f| %> <%= f.errors_summary %> <% if entry.new_record? %>
@@ -20,11 +20,12 @@
<%= f.field :text, as: :textarea, label: "Entry text", rows: 8, data: { entry_template_target: "text" } %>

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" %>
- <%= f.field :signoff_person, label: "Signed off by", placeholder: "Name" %> + <%= f.field :signoff_person, label: "Signed off by", placeholder: "Name", data: { signer_target: "name" } %> <%= f.field :signoff_certificate_type, as: :select, label: "Kind of certificate", include_blank: "—", - collection: LogbookEntry::CERTIFICATE_TYPES.invert, data: { entry_template_target: "certificate" } %> - <%= f.field :signoff_certificate_number, label: "Certificate number", placeholder: "e.g. 1234567" %> + collection: LogbookEntry::CERTIFICATE_TYPES.invert, data: { entry_template_target: "certificate", signer_target: "kind" } %> + <%= f.field :signoff_certificate_number, label: "Certificate number", placeholder: "e.g. 1234567", data: { signer_target: "number" } %>
<%= f.field :inspection_result, as: :select, label: "Inspection result", include_blank: "— Not an inspection —", diff --git a/app/views/profiles/edit.html.erb b/app/views/profiles/edit.html.erb index 4451c84..0fc7e95 100644 --- a/app/views/profiles/edit.html.erb +++ b/app/views/profiles/edit.html.erb @@ -4,7 +4,7 @@ <%= f.field :name %> <%= f.field :email_address, as: :email, label: "Email" %> <%= f.field :reminder_emails, as: :checkbox, label: "Weekly maintenance reminders", - hint: "A Monday email about overdue and upcoming inspections, ADs, parts and documents on aircraft you own. Only sent when something needs attention#{' (once email is set up)' unless MailSettings.configured?}." %> + hint: "A Monday email about overdue and upcoming inspections, ADs, parts and documents on aircraft you own, and certificates of yours about to expire. Only sent when something needs attention#{' (once email is set up)' unless MailSettings.configured?}." %> <%= f.field :password, as: :password, label: "New password", hint: "Leave blank to keep your current password.", autocomplete: "new-password" %> <%= f.field :password_confirmation, as: :password, label: "Confirm new password", autocomplete: "new-password" %>
@@ -31,6 +31,31 @@ <% end %> +
+

Certificates

+

Your FAA certificates, for signing logbook entries: “Sign as” fills in your name, the kind of certificate and its number.

+ <% if @user.certificates.any? %> +
    + <% @user.certificates.each do |c| %> +
  • + <%= c.kind_label %> · <%= c.number %> + <% if c.expires_on %><%= c.expired? ? "Expired" : "Expires" %> <%= c.expires_on %><% end %> + <%= button_to "Remove", certificate_path(c), method: :delete, class: "btn-secondary btn-sm", form: { data: { turbo_confirm: "Remove your #{c.kind_label} certificate?" } } %> +
  • + <% end %> +
+ <% end %> + <%= form_with model: UserCertificate.new, url: certificates_path, class: "mt-3 grid gap-2 sm:grid-cols-[1fr_8rem]" do |f| %> + <%= f.select :kind, UserCertificate::KINDS.invert, {}, class: "input", "aria-label": "Kind of certificate" %> + <%= f.text_field :number, class: "input", placeholder: "Number", required: true, maxlength: 40, "aria-label": "Certificate number" %> +
+
<%= f.label :expires_on, "Expires (optional)", class: "label" %><%= f.date_field :expires_on, class: "input" %>
+ <%= f.submit "Add certificate", class: "btn-secondary" %> +
+

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 %> +
+

Sessions

<% others = @user.sessions.where.not(id: Current.session.id).count %> diff --git a/app/views/reminder_mailer/weekly.html.erb b/app/views/reminder_mailer/weekly.html.erb index 42f5f94..089b727 100644 --- a/app/views/reminder_mailer/weekly.html.erb +++ b/app/views/reminder_mailer/weekly.html.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| %>

<%= link_to aircraft.tail_number, aircraft_url(aircraft) %> <%= aircraft.airworthiness.label %>

    @@ -12,6 +12,12 @@ <%= alerts.map { _1.ad_number || _1.fr_document_number }.to_sentence %>. <%= link_to "Review them", 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 <%= 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? %> +
    + <%= label_tag :sign_as, "Sign as", class: "text-slate-600" %> + +
    +<% end %> diff --git a/app/views/squawks/deferral.html.erb b/app/views/squawks/deferral.html.erb index 548cdcf..55f045a 100644 --- a/app/views/squawks/deferral.html.erb +++ b/app/views/squawks/deferral.html.erb @@ -33,7 +33,8 @@
<%= label_tag :deferred_by_role, "As", class: "label" %> - <%= select_tag :deferred_by_role, options_for_select(Squawk::DEFERRAL_ROLES.invert, params[:deferred_by_role]), class: "input" %> + <%= select_tag :deferred_by_role, options_for_select(Squawk::DEFERRAL_ROLES.invert, + params[:deferred_by_role] || (current_user.certificates.any?(&:mechanic?) ? "mechanic" : "pilot")), class: "input" %>
diff --git a/app/views/squawks/resolution.html.erb b/app/views/squawks/resolution.html.erb index c56e580..13e67b6 100644 --- a/app/views/squawks/resolution.html.erb +++ b/app/views/squawks/resolution.html.erb @@ -19,8 +19,9 @@ -
+

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| %>
<%= f.field :date, as: :date %> @@ -29,9 +30,10 @@
<%= f.field :text, as: :textarea, label: "Work performed", rows: 3 %>
- <%= f.field :signoff_person, label: "Signed off by" %> - <%= f.field :signoff_certificate_type, as: :select, label: "Kind of certificate", include_blank: "—", collection: LogbookEntry::CERTIFICATE_TYPES.invert %> - <%= f.field :signoff_certificate_number, label: "Certificate number" %> + <%= f.field :signoff_person, label: "Signed off by", data: { signer_target: "name" } %> + <%= f.field :signoff_certificate_type, as: :select, label: "Kind of certificate", include_blank: "—", collection: LogbookEntry::CERTIFICATE_TYPES.invert, + data: { signer_target: "kind" } %> + <%= f.field :signoff_certificate_number, label: "Certificate number", data: { signer_target: "number" } %>
<% end %>
diff --git a/config/routes.rb b/config/routes.rb index 4d185d3..af14565 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -15,6 +15,7 @@ Rails.application.routes.draw do resources :passwords, param: :token resource :setup, only: %i[new create], controller: "setup" resource :profile, only: %i[edit update] + resources :certificates, path: "profile/certificates", controller: "user_certificates", only: %i[create update destroy] resource :air_trail_connection, path: "profile/airtrail", only: %i[show create update destroy] do post :test post :sync diff --git a/db/migrate/20261107230000_create_user_certificates.rb b/db/migrate/20261107230000_create_user_certificates.rb new file mode 100644 index 0000000..55640a2 --- /dev/null +++ b/db/migrate/20261107230000_create_user_certificates.rb @@ -0,0 +1,11 @@ +class CreateUserCertificates < ActiveRecord::Migration[8.1] + def change + create_table :user_certificates do |t| + t.references :user, null: false, foreign_key: true + t.string :kind, null: false + t.string :number, null: false + t.date :expires_on + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 42eaf5e..2d5341e 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[8.1].define(version: 2026_11_07_220000) do +ActiveRecord::Schema[8.1].define(version: 2026_11_07_230000) do create_table "active_storage_attachments", force: :cascade do |t| t.string "name", null: false t.string "record_type", null: false @@ -583,6 +583,16 @@ ActiveRecord::Schema[8.1].define(version: 2026_11_07_220000) do t.index ["reported_by_id"], name: "index_squawks_on_reported_by_id" end + create_table "user_certificates", force: :cascade do |t| + t.integer "user_id", null: false + t.string "kind", null: false + t.string "number", null: false + t.date "expires_on" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["user_id"], name: "index_user_certificates_on_user_id" + end + create_table "users", force: :cascade do |t| t.string "email_address", null: false t.string "password_digest", null: false @@ -672,6 +682,7 @@ ActiveRecord::Schema[8.1].define(version: 2026_11_07_220000) do add_foreign_key "squawks", "components", on_delete: :nullify add_foreign_key "squawks", "logbook_entries", on_delete: :nullify add_foreign_key "squawks", "users", column: "reported_by_id", on_delete: :nullify + add_foreign_key "user_certificates", "users" add_foreign_key "weight_balance_records", "aircraft" add_foreign_key "weight_balance_records", "documents", on_delete: :nullify add_foreign_key "weight_balance_records", "major_records", on_delete: :nullify diff --git a/test/integration/user_certificates_test.rb b/test/integration/user_certificates_test.rb new file mode 100644 index 0000000..693a24c --- /dev/null +++ b/test/integration/user_certificates_test.rb @@ -0,0 +1,70 @@ +require "test_helper" + +class UserCertificatesTest < ActionDispatch::IntegrationTest + include ActionMailer::TestHelper + + setup do + @owner = create_user("owner@example.com") + @owner.update!(name: "Sam Mechanic") + @a = create_aircraft(owner: @owner, tail_number: "N12345") + end + + test "users add and remove their own certificates" do + sign_in @owner + get edit_profile_path + assert_select "#certificates select[name='user_certificate[kind]'] option[value=IA]" + post certificates_path, params: { user_certificate: { kind: "AP", number: " 7654321 " } } + post certificates_path, params: { user_certificate: { kind: "IA", number: "7654321", expires_on: "2027-03-31" } } + post certificates_path, params: { user_certificate: { kind: "BOGUS", number: "" } } + assert_match "Certificate not added", flash[:alert] + assert_equal [ [ "IA", "7654321" ], [ "AP", "7654321" ] ], @owner.certificates.reload.map { [ _1.kind, _1.number ] } # IA first + + other = create_user("other@example.com") + theirs = other.certificates.create!(kind: "PILOT", number: "1111111") + delete certificate_path(theirs) + assert_response :not_found + delete certificate_path(@owner.certificates.find_by!(kind: "AP")) + assert_equal [ "IA" ], @owner.certificates.reload.map(&:kind) + + sign_in create_user("admin@example.com", admin: true) + get admin_users_path + assert_includes response.body, "IA 7654321" + end + + test "Sign as fills the logbook entry and squawk resolution from the user's certificates" do + @owner.certificates.create!(kind: "AP", number: "7654321") + @owner.certificates.create!(kind: "IA", number: "7654321", expires_on: 1.day.ago.to_date) # expired: not offered + sign_in @owner + get new_aircraft_logbook_entry_path(@a) + assert_select "form[data-controller='entry-template signer']" + assert_select "select[data-signer-target=choice] option[data-kind=AP][data-number='7654321'][data-name='Sam Mechanic']" + assert_select "select[data-signer-target=choice] option[data-kind=IA]", 0 + assert_select "input[name='logbook_entry[signoff_person]'][data-signer-target=name]" + assert_select "select[name='logbook_entry[signoff_certificate_type]'][data-signer-target=kind][data-entry-template-target=certificate]" + + squawk = @a.squawks.create!(priority: 1, issue_reported: "Nav light out") + get resolution_aircraft_squawk_path(@a, squawk) + assert_select "[data-controller=signer] select[data-signer-target=choice]" + get deferral_aircraft_squawk_path(@a, squawk) + assert_select "select[name=deferred_by_role] option[selected][value=mechanic]" + + nobody = create_user + @a.roles.create!(user: nobody, role: "owner") + sign_in nobody + get new_aircraft_logbook_entry_path(@a) + assert_select "select[data-signer-target=choice]", 0 # no certificates, no picker + end + + test "an expiring certificate is in the weekly reminder, even with nothing else due" do + @owner.certificates.create!(kind: "IA", number: "7654321", expires_on: 30.days.from_now.to_date) + @owner.certificates.create!(kind: "PILOT", number: "1111111", expires_on: 2.years.ago.to_date) # long lapsed: not repeated + assert_emails 1 do + WeeklyReminderJob.perform_now + end + mail = ActionMailer::Base.deliveries.last + assert_equal "Certificate expiring", mail.subject + body = mail.text_part.body.to_s + assert_includes body, "A&P with Inspection Authorization 7654321" + refute_includes body, "1111111" + end +end