AI Implementation feature(857): Authenticated Shell: Header, Quick Tabs and Change Password (#14)
This commit was merged in pull request #14.
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { EntityManager } from 'typeorm';
|
||||
import { SolveEntity } from '../../database/entities/solve.entity';
|
||||
|
||||
export interface RankInfo {
|
||||
rank: number | null;
|
||||
points: number;
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class UsersRankService {
|
||||
async rankOfUser(manager: EntityManager, userId: string): Promise<RankInfo> {
|
||||
const solveRepo = manager.getRepository(SolveEntity);
|
||||
const pointsRow = await solveRepo
|
||||
.createQueryBuilder('s')
|
||||
.select('COALESCE(SUM(s.pointsAwarded), 0)', 'total')
|
||||
.where('s.userId = :userId', { userId })
|
||||
.getRawOne<{ total: string | number | null }>();
|
||||
const points = Number(pointsRow?.total ?? 0);
|
||||
|
||||
if (points <= 0) {
|
||||
return { rank: null, points: 0 };
|
||||
}
|
||||
|
||||
const ahead = await solveRepo
|
||||
.createQueryBuilder('s')
|
||||
.select('s.userId', 'userId')
|
||||
.addSelect('SUM(s.pointsAwarded)', 'total')
|
||||
.groupBy('s.userId')
|
||||
.having('SUM(s.pointsAwarded) > :points', { points })
|
||||
.getRawMany<{ userId: string }>();
|
||||
return { rank: ahead.length + 1, points };
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,16 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { forwardRef, Module } from '@nestjs/common';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { UserEntity } from '../../database/entities/user.entity';
|
||||
import { SolveEntity } from '../../database/entities/solve.entity';
|
||||
import { UsersService } from './users.service';
|
||||
import { UsersRankService } from './users-rank.service';
|
||||
import { UsersController } from './users.controller';
|
||||
import { AuthModule } from '../auth/auth.module';
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([UserEntity]), AuthModule],
|
||||
providers: [UsersService],
|
||||
imports: [TypeOrmModule.forFeature([UserEntity, SolveEntity]), forwardRef(() => AuthModule)],
|
||||
providers: [UsersService, UsersRankService],
|
||||
controllers: [UsersController],
|
||||
exports: [UsersService],
|
||||
exports: [UsersService, UsersRankService],
|
||||
})
|
||||
export class UsersModule {}
|
||||
Reference in New Issue
Block a user