Skip to content

Commit

Permalink
fix(api/users): PVランキングに誰もいない場合エラーになる問題を修正
Browse files Browse the repository at this point in the history
  • Loading branch information
u1-liquid committed Sep 17, 2024
1 parent 8e9b6dc commit a8334e1
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions packages/backend/src/server/api/endpoints/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
query.andWhere('user.host = :hostname', { hostname: ps.hostname.toLowerCase() });
}

let pvUsers: { userId: string; count: number; }[] | undefined = undefined;
let pvRankedUsers: { userId: string; count: number; }[] | undefined = undefined;
if (ps.sort?.endsWith('pv')) {
// 直近12時間のPVランキングを取得
pvUsers = await this.perUserPvChart.getUsersRanking(
pvRankedUsers = await this.perUserPvChart.getUsersRanking(
'hour', ps.sort.startsWith('+') ? 'DESC' : 'ASC',
12, null, ps.limit, ps.offset,
);
Expand All @@ -89,8 +89,8 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
case '-createdAt': query.orderBy('user.id', 'ASC'); break;
case '+updatedAt': query.andWhere('user.updatedAt IS NOT NULL').orderBy('user.updatedAt', 'DESC'); break;
case '-updatedAt': query.andWhere('user.updatedAt IS NOT NULL').orderBy('user.updatedAt', 'ASC'); break;
case '+pv': query.andWhere('user.id IN (:...userIds)', { userIds: pvUsers?.map(user => user.userId) ?? [] }); break;
case '-pv': query.andWhere('user.id IN (:...userIds)', { userIds: pvUsers?.map(user => user.userId) ?? [] }); break;
case '+pv': query.andWhere((pvRankedUsers?.length ?? 0) > 0 ? 'user.id IN (:...userIds)' : '1 = 0', { userIds: pvRankedUsers?.map(user => user.userId) ?? [] }); break;
case '-pv': query.andWhere((pvRankedUsers?.length ?? 0) > 0 ? 'user.id IN (:...userIds)' : '1 = 0', { userIds: pvRankedUsers?.map(user => user.userId) ?? [] }); break;
default: query.orderBy('user.id', 'ASC'); break;
}

Expand All @@ -103,14 +103,14 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
const users = await query.getMany();
if (ps.sort === '+pv') {
users.sort((a, b) => {
const aPv = pvUsers?.find(u => u.userId === a.id)?.count ?? 0;
const bPv = pvUsers?.find(u => u.userId === b.id)?.count ?? 0;
const aPv = pvRankedUsers?.find(u => u.userId === a.id)?.count ?? 0;
const bPv = pvRankedUsers?.find(u => u.userId === b.id)?.count ?? 0;
return bPv - aPv;
});
} else if (ps.sort === '-pv') {
users.sort((a, b) => {
const aPv = pvUsers?.find(u => u.userId === a.id)?.count ?? 0;
const bPv = pvUsers?.find(u => u.userId === b.id)?.count ?? 0;
const aPv = pvRankedUsers?.find(u => u.userId === a.id)?.count ?? 0;
const bPv = pvRankedUsers?.find(u => u.userId === b.id)?.count ?? 0;
return aPv - bPv;
});
}
Expand Down

0 comments on commit a8334e1

Please sign in to comment.