mirror of
https://github.com/rickbarrette/simple-aircraft-manager-rails.git
synced 2026-11-17 23:50:36 -05:00
Certificates on user profiles, with "Sign as"
- Users add their FAA certificates (kind, number, optional expiry) on their profile; admins see them on the Users list - "Sign as" on new/edited logbook entries and the squawk Resolve page fills the signer's name, kind of certificate and number (expired certificates aren't offered); deferrals default to mechanic for users with a mechanic certificate - Weekly reminder lists certificates expiring within 60 days (or lapsed in the last 30), even when nothing else is due - Preventive maintenance templates keep a certificate already chosen Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
5584ad59c5
commit
aa262065b2
21 files changed
+253
-22
No files matched your search
@@ -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}."
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
@@ -4,7 +4,7 @@
|
||||
<thead><tr><th>Name</th><th>Email</th><th>Aircraft</th><th>Last sign-in</th><th></th></tr></thead>
|
||||
<tbody><% @users.each do |u| %><tr>
|
||||
<td><%= u.name %> <% if u.admin? %><span class="badge-blue">Admin</span><% end %><% if u.sso_uid? %> <span class="badge-gray" title="Signs in with <%= Sso.label %>"><%= Sso.label %></span><% end %></td>
|
||||
<td><%= u.email_address %></td>
|
||||
<td><%= u.email_address %><% if u.certificates.any? %><div class="text-xs text-slate-500"><%= u.certificates.map { "#{_1.short_label} #{_1.number}" }.join(", ") %></div><% end %></td>
|
||||
<td class="text-xs"><%= u.aircraft_roles.map { "#{_1.aircraft.tail_number} (#{_1.role})" }.join(", ").presence || "—" %></td>
|
||||
<td class="text-xs text-slate-500"><%= u.last_signed_in_at ? "#{time_ago_in_words(u.last_signed_in_at)} ago" : "never" %></td>
|
||||
<td class="text-right"><%= link_to "Edit", edit_admin_user_path(u), class: "btn-secondary btn-sm" %></td>
|
||||
|
||||
@@ -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? %>
|
||||
<div>
|
||||
@@ -20,11 +20,12 @@
|
||||
</div>
|
||||
<%= f.field :text, as: :textarea, label: "Entry text", rows: 8, data: { entry_template_target: "text" } %>
|
||||
<p class="hint !mt-1">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).</p>
|
||||
<%= render "shared/sign_as" %>
|
||||
<div class="grid gap-4 sm:grid-cols-3">
|
||||
<%= 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" } %>
|
||||
</div>
|
||||
<div class="grid gap-4 sm:grid-cols-2">
|
||||
<%= f.field :inspection_result, as: :select, label: "Inspection result", include_blank: "— Not an inspection —",
|
||||
|
||||
@@ -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" %>
|
||||
<div>
|
||||
@@ -31,6 +31,31 @@
|
||||
</section>
|
||||
<% end %>
|
||||
|
||||
<section class="card card-body mt-5 max-w-md" id="certificates">
|
||||
<h2 class="card-title">Certificates</h2>
|
||||
<p class="mt-1 text-sm text-slate-600">Your FAA certificates, for signing logbook entries: “Sign as” fills in your name, the kind of certificate and its number.</p>
|
||||
<% if @user.certificates.any? %>
|
||||
<ul class="mt-3 divide-y divide-slate-100 rounded-md ring-1 ring-slate-200">
|
||||
<% @user.certificates.each do |c| %>
|
||||
<li class="flex flex-wrap items-center justify-between gap-2 px-3 py-2 text-sm">
|
||||
<span><span class="font-medium"><%= c.kind_label %></span> · <%= c.number %>
|
||||
<% if c.expires_on %><span class="ml-1 <%= c.expired? ? 'badge-red' : (Date.current + UserCertificate::EXPIRY_WARNING_DAYS >= c.expires_on ? 'badge-orange' : 'badge-gray') %>"><%= c.expired? ? "Expired" : "Expires" %> <%= c.expires_on %></span><% end %></span>
|
||||
<%= button_to "Remove", certificate_path(c), method: :delete, class: "btn-secondary btn-sm", form: { data: { turbo_confirm: "Remove your #{c.kind_label} certificate?" } } %>
|
||||
</li>
|
||||
<% end %>
|
||||
</ul>
|
||||
<% 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" %>
|
||||
<div class="sm:col-span-2 flex flex-wrap items-end gap-2">
|
||||
<div><%= f.label :expires_on, "Expires (optional)", class: "label" %><%= f.date_field :expires_on, class: "input" %></div>
|
||||
<%= f.submit "Add certificate", class: "btn-secondary" %>
|
||||
</div>
|
||||
<p class="hint sm:col-span-2">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.</p>
|
||||
<% end %>
|
||||
</section>
|
||||
|
||||
<section class="card card-body mt-5 max-w-md">
|
||||
<h2 class="card-title">Sessions</h2>
|
||||
<% others = @user.sessions.where.not(id: Current.session.id).count %>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
<p>Here's what needs attention on your aircraft.</p>
|
||||
<p>Here's what needs attention<%= " on your aircraft" if @items.any? %>.</p>
|
||||
<% @items.each do |aircraft, issues, alerts| %>
|
||||
<h3 style="margin: 16px 0 4px;"><%= link_to aircraft.tail_number, aircraft_url(aircraft) %> <span style="font-weight: normal; color: #64748b;"><%= aircraft.airworthiness.label %></span></h3>
|
||||
<ul style="margin: 0; padding-left: 20px;">
|
||||
@@ -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") %>.</p>
|
||||
<% end %>
|
||||
<% end %>
|
||||
<% if @certificates.any? %>
|
||||
<h3 style="margin: 16px 0 4px;">Your certificates</h3>
|
||||
<ul style="margin: 0; padding-left: 20px;">
|
||||
<% @certificates.each do |c| %><li><strong><%= c.expired? ? "Expired" : "Expires" %> <%= c.expires_on %>:</strong> <%= c.kind_label %> <%= c.number %></li><% end %>
|
||||
</ul>
|
||||
<% end %>
|
||||
<p style="color: #64748b; font-size: 12px; margin-top: 20px;">
|
||||
Based on the records in Simple Aircraft Manager. Turn these emails off on your <%= link_to "profile", edit_profile_url %>.
|
||||
</p>
|
||||
@@ -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 %>
|
||||
@@ -0,0 +1,14 @@
|
||||
<%# "Sign as" picker for the current user's certificates (signer controller). %>
|
||||
<% certificates = current_user.certificates.reject(&:expired?) %>
|
||||
<% if certificates.any? %>
|
||||
<div class="flex flex-wrap items-center gap-2 text-sm">
|
||||
<%= label_tag :sign_as, "Sign as", class: "text-slate-600" %>
|
||||
<select id="sign_as" class="input w-auto" data-signer-target="choice" data-action="signer#fill">
|
||||
<option value="">— Someone else —</option>
|
||||
<% certificates.each do |c| %>
|
||||
<option value="<%= c.id %>" data-name="<%= current_user.display_name %>" data-kind="<%= c.kind %>" data-number="<%= c.number %>">
|
||||
<%= current_user.display_name %>, <%= c.kind_label %> <%= c.number %></option>
|
||||
<% end %>
|
||||
</select>
|
||||
</div>
|
||||
<% end %>
|
||||
@@ -33,7 +33,8 @@
|
||||
</div>
|
||||
<div>
|
||||
<%= 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" %>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -19,8 +19,9 @@
|
||||
<label class="flex items-center gap-2"><%= radio_button_tag :logbook, "none", choice == "none" %> No logbook entry (e.g. it wasn't a defect)</label>
|
||||
</fieldset>
|
||||
|
||||
<div class="space-y-4 rounded-md border border-slate-200 p-3">
|
||||
<div class="space-y-4 rounded-md border border-slate-200 p-3" data-controller="signer">
|
||||
<p class="text-xs text-slate-500">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.</p>
|
||||
<%= render "shared/sign_as" %>
|
||||
<%= fields_for :logbook_entry, @entry do |f| %>
|
||||
<div class="grid gap-4 sm:grid-cols-3">
|
||||
<%= f.field :date, as: :date %>
|
||||
@@ -29,9 +30,10 @@
|
||||
</div>
|
||||
<%= f.field :text, as: :textarea, label: "Work performed", rows: 3 %>
|
||||
<div class="grid gap-4 sm:grid-cols-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" } %>
|
||||
</div>
|
||||
<% end %>
|
||||
</div>
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
Generated
+12
-1
@@ -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
|
||||
|
||||
@@ -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
|
||||
Reference in new issue
Block a user