|
1 | 1 | import { Injectable } from '@nestjs/common'; |
2 | 2 | import { InjectRepository } from '@nestjs/typeorm'; |
3 | | -import { DataSource, Repository } from 'typeorm'; |
| 3 | +import { In, Not, Repository } from 'typeorm'; |
4 | 4 | import { Couple } from './entities/couple.entity'; |
5 | | -import { PaginatedDto } from '../../common/dto/paginated.dto'; |
6 | 5 | import { RankingDto } from './dto/ranking.dto'; |
7 | 6 | import { IGNORE_COUPLE_IDS } from '../eco-verification/constant/ignore-couple-ids'; |
8 | 7 | import { coupleScoreQB } from './constant/score-query.helper'; |
| 8 | +import { PaginatedDto } from '../../common/dto/paginated.dto'; |
9 | 9 |
|
10 | 10 | @Injectable() |
11 | 11 | export class CoupleRankingService { |
12 | 12 | constructor( |
13 | 13 | @InjectRepository(Couple) private coupleRepo: Repository<Couple>, |
14 | | - private readonly dataSource: DataSource, |
15 | 14 | ) {} |
16 | 15 |
|
| 16 | + /* ---------------- 랭킹 리스트 ---------------- */ |
17 | 17 | async getRankings(page = 1, limit = 30): Promise<PaginatedDto<RankingDto>> { |
18 | 18 | const offset = (page - 1) * limit; |
19 | 19 |
|
20 | 20 | const qb = coupleScoreQB( |
21 | | - this.dataSource, |
22 | | - { cum: 700, avg: 300 }, |
| 21 | + this.coupleRepo.manager.connection, |
| 22 | + { cum: 1, avg: 0.3 }, |
23 | 23 | IGNORE_COUPLE_IDS, |
24 | 24 | ) |
25 | | - .orderBy('ecoScore', 'DESC') // 이미 윈도 함수 기준으로 맞춤 |
26 | | - .addOrderBy('b.ecoCnt', 'DESC') |
27 | | - .addOrderBy('b.createdAt', 'ASC') |
| 25 | + .orderBy('ecoScore', 'DESC') |
| 26 | + .addOrderBy('ecoVerificationCount', 'DESC') |
| 27 | + .addOrderBy('createdAt', 'ASC') |
28 | 28 | .offset(offset) |
29 | 29 | .limit(limit); |
30 | 30 |
|
31 | 31 | const [rows, total] = await Promise.all([ |
32 | 32 | qb.getRawMany<RankingDto>(), |
33 | | - (() => { |
34 | | - const cntQb = this.coupleRepo.createQueryBuilder('c'); |
35 | | - if (IGNORE_COUPLE_IDS.length) { |
36 | | - cntQb.where('c.id NOT IN (:...excludeIds)', { |
37 | | - excludeIds: IGNORE_COUPLE_IDS, |
38 | | - }); |
39 | | - } |
40 | | - return cntQb.getCount(); |
41 | | - })(), |
| 33 | + this.coupleRepo.count({ |
| 34 | + where: { id: Not(In(IGNORE_COUPLE_IDS)) }, |
| 35 | + }), |
42 | 36 | ]); |
43 | 37 |
|
44 | | - const results: RankingDto[] = rows.map((r) => ({ |
45 | | - coupleId: r.coupleId, |
46 | | - name: r.name, |
47 | | - profileImageUrl: r.profileImageUrl, |
48 | | - ecoScore: Number(r.ecoScore), |
49 | | - cumulativeEcoLovePoints: Number(r.cumulativeEcoLovePoints), |
50 | | - ecoVerificationCount: Number(r.ecoVerificationCount), |
51 | | - })); |
52 | | - |
53 | | - return { results, total, limit, page }; |
| 38 | + return { |
| 39 | + results: rows.map((r) => ({ |
| 40 | + coupleId: r.coupleId, |
| 41 | + name: r.name, |
| 42 | + profileImageUrl: r.profileImageUrl, |
| 43 | + ecoScore: Number(r.ecoScore), |
| 44 | + cumulativeEcoLovePoints: Number(r.cumulativeEcoLovePoints), |
| 45 | + ecoVerificationCount: Number(r.ecoVerificationCount), |
| 46 | + })), |
| 47 | + total, |
| 48 | + limit, |
| 49 | + page, |
| 50 | + }; |
54 | 51 | } |
55 | 52 | } |
0 commit comments