| 1 | import { prisma } from 'src/db'; |
| 2 | |
| 3 | import { Injectable } from '@nestjs/common'; |
| 4 | |
| 5 | import { ProfileStatsResponseEntity } from './entities/profile-stats.entity'; |
| 6 | import { FlightSummaryEntity } from './entities/profile-stats.entity'; |
| 7 | |
| 8 | @Injectable() |
| 9 | export class ProfileService { |
| 10 | async getProfileStats(userId: string): Promise<ProfileStatsResponseEntity> { |
| 11 | const flights = await prisma.flight.findMany({ |
| 12 | include: { |
| 13 | greatCircleDistance: true, |
| 14 | flightAwareData: true, |
| 15 | }, |
| 16 | where: { userId }, |
| 17 | }); |
| 18 | |
| 19 | if (flights.length === 0) { |
| 20 | return this.getEmptyStats(); |
| 21 | } |
| 22 | |
| 23 | // Calculate most flown aircraft using DB typeName |
| 24 | const aircraftCounts = new Map<string, number>(); |
| 25 | const aircraftTypeToFlights = new Map<string, typeof flights>(); |
| 26 | const regs = new Set<string>(); |
| 27 | const modelCodes = new Set<string>(); |
| 28 | const candidateTypeNames = new Set<string>(); |
| 29 | const tailNumbers = new Map<string, number>(); // Track tail number counts |
| 30 | const tailNumberToAircraft = new Map< |
| 31 | string, |
| 32 | { typeName: string; age?: number; image?: string } |
| 33 | >(); |
| 34 | |
| 35 | flights.forEach((flight) => { |
| 36 | const aircraft = flight.aircraft as any; |
| 37 | const fa = (flight as any).flightAwareData as any; |
| 38 | |
| 39 | // Track tail numbers |
| 40 | const tailNumber = |
| 41 | aircraft?.reg || aircraft?.registration || fa?.registration || ''; |
| 42 | if (tailNumber) { |
| 43 | tailNumbers.set(tailNumber, (tailNumbers.get(tailNumber) || 0) + 1); |
| 44 | } |
| 45 | |
| 46 | if (aircraft?.reg) regs.add(aircraft.reg); |
| 47 | if (aircraft?.registration) regs.add(aircraft.registration); |
| 48 | if (aircraft?.modelCode) modelCodes.add(aircraft.modelCode); |
| 49 | if (aircraft?.typeName) candidateTypeNames.add(aircraft.typeName); |
| 50 | if (fa?.aircraftType) modelCodes.add(fa.aircraftType); |
| 51 | }); |
| 52 | |
| 53 | const registrations = regs.size |
| 54 | ? await prisma.aircraftRegistration.findMany({ |
| 55 | where: { reg: { in: Array.from(regs) } }, |
| 56 | include: { Aircraft: true }, |
| 57 | }) |
| 58 | : []; |
| 59 | const regToTypeName = new Map<string, string>(); |
| 60 | const regToAircraftId = new Map<string, string>(); |
| 61 | registrations.forEach((r) => { |
| 62 | if (r.reg && r.Aircraft?.typeName) { |
| 63 | regToTypeName.set(r.reg, r.Aircraft.typeName); |
| 64 | if (r.Aircraft.id) regToAircraftId.set(r.reg, r.Aircraft.id); |
| 65 | } |
| 66 | }); |
| 67 | |
| 68 | // Fetch all aircraft data for images and ages |
| 69 | const allAircraftIds = Array.from(regToAircraftId.values()); |
| 70 | const aircraftRows = |
| 71 | modelCodes.size || candidateTypeNames.size || allAircraftIds.length > 0 |
| 72 | ? await prisma.aircraft.findMany({ |
| 73 | where: { |
| 74 | OR: [ |
| 75 | modelCodes.size |
| 76 | ? { modelCode: { in: Array.from(modelCodes) } } |
| 77 | : undefined, |
| 78 | candidateTypeNames.size |
| 79 | ? { typeName: { in: Array.from(candidateTypeNames) } } |
| 80 | : undefined, |
| 81 | allAircraftIds.length > 0 |
| 82 | ? { id: { in: allAircraftIds } } |
| 83 | : undefined, |
| 84 | ].filter(Boolean) as any, |
| 85 | }, |
| 86 | select: { |
| 87 | id: true, |
| 88 | modelCode: true, |
| 89 | typeName: true, |
| 90 | image: true, |
| 91 | age: true, |
| 92 | reg: true, |
| 93 | createdAt: true, |
| 94 | }, |
| 95 | }) |
| 96 | : []; |
| 97 | |
| 98 | const modelCodeToTypeName = new Map<string, string>(); |
| 99 | const typeNameToImage = new Map<string, string>(); |
| 100 | const regToAge = new Map<string, number>(); |
| 101 | const regToImage = new Map<string, string>(); |
| 102 | |
| 103 | const now = new Date(); |
| 104 | |
| 105 | aircraftRows.forEach((a) => { |
| 106 | if (a.modelCode && a.typeName) { |
| 107 | modelCodeToTypeName.set(a.modelCode, a.typeName); |
| 108 | if (a.image) typeNameToImage.set(a.typeName, a.image); |
| 109 | } |
| 110 | if (a.typeName && a.image) { |
| 111 | typeNameToImage.set(a.typeName, a.image); |
| 112 | } |
| 113 | if (a.reg && a.age !== null) { |
| 114 | // Calculate age offset based on when the record was created |
| 115 | const ageAtCreation = Number(a.age); |
| 116 | const yearsSinceCreation = |
| 117 | (now.getTime() - a.createdAt.getTime()) / (1000 * 60 * 60 * 24 * 365.25); |
| 118 | const adjustedAge = ageAtCreation + yearsSinceCreation; |
| 119 | regToAge.set(a.reg, adjustedAge); |
| 120 | } |
| 121 | if (a.reg && a.image) { |
| 122 | regToImage.set(a.reg, a.image); |
| 123 | } |
| 124 | }); |
| 125 | |
| 126 | flights.forEach((flight) => { |
| 127 | const aircraft = flight.aircraft as any; |
| 128 | const fa = (flight as any).flightAwareData as any; |
| 129 | let typeName = ''; |
| 130 | if (aircraft?.typeName) typeName = aircraft.typeName; |
| 131 | else if (aircraft?.reg && regToTypeName.get(aircraft.reg)) |
| 132 | typeName = regToTypeName.get(aircraft.reg)!; |
| 133 | else if ( |
| 134 | aircraft?.registration && |
| 135 | regToTypeName.get(aircraft.registration) |
| 136 | ) |
| 137 | typeName = regToTypeName.get(aircraft.registration)!; |
| 138 | else if ( |
| 139 | aircraft?.modelCode && |
| 140 | modelCodeToTypeName.get(aircraft.modelCode) |
| 141 | ) |
| 142 | typeName = modelCodeToTypeName.get(aircraft.modelCode)!; |
| 143 | else if (fa?.aircraftType && modelCodeToTypeName.get(fa.aircraftType)) |
| 144 | typeName = modelCodeToTypeName.get(fa.aircraftType)!; |
| 145 | |
| 146 | if (typeName) { |
| 147 | aircraftCounts.set(typeName, (aircraftCounts.get(typeName) || 0) + 1); |
| 148 | if (!aircraftTypeToFlights.has(typeName)) { |
| 149 | aircraftTypeToFlights.set(typeName, []); |
| 150 | } |
| 151 | aircraftTypeToFlights.get(typeName)!.push(flight); |
| 152 | } |
| 153 | |
| 154 | // Track tail number to aircraft info (for all flights, not just those with typeName) |
| 155 | const tailNumber = |
| 156 | aircraft?.reg || aircraft?.registration || fa?.registration || ''; |
| 157 | if (tailNumber && !tailNumberToAircraft.has(tailNumber)) { |
| 158 | // Get typeName for this tail number if we don't have it from above |
| 159 | let aircraftTypeName = typeName; |
| 160 | if (!aircraftTypeName && regToTypeName.has(tailNumber)) { |
| 161 | aircraftTypeName = regToTypeName.get(tailNumber)!; |
| 162 | } |
| 163 | tailNumberToAircraft.set(tailNumber, { |
| 164 | typeName: aircraftTypeName || '', |
| 165 | age: regToAge.get(tailNumber), |
| 166 | image: regToImage.get(tailNumber), |
| 167 | }); |
| 168 | } |
| 169 | }); |
| 170 | |
| 171 | // Collect unique aircraft ages for median calculation |
| 172 | const uniqueAircraftAges: number[] = []; |
| 173 | tailNumberToAircraft.forEach((info) => { |
| 174 | if (info.age !== undefined && info.age > 0) { |
| 175 | uniqueAircraftAges.push(info.age); |
| 176 | } |
| 177 | }); |
| 178 | |
| 179 | const mostFlownAircraft = Array.from(aircraftCounts.entries()).sort( |
| 180 | (a, b) => b[1] - a[1], |
| 181 | )[0] || ['', 0]; |
| 182 | |
| 183 | // Find most common tail number |
| 184 | const mostCommonTailNumber = Array.from(tailNumbers.entries()).sort( |
| 185 | (a, b) => b[1] - a[1], |
| 186 | )[0] || ['', 0]; |
| 187 | |
| 188 | // Calculate median age (using unique aircraft ages) |
| 189 | let medianAge: number | undefined; |
| 190 | if (uniqueAircraftAges.length > 0) { |
| 191 | const sortedAges = [...uniqueAircraftAges].sort((a, b) => a - b); |
| 192 | const mid = Math.floor(sortedAges.length / 2); |
| 193 | const calculatedMedian = |
| 194 | sortedAges.length % 2 === 0 |
| 195 | ? (sortedAges[mid - 1] + sortedAges[mid]) / 2 |
| 196 | : sortedAges[mid]; |
| 197 | medianAge = Math.round(calculatedMedian * 10) / 10; // Round to 1 decimal place |
| 198 | } |
| 199 | |
| 200 | // Find youngest and oldest aircraft with images |
| 201 | let youngestAircraft: { |
| 202 | aircraftName: string; |
| 203 | tailNumber: string; |
| 204 | age?: number; |
| 205 | image?: string; |
| 206 | } | undefined; |
| 207 | let oldestAircraft: { |
| 208 | aircraftName: string; |
| 209 | tailNumber: string; |
| 210 | age?: number; |
| 211 | image?: string; |
| 212 | } | undefined; |
| 213 | |
| 214 | const aircraftWithAgeAndImage = Array.from(tailNumberToAircraft.entries()) |
| 215 | .map(([tailNumber, info]) => ({ |
| 216 | tailNumber, |
| 217 | typeName: info.typeName, |
| 218 | age: info.age, |
| 219 | image: info.image, |
| 220 | })) |
| 221 | .filter((a) => a.age !== undefined && a.age > 0 && a.image); |
| 222 | |
| 223 | if (aircraftWithAgeAndImage.length > 0) { |
| 224 | const sortedByAge = [...aircraftWithAgeAndImage].sort( |
| 225 | (a, b) => (a.age || 0) - (b.age || 0), |
| 226 | ); |
| 227 | youngestAircraft = { |
| 228 | aircraftName: sortedByAge[0].typeName, |
| 229 | tailNumber: sortedByAge[0].tailNumber, |
| 230 | age: sortedByAge[0].age, |
| 231 | image: sortedByAge[0].image, |
| 232 | }; |
| 233 | oldestAircraft = { |
| 234 | aircraftName: sortedByAge[sortedByAge.length - 1].typeName, |
| 235 | tailNumber: sortedByAge[sortedByAge.length - 1].tailNumber, |
| 236 | age: sortedByAge[sortedByAge.length - 1].age, |
| 237 | image: sortedByAge[sortedByAge.length - 1].image, |
| 238 | }; |
| 239 | } |
| 240 | |
| 241 | // Calculate passport stats |
| 242 | const uniqueAirports = new Set<string>(); |
| 243 | const uniqueAirlines = new Set<string>(); |
| 244 | let totalDistanceKm = 0; |
| 245 | let totalDurationMinutes = 0; |
| 246 | |
| 247 | flights.forEach((flight) => { |
| 248 | const departure = flight.departure as any; |
| 249 | const arrival = flight.arrival as any; |
| 250 | const airline = flight.airline as any; |
| 251 | |
| 252 | // Track airports |
| 253 | if (departure?.airport?.iata) { |
| 254 | uniqueAirports.add(departure.airport.iata); |
| 255 | } |
| 256 | if (departure?.airport?.icao) { |
| 257 | uniqueAirports.add(departure.airport.icao); |
| 258 | } |
| 259 | if (arrival?.airport?.iata) { |
| 260 | uniqueAirports.add(arrival.airport.iata); |
| 261 | } |
| 262 | if (arrival?.airport?.icao) { |
| 263 | uniqueAirports.add(arrival.airport.icao); |
| 264 | } |
| 265 | |
| 266 | // Track airlines |
| 267 | if (airline?.iata) { |
| 268 | uniqueAirlines.add(airline.iata); |
| 269 | } |
| 270 | if (airline?.icao) { |
| 271 | uniqueAirlines.add(airline.icao); |
| 272 | } |
| 273 | |
| 274 | // Sum distance |
| 275 | if (flight.greatCircleDistance?.km) { |
| 276 | const km = Number(flight.greatCircleDistance.km); |
| 277 | if (!isNaN(km)) { |
| 278 | totalDistanceKm += km; |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | // Calculate duration (including delays) |
| 283 | if (flight.flightAwareData) { |
| 284 | const faData = flight.flightAwareData; |
| 285 | let durationMinutes = 0; |
| 286 | |
| 287 | // Use filed ETE if available (in seconds) |
| 288 | if (faData.filedEte) { |
| 289 | durationMinutes = faData.filedEte / 60; |
| 290 | } else { |
| 291 | // Calculate from actual times if available |
| 292 | const departureTime = |
| 293 | faData.actualOff || |
| 294 | faData.actualOut || |
| 295 | faData.scheduledOff || |
| 296 | faData.scheduledOut; |
| 297 | const arrivalTime = |
| 298 | faData.actualOn || |
| 299 | faData.actualIn || |
| 300 | faData.scheduledOn || |
| 301 | faData.scheduledIn; |
| 302 | |
| 303 | if (departureTime && arrivalTime) { |
| 304 | durationMinutes = |
| 305 | (arrivalTime.getTime() - departureTime.getTime()) / (1000 * 60); |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | // Add delays if available (delays are in seconds) |
| 310 | if (faData.departureDelay) { |
| 311 | durationMinutes += faData.departureDelay / 60; |
| 312 | } |
| 313 | if (faData.arrivalDelay) { |
| 314 | durationMinutes += faData.arrivalDelay / 60; |
| 315 | } |
| 316 | |
| 317 | if (durationMinutes > 0) { |
| 318 | totalDurationMinutes += durationMinutes; |
| 319 | } |
| 320 | } |
| 321 | }); |
| 322 | |
| 323 | // Find shortest and longest distance flights |
| 324 | const flightsWithDistance = flights |
| 325 | .map((flight) => ({ |
| 326 | flight, |
| 327 | distanceKm: flight.greatCircleDistance?.km |
| 328 | ? Number(flight.greatCircleDistance.km) |
| 329 | : null, |
| 330 | })) |
| 331 | .filter((f) => f.distanceKm !== null && !isNaN(f.distanceKm)); |
| 332 | |
| 333 | const shortestDistanceFlight = |
| 334 | flightsWithDistance.length > 0 |
| 335 | ? flightsWithDistance.reduce((prev, curr) => |
| 336 | curr.distanceKm! < prev.distanceKm! ? curr : prev, |
| 337 | ) |
| 338 | : null; |
| 339 | |
| 340 | const longestDistanceFlight = |
| 341 | flightsWithDistance.length > 0 |
| 342 | ? flightsWithDistance.reduce((prev, curr) => |
| 343 | curr.distanceKm! > prev.distanceKm! ? curr : prev, |
| 344 | ) |
| 345 | : null; |
| 346 | |
| 347 | // Find shortest and longest duration flights |
| 348 | const flightsWithDuration = flights |
| 349 | .map((flight) => { |
| 350 | if (!flight.flightAwareData) return null; |
| 351 | |
| 352 | const faData = flight.flightAwareData; |
| 353 | let durationMinutes = 0; |
| 354 | |
| 355 | if (faData.filedEte) { |
| 356 | durationMinutes = faData.filedEte / 60; |
| 357 | } else { |
| 358 | const departureTime = |
| 359 | faData.actualOff || |
| 360 | faData.actualOut || |
| 361 | faData.scheduledOff || |
| 362 | faData.scheduledOut; |
| 363 | const arrivalTime = |
| 364 | faData.actualOn || |
| 365 | faData.actualIn || |
| 366 | faData.scheduledOn || |
| 367 | faData.scheduledIn; |
| 368 | |
| 369 | if (departureTime && arrivalTime) { |
| 370 | durationMinutes = |
| 371 | (arrivalTime.getTime() - departureTime.getTime()) / (1000 * 60); |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | if (faData.departureDelay) { |
| 376 | durationMinutes += faData.departureDelay / 60; |
| 377 | } |
| 378 | if (faData.arrivalDelay) { |
| 379 | durationMinutes += faData.arrivalDelay / 60; |
| 380 | } |
| 381 | |
| 382 | return durationMinutes > 0 ? { flight, durationMinutes } : null; |
| 383 | }) |
| 384 | .filter( |
| 385 | (f): f is { flight: (typeof flights)[0]; durationMinutes: number } => |
| 386 | f !== null, |
| 387 | ); |
| 388 | |
| 389 | const shortestDurationFlight = |
| 390 | flightsWithDuration.length > 0 |
| 391 | ? flightsWithDuration.reduce((prev, curr) => |
| 392 | curr.durationMinutes < prev.durationMinutes ? curr : prev, |
| 393 | ) |
| 394 | : null; |
| 395 | |
| 396 | const longestDurationFlight = |
| 397 | flightsWithDuration.length > 0 |
| 398 | ? flightsWithDuration.reduce((prev, curr) => |
| 399 | curr.durationMinutes > prev.durationMinutes ? curr : prev, |
| 400 | ) |
| 401 | : null; |
| 402 | |
| 403 | // Bulk fetch airlines to enrich images and compute stats |
| 404 | const airlineIatas = new Set<string>(); |
| 405 | const airlineIcaos = new Set<string>(); |
| 406 | flights.forEach((flight) => { |
| 407 | const airline = flight.airline as any; |
| 408 | if (airline?.iata) airlineIatas.add(airline.iata); |
| 409 | if (airline?.icao) airlineIcaos.add(airline.icao); |
| 410 | }); |
| 411 | const airlinesFromDb = |
| 412 | airlineIatas.size || airlineIcaos.size |
| 413 | ? await prisma.airline.findMany({ |
| 414 | where: { |
| 415 | OR: [ |
| 416 | airlineIatas.size |
| 417 | ? { iata: { in: Array.from(airlineIatas) } } |
| 418 | : undefined, |
| 419 | airlineIcaos.size |
| 420 | ? { icao: { in: Array.from(airlineIcaos) } } |
| 421 | : undefined, |
| 422 | ].filter(Boolean) as any, |
| 423 | }, |
| 424 | select: { iata: true, icao: true, image: true, name: true }, |
| 425 | }) |
| 426 | : []; |
| 427 | const airlineByIata = new Map<string, { image?: string; name: string }>(); |
| 428 | const airlineByIcao = new Map<string, { image?: string; name: string }>(); |
| 429 | airlinesFromDb.forEach((a) => { |
| 430 | if (a.iata) |
| 431 | airlineByIata.set(a.iata, { |
| 432 | image: a.image || undefined, |
| 433 | name: a.name, |
| 434 | }); |
| 435 | if (a.icao) |
| 436 | airlineByIcao.set(a.icao, { |
| 437 | image: a.image || undefined, |
| 438 | name: a.name, |
| 439 | }); |
| 440 | }); |
| 441 | |
| 442 | // Calculate airline stats |
| 443 | const airlineCounts = new Map< |
| 444 | string, |
| 445 | { name: string; iata: string; icao?: string; count: number } |
| 446 | >(); |
| 447 | |
| 448 | flights.forEach((flight) => { |
| 449 | const airline = flight.airline as any; |
| 450 | const key = airline?.iata || airline?.icao || ''; |
| 451 | const dbMatch = |
| 452 | (airline?.iata && airlineByIata.get(airline.iata)) || |
| 453 | (airline?.icao && airlineByIcao.get(airline.icao)) || |
| 454 | undefined; |
| 455 | const existing = airlineCounts.get(key) || { |
| 456 | name: dbMatch?.name || airline?.name || '', |
| 457 | iata: airline?.iata || '', |
| 458 | icao: airline?.icao, |
| 459 | count: 0, |
| 460 | }; |
| 461 | existing.count++; |
| 462 | airlineCounts.set(key, existing); |
| 463 | }); |
| 464 | |
| 465 | const airlines = Array.from(airlineCounts.values()) |
| 466 | .map((airline) => ({ |
| 467 | airlineName: airline.name, |
| 468 | airlineIata: airline.iata, |
| 469 | airlineIcao: airline.icao, |
| 470 | image: |
| 471 | (airline.iata && airlineByIata.get(airline.iata)?.image) || |
| 472 | (airline.icao && airlineByIcao.get(airline.icao)?.image) || |
| 473 | undefined, |
| 474 | flightCount: airline.count, |
| 475 | })) |
| 476 | .sort((a, b) => b.flightCount - a.flightCount); |
| 477 | |
| 478 | // Calculate airport stats (departures) |
| 479 | const airportDepartureCounts = new Map< |
| 480 | string, |
| 481 | { code: string; name: string; count: number } |
| 482 | >(); |
| 483 | |
| 484 | flights.forEach((flight) => { |
| 485 | const departure = flight.departure as any; |
| 486 | const airportCode = departure?.airport?.iata || departure?.airport?.icao; |
| 487 | const airportName = |
| 488 | departure?.airport?.name || departure?.airport?.shortName || ''; |
| 489 | |
| 490 | if (airportCode) { |
| 491 | const existing = airportDepartureCounts.get(airportCode) || { |
| 492 | code: airportCode, |
| 493 | name: airportName, |
| 494 | count: 0, |
| 495 | }; |
| 496 | existing.count++; |
| 497 | airportDepartureCounts.set(airportCode, existing); |
| 498 | } |
| 499 | }); |
| 500 | |
| 501 | const airports = Array.from(airportDepartureCounts.values()) |
| 502 | .map((airport) => ({ |
| 503 | airportCode: airport.code, |
| 504 | airportName: airport.name, |
| 505 | departureCount: airport.count, |
| 506 | })) |
| 507 | .sort((a, b) => b.departureCount - a.departureCount); |
| 508 | |
| 509 | // Calculate top routes |
| 510 | const routeCounts = new Map<string, number>(); |
| 511 | |
| 512 | flights.forEach((flight) => { |
| 513 | const departure = flight.departure as any; |
| 514 | const arrival = flight.arrival as any; |
| 515 | const origin = departure?.airport?.iata || departure?.airport?.icao; |
| 516 | const destination = arrival?.airport?.iata || arrival?.airport?.icao; |
| 517 | |
| 518 | if (origin && destination) { |
| 519 | const routeKey = `${origin}-${destination}`; |
| 520 | routeCounts.set(routeKey, (routeCounts.get(routeKey) || 0) + 1); |
| 521 | } |
| 522 | }); |
| 523 | |
| 524 | const topRoutes = Array.from(routeCounts.entries()) |
| 525 | .map(([route, count]) => { |
| 526 | const [origin, destination] = route.split('-'); |
| 527 | return { origin, destination, flightCount: count }; |
| 528 | }) |
| 529 | .sort((a, b) => b.flightCount - a.flightCount) |
| 530 | .slice(0, 10); // Top 10 routes |
| 531 | |
| 532 | // Transform to lightweight flight summary (no heavy payloads) |
| 533 | const transformFlight = ( |
| 534 | flight: (typeof flights)[0], |
| 535 | ): FlightSummaryEntity => { |
| 536 | const departure = flight.departure as any; |
| 537 | const arrival = flight.arrival as any; |
| 538 | const airline = flight.airline as any; |
| 539 | |
| 540 | return { |
| 541 | id: flight.id, |
| 542 | flightNo: flight.flightNo, |
| 543 | callSign: flight.callSign, |
| 544 | airline: { |
| 545 | name: |
| 546 | (airline?.iata && airlineByIata.get(airline.iata)?.name) || |
| 547 | (airline?.icao && airlineByIcao.get(airline.icao)?.name) || |
| 548 | airline?.name || |
| 549 | '', |
| 550 | iata: airline?.iata || '', |
| 551 | icao: airline?.icao, |
| 552 | image: |
| 553 | (airline?.iata && airlineByIata.get(airline.iata)?.image) || |
| 554 | (airline?.icao && airlineByIcao.get(airline.icao)?.image) || |
| 555 | airline?.image || |
| 556 | undefined, |
| 557 | }, |
| 558 | departureAirportCode: |
| 559 | departure?.airport?.iata || departure?.airport?.icao || '', |
| 560 | departureAirportName: |
| 561 | departure?.airport?.name || departure?.airport?.shortName || '', |
| 562 | arrivalAirportCode: |
| 563 | arrival?.airport?.iata || arrival?.airport?.icao || '', |
| 564 | arrivalAirportName: |
| 565 | arrival?.airport?.name || arrival?.airport?.shortName || '', |
| 566 | date: flight.date.toISOString(), |
| 567 | }; |
| 568 | }; |
| 569 | |
| 570 | // Get flights for most flown aircraft |
| 571 | const mostFlownFlights = |
| 572 | mostFlownAircraft[0] && aircraftTypeToFlights.has(mostFlownAircraft[0]) |
| 573 | ? aircraftTypeToFlights.get(mostFlownAircraft[0])!.map(transformFlight) |
| 574 | : []; |
| 575 | |
| 576 | return { |
| 577 | mostFlownAircraft: { |
| 578 | aircraftName: mostFlownAircraft[0], |
| 579 | flightCount: mostFlownAircraft[1], |
| 580 | }, |
| 581 | aircraftStats: { |
| 582 | mostFlownAircraftName: mostFlownAircraft[0], |
| 583 | mostFlownAircraftFlightCount: mostFlownAircraft[1], |
| 584 | mostFlownAircraftImage: mostFlownAircraft[0] |
| 585 | ? typeNameToImage.get(mostFlownAircraft[0]) |
| 586 | : undefined, |
| 587 | mostFlownAircraftFlights: mostFlownFlights, |
| 588 | mostCommonTailNumber: mostCommonTailNumber[0], |
| 589 | mostCommonTailNumberCount: mostCommonTailNumber[1], |
| 590 | medianAge: medianAge, |
| 591 | youngestAircraft: youngestAircraft, |
| 592 | oldestAircraft: oldestAircraft, |
| 593 | }, |
| 594 | passport: { |
| 595 | totalFlights: flights.length, |
| 596 | totalDistanceKm: Math.round(totalDistanceKm * 100) / 100, |
| 597 | totalDurationMinutes: Math.round(totalDurationMinutes), |
| 598 | totalAirports: uniqueAirports.size, |
| 599 | totalAirlines: uniqueAirlines.size, |
| 600 | }, |
| 601 | shortestDistanceFlight: shortestDistanceFlight |
| 602 | ? { |
| 603 | flight: transformFlight(shortestDistanceFlight.flight), |
| 604 | distanceKm: |
| 605 | Math.round(shortestDistanceFlight.distanceKm! * 100) / 100, |
| 606 | } |
| 607 | : { |
| 608 | flight: transformFlight(flights[0]), |
| 609 | distanceKm: 0, |
| 610 | }, |
| 611 | longestDistanceFlight: longestDistanceFlight |
| 612 | ? { |
| 613 | flight: transformFlight(longestDistanceFlight.flight), |
| 614 | distanceKm: |
| 615 | Math.round(longestDistanceFlight.distanceKm! * 100) / 100, |
| 616 | } |
| 617 | : { |
| 618 | flight: transformFlight(flights[0]), |
| 619 | distanceKm: 0, |
| 620 | }, |
| 621 | shortestDurationFlight: shortestDurationFlight |
| 622 | ? { |
| 623 | flight: transformFlight(shortestDurationFlight.flight), |
| 624 | durationMinutes: Math.round(shortestDurationFlight.durationMinutes), |
| 625 | } |
| 626 | : { |
| 627 | flight: transformFlight(flights[0]), |
| 628 | durationMinutes: 0, |
| 629 | }, |
| 630 | longestDurationFlight: longestDurationFlight |
| 631 | ? { |
| 632 | flight: transformFlight(longestDurationFlight.flight), |
| 633 | durationMinutes: Math.round(longestDurationFlight.durationMinutes), |
| 634 | } |
| 635 | : { |
| 636 | flight: transformFlight(flights[0]), |
| 637 | durationMinutes: 0, |
| 638 | }, |
| 639 | airlines, |
| 640 | airports, |
| 641 | topRoutes, |
| 642 | }; |
| 643 | } |
| 644 | |
| 645 | private getEmptyStats(): ProfileStatsResponseEntity { |
| 646 | return { |
| 647 | mostFlownAircraft: { |
| 648 | aircraftName: '', |
| 649 | flightCount: 0, |
| 650 | }, |
| 651 | aircraftStats: { |
| 652 | mostFlownAircraftName: '', |
| 653 | mostFlownAircraftFlightCount: 0, |
| 654 | mostFlownAircraftFlights: [], |
| 655 | mostCommonTailNumber: '', |
| 656 | mostCommonTailNumberCount: 0, |
| 657 | }, |
| 658 | passport: { |
| 659 | totalFlights: 0, |
| 660 | totalDistanceKm: 0, |
| 661 | totalDurationMinutes: 0, |
| 662 | totalAirports: 0, |
| 663 | totalAirlines: 0, |
| 664 | }, |
| 665 | shortestDistanceFlight: { |
| 666 | flight: {} as unknown as FlightSummaryEntity, |
| 667 | distanceKm: 0, |
| 668 | }, |
| 669 | longestDistanceFlight: { |
| 670 | flight: {} as unknown as FlightSummaryEntity, |
| 671 | distanceKm: 0, |
| 672 | }, |
| 673 | shortestDurationFlight: { |
| 674 | flight: {} as unknown as FlightSummaryEntity, |
| 675 | durationMinutes: 0, |
| 676 | }, |
| 677 | longestDurationFlight: { |
| 678 | flight: {} as unknown as FlightSummaryEntity, |
| 679 | durationMinutes: 0, |
| 680 | }, |
| 681 | airlines: [], |
| 682 | airports: [], |
| 683 | topRoutes: [], |
| 684 | }; |
| 685 | } |
| 686 | } |