Skip to content

Commit

Permalink
Fix vehicle date departure
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexis06030631 committed Oct 26, 2023
1 parent 6caae07 commit 17537b8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
13 changes: 8 additions & 5 deletions src/structures/StopArea.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {dateToNavitiaDate, isValidID} from "../util";
import {dateToNavitiaDate, extractDateInTripID, isValidID} from "../util";
import {Coord} from "../models";
import {AdministrativeRegion} from "./AdministrativeRegion";
import {Departure} from "./Departure";
Expand Down Expand Up @@ -141,22 +141,25 @@ export class StopArea{

/**
* Get vehicle journeys of the stop area
* @param date - The date of the vehicle journeys
* @param since - The start date of the vehicle journeys to get
* @param until - The end date of the vehicle journeys to get (default: since + 1 day)
*/
vehicle_journeys(date:Date= new Date()): Promise<Vehicle[]> {
vehicle_journeys(since:Date= new Date(), until:Date= new Date(since.getTime() + 86400000)): Promise<Vehicle[]> {
return new Promise(async (resolve, reject) => {
const request = await this.client.requestManager.request(`stop_areas/${this.id}/vehicle_journeys`, {from_datetime: dateToNavitiaDate(date)})
if(since > until) return reject(new Error("The since date must be before the until date"))
const request = await this.client.requestManager.request(`stop_areas/${this.id}/vehicle_journeys`, {since: dateToNavitiaDate(since), until: dateToNavitiaDate(until)})
if(request.error) {
reject(request.error)
}else {
const date = since
resolve(request.vehicle_journeys.map((vehicle_journey:any) => {
vehicle_journey.disruptions.forEach((disruption:any) => {
if(request.disruptions.map((disruption:any) => disruption.id).includes(disruption.id)) {
vehicle_journey.disruptions[vehicle_journey.disruptions.indexOf(disruption)] = request.disruptions
.find((disruption2:any) => disruption2.id === disruption.id)
}
})
return new Vehicle(this.client, vehicle_journey, date)
return new Vehicle(this.client, vehicle_journey)
}))
}
})
Expand Down
10 changes: 5 additions & 5 deletions src/structures/Vehicle.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {StructuresManager} from "./StructuresManager";
import {dateWithDateAndHour, hourNativiaToHour} from "../util";
import {dateWithDateAndHour, extractDateInTripID, hourNativiaToHour} from "../util";
import {Calendar} from "./Calendar";
import {StopTime} from "./StopTime";
import {Disruption} from "./Disruption";
Expand Down Expand Up @@ -56,7 +56,7 @@ export class Vehicle {
*/
client: any;

constructor(Client:any, data:any, date:Date = new Date()) {
constructor(Client:any, data:any) {
Object.defineProperty(this, "client", {value: Client})
Object.defineProperty(this, "data", {value: data})

Expand All @@ -67,9 +67,9 @@ export class Vehicle {
this.calendar = data.calendars.map((calendar:any) => new Calendar(this.client, calendar))
this.departure_time = this.get_departure_time
this.arrival_time = this.get_arrival_time
this.departure_date = dateWithDateAndHour(this.get_departure_time, date)
this.arrival_date = dateWithDateAndHour(this.get_arrival_time, date)
this.stop_times = data.stop_times.sort((a:any, b:any) => Number(a.departure_time) - Number(b.departure_time)).map((stop_time:any) => new StopTime(this.client, stop_time, date))
this.departure_date = dateWithDateAndHour(this.get_departure_time, extractDateInTripID(data.trip.id))
this.arrival_date = dateWithDateAndHour(this.get_arrival_time, extractDateInTripID(data.trip.id))
this.stop_times = data.stop_times.sort((a:any, b:any) => Number(a.departure_time) - Number(b.departure_time)).map((stop_time:any) => new StopTime(this.client, stop_time, extractDateInTripID(data.trip.id)))

}

Expand Down
5 changes: 5 additions & 0 deletions src/util/Converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,9 @@ export function dateWithDateAndHour(hour:string, date:Date): Date {
seconds: Number(hour.substring(6, 8))
}
return new Date(date.getFullYear(), date.getMonth(), date.getDate(), hourDate.hours, hourDate.minutes, hourDate.seconds)
}

export function extractDateInTripID(id:string): Date {
const date = id.split(':')[1]
return new Date(date)
}

0 comments on commit 17537b8

Please sign in to comment.