Skip to content

Commit

Permalink
refactor(back): more appropriate status codes in leaderboard endpoints
Browse files Browse the repository at this point in the history
sorry ImATeapotException
  • Loading branch information
tsa96 committed Feb 8, 2024
1 parent 742dfbe commit 96484c9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
4 changes: 2 additions & 2 deletions apps/backend-e2e/src/maps-2.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1621,13 +1621,13 @@ describe('Maps Part 2', () => {
expect(mockSteamIDs).toContain(BigInt(run.user.steamID));
});

it('should 418 if the user has no Steam friends', async () => {
it('should 410 if the user has no Steam friends', async () => {
jest.spyOn(steamService, 'getSteamFriends').mockResolvedValueOnce([]);

return req.get({
url: `maps/${map.id}/leaderboard`,
query: { gamemode: Gamemode.AHOP, filter: 'friends' },
status: 418,
status: 410,
token
});
});
Expand Down
17 changes: 16 additions & 1 deletion apps/backend/src/app/modules/maps/maps.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ import {
ApiConsumes,
ApiCreatedResponse,
ApiForbiddenResponse,
ApiGoneResponse,
ApiNoContentResponse,
ApiNotFoundResponse,
ApiOkResponse,
ApiOperation,
ApiParam,
ApiServiceUnavailableResponse,
ApiTags
} from '@nestjs/swagger';
import {
Expand Down Expand Up @@ -649,6 +651,18 @@ export class MapsController {
required: true
})
@ApiOkResponse({ description: "The found leaderboard's runs" })
@ApiNotFoundResponse({ description: "When the map doesn't exist" })
@ApiGoneResponse({
description:
"When the filtering by 'around', and the user doesn't have a PB"
})
@ApiGoneResponse({
description:
"When the filtering by 'friends', and the user doesn't have any Steam friends"
})
@ApiServiceUnavailableResponse({
description: "Steam fails to return the user's friends list (Tuesdays lol)"
})
getLeaderboards(
@Param('mapID', ParseIntSafePipe) mapID: number,
@LoggedInUser() { id, steamID }: UserJwtAccessPayload,
Expand All @@ -666,7 +680,8 @@ export class MapsController {
required: true
})
@ApiOkResponse({ description: 'The found run' })
@ApiNotFoundResponse({ description: 'Either map or run not found' })
@ApiNotFoundResponse({ description: 'Map not found' })
@ApiNotFoundResponse({ description: 'Run not found' })
getLeaderboardRun(
@Param('mapID', ParseIntSafePipe) mapID: number,
@LoggedInUser('id') userID: number,
Expand Down
16 changes: 10 additions & 6 deletions apps/backend/src/app/modules/runs/leaderboard-runs.service.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import {
BadRequestException,
forwardRef,
ImATeapotException,
GoneException,
Inject,
Injectable,
NotFoundException
NotFoundException,
ServiceUnavailableException
} from '@nestjs/common';
import { Prisma } from '@prisma/client';
import { runPath } from '@momentum/constants';
Expand Down Expand Up @@ -113,7 +114,7 @@ export class LeaderboardRunsService {
});

if (!userRun)
throw new NotFoundException('User has no runs on this leaderboard');
throw new GoneException('User has no runs on this leaderboard');

// Start at your rank, then backtrack by half of `take`, then 1 for your rank
skip = Math.max(userRun.rank - Math.floor(take / 2) - 1, 0);
Expand All @@ -122,11 +123,14 @@ export class LeaderboardRunsService {
} else if (query.filter?.[0] === 'friends') {
// Regular skip/take should work fine here.

const steamFriends =
await this.steamService.getSteamFriends(loggedInUserSteamID);
const steamFriends = await this.steamService
.getSteamFriends(loggedInUserSteamID)
.catch(() => {
throw new ServiceUnavailableException();
});

if (steamFriends.length === 0)
throw new ImATeapotException('No friends detected :(');
throw new GoneException('No friends detected :(');

// Doing this with a window function is gonna be fun...
where.user = {
Expand Down

0 comments on commit 96484c9

Please sign in to comment.