Skip to content

Commit

Permalink
🐞 Fixed bug where generateJSON had to be run twice to get accurate data
Browse files Browse the repository at this point in the history
  • Loading branch information
TechSupportz committed Apr 1, 2024
1 parent 155adde commit 3be2762
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
13 changes: 9 additions & 4 deletions src/routes/generateJSON.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ export const generateJSON = createRouteSpec({

await writeJSON("bus-stops", transformedBusStops)

const transformedBusServices = await transformBusServices(busRoutes, busServices)
const transformedBusServices = await transformBusServices(
busRoutes,
busServices,
transformedBusStops.data,
)

await writeJSON("bus-services", transformedBusServices)

Expand Down Expand Up @@ -111,6 +115,7 @@ async function transformBusStops(
async function transformBusServices(
busRoutes: LTABusRoute[],
busServices: LTABusService[],
busStopData: BusStop[],
): Promise<BusServiceJSON> {
const tempBusServices: BusService[] = []

Expand All @@ -121,7 +126,7 @@ async function transformBusServices(

const parsedBusRoutes = busRoutes.flatMap((route) => {
if (route.ServiceNo === v.ServiceNo) {
const busStop = getBusStopFromCode(route.BusStopCode)
const busStop = getBusStopFromCode(route.BusStopCode, busStopData)

return {
busStop: {
Expand Down Expand Up @@ -159,8 +164,8 @@ async function transformBusServices(
throw new Error("Error parsing bus route")
}

const originBusStopInfo = getBusStopFromCode(v.OriginCode)
const destinationBusStopInfo = getBusStopFromCode(v.DestinationCode)
const originBusStopInfo = getBusStopFromCode(v.OriginCode, busStopData)
const destinationBusStopInfo = getBusStopFromCode(v.DestinationCode, busStopData)

const busService: BusService = {
serviceNo: v.ServiceNo,
Expand Down
2 changes: 1 addition & 1 deletion src/routes/getBusService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const getBusService = createRouteSpec({
},
handler: async (ctx) => {
const { serviceNo } = ctx.params
const includeRoutes = ctx.query.includeRoutes === ''
const includeRoutes = ctx.query.includeRoutes === ""

const busService = getBusServiceFromServiceNo(serviceNo)

Expand Down
11 changes: 9 additions & 2 deletions src/utils/bus-stops.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { busStops } from "../json"
import { BusStop } from "../types/bus-stop-type"
import { BusStop, BusStopJSON } from "../types/bus-stop-type"

export function getBusStopFromCode(
busStopCode: string,
busStopData?: BusStopJSON["data"],
): BusStop | undefined {
if (busStopData) {
return busStopData.find((busStop) => busStop.code === busStopCode)
}

export function getBusStopFromCode(busStopCode: string): BusStop | undefined {
return busStops.find((busStop) => busStop.code === busStopCode)
}

Expand Down

0 comments on commit 3be2762

Please sign in to comment.