Skip to content

Commit

Permalink
refactor(be): remove deprecated endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
krystxf committed Nov 20, 2024
1 parent ecfe210 commit ed69b16
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 226 deletions.
6 changes: 0 additions & 6 deletions apps/backend/src/constants/swagger.const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,4 @@ Thank you for checking out my project.
- [GraphQL Playground](${GRAPHQL_PATH})
- [Swagger JSON file](${SWAGGER_JSON_PATH})
- [Source code](${SOURCE_CODE_URL})
### ⚠️ Warning
Do __NOT__ use non-versioned endpoints (e.g. \`/stop/all\`). Use versioned endpoints instead (e.g. \`/v1/stop/all\`).
Non-versioned endpoints are deprecated and will be removed in the future.
`;
94 changes: 2 additions & 92 deletions apps/backend/src/modules/departure/departure.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ import {
Query,
UseInterceptors,
Version,
VERSION_NEUTRAL,
} from "@nestjs/common";
import { ApiOperation, ApiTags } from "@nestjs/swagger";
import { ApiTags } from "@nestjs/swagger";
import { z } from "zod";

import { QUERY_IDS_COUNT_MAX } from "src/constants/constants";
import { ApiDescription, ApiQueries } from "src/decorators/swagger.decorator";
import { ApiQueries } from "src/decorators/swagger.decorator";
import { EndpointVersion } from "src/enums/endpoint-version";
import { DepartureServiceV1 } from "src/modules/departure/departure-v1.service";
import { DepartureServiceV2 } from "src/modules/departure/departure-v2.service";
Expand All @@ -27,7 +25,6 @@ import {
vehicleTypeSchema,
} from "src/schema/metro-only.schema";
import { metroOnlyQuery } from "src/swagger/query.swagger";
import { toArray } from "src/utils/array.utils";

@ApiTags("departure")
@Controller("departure")
Expand All @@ -39,64 +36,6 @@ export class DepartureController {
private readonly departureServiceV2: DepartureServiceV2,
) {}

@Get()
@Version([VERSION_NEUTRAL])
@ApiQueries([
metroOnlyQuery,
{
name: "platform[]",
description: "Platform IDs",
type: [String],
isArray: true,
allowEmptyValue: true,
required: false,
},
{
name: "stop[]",
description: "Stop IDs",
type: [String],
isArray: true,
allowEmptyValue: true,
required: false,
},
])
@ApiOperation({
deprecated: true,
})
@ApiDescription({
deprecated: true,
})
async getDepartures(@Query() query): Promise<DepartureSchema[]> {
const schema = z.object({
metroOnly: metroOnlySchema,
platform: z.string().array().optional().default([]),
stop: z.string().array().optional().default([]),
});
const parsed = schema.safeParse(query);
if (!parsed.success) {
throw new HttpException(
"Invalid query params",
HttpStatus.BAD_REQUEST,
);
}
const parsedQuery = parsed.data;

if (parsedQuery.platform.length + parsedQuery.stop.length === 0) {
throw new HttpException(
"At least one platform or stop ID must be provided",
HttpStatus.BAD_REQUEST,
);
}

const departures = await this.departureServiceV1.getDepartures({
stopIds: parsedQuery.stop,
platformIds: parsedQuery.platform,
metroOnly: parsedQuery.metroOnly,
});

return departureSchema.array().parse(departures);
}

@Get()
@Version([EndpointVersion.v1])
@ApiQueries([
Expand Down Expand Up @@ -149,35 +88,6 @@ export class DepartureController {
return departureSchema.array().parse(departures);
}

@Get("/platform")
@Version([VERSION_NEUTRAL, EndpointVersion.v1])
@ApiDescription({
deprecated: true,
})
async getDeparturesByPlatform(@Query("id") id): Promise<DepartureSchema[]> {
const platformSchema = z
.string()
.array()
.min(1)
.max(QUERY_IDS_COUNT_MAX);
const parsed = platformSchema.safeParse(toArray(id));

if (!parsed.success) {
throw new HttpException(
"Invalid query params",
HttpStatus.BAD_REQUEST,
);
}

const departures = await this.departureServiceV1.getDepartures({
stopIds: [],
platformIds: parsed.data,
metroOnly: false,
});

return departureSchema.array().parse(departures);
}

@Get()
@Version([EndpointVersion.v2])
@ApiQueries([
Expand Down
111 changes: 0 additions & 111 deletions apps/backend/src/modules/platform/platform.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
Query,
UseInterceptors,
Version,
VERSION_NEUTRAL,
} from "@nestjs/common";
import { ApiQuery, ApiTags } from "@nestjs/swagger";
import { z } from "zod";
Expand Down Expand Up @@ -39,32 +38,6 @@ import {
export class PlatformController {
constructor(private readonly platformService: PlatformService) {}

@Get("/all")
@Version([VERSION_NEUTRAL])
@ApiDescription({
summary: "List of all platforms",
deprecated: true,
})
@ApiQuery(metroOnlyQuery)
async getAllPlatforms(@Query() query): Promise<PlatformSchema[]> {
const schema = z.object({
metroOnly: metroOnlySchema,
});
const parsed = schema.safeParse(query);
if (!parsed.success) {
throw new HttpException(
parsed.error.format(),
HttpStatus.BAD_REQUEST,
);
}

const platforms = await this.platformService.getAllPlatforms(
parsed.data,
);

return platformSchema.array().parse(platforms);
}

@Get("/all")
@Version([EndpointVersion.v1])
@ApiDescription({
Expand All @@ -90,56 +63,6 @@ export class PlatformController {
return platformSchema.array().parse(platforms);
}

@Get("/closest")
@Version([VERSION_NEUTRAL])
@ApiDescription({
description: `
⚠️ _For better privacy consider using \`/in-box\`_
Sort platforms by distance to a given location. Location may be saved in logs.
`,
summary: "List of platforms sorted by distance to a given location",
deprecated: true,
})
@ApiQueries([
metroOnlyQuery,
latitudeQuery,
longitudeQuery,
{
name: "count",
type: Number,
required: false,
example: 100,
description: "number of platforms to return, default is `0` (all)",
},
])
async getPlatformsByDistance(
@Query() query,
): Promise<PlatformWithDistanceSchema[]> {
const schema = z.object({
latitude: z.coerce.number(),
longitude: z.coerce.number(),
count: z.coerce.number().int().nonnegative().default(0),
metroOnly: metroOnlySchema,
});

const parsed = schema.safeParse(query);

if (!parsed.success) {
throw new HttpException(
parsed.error.format(),
HttpStatus.BAD_REQUEST,
);
}

const platforms = await this.platformService.getPlatformsByDistance(
parsed.data,
);

return platformWithDistanceSchema.array().parse(platforms);
}

@Get("/closest")
@Version([EndpointVersion.v1])
@ApiDescription({
Expand Down Expand Up @@ -189,40 +112,6 @@ Sort platforms by distance to a given location. Location may be saved in logs.
return platformWithDistanceSchema.array().parse(platforms);
}

@Get("/in-box")
@Version([VERSION_NEUTRAL])
@ApiDescription({
summary: "List of platforms within a given bounding box",
deprecated: true,
})
@ApiQueries([metroOnlyQuery, ...boundingBoxQuery])
async getPlatformsInBoundingBox(@Query() query): Promise<PlatformSchema[]> {
const schema = z.object({
boundingBox: boundingBoxSchema,
metroOnly: metroOnlySchema,
});
const parsed = schema.safeParse({
boundingBox: {
latitude: query?.latitude,
longitude: query?.longitude,
},
metroOnly: query?.metroOnly,
});

if (!parsed.success) {
throw new HttpException(
"Invalid query params",
HttpStatus.BAD_REQUEST,
);
}

const platforms = await this.platformService.getPlatformsInBoundingBox(
parsed.data,
);

return platformSchema.array().parse(platforms);
}

@Get("/in-box")
@Version([EndpointVersion.v1])
@ApiDescription({
Expand Down
17 changes: 0 additions & 17 deletions apps/backend/src/modules/stop/stop.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ import {
Query,
UseInterceptors,
Version,
VERSION_NEUTRAL,
} from "@nestjs/common";
import { ApiParam, ApiQuery, ApiTags } from "@nestjs/swagger";

import { ApiDescription } from "src/decorators/swagger.decorator";
import { EndpointVersion } from "src/enums/endpoint-version";
import { LogInterceptor } from "src/modules/logger/log.interceptor";
import { StopService } from "src/modules/stop/stop.service";
Expand All @@ -24,21 +22,6 @@ import { metroOnlyQuery } from "src/swagger/query.swagger";
export class StopController {
constructor(private readonly stopService: StopService) {}

@Get("/all")
@Version([VERSION_NEUTRAL])
@ApiQuery(metroOnlyQuery)
@ApiDescription({
deprecated: true,
})
async getAllStops(
@Query("metroOnly")
metroOnlyQuery: unknown,
) {
const metroOnly: boolean = metroOnlyQuery === "true";

return this.stopService.getAll({ metroOnly });
}

@Get("/all")
@Version([EndpointVersion.v1])
@ApiQuery(metroOnlyQuery)
Expand Down

0 comments on commit ed69b16

Please sign in to comment.