From a8334e14998459894cb2492c9af3c9454e4c5b75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=81=BE=E3=81=A3=E3=81=A1=E3=82=83=E3=81=A8=E3=83=BC?= =?UTF-8?q?=E3=81=AB=E3=82=85?= <17376330+u1-liquid@users.noreply.github.com> Date: Tue, 17 Sep 2024 12:40:16 +0900 Subject: [PATCH] =?UTF-8?q?fix(api/users):=20PV=E3=83=A9=E3=83=B3=E3=82=AD?= =?UTF-8?q?=E3=83=B3=E3=82=B0=E3=81=AB=E8=AA=B0=E3=82=82=E3=81=84=E3=81=AA?= =?UTF-8?q?=E3=81=84=E5=A0=B4=E5=90=88=E3=82=A8=E3=83=A9=E3=83=BC=E3=81=AB?= =?UTF-8?q?=E3=81=AA=E3=82=8B=E5=95=8F=E9=A1=8C=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../backend/src/server/api/endpoints/users.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/backend/src/server/api/endpoints/users.ts b/packages/backend/src/server/api/endpoints/users.ts index 8c02b9ed54d7..3ba4f9e3f4b7 100644 --- a/packages/backend/src/server/api/endpoints/users.ts +++ b/packages/backend/src/server/api/endpoints/users.ts @@ -73,10 +73,10 @@ export default class extends Endpoint { // 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, ); @@ -89,8 +89,8 @@ export default class extends Endpoint { // 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; } @@ -103,14 +103,14 @@ export default class extends Endpoint { // 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; }); }