From 8bea2abf7502ce00f3355989fb799f179744300c Mon Sep 17 00:00:00 2001 From: Rick Barrette Date: Wed, 11 Nov 2026 20:56:20 -0500 Subject: [PATCH] AD baseline: record DRS imports and the models they covered; weekly search starts from the last import; reminders and audit check Co-Authored-By: Claude Opus 5.5 --- app/controllers/ad_imports_controller.rb | 3 ++ app/mailers/reminder_mailer.rb | 17 ++++++++- app/models/aircraft.rb | 1 + app/models/drs_import.rb | 37 +++++++++++++++++++ app/services/ad_watch.rb | 11 +++++- app/services/records_audit.rb | 7 ++++ .../airworthiness_directives/index.html.erb | 21 +++++++++++ app/views/reminder_mailer/weekly.html.erb | 2 + app/views/reminder_mailer/weekly.text.erb | 7 ++++ .../20261111010000_create_drs_imports.rb | 16 ++++++++ db/schema.rb | 18 ++++++++- docs/guide/ads.md | 4 ++ test/integration/ad_import_flow_test.rb | 29 +++++++++++++++ 13 files changed, 169 insertions(+), 4 deletions(-) create mode 100644 app/models/drs_import.rb create mode 100644 db/migrate/20261111010000_create_drs_imports.rb diff --git a/app/controllers/ad_imports_controller.rb b/app/controllers/ad_imports_controller.rb index e92e79c..b716bba 100644 --- a/app/controllers/ad_imports_controller.rb +++ b/app/controllers/ad_imports_controller.rb @@ -29,6 +29,9 @@ class AdImportsController < ApplicationController # Only AD numbers present in the uploaded file; values are checked against AdImport::ACTIONS. decisions = params.fetch(:decisions, {}).permit(*import.rows.map(&:number)).to_h result = import.import!(decisions: decisions, user: current_user) + @aircraft.drs_imports.create!(user: current_user, filename: params[:filename].to_s.first(200), ad_count: import.rows.size, + tracked: result[:tracked], not_applicable: result[:not_applicable], + models_covered: DrsImport.covered(@aircraft, import.rows.flat_map(&:models))) discard_upload(token) redirect_to aircraft_airworthiness_directives_path(@aircraft), notice: "Imported #{result[:tracked]} ADs to track and #{result[:not_applicable]} marked N/A; " \ diff --git a/app/mailers/reminder_mailer.rb b/app/mailers/reminder_mailer.rb index 7d20eff..466a32b 100644 --- a/app/mailers/reminder_mailer.rb +++ b/app/mailers/reminder_mailer.rb @@ -10,16 +10,29 @@ class ReminderMailer < ApplicationMailer def self.audit_due_for?(user) = user.admin? && AuditRun.due_soon? - def self.anything_for?(user) = items_for(user).any? || expiring_certificates_for(user).any? || audit_due_for?(user) + # 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) @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? ? "#{overdue} #{'item'.pluralize(overdue)} overdue" : "Maintenance coming due" - subject = (@certificates.any? ? "Certificate expiring" : "Records audit due") if @items.empty? + subject = (@certificates.any? ? "Certificate expiring" : (@audit_due ? "Records audit due" : "Refresh the AD list from DRS")) 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/aircraft.rb b/app/models/aircraft.rb index 060317c..d1f6fc8 100644 --- a/app/models/aircraft.rb +++ b/app/models/aircraft.rb @@ -21,6 +21,7 @@ class Aircraft < ApplicationRecord has_many :documents, dependent: :destroy has_many :inspection_records, dependent: :destroy has_many :ad_compliances, dependent: :destroy + has_many :drs_imports, dependent: :delete_all has_many :major_records, dependent: :destroy has_many :flight_logs, dependent: :destroy has_many :consumable_records, dependent: :destroy diff --git a/app/models/drs_import.rb b/app/models/drs_import.rb new file mode 100644 index 0000000..1330364 --- /dev/null +++ b/app/models/drs_import.rb @@ -0,0 +1,37 @@ +# One import of ADs from an FAA DRS export, and which of the aircraft's +# models it covered. Together they're the AD baseline; the weekly +# Federal Register search covers what's published after. +class DrsImport < ApplicationRecord + belongs_to :aircraft + belongs_to :user, optional: true + + RENEW_AFTER = 12.months + + scope :latest_first, -> { order(created_at: :desc, id: :desc) } + + def self.normalize(model) = model.to_s.upcase.gsub(/[\s\-]/, "") + + # The aircraft's own models worth a DRS search: airframe, engines, propellers. + def self.models_for(aircraft) + ([ aircraft.model ] + aircraft.components.in_use.includes(:component_type) + .select { _1.component_type&.name.to_s.match?(AdWatch::ENGINE_OR_PROP) }.map(&:model)) + .map { _1.to_s.strip }.compact_blank.uniq + end + + # Which of the aircraft's models appear in the file's model lists ("O-200A" matches "O-200-A"). + def self.covered(aircraft, file_models) + listed = file_models.map { normalize(_1) }.to_set + models_for(aircraft).select { listed.include?(normalize(_1)) } + end + + # Models never covered by an import. + def self.missing_models(aircraft) + done = aircraft.drs_imports.flat_map { Array(_1.models_covered) }.map { normalize(_1) }.to_set + models_for(aircraft).reject { done.include?(normalize(_1)) } + end + + def self.renewal_due?(aircraft) + latest = aircraft.drs_imports.latest_first.first + latest.nil? ? AirworthinessDirective.applicable_to(aircraft).exists? || aircraft.components.any? : latest.created_at < RENEW_AFTER.ago + end +end diff --git a/app/services/ad_watch.rb b/app/services/ad_watch.rb index de39ebc..42ef3cf 100644 --- a/app/services/ad_watch.rb +++ b/app/services/ad_watch.rb @@ -92,13 +92,22 @@ class AdWatch # Returns the new AdAlerts. def check! - since = (@aircraft.ad_watch_checked_at || FIRST_LOOKBACK.ago).to_date - 7 # overlap; duplicates are skipped + since = start_date - 7 # overlap; duplicates are skipped known = AirworthinessDirective.pluck(:name).to_set created = terms.flat_map { |term| search(term.words, since).filter_map { record(_1, term, known) } } @aircraft.update_column(:ad_watch_checked_at, Time.current) created end + # Where the search picks up: the last check; before the first one, the + # last DRS import (the baseline, less 60 days for ADs that hadn't reached + # DRS yet), else two years back. + def start_date + return @aircraft.ad_watch_checked_at.to_date if @aircraft.ad_watch_checked_at + imported = @aircraft.drs_imports.maximum(:created_at) + imported ? (imported - 60.days).to_date : FIRST_LOOKBACK.ago.to_date + end + private def record(doc, term, known) diff --git a/app/services/records_audit.rb b/app/services/records_audit.rb index 81b57f1..8391948 100644 --- a/app/services/records_audit.rb +++ b/app/services/records_audit.rb @@ -29,6 +29,7 @@ class RecordsAudit backup.present? && backup.ok && backup.created_at > 2.days.ago, backup ? "#{backup.created_at.to_date}: #{backup.message}" : "Never run"), *offsite_items, + drs_item, Item.new("procedures", "The procedures document is adopted and its details filled in", true, revision.present? && @policy.complete?, revision ? "Revision #{revision.number}, adopted #{revision.adopted_at.to_date}" : "Not adopted yet"), @@ -68,6 +69,12 @@ class RecordsAudit private + def drs_item + due = Aircraft.ordered.select { DrsImport.renewal_due?(_1) || DrsImport.missing_models(_1).any? } + Item.new("drs", "Each aircraft's AD list was refreshed from FAA DRS in the last 12 months, for all its models", true, due.empty?, + due.any? ? "Needs a DRS import: #{due.map(&:tail_number).join(', ')}" : "Up to date") + end + def offsite_items return [] unless OffsiteConnection.connected? run = OffsiteRun.latest_backup diff --git a/app/views/airworthiness_directives/index.html.erb b/app/views/airworthiness_directives/index.html.erb index 31ae374..9bb403c 100644 --- a/app/views/airworthiness_directives/index.html.erb +++ b/app/views/airworthiness_directives/index.html.erb @@ -51,6 +51,27 @@ <% end %> +<% last_drs = @aircraft.drs_imports.latest_first.first %> +<% missing = DrsImport.missing_models(@aircraft) %> +
+
+
+

AD baseline from FAA DRS

+

The full list comes from DRS imports; the weekly search below adds what's published after.

+
+ <% if owner? %><%= link_to "Import from FAA DRS", new_aircraft_ad_import_path(@aircraft), class: "btn-secondary btn-sm" %><% end %> +
+
+ <% if last_drs %> +

Last import <%= last_drs.created_at.to_date %> by <%= last_drs.user&.display_name || "—" %>: <%= last_drs.ad_count %> ADs<%= ", covering #{Array(last_drs.models_covered).to_sentence}" if Array(last_drs.models_covered).any? %>.

+ <% if last_drs.created_at < DrsImport::RENEW_AFTER.ago %>

Over a year old: import again to pick up revised ADs and anything the weekly search missed.

<% end %> + <% else %> +

No DRS import yet. Export the ADs for each model from drs.faa.gov and import them here.

+ <% end %> + <% if missing.any? %>

Never imported for: <%= missing.join(", ") %>. Search DRS for each and import the file.

<% end %> +
+
+
diff --git a/app/views/reminder_mailer/weekly.html.erb b/app/views/reminder_mailer/weekly.html.erb index 8af7a12..7ad26da 100644 --- a/app/views/reminder_mailer/weekly.html.erb +++ b/app/views/reminder_mailer/weekly.html.erb @@ -18,6 +18,8 @@ <% @certificates.each do |c| %>
  • <%= c.expired? ? "Expired" : "Expires" %> <%= c.expires_on %>: <%= c.kind_label %> <%= c.number %>
  • <% end %> <% end %> +<% if @drs_due.any? %>

    Refresh the AD list from FAA DRS (new ADs are found weekly, but revisions and anything missed come from a fresh import):

    +
      <% @drs_due.each do |a| %>
    • <%= link_to a.tail_number, aircraft_airworthiness_directives_url(a) %>: <%= (m = DrsImport.missing_models(a)).any? ? "never imported for #{m.to_sentence}" : "last import #{a.drs_imports.latest_first.first&.created_at&.to_date || 'never'}" %>
    • <% end %>
    <% end %> <% if @audit_due %>

    Records audit due <%= @audit_due %> (AC 120-78B quality control): <%= link_to "start it", new_admin_audit_url %>.

    <% 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 abbb1bd..d2ace32 100644 --- a/app/views/reminder_mailer/weekly.text.erb +++ b/app/views/reminder_mailer/weekly.text.erb @@ -16,6 +16,13 @@ Your certificates: - <%= c.expired? ? "Expired" : "Expires" %> <%= c.expires_on %>: <%= c.kind_label %> <%= c.number %> <% end %> <% end %> +<% if @drs_due.any? %> + +AD list from FAA DRS needs a refresh: +<% @drs_due.each do |a| %> +- <%= a.tail_number %>: <%= (m = DrsImport.missing_models(a)).any? ? "never imported for #{m.to_sentence}" : "last import #{a.drs_imports.latest_first.first&.created_at&.to_date || 'never'}" %> (<%= aircraft_airworthiness_directives_url(a) %>) +<% end %> +<% end %> <% if @audit_due %> Records audit due <%= @audit_due %>: <%= new_admin_audit_url %> diff --git a/db/migrate/20261111010000_create_drs_imports.rb b/db/migrate/20261111010000_create_drs_imports.rb new file mode 100644 index 0000000..9426842 --- /dev/null +++ b/db/migrate/20261111010000_create_drs_imports.rb @@ -0,0 +1,16 @@ +class CreateDrsImports < ActiveRecord::Migration[8.1] + def change + # Each FAA DRS import: when, by whom, and which of the aircraft's models + # the file covered (the baseline the weekly new-AD search builds on). + create_table :drs_imports do |t| + t.references :aircraft, null: false, foreign_key: { on_delete: :cascade } + t.references :user, foreign_key: { on_delete: :nullify } + t.string :filename + t.json :models_covered + t.integer :ad_count, null: false, default: 0 + t.integer :tracked, null: false, default: 0 + t.integer :not_applicable, null: false, default: 0 + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 86633f1..99a8643 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_09_010000) do +ActiveRecord::Schema[8.1].define(version: 2026_11_11_010000) do create_table "active_storage_attachments", force: :cascade do |t| t.string "name", null: false t.string "record_type", null: false @@ -407,6 +407,20 @@ ActiveRecord::Schema[8.1].define(version: 2026_11_09_010000) do t.index ["document_collection_id"], name: "index_documents_on_document_collection_id" end + create_table "drs_imports", force: :cascade do |t| + t.integer "aircraft_id", null: false + t.integer "user_id" + t.string "filename" + t.json "models_covered" + t.integer "ad_count", default: 0, null: false + t.integer "tracked", default: 0, null: false + t.integer "not_applicable", default: 0, null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["aircraft_id"], name: "index_drs_imports_on_aircraft_id" + t.index ["user_id"], name: "index_drs_imports_on_user_id" + end + create_table "entry_signatures", force: :cascade do |t| t.integer "logbook_entry_id", null: false t.integer "user_id" @@ -854,6 +868,8 @@ ActiveRecord::Schema[8.1].define(version: 2026_11_09_010000) do add_foreign_key "document_collections", "aircraft" add_foreign_key "documents", "aircraft" add_foreign_key "documents", "document_collections" + add_foreign_key "drs_imports", "aircraft", on_delete: :cascade + add_foreign_key "drs_imports", "users", on_delete: :nullify add_foreign_key "entry_signatures", "logbook_entries" add_foreign_key "entry_signatures", "users", on_delete: :nullify add_foreign_key "flight_logs", "aircraft" diff --git a/docs/guide/ads.md b/docs/guide/ads.md index 9d9b9c9..b15216c 100644 --- a/docs/guide/ads.md +++ b/docs/guide/ads.md @@ -8,6 +8,10 @@ The **ADs** tab lists the ADs and bulletins that apply to the aircraft or its co - **Link existing**: ADs are shared between aircraft; link one already entered for another aircraft. - **Import from FAA DRS**: export the ADs for the aircraft, engine and propeller from the FAA's Dynamic Regulatory System, preferably as `.xlsx` so each AD keeps its DRS link, and upload it. The preview suggests **Track**, **Not applicable** or **Skip** for each AD and flags superseded ADs, model mismatches, appliance and STC-only ADs, and recurring actions. Nothing is duplicated. +## The AD baseline + +The full list of ADs comes from DRS imports; the weekly search (below) adds what's published afterwards. The **AD baseline** panel on the ADs tab shows the last import, which of the aircraft's models it covered, and any model (airframe, engine or propeller) that was never imported. Import again at least once a year to pick up revised ADs: the weekly email and the records audit remind you when it's due. + ## Newly published ADs The app searches the Federal Register weekly for new ADs mentioning the aircraft make and model, the engine and propeller models (and their families, e.g. O-200 for an O-200A), and every other installed component by its maker, type and model, such as magnetos, the ELT and radios. ADs often name a series rather than the exact model, so "Bendix" + "magneto" is searched as well as the model. You can add your own terms (an STC, a part that isn't a component); they're searched as well. Tracking an AD found through a component makes it apply to that component. **Check now** runs it straight away. For each match, **Track** it or **Dismiss** it. Matches are only *possibly* applicable: check the AD's applicability and serial number ranges first. diff --git a/test/integration/ad_import_flow_test.rb b/test/integration/ad_import_flow_test.rb index 4241976..860a69e 100644 --- a/test/integration/ad_import_flow_test.rb +++ b/test/integration/ad_import_flow_test.rb @@ -27,6 +27,8 @@ class AdImportFlowTest < ActionDispatch::IntegrationTest } assert_redirected_to aircraft_airworthiness_directives_path(@aircraft) assert_equal %w[2008-22-18 2011-10-09], @aircraft.applicable_ads.order(:name).pluck(:name) + drs = @aircraft.drs_imports.sole # the AD baseline is recorded + assert_equal [ [ "150L" ], @owner, "AD Final Rules.csv" ], [ drs.models_covered, drs.user, drs.filename ] follow_redirect! assert_select "span", text: "N/A" # CSV exports carry no links, so the AD number isn't a link @@ -51,4 +53,31 @@ class AdImportFlowTest < ActionDispatch::IntegrationTest assert_response :redirect assert_equal 0, @aircraft.applicable_ads.count end + + test "the AD baseline: missing models, renewal, where the weekly search starts, reminders and the audit" do + create_component(@aircraft, component_type: component_type("Engine"), model: "O-200A") + assert_equal %w[150L O-200A], DrsImport.missing_models(@aircraft) + assert DrsImport.renewal_due?(@aircraft) + assert_equal 2.years.ago.to_date, AdWatch.new(@aircraft).start_date + + travel_to Time.zone.local(2026, 11, 2) do + @aircraft.drs_imports.create!(user: @owner, models_covered: [ "150L" ]) + @aircraft.drs_imports.create!(user: @owner, models_covered: [ "O-200-A" ]) # matches O-200A + end + assert_empty DrsImport.missing_models(@aircraft) + travel_to Time.zone.local(2026, 11, 20) do + refute DrsImport.renewal_due?(@aircraft) + assert_equal Date.new(2026, 9, 3), AdWatch.new(@aircraft).start_date # last import less 60 days + sign_in @owner + get aircraft_airworthiness_directives_path(@aircraft) + assert_includes response.body, "Last import 2026-11-02" + end + travel_to Time.zone.local(2027, 12, 1) do # over a year: the first Monday-of-month email and the audit flag it + assert DrsImport.renewal_due?(@aircraft) + assert ReminderMailer.anything_for?(@owner, today: Date.new(2027, 12, 6)) + refute ReminderMailer.anything_for?(@owner, today: Date.new(2027, 12, 13)) + assert_includes ReminderMailer.weekly(@owner).text_part.body.to_s, "needs a refresh" + refute RecordsAudit.new.auto_items.find { _1.key == "drs" }.passed + end + end end