Skip to content

Commit c4ae51a

Browse files
committed
fix: calculate user score only for their current coalition
1 parent 049a887 commit c4ae51a

3 files changed

Lines changed: 8 additions & 4 deletions

File tree

src/routes/charts.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ export const setupChartRoutes = function(app: Express, prisma: PrismaClient): vo
259259
}
260260
dates.push(now); // always add the current score
261261

262-
// Get all scores for this user for the given timespan
262+
// Get all scores for this user for the given timespan, and in the coalition they are currently in
263263
const scoreSumsPerDate: { [key: number]: number } = {};
264264
for (const date of dates) {
265265
const scores = await prisma.codamCoalitionScore.groupBy({
@@ -269,6 +269,7 @@ export const setupChartRoutes = function(app: Express, prisma: PrismaClient): vo
269269
},
270270
where: {
271271
user_id: user.id,
272+
coalition_id: user.coalition_users[0].coalition.id,
272273
created_at: {
273274
gte: blocStart,
274275
lte: date,
@@ -390,7 +391,7 @@ export const setupChartRoutes = function(app: Express, prisma: PrismaClient): vo
390391
// @ts-ignore
391392
fixedPointTypes.push({ type: null }); // replacement for null
392393

393-
// Get all scores for this user for the given timespan
394+
// Get all scores for this user for the given timespan and coalition, split by fixed point type
394395
const scoreSumsPerTypePerDate: { [key: string]: { [key: number]: number } } = {};
395396
for (const fixedPointType of fixedPointTypes) {
396397
scoreSumsPerTypePerDate[fixedPointType.type || 'null'] = {};
@@ -403,6 +404,7 @@ export const setupChartRoutes = function(app: Express, prisma: PrismaClient): vo
403404
},
404405
where: {
405406
user_id: user.id,
407+
coalition_id: user.coalition_users[0].coalition.id,
406408
created_at: {
407409
gte: blocStart,
408410
lte: date,

src/routes/profile.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ export const setupProfileRoutes = function(app: Express, prisma: PrismaClient):
3030
}
3131

3232
const now = new Date();
33-
const { userScores, totalScore } = await getUserScores(prisma, profileUser.id, now);
33+
const userCoalitionId = profileUser.coalition_users.length > 0 ? profileUser.coalition_users[0].coalition_id : undefined;
34+
const { userScores, totalScore } = await getUserScores(prisma, profileUser.id, userCoalitionId, now);
3435
const ranking = await getUserSeasonRanking(prisma, profileUser.id);
3536

3637
const latestScores = await prisma.codamCoalitionScore.findMany({

src/utils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ export const getUsersScores = async function(prisma: PrismaClient, coalitionId:
427427
return usersScores;
428428
};
429429

430-
export const getUserScores = async function(prisma: PrismaClient, userId: number, untilDate: Date = new Date()): Promise<{ userScores: { fixed_type_id: string | null, _sum: { amount: number | null } }[], totalScore: number }> {
430+
export const getUserScores = async function(prisma: PrismaClient, userId: number, coalitionId: number | undefined = undefined, untilDate: Date = new Date()): Promise<{ userScores: { fixed_type_id: string | null, _sum: { amount: number | null } }[], totalScore: number }> {
431431
const bloc = await getBlocAtDate(prisma, untilDate);
432432
const userScores = (bloc ? (await prisma.codamCoalitionScore.groupBy({
433433
by: ['fixed_type_id'],
@@ -436,6 +436,7 @@ export const getUserScores = async function(prisma: PrismaClient, userId: number
436436
},
437437
where: {
438438
user_id: userId,
439+
coalition_id: coalitionId, // if set to undefined, get user's scores across multiple coalitions (can happen if user switched coalitions mid-season)
439440
created_at: {
440441
gte: bloc.begin_at,
441442
lte: untilDate,

0 commit comments

Comments
 (0)