Skip to content

Commit

Permalink
fix: use estimateDistance helper when calculating flight time without…
Browse files Browse the repository at this point in the history
… departure and arrival times
  • Loading branch information
johanohly committed Sep 17, 2024
1 parent c1c0a83 commit 50d785e
Showing 1 changed file with 3 additions and 11 deletions.
14 changes: 3 additions & 11 deletions src/routes/api/flight/save/+server.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import {
actionResult,
message,
setError,
superValidate,
} from 'sveltekit-superforms';
import { actionResult, setError, superValidate } from 'sveltekit-superforms';
import { zod } from 'sveltekit-superforms/adapters';
import { flightSchema } from '$lib/zod/flight';
import { error, fail } from '@sveltejs/kit';
import { airportFromICAO } from '$lib/utils/data/airports';
import dayjs from 'dayjs';
import { distanceBetween, toISOString } from '$lib/utils';
import { estimateDuration, toISOString } from '$lib/utils';
import {
createFlight,
getFlight,
Expand Down Expand Up @@ -103,9 +97,7 @@ export const POST: RequestHandler = async ({ locals, request }) => {
// if the airports are the same, the duration can't be calculated
const fromLonLat = { lon: fromAirport.lon, lat: fromAirport.lat };
const toLonLat = { lon: toAirport.lon, lat: toAirport.lat };
const distance = distanceBetween(fromLonLat, toLonLat) / 1000;
const durationHours = distance / 805 + 0.5; // 805 km/h is the average speed of a commercial jet, add 0.5 hours for takeoff and landing
duration = Math.round(dayjs.duration(durationHours, 'hours').asSeconds());
duration = estimateDuration(fromLonLat, toLonLat);
}

const { flightNumber, aircraft, aircraftReg, airline, flightReason, note } =
Expand Down

0 comments on commit 50d785e

Please sign in to comment.