|
| 1 | +import { incrementApiCount } from "@/utils/apiCounter"; |
| 2 | +import { vendoJourneySchema, type VendoJourney } from "@/utils/schemas"; |
| 3 | +import type { SplitPoint } from "@/utils/types"; |
| 4 | +import { createClient } from "db-vendo-client"; |
| 5 | +import { profile as dbProfile } from "db-vendo-client/p/db/index"; |
| 6 | +import { z } from "zod/v4"; |
| 7 | +import type { QueryOptions } from "../splitJourney/QueryOptions"; |
| 8 | +import { createSplitResult } from "../splitJourney/createSplitResult"; |
| 9 | +import { findMatchingJourney } from "../splitJourney/findMatchingJourney"; |
| 10 | + |
| 11 | +const client = createClient(dbProfile, "mail@lukasweihrauch.de"); |
| 12 | + |
| 13 | +// Split Analysis Functions |
| 14 | +export async function analyzeSingleSplit( |
| 15 | + originalJourney: VendoJourney, |
| 16 | + splitPoint: SplitPoint, |
| 17 | + queryOptions: QueryOptions, |
| 18 | + originalPrice: number |
| 19 | +) { |
| 20 | + const origin = originalJourney.legs[0].origin; |
| 21 | + const destination = |
| 22 | + originalJourney.legs[originalJourney.legs.length - 1].destination; |
| 23 | + const originalDeparture = new Date(originalJourney.legs[0].departure); |
| 24 | + const splitDeparture = new Date(splitPoint.departure); |
| 25 | + |
| 26 | + try { |
| 27 | + // Increment API counters for both segments |
| 28 | + incrementApiCount( |
| 29 | + "SPLIT_SEARCH_SEGMENT_1", |
| 30 | + `${origin?.name} → ${splitPoint.station?.name}` |
| 31 | + ); |
| 32 | + incrementApiCount( |
| 33 | + "SPLIT_SEARCH_SEGMENT_2", |
| 34 | + `${splitPoint.station?.name} → ${destination?.name}` |
| 35 | + ); |
| 36 | + |
| 37 | + // Schema validation at entry point ensures origin/destination IDs exist |
| 38 | + |
| 39 | + // Make both API calls in parallel using Promise.all |
| 40 | + const [firstSegmentUntyped, secondSegmentUntyped] = await Promise.all([ |
| 41 | + client.journeys(origin!.id, splitPoint.station.id, { |
| 42 | + ...queryOptions, |
| 43 | + departure: originalDeparture, |
| 44 | + }), |
| 45 | + |
| 46 | + client.journeys(splitPoint.station.id, destination!.id, { |
| 47 | + ...queryOptions, |
| 48 | + departure: splitDeparture, |
| 49 | + }), |
| 50 | + ]); |
| 51 | + |
| 52 | + const clientJourneySchema = z.object({ |
| 53 | + journeys: z.array(vendoJourneySchema), |
| 54 | + }); |
| 55 | + |
| 56 | + const firstSegment = clientJourneySchema.parse(firstSegmentUntyped); |
| 57 | + const secondSegment = clientJourneySchema.parse(secondSegmentUntyped); |
| 58 | + |
| 59 | + if ( |
| 60 | + firstSegment.journeys === undefined || |
| 61 | + secondSegment.journeys === undefined |
| 62 | + ) { |
| 63 | + return null; |
| 64 | + } |
| 65 | + |
| 66 | + const firstJourney = findMatchingJourney( |
| 67 | + firstSegment.journeys, |
| 68 | + originalDeparture |
| 69 | + ); |
| 70 | + |
| 71 | + if (!firstJourney) { |
| 72 | + return null; |
| 73 | + } |
| 74 | + |
| 75 | + const secondJourney = findMatchingJourney( |
| 76 | + secondSegment.journeys, |
| 77 | + splitDeparture |
| 78 | + ); |
| 79 | + |
| 80 | + if (!secondJourney) { |
| 81 | + return null; |
| 82 | + } |
| 83 | + |
| 84 | + // Calculate pricing |
| 85 | + const firstPrice = firstJourney.price?.amount || 0; |
| 86 | + const secondPrice = secondJourney.price?.amount || 0; |
| 87 | + const totalPrice = firstPrice + secondPrice; |
| 88 | + |
| 89 | + if (totalPrice > 0 && totalPrice < originalPrice) { |
| 90 | + return createSplitResult( |
| 91 | + "single", |
| 92 | + [splitPoint.station], |
| 93 | + [firstJourney, secondJourney], |
| 94 | + totalPrice, |
| 95 | + originalPrice, |
| 96 | + splitPoint.trainLine |
| 97 | + ); |
| 98 | + } |
| 99 | + |
| 100 | + return null; |
| 101 | + } catch (error) { |
| 102 | + const typedError = error as { message: string }; |
| 103 | + console.log( |
| 104 | + `Single split analysis error at ${splitPoint.station.name}:`, |
| 105 | + typedError.message |
| 106 | + ); |
| 107 | + throw error; // Re-throw to be handled by Promise.allSettled |
| 108 | + } |
| 109 | +} |
0 commit comments