From 24d271a351482547b4127d60355b516d2dd5c63d Mon Sep 17 00:00:00 2001 From: PleBea Date: Fri, 12 Jan 2024 19:50:29 +0900 Subject: [PATCH] chore: Fix score sort --- src/modules/score/score.service.ts | 35 ++++++++++++++++-------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/src/modules/score/score.service.ts b/src/modules/score/score.service.ts index 5eb30d0..28c1e6e 100644 --- a/src/modules/score/score.service.ts +++ b/src/modules/score/score.service.ts @@ -1,7 +1,7 @@ import { Injectable } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { User } from 'src/shared/entities/user.entity'; -import { Repository } from 'typeorm'; +import { Brackets, Repository } from 'typeorm'; @Injectable() export class ScoreService { @@ -11,21 +11,24 @@ export class ScoreService { ) {} public async findAll() { - const users = await this.userRepository.find({ - select: ['id', 'name', 'score', 'solves'], - order: { - score: 'DESC', - solves: { - createdAt: 'ASC', - }, - }, - where: { - isAdmin: false, - solves: { - correct: true, - }, - }, - }); + const users = await this.userRepository + .createQueryBuilder('user') + .leftJoinAndSelect('user.solves', 'solve') + .where('user.isAdmin = :isAdmin', { isAdmin: false }) + .andWhere( + new Brackets((qb) => { + qb.where('solve.correct = :correct', { correct: true }) + .orWhere('solve.correct IS NULL') + .orWhere( + 'NOT EXISTS (SELECT 1 FROM solve subSolve WHERE subSolve.userId = user.id AND subSolve.correct = :trueCorrect)', + { trueCorrect: true }, + ); + }), + ) + .orderBy('user.score', 'DESC') + .addOrderBy('solve.createdAt', 'ASC') + .select(['user.id', 'user.name', 'user.score', 'solve']) + .getMany(); return users; }