Skip to content

Commit

Permalink
chore: Fix score sort
Browse files Browse the repository at this point in the history
  • Loading branch information
PleBea committed Jan 12, 2024
1 parent 2b1f848 commit 24d271a
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions src/modules/score/score.service.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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;
}
Expand Down

0 comments on commit 24d271a

Please sign in to comment.