@@ -4,6 +4,7 @@ import { validateSessionAccess } from '../middleware/sessionAccess.js';
44import { updateSession , getAllSessions , getSessionById } from '../db/sessions.js' ;
55import { getArrivalsIO } from './arrivalsWebsocket.js' ;
66import { handleFlightStatusChange } from '../services/logbookStatusHandler.js' ;
7+ import flightsPool from '../db/connections/flightsConnection.js' ;
78
89let io ;
910const updateTimers = new Map ( ) ;
@@ -21,16 +22,11 @@ export function setupFlightsWebsocket(httpServer) {
2122 const sessionId = socket . handshake . query . sessionId ;
2223 const accessId = socket . handshake . query . accessId ;
2324
24- // Basic session check
2525 if ( ! sessionId ) {
2626 socket . disconnect ( true ) ;
2727 return ;
2828 }
2929
30- // Determine role:
31- // - If an accessId is provided and validates -> controller (full privileges)
32- // - If no accessId provided -> pilot (limited privileges: can listen & addFlight)
33- // This avoids giving pilots controller access while still allowing them to receive events.
3430 let role = 'pilot' ;
3531 if ( accessId ) {
3632 const valid = await validateSessionAccess ( sessionId , accessId ) ;
@@ -45,20 +41,13 @@ export function setupFlightsWebsocket(httpServer) {
4541 socket . join ( sessionId ) ;
4642
4743 socket . on ( 'updateFlight' , async ( { flightId, updates } ) => {
48- // restrict updates from pilots (controllers only)
4944 if ( socket . data . role !== 'controller' ) {
5045 socket . emit ( 'flightError' , { action : 'update' , flightId, error : 'Not authorized' } ) ;
5146 return ;
5247 }
5348 try {
54- // Log all updates to see what's being received
55- if ( updates . status || updates . callsign ) {
56- console . log ( `[FlightWS] Received update for ${ updates . callsign || flightId } :` , JSON . stringify ( updates ) ) ;
57- }
58-
59- // Handle local hide/unhide - don't process these on server
6049 if ( updates . hasOwnProperty ( 'hidden' ) ) {
61- return ; // Ignore hidden field updates
50+ return ;
6251 }
6352
6453 if ( updates . callsign && updates . callsign . length > 16 ) {
@@ -83,10 +72,7 @@ export function setupFlightsWebsocket(httpServer) {
8372
8473 await broadcastToArrivalSessions ( updatedFlight ) ;
8574
86- // Handle logbook status changes
8775 if ( updates . status && updatedFlight . callsign ) {
88- console . log ( `[FlightWS] Detected status change: ${ updatedFlight . callsign } -> ${ updates . status } ` ) ;
89- // Get session's airport to determine origin vs destination
9076 const session = await getSessionById ( sessionId ) ;
9177 const controllerAirport = session ?. airport_icao || null ;
9278 await handleFlightStatusChange ( updatedFlight . callsign , updates . status , controllerAirport ) ;
@@ -104,12 +90,10 @@ export function setupFlightsWebsocket(httpServer) {
10490 await updateFlight ( sessionId , flightId , updates ) ;
10591 updateTimers . delete ( timerKey ) ;
10692 } catch ( error ) {
107- console . error ( 'Error saving flight update to DB:' , error ) ;
10893 }
10994 } , 1000 ) ) ;
11095
11196 } catch ( error ) {
112- console . error ( 'Error updating flight via websocket:' , error ) ;
11397 socket . emit ( 'flightError' , { action : 'update' , flightId, error : 'Failed to update flight' } ) ;
11498 }
11599 } ) ;
@@ -124,17 +108,18 @@ export function setupFlightsWebsocket(httpServer) {
124108
125109 const flight = await addFlight ( sessionId , enhancedFlightData ) ;
126110
127- io . to ( sessionId ) . emit ( 'flightAdded' , flight ) ;
111+ socket . emit ( 'flightAdded' , flight ) ;
128112
129- await broadcastToArrivalSessions ( flight ) ;
113+ const { acars_token, user_id, ip_address, ...sanitizedFlight } = flight ;
114+ socket . to ( sessionId ) . emit ( 'flightAdded' , sanitizedFlight ) ;
115+
116+ await broadcastToArrivalSessions ( sanitizedFlight ) ;
130117 } catch ( error ) {
131- console . error ( 'Error adding flight via websocket:' , error ) ;
132118 socket . emit ( 'flightError' , { action : 'add' , error : 'Failed to add flight' } ) ;
133119 }
134120 } ) ;
135121
136122 socket . on ( 'deleteFlight' , async ( flightId ) => {
137- // controllers only
138123 if ( socket . data . role !== 'controller' ) {
139124 socket . emit ( 'flightError' , { action : 'delete' , flightId, error : 'Not authorized' } ) ;
140125 return ;
@@ -143,13 +128,11 @@ export function setupFlightsWebsocket(httpServer) {
143128 await deleteFlight ( sessionId , flightId ) ;
144129 io . to ( sessionId ) . emit ( 'flightDeleted' , { flightId } ) ;
145130 } catch ( error ) {
146- console . error ( 'Error deleting flight via websocket:' , error ) ;
147131 socket . emit ( 'flightError' , { action : 'delete' , flightId, error : 'Failed to delete flight' } ) ;
148132 }
149133 } ) ;
150134
151135 socket . on ( 'updateSession' , async ( updates ) => {
152- // controllers only
153136 if ( socket . data . role !== 'controller' ) {
154137 socket . emit ( 'sessionError' , { error : 'Not authorized' } ) ;
155138 return ;
@@ -164,14 +147,11 @@ export function setupFlightsWebsocket(httpServer) {
164147 socket . emit ( 'sessionError' , { error : 'Session not found or update failed' } ) ;
165148 }
166149 } catch ( error ) {
167- console . error ( 'Error updating session via websocket:' , error ) ;
168150 socket . emit ( 'sessionError' , { error : 'Failed to update session' } ) ;
169151 }
170152 } ) ;
171153
172- // NEW: controller issues a PDC for a flight -> persist & broadcast
173154 socket . on ( 'issuePDC' , async ( { flightId, pdcText, targetPilotUserId } ) => {
174- // controllers only
175155 if ( socket . data . role !== 'controller' ) {
176156 socket . emit ( 'flightError' , { action : 'issuePDC' , flightId, error : 'Not authorized' } ) ;
177157 return ;
@@ -181,8 +161,6 @@ export function setupFlightsWebsocket(httpServer) {
181161 socket . emit ( 'flightError' , { action : 'issuePDC' , error : 'Missing flightId' } ) ;
182162 return ;
183163 }
184-
185- // Use pdc_remarks (frontend checks this). Avoid writing unknown columns.
186164 const updates = {
187165 pdc_remarks : pdcText
188166 } ;
@@ -197,15 +175,12 @@ export function setupFlightsWebsocket(httpServer) {
197175 socket . emit ( 'flightError' , { action : 'issuePDC' , flightId, error : 'Flight not found' } ) ;
198176 }
199177 } catch ( error ) {
200- console . error ( 'Error issuing PDC via websocket:' , error ) ;
201178 socket . emit ( 'flightError' , { action : 'issuePDC' , flightId, error : 'Failed to issue PDC' } ) ;
202179 }
203180 } ) ;
204181
205- // client requests that controllers issue a PDC for a flight
206182 socket . on ( 'requestPDC' , ( { flightId, callsign, note } ) => {
207183 try {
208- // broadcast to everyone in session (controllers should respond by flashing their flightstrip)
209184 io . to ( sessionId ) . emit ( 'pdcRequest' , {
210185 flightId,
211186 callsign : callsign ?? null ,
@@ -214,24 +189,41 @@ export function setupFlightsWebsocket(httpServer) {
214189 ts : new Date ( ) . toISOString ( )
215190 } ) ;
216191 } catch ( err ) {
217- console . error ( 'Error handling requestPDC:' , err ) ;
218192 socket . emit ( 'flightError' , { action : 'requestPDC' , flightId, error : 'Failed to request PDC' } ) ;
219193 }
220194 } ) ;
221195
222- socket . on ( 'contactMe' , ( { flightId, message } ) => {
196+ socket . on ( 'contactMe' , async ( { flightId, message } ) => {
223197 if ( socket . data . role !== 'controller' ) {
224198 socket . emit ( 'flightError' , { action : 'contactMe' , flightId, error : 'Not authorized' } ) ;
225199 return ;
226200 }
227201 try {
228- io . to ( sessionId ) . emit ( 'contactMe' , {
202+ const allSessions = await getAllSessions ( ) ;
203+ let targetSessionId = sessionId ;
204+
205+ for ( const session of allSessions ) {
206+ try {
207+ const tableName = `flights_${ session . session_id } ` ;
208+ const result = await flightsPool . query (
209+ `SELECT session_id FROM ${ tableName } WHERE id = $1` ,
210+ [ flightId ]
211+ ) ;
212+ if ( result . rows . length > 0 ) {
213+ targetSessionId = session . session_id ;
214+ break ;
215+ }
216+ } catch ( err ) {
217+ continue ;
218+ }
219+ }
220+
221+ io . to ( targetSessionId ) . emit ( 'contactMe' , {
229222 flightId,
230223 message : message || 'CONTACT CONTROLLER ON FREQUENCY' ,
231224 ts : new Date ( ) . toISOString ( )
232225 } ) ;
233226 } catch ( err ) {
234- console . error ( 'Error handling contactMe:' , err ) ;
235227 socket . emit ( 'flightError' , { action : 'contactMe' , flightId, error : 'Failed to send contact message' } ) ;
236228 }
237229 } ) ;
@@ -257,7 +249,6 @@ async function broadcastToArrivalSessions(flight) {
257249 }
258250 }
259251 } catch ( error ) {
260- console . error ( 'Error broadcasting to arrival sessions:' , error ) ;
261252 }
262253}
263254
@@ -269,4 +260,4 @@ export function broadcastFlightEvent(sessionId, event, data) {
269260 if ( io ) {
270261 io . to ( sessionId ) . emit ( event , data ) ;
271262 }
272- }
263+ }
0 commit comments