mirror of
https://github.com/rickbarrette/simple-aircraft-manager-rails.git
synced 2026-11-17 15:50:36 -05:00
AirTrail (https://github.com/johanohly/AirTrail): - File export (Flights → Export for AirTrail) in AirTrail's import format, validated against its schema; same-day repeat legs get leg numbers so AirTrail's de-duplication doesn't merge them. - Automatic sync through AirTrail's REST API (Profile → AirTrail sync): per-user connection with an encrypted API key and scope check; flights are created/updated/deleted in background jobs; a full sync first links flights already in AirTrail (e.g. from a file import) instead of duplicating them; airports are resolved and cached, with manual mapping for unknown codes. - GPS tracks: GPX, KML and CSV track logs are converted to AirTrail's track format for both sync and export; the flight page shows a summary. - Aircraft ICAO type (e.g. C150) for AirTrail's aircraft type. Airport database (OurAirports, the source AirTrail uses): - Managed like AirTrail: downloaded in the background on first start, updated in place from Admin → Airports (adds, changes, removes; ids stable), with admin-managed custom airports that updates never touch. - Each airport carries AirTrail's code, so logged codes translate to AirTrail's exactly (1B9 → K1B9, 4MB → K5M3). - From/To autocomplete on new flights (airports this aircraft has used rank first); From is pre-filled with the last flight's destination; codes are normalized to uppercase. - Flights → Standardize airport codes: rewrite logged codes to the standard code, and fix codes not in the database (typos such as 0R8 → 08R are suggested). Also: admin pages grouped under an Admin dropdown menu. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
95 lines
3.5 KiB
Ruby
95 lines
3.5 KiB
Ruby
# The signed-in user's AirTrail sync settings (Profile → AirTrail).
|
|
class AirTrailConnectionsController < ApplicationController
|
|
before_action :set_connection, except: %i[show create]
|
|
|
|
def show
|
|
@connection = current_user.air_trail_connection || current_user.build_air_trail_connection
|
|
return unless @connection.persisted?
|
|
@counts = @connection.counts
|
|
@failed = @connection.syncs.failed.includes(flight_log: :aircraft).order(updated_at: :desc).limit(20)
|
|
@unresolved = @connection.unresolved_airport_codes
|
|
@flight_count = FlightLog.where(user: current_user).count
|
|
end
|
|
|
|
def create
|
|
@connection = current_user.build_air_trail_connection(connection_params)
|
|
save_and_verify
|
|
end
|
|
|
|
def update
|
|
attrs = connection_params
|
|
attrs.delete(:api_key) if attrs[:api_key].blank? # keep the stored key
|
|
url_changed = attrs[:base_url].present? && attrs[:base_url].chomp("/") != @connection.base_url
|
|
@connection.assign_attributes(attrs)
|
|
@connection.remote_user_id = nil if url_changed || attrs.key?(:api_key) # re-identify
|
|
save_and_verify
|
|
end
|
|
|
|
def test
|
|
verify
|
|
redirect_to air_trail_connection_path
|
|
end
|
|
|
|
# Link existing AirTrail flights and push everything else, in the background.
|
|
def sync
|
|
AirTrailBackfillJob.perform_later(@connection.id)
|
|
redirect_to air_trail_connection_path, notice: "Sync started. Refresh this page in a minute to see progress."
|
|
end
|
|
|
|
def map_airport
|
|
code = params[:code].to_s.strip.upcase
|
|
target = params[:airtrail_code].to_s.strip.upcase
|
|
match = target.present? && AirTrailSyncer.new(@connection).map_airport(code, target)
|
|
if match
|
|
AirTrailBackfillJob.perform_later(@connection.id) # retry flights that needed it
|
|
redirect_to air_trail_connection_path, notice: "#{code} mapped to #{match['icao']} (#{match['name']}). Retrying affected flights."
|
|
else
|
|
redirect_to air_trail_connection_path, alert: "AirTrail has no airport matching “#{target}”."
|
|
end
|
|
rescue AirTrailClient::Error => e
|
|
redirect_to air_trail_connection_path, alert: e.message
|
|
end
|
|
|
|
# Stops syncing. Flights already in AirTrail are left there.
|
|
def destroy
|
|
@connection.destroy!
|
|
redirect_to air_trail_connection_path, notice: "AirTrail disconnected. Flights already in AirTrail were left as they are."
|
|
end
|
|
|
|
private
|
|
|
|
def set_connection
|
|
@connection = current_user.air_trail_connection or redirect_to(air_trail_connection_path)
|
|
end
|
|
|
|
def connection_params
|
|
params.require(:air_trail_connection).permit(:base_url, :api_key, :enabled)
|
|
end
|
|
|
|
def save_and_verify
|
|
if @connection.save
|
|
verify
|
|
redirect_to air_trail_connection_path
|
|
else
|
|
render :show, status: :unprocessable_entity
|
|
end
|
|
end
|
|
|
|
def verify
|
|
me = @connection.client.me
|
|
scopes = Array(me.dig("credential", "scopes"))
|
|
missing = me.dig("credential", "kind") == "apiKey" ? AirTrailConnection::REQUIRED_SCOPES - scopes : []
|
|
@connection.update!(remote_user_id: me["id"], remote_username: me["username"], checked_at: Time.current,
|
|
status: missing.empty? ? "ok" : "error",
|
|
last_error: missing.empty? ? nil : "API key is missing scopes: #{missing.join(', ')}")
|
|
if missing.empty?
|
|
flash[:notice] = "Connected to AirTrail as #{me['username']}."
|
|
else
|
|
flash[:alert] = @connection.last_error
|
|
end
|
|
rescue AirTrailClient::Error => e
|
|
@connection.update!(status: "error", last_error: e.message, checked_at: Time.current)
|
|
flash[:alert] = e.message
|
|
end
|
|
end
|