Conversation
| description: 'Returns stop times for origin and destination stops', | ||
| }) | ||
| async getRoundTrip(@Query() query: RoundTripQueryDto) { | ||
| const targetDate = new Date(query.date); |
There was a problem hiding this comment.
Correct me if I'm wrong, but it seems like the logic here would be just getting any time of any bus/train that is stopped at the specified stop, regardless of direction and where it's headed.
|
You'll want to work off of baseEntitiesV2 |
| const RoundTripQuerySchema = z.object({ | ||
| orgStopId: z.string().min(1, 'Origin stop ID is required'), | ||
| destStopId: z.string().min(1, 'Destination stop ID is required'), | ||
| date: z | ||
| .string() | ||
| .regex(/^\d{4}-\d{2}-\d{2}$/, 'Date must be in YYYY-MM-DD format'), | ||
| }); |
There was a problem hiding this comment.
We should block origin and destination from being the same stop. Zod can do this with a refine/superRefine. We could also add a safer date parse/validation so we don’t accept a bad date that later becomes Invalid Date.
| return { | ||
| orgStopTimes: orgStopTimes.map((st) => ({ | ||
| id: st.id, | ||
| trip_id: st.trip.trip_id, | ||
| arrival_time: st.arrivalTime as unknown as string, | ||
| departure_time: st.departureTime as unknown as string, | ||
| stop_id: st.stop.stopid, | ||
| })), | ||
| destStopTimes: destStopTimes.map((st) => ({ | ||
| id: st.id, | ||
| trip_id: st.trip.trip_id, | ||
| arrival_time: st.arrivalTime as unknown as string, | ||
| departure_time: st.departureTime as unknown as string, | ||
| stop_id: st.stop.stopid, | ||
| })), | ||
| }; |
There was a problem hiding this comment.
We return two arrays and the client has to match them up. Could we instead return a list of matched trips, each with its origin/destination times, sorted by origin departure? That would make the endpoint easier to use.
| const orgStopTimes = await this.stopTimeRepository.find( | ||
| { | ||
| stop: { stopid: query.orgStopId }, | ||
| trip: { | ||
| calendarDate: { | ||
| date: targetDate, | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| populate: ['trip', 'stop'], | ||
| orderBy: { departureTime: 'ASC' }, | ||
| }, | ||
| ); | ||
|
|
||
| const destStopTimes = await this.stopTimeRepository.find( | ||
| { | ||
| stop: { stopid: query.destStopId }, | ||
| trip: { | ||
| calendarDate: { | ||
| date: targetDate, | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| populate: ['trip', 'stop'], | ||
| orderBy: { departureTime: 'ASC' }, | ||
| }, | ||
| ); |
There was a problem hiding this comment.
Right now we fetch origin and destination stop times separately, so the client could accidentally pair different trips or the wrong direction. Could we query trips that contain both stops, ensure the origin stop_sequence is before the destination’s, and (if available) match direction/headsign? Also, we should skip trips canceled for that date (exceptionType=2).
| description: 'Returns stop times for origin and destination stops', | ||
| }) | ||
| async getRoundTrip(@Query() query: RoundTripQueryDto) { | ||
| const targetDate = new Date(query.date); |
There was a problem hiding this comment.
Parsing the YYYY-MM-DD string with new Date() can shift the day because of timezones. Can we parse it as a plain date (e.g., split year/month/day or use a UTC parse) so it always matches the intended service date?
|
So we don't want to be working off the GTFS service here, since that services purpose is for importing gtfs data. Since we will be interfacing with the combination of data (calling it trip schedule for now), it will be easier as an api developer to work off of a view. In mikroorm we can create entities that are virtual (which means we interact with it like we would a real object, but we don't actually create a new table). We want to return the stop time ids, and route ids to the frontend because they will need this to create an itinerary. The round trip controller should query this entity and pull the appropriate trip schedules. |
|
Ended up opening a pr so you can use this, after my comment i realized I also needed to filter on active feeds. #123 |
|
Relevant documentation: https://mikro-orm.io/docs/virtual-entities |
Added an API endpoint to get trip times from ids and date.