aboutsummaryrefslogtreecommitdiff
path: root/src/Enmarcha.Backend/Services/Processors/NextStopsProcessor.cs
blob: 5d0206674a2db90be0c2142bb7b82c88162eb56a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
using Enmarcha.Sources.OpenTripPlannerGql.Queries;

namespace Enmarcha.Backend.Services.Processors;

public class NextStopsProcessor : IArrivalsProcessor
{
    private readonly FeedService _feedService;

    public NextStopsProcessor(FeedService feedService)
    {
        _feedService = feedService;
    }

    public Task ProcessAsync(ArrivalsContext context)
    {
        var feedId = context.StopId.Split(':')[0];

        foreach (var arrival in context.Arrivals)
        {
            if (arrival.RawOtpTrip is not ArrivalsAtStopResponse.Arrival otpArrival) continue;

            // Filter stoptimes that are after the current stop's departure
            var currentStopDeparture = otpArrival.ScheduledDepartureSeconds;

            arrival.NextStops = otpArrival.Trip.Stoptimes
                .Where(s => s.ScheduledDeparture > currentStopDeparture)
                .OrderBy(s => s.ScheduledDeparture)
                .Select(s => _feedService.NormalizeStopName(feedId, s.Stop.Name))
                .ToList();
        }

        return Task.CompletedTask;
    }
}