1- import { generateSID , generateSquawk , getWakeTurbulence , generateRandomId } from '../utils/flightUtils.js' ;
1+ import {
2+ generateSID ,
3+ generateSquawk ,
4+ getWakeTurbulence ,
5+ generateRandomId ,
6+ } from '../utils/flightUtils.js' ;
27import { getSessionById } from './sessions.js' ;
38import flightsPool from './connections/flightsConnection.js' ;
9+ import crypto from 'crypto' ;
410
511function sanitizeFlightForClient ( flight ) {
6- const { user_id, ip_address, ...sanitizedFlight } = flight ;
12+ const { user_id, ip_address, acars_token , ...sanitizedFlight } = flight ;
713 return {
814 ...sanitizedFlight ,
915 cruisingFL : flight . cruisingfl ,
@@ -17,35 +23,59 @@ export async function getFlightsBySession(sessionId) {
1723 `SELECT * FROM ${ tableName } ORDER BY created_at ASC`
1824 ) ;
1925
20- const flights = result . rows . map ( flight => sanitizeFlightForClient ( flight ) ) ;
26+ const flights = result . rows . map ( ( flight ) =>
27+ sanitizeFlightForClient ( flight )
28+ ) ;
2129 return flights ;
2230}
2331
32+ export async function validateAcarsAccess ( sessionId , flightId , acarsToken ) {
33+ const tableName = `flights_${ sessionId } ` ;
34+ const result = await flightsPool . query (
35+ `SELECT acars_token FROM ${ tableName } WHERE id = $1` ,
36+ [ flightId ]
37+ ) ;
38+
39+ if ( result . rows . length === 0 ) {
40+ return false ;
41+ }
42+
43+ return result . rows [ 0 ] . acars_token === acarsToken ;
44+ }
45+
2446export async function getFlightsBySessionWithTime ( sessionId , hoursBack = 2 ) {
2547 try {
2648 const tableName = `flights_${ sessionId } ` ;
2749
28- const tableExists = await flightsPool . query ( `
50+ const tableExists = await flightsPool . query (
51+ `
2952 SELECT EXISTS (
3053 SELECT FROM information_schema.tables
3154 WHERE table_name = $1
3255 )
33- ` , [ tableName ] ) ;
56+ ` ,
57+ [ tableName ]
58+ ) ;
3459
3560 if ( ! tableExists . rows [ 0 ] . exists ) {
3661 return [ ] ;
3762 }
3863
3964 const result = await flightsPool . query (
4065 `SELECT * FROM ${ tableName }
41- WHERE created_at >= NOW() - INTERVAL '${ hoursBack } hours'
42- ORDER BY created_at ASC`
66+ WHERE created_at >= NOW() - INTERVAL '${ hoursBack } hours'
67+ ORDER BY created_at ASC`
4368 ) ;
4469
45- const flights = result . rows . map ( flight => sanitizeFlightForClient ( flight ) ) ;
70+ const flights = result . rows . map ( ( flight ) =>
71+ sanitizeFlightForClient ( flight )
72+ ) ;
4673 return flights ;
4774 } catch ( error ) {
48- console . error ( `Error fetching flights for session ${ sessionId } :` , error ) ;
75+ console . error (
76+ `Error fetching flights for session ${ sessionId } :` ,
77+ error
78+ ) ;
4979 return [ ] ;
5080 }
5181}
@@ -68,21 +98,27 @@ function validateFlightFields(updates) {
6898 if ( updates . cruisingFL !== undefined ) {
6999 const fl = parseInt ( updates . cruisingFL , 10 ) ;
70100 if ( isNaN ( fl ) || fl < 0 || fl > 200 || fl % 5 !== 0 ) {
71- throw new Error ( 'Cruising FL must be between 0 and 200 in 50-step increments' ) ;
101+ throw new Error (
102+ 'Cruising FL must be between 0 and 200 in 50-step increments'
103+ ) ;
72104 }
73105 }
74106 if ( updates . clearedFL !== undefined ) {
75107 const fl = parseInt ( updates . clearedFL , 10 ) ;
76108 if ( isNaN ( fl ) || fl < 0 || fl > 200 || fl % 5 !== 0 ) {
77- throw new Error ( 'Cleared FL must be between 0 and 200 in 50-step increments' ) ;
109+ throw new Error (
110+ 'Cleared FL must be between 0 and 200 in 50-step increments'
111+ ) ;
78112 }
79113 }
80114}
81115
82116export async function addFlight ( sessionId , flightData ) {
83117 const tableName = `flights_${ sessionId } ` ;
84118 try {
85- await flightsPool . query ( `ALTER TABLE ${ tableName } ADD COLUMN IF NOT EXISTS gate VARCHAR(8);` ) ;
119+ await flightsPool . query (
120+ `ALTER TABLE ${ tableName } ADD COLUMN IF NOT EXISTS gate VARCHAR(8);`
121+ ) ;
86122 } catch ( error ) {
87123 // Column might already exist, continue
88124 }
@@ -98,6 +134,7 @@ export async function addFlight(sessionId, flightData) {
98134 if ( ! flightData . timestamp ) {
99135 flightData . timestamp = new Date ( ) . toISOString ( ) ;
100136 }
137+ flightData . acars_token = crypto . randomBytes ( 4 ) . toString ( 'hex' ) ;
101138
102139 if ( flightData . aircraft_type ) {
103140 flightData . aircraft = flightData . aircraft_type ;
@@ -113,7 +150,10 @@ export async function addFlight(sessionId, flightData) {
113150 flightData . runway = session . active_runway ;
114151 }
115152 } catch ( error ) {
116- console . error ( 'Error fetching session for runway assignment:' , error ) ;
153+ console . error (
154+ 'Error fetching session for runway assignment:' ,
155+ error
156+ ) ;
117157 }
118158 }
119159
@@ -147,20 +187,32 @@ export async function addFlight(sessionId, flightData) {
147187 const result = await flightsPool . query ( query , values ) ;
148188
149189 const flight = result . rows [ 0 ] ;
150- return sanitizeFlightForClient ( flight ) ;
190+ return {
191+ ...flight ,
192+ cruisingFL : flight . cruisingfl ,
193+ clearedFL : flight . clearedfl ,
194+ } ;
151195}
152196
153197export async function updateFlight ( sessionId , flightId , updates ) {
154198 const tableName = `flights_${ sessionId } ` ;
155199
156200 // add this block to create missing text columns safely
157- const safeCols = Object . keys ( updates ) . filter ( k => / ^ [ a - z A - Z 0 - 9 _ ] + $ / . test ( k ) ) ;
201+ const safeCols = Object . keys ( updates ) . filter ( ( k ) =>
202+ / ^ [ a - z A - Z 0 - 9 _ ] + $ / . test ( k )
203+ ) ;
158204 for ( const col of safeCols ) {
159205 try {
160- await flightsPool . query ( `ALTER TABLE ${ tableName } ADD COLUMN IF NOT EXISTS "${ col } " text;` ) ;
206+ await flightsPool . query (
207+ `ALTER TABLE ${ tableName } ADD COLUMN IF NOT EXISTS "${ col } " text;`
208+ ) ;
161209 } catch ( err ) {
162210 // ignore - column creation failure shouldn't stop update
163- console . error ( 'Could not ensure column exists:' , col , err ?. message || err ) ;
211+ console . error (
212+ 'Could not ensure column exists:' ,
213+ col ,
214+ err ?. message || err
215+ ) ;
164216 }
165217 }
166218
@@ -204,5 +256,7 @@ export async function updateFlight(sessionId, flightId, updates) {
204256
205257export async function deleteFlight ( sessionId , flightId ) {
206258 const tableName = `flights_${ sessionId } ` ;
207- await flightsPool . query ( `DELETE FROM ${ tableName } WHERE id = $1` , [ flightId ] ) ;
208- }
259+ await flightsPool . query ( `DELETE FROM ${ tableName } WHERE id = $1` , [
260+ flightId ,
261+ ] ) ;
262+ }
0 commit comments