Skip to content

Commit

Permalink
feat(back): filterUserIDs query param on maps/{id}/leaderboard
Browse files Browse the repository at this point in the history
  • Loading branch information
tsa96 committed Feb 6, 2024
1 parent d721489 commit d67db78
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 13 deletions.
38 changes: 35 additions & 3 deletions apps/backend-e2e/src/maps-2.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1461,6 +1461,21 @@ describe('Maps Part 2', () => {
token
}));

it('should respond with filtered runs given using the filterUserID param', async () => {
const res = await req.get({
url: `maps/${map.id}/leaderboard`,
query: {
gamemode: Gamemode.AHOP,
filterUserIDs: `${u1.id},${u3.id}`
},
validatePaged: { type: MinimalLeaderboardRunDto, count: 2 },
token
});

expect(res.body.data[0].userID).toBe(u1.id);
expect(res.body.data[1].userID).toBe(u3.id);
});

// Test that permissions checks are getting called
// Yes, u1 has runs on the map, but we don't actually test for that
it('should 403 if the user does not have permission to access to the map', async () => {
Expand Down Expand Up @@ -1495,7 +1510,7 @@ describe('Maps Part 2', () => {
});

describe("GET - 'around' filter", () => {
let map, user7Token, runs;
let map, u7, u7Token, runs;

beforeAll(async () => {
map = await db.createMap();
Expand All @@ -1508,7 +1523,8 @@ describe('Maps Part 2', () => {
})
)
);
user7Token = auth.login(runs[6].user);
u7 = runs[6].user;
u7Token = auth.login(u7);
});

afterAll(() => db.cleanup('leaderboardRun', 'pastRun', 'user', 'mMap'));
Expand All @@ -1518,7 +1534,7 @@ describe('Maps Part 2', () => {
url: `maps/${map.id}/leaderboard`,
query: { gamemode: Gamemode.AHOP, filter: 'around', take: 8 },
status: 200,
token: user7Token,
token: u7Token,
validatePaged: { type: MinimalLeaderboardRunDto, count: 9 }
});

Expand All @@ -1534,6 +1550,22 @@ describe('Maps Part 2', () => {
// 12.
expect(rankIndex).toBe(12);
});

it('should return a list of ranks around your rank filter by userID if given', async () => {
const res = await req.get({
url: `maps/${map.id}/leaderboard`,
query: {
gamemode: Gamemode.AHOP,
filter: 'around',
filterUserIDs: u7.id
},
status: 200,
token: u7Token,
validatePaged: { type: MinimalLeaderboardRunDto, count: 1 }
});

expect(res.body.data[0].userID).toBe(u7.id);
});
});

describe("GET - 'friends' filter", () => {
Expand Down
4 changes: 4 additions & 0 deletions apps/backend/src/app/dto/queries/map-queries.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
EnumQueryProperty,
ExpandQueryProperty,
FilterQueryProperty,
IntCsvQueryProperty,
IntQueryProperty,
SingleExpandQueryProperty,
SkipQueryProperty,
Expand Down Expand Up @@ -266,6 +267,9 @@ export class MapLeaderboardGetQueryDto
})
readonly filter?: MapRunsGetFilter;

@IntCsvQueryProperty({ description: 'List of users to limit results to' })
readonly filterUserIDs?: number[];

@BooleanQueryProperty({
description: 'Whether to order by date or not (false for reverse)'
})
Expand Down
30 changes: 20 additions & 10 deletions apps/backend/src/app/modules/runs/leaderboard-runs.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ export class LeaderboardRunsService {
style: query.style
};

if (query.filterUserIDs) {
where.userID = { in: query.filterUserIDs };
}

const select = {
...this.minimalRunsSelect,
stats: Boolean(query.expand)
Expand All @@ -89,17 +93,23 @@ export class LeaderboardRunsService {
// Potentially a faster way of doing this in one query in raw SQL, something
// to investigate when we move to that/query builder.
if (query.filter?.[0] === 'around') {
const userRun = await this.db.leaderboardRun.findUnique({
where: {
userID_gamemode_style_mapID_trackType_trackNum: {
mapID,
userID: loggedInUserID,
gamemode: query.gamemode,
trackType: query.trackType,
trackNum: query.trackNum,
style: query.style
}
const whereAround: Prisma.LeaderboardRunWhereUniqueInput = {
userID_gamemode_style_mapID_trackType_trackNum: {
mapID,
userID: loggedInUserID,
gamemode: query.gamemode,
trackType: query.trackType,
trackNum: query.trackNum,
style: query.style
}
};

// if (query.filterUserIDs) {
// where.userID = { in: query.filterUserIDs };
// }

const userRun = await this.db.leaderboardRun.findUnique({
where: whereAround
});

if (!userRun)
Expand Down
1 change: 1 addition & 0 deletions libs/constants/src/types/queries/map-queries.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ export type MapLeaderboardGetQuery = PagedQuery & {
style?: Style; // Default 0
expand?: MapRunsGetExpand;
filter?: MapRunsGetFilter;
filterUserIDs?: number[];
orderByDate?: boolean;
};

Expand Down

0 comments on commit d67db78

Please sign in to comment.