-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from krystxf/fix/be-swagger-mark-endpoints-as-…
…deprecated refactor(be): mark old endpoints as deprecated in swagger
- Loading branch information
Showing
7 changed files
with
367 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export enum EndpointVersion { | ||
v1 = "1", | ||
v2 = "2", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
apps/backend/src/modules/departure/departure-v2.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { Injectable } from "@nestjs/common"; | ||
import { unique } from "radash"; | ||
|
||
import { departureBoardsSchema } from "src/modules/departure/schema/departure-boards.schema"; | ||
import type { DepartureSchema } from "src/modules/departure/schema/departure.schema"; | ||
import { GolemioService } from "src/modules/golemio/golemio.service"; | ||
import { PrismaService } from "src/modules/prisma/prisma.service"; | ||
import { getDelayInSeconds } from "src/utils/delay"; | ||
|
||
@Injectable() | ||
export class DepartureServiceV2 { | ||
constructor( | ||
private prisma: PrismaService, | ||
private golemioService: GolemioService, | ||
) {} | ||
|
||
async getDepartures(args: { | ||
stopIds: string[]; | ||
platformIds: string[]; | ||
metroOnly: boolean; | ||
}): Promise<DepartureSchema[]> { | ||
const dbPlatforms = ( | ||
await this.prisma.platform.findMany({ | ||
select: { id: true }, | ||
where: { | ||
id: { in: args.platformIds }, | ||
...(args.metroOnly ? { isMetro: true } : {}), | ||
}, | ||
}) | ||
).map((platform) => platform.id); | ||
|
||
const stopPlatforms = ( | ||
await this.prisma.stop.findMany({ | ||
select: { | ||
platforms: { | ||
select: { id: true }, | ||
where: { ...(args.metroOnly ? { isMetro: true } : {}) }, | ||
}, | ||
}, | ||
where: { id: { in: args.stopIds } }, | ||
}) | ||
).flatMap((stop) => stop.platforms.map((platform) => platform.id)); | ||
|
||
const allPlatformIds = unique([...dbPlatforms, ...stopPlatforms]).slice( | ||
0, | ||
100, | ||
); | ||
|
||
if (allPlatformIds.length === 0) { | ||
return []; | ||
} | ||
|
||
const searchParams = new URLSearchParams( | ||
allPlatformIds | ||
.map((id) => ["ids", id]) | ||
.concat( | ||
Object.entries({ | ||
skip: "canceled", | ||
mode: "departures", | ||
order: "real", | ||
minutesBefore: String(5), | ||
minutesAfter: String(10 * 60), | ||
}), | ||
), | ||
); | ||
|
||
const res = await this.golemioService.getGolemioData( | ||
`/v2/pid/departureboards&${searchParams.toString()}`, | ||
); | ||
|
||
if (!res.ok) { | ||
throw new Error( | ||
`Failed to fetch departure data: ${res.status} ${res.statusText}`, | ||
); | ||
} | ||
|
||
const json = await res.json(); | ||
const parsed = departureBoardsSchema.safeParse(json); | ||
|
||
if (!parsed.success) { | ||
throw new Error(parsed.error.message); | ||
} | ||
|
||
const parsedDepartures = parsed.data.departures.map((departure) => { | ||
return { | ||
departure: departure.departure_timestamp, | ||
delay: getDelayInSeconds(departure.delay), | ||
headsign: departure.trip.headsign, | ||
route: departure.route.short_name, | ||
platformId: departure.stop.id, | ||
platformCode: departure.stop.platform_code, | ||
}; | ||
}); | ||
|
||
return parsedDepartures; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,13 @@ | ||
import { Module } from "@nestjs/common"; | ||
|
||
import { DepartureServiceV1 } from "src/modules/departure/departure-v1.service"; | ||
import { DepartureServiceV2 } from "src/modules/departure/departure-v2.service"; | ||
import { DepartureController } from "src/modules/departure/departure.controller"; | ||
import { DepartureService } from "src/modules/departure/departure.service"; | ||
import { GolemioService } from "src/modules/golemio/golemio.service"; | ||
|
||
@Module({ | ||
controllers: [DepartureController], | ||
providers: [DepartureService, GolemioService], | ||
providers: [DepartureServiceV1, DepartureServiceV2, GolemioService], | ||
imports: [], | ||
}) | ||
export class DepartureModule {} |
Oops, something went wrong.
18432d8
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
metro-now – ./
metro-now-git-main-krystofs-projects-e2322253.vercel.app
metronow.vercel.app
metro-now-krystofs-projects-e2322253.vercel.app