-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* power rankings * add power rankings to spec * fix coverage reporting
- Loading branch information
Showing
6 changed files
with
170 additions
and
18 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
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
49 changes: 49 additions & 0 deletions
49
src/data-access-layer/__test__/leaderboard/power-rankings.test.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,49 @@ | ||
import { z } from "zod" | ||
import { cleanupPostgresAfterAll } from "../../../routes/testUtil" | ||
import { zIndividualLeaderboardEntry } from "../../../schema/components/LeaderboardData" | ||
import { zNaturalNumber } from "../../../schema/util" | ||
import { | ||
getIndividualWorldFirstPowerRankingsLeaderboard, | ||
searchIndividualWorldFirstPowerRankingsLeaderboard | ||
} from "../../leaderboard/individual/power-rankings" | ||
|
||
cleanupPostgresAfterAll() | ||
|
||
describe("getIndividualWorldFirstPowerRankingsLeaderboard", () => { | ||
it("returns the correct shape", async () => { | ||
const data = await getIndividualWorldFirstPowerRankingsLeaderboard({ | ||
skip: 24921, | ||
take: 27 | ||
}).catch(console.error) | ||
|
||
const parsed = z.array(zIndividualLeaderboardEntry).safeParse(data) | ||
if (!parsed.success) { | ||
expect(parsed.error.errors).toHaveLength(0) | ||
} else { | ||
expect(parsed.data.length).toBeGreaterThan(0) | ||
expect(parsed.success).toBe(true) | ||
} | ||
}) | ||
}) | ||
|
||
describe("searchIndividualWorldFirstPowerRankingsLeaderboard", () => { | ||
it("returns the correct shape", async () => { | ||
const data = await searchIndividualWorldFirstPowerRankingsLeaderboard({ | ||
take: 4, | ||
membershipId: "4611686018488107374" | ||
}).catch(console.error) | ||
|
||
const parsed = z | ||
.object({ | ||
page: zNaturalNumber(), | ||
entries: z.array(zIndividualLeaderboardEntry) | ||
}) | ||
.safeParse(data) | ||
if (!parsed.success) { | ||
expect(parsed.error.errors).toHaveLength(0) | ||
} else { | ||
expect(parsed.data.entries.length).toBeGreaterThan(0) | ||
expect(parsed.success).toBe(true) | ||
} | ||
}) | ||
}) |
64 changes: 64 additions & 0 deletions
64
src/data-access-layer/leaderboard/individual/power-rankings.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,64 @@ | ||
import { IndividualLeaderboardEntry } from "../../../schema/components/LeaderboardData" | ||
import { postgres } from "../../../services/postgres" | ||
|
||
export const getIndividualWorldFirstPowerRankingsLeaderboard = async ({ | ||
skip, | ||
take | ||
}: { | ||
skip: number | ||
take: number | ||
}) => { | ||
return await postgres.queryRows<IndividualLeaderboardEntry>( | ||
`SELECT | ||
world_first_player_rankings.position, | ||
world_first_player_rankings.rank, | ||
ROUND(world_first_player_rankings.score::numeric, 3) AS "value", | ||
JSONB_BUILD_OBJECT( | ||
'membershipId', membership_id::text, | ||
'membershipType', membership_type, | ||
'iconPath', icon_path, | ||
'displayName', display_name, | ||
'bungieGlobalDisplayName', bungie_global_display_name, | ||
'bungieGlobalDisplayNameCode', bungie_global_display_name_code, | ||
'lastSeen', last_seen, | ||
'isPrivate', is_private | ||
) as "playerInfo" | ||
FROM world_first_player_rankings | ||
JOIN player USING (membership_id) | ||
WHERE position > $1 AND position <= ($1 + $2) | ||
ORDER BY position ASC`, | ||
{ | ||
params: [skip, take], | ||
fetchCount: take | ||
} | ||
) | ||
} | ||
|
||
export const searchIndividualWorldFirstPowerRankingsLeaderboard = async ({ | ||
membershipId, | ||
take | ||
}: { | ||
membershipId: bigint | string | ||
take: number | ||
}) => { | ||
const result = await postgres.queryRow<{ position: number }>( | ||
`SELECT position | ||
FROM world_first_player_rankings | ||
WHERE membership_id = $1::bigint | ||
ORDER BY position ASC | ||
LIMIT 1`, | ||
{ | ||
params: [membershipId] | ||
} | ||
) | ||
if (!result) return null | ||
|
||
const page = Math.ceil(result.position / take) | ||
return { | ||
page, | ||
entries: await getIndividualWorldFirstPowerRankingsLeaderboard({ | ||
skip: (page - 1) * take, | ||
take | ||
}) | ||
} | ||
} |
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