aboutsummaryrefslogtreecommitdiff
path: root/src/Costasdev.Busurbano.Backend/Services/Processors/NextStopsProcessor.cs
blob: 6273e0dc930ffa58d6e31a898fb280ddc80d9ee2 (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 Costasdev.Busurbano.Backend.GraphClient.App;

namespace Costasdev.Busurbano.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;
    }
}