|
| 1 | +import { HttpException, Injectable, InternalServerErrorException } from "@nestjs/common"; |
| 2 | +import { AppInfoGender } from "generated/prisma/enums"; |
| 3 | +import { LoggerService } from "src/core/logger/logger.service"; |
| 4 | +import { PrismaService } from "src/core/prisma/prisma.service"; |
| 5 | +import { EducationLevel } from "./staff-leaderboard.controller"; |
| 6 | + |
| 7 | +const educationLevelKey = { |
| 8 | + m4: "มัธยมศึกษาปีที่ 4", |
| 9 | + m5: "มัธยมศึกษาปีที่ 5", |
| 10 | + v1: "ปวช. 1", |
| 11 | + v2: "ปวช. 2", |
| 12 | +}; |
| 13 | + |
| 14 | +@Injectable() |
| 15 | +export class StaffLeaderboardService { |
| 16 | + constructor( |
| 17 | + private readonly prisma: PrismaService, |
| 18 | + private readonly logger: LoggerService, |
| 19 | + ) {} |
| 20 | + |
| 21 | + async getPassApplication(gender: AppInfoGender, level: EducationLevel) { |
| 22 | + try { |
| 23 | + const getTopScore = await this.prisma.studentApplication.findMany({ |
| 24 | + where: { |
| 25 | + std_total_score: { |
| 26 | + isNot: null, |
| 27 | + }, |
| 28 | + std_info: { |
| 29 | + std_info_gender: gender, |
| 30 | + OR: [ |
| 31 | + { |
| 32 | + std_info_education_level: encodeURI(level === EducationLevel.M4 ? educationLevelKey.m4 : educationLevelKey.m5), |
| 33 | + }, |
| 34 | + { |
| 35 | + std_info_education_level: encodeURI(level === EducationLevel.M4 ? educationLevelKey.v1 : educationLevelKey.v2), |
| 36 | + }, |
| 37 | + ], |
| 38 | + }, |
| 39 | + }, |
| 40 | + orderBy: { |
| 41 | + std_total_score: { |
| 42 | + std_total_score: "desc", |
| 43 | + }, |
| 44 | + }, |
| 45 | + include: { |
| 46 | + std_total_score: true, |
| 47 | + std_status: true, |
| 48 | + std_info: true, |
| 49 | + }, |
| 50 | + skip: 0, |
| 51 | + take: 20, |
| 52 | + }); |
| 53 | + |
| 54 | + return getTopScore; |
| 55 | + } catch (e) { |
| 56 | + this.logger.error(e); |
| 57 | + if (e instanceof HttpException) { |
| 58 | + throw e; |
| 59 | + } |
| 60 | + |
| 61 | + throw new InternalServerErrorException(e); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + async getReserveApplication(gender: AppInfoGender, level: EducationLevel) { |
| 66 | + try { |
| 67 | + const getTopScore = await this.prisma.studentApplication.findMany({ |
| 68 | + where: { |
| 69 | + std_total_score: { |
| 70 | + isNot: null, |
| 71 | + }, |
| 72 | + std_info: { |
| 73 | + std_info_gender: gender, |
| 74 | + OR: [ |
| 75 | + { |
| 76 | + std_info_education_level: encodeURI(level === EducationLevel.M4 ? educationLevelKey.m4 : educationLevelKey.m5), |
| 77 | + }, |
| 78 | + { |
| 79 | + std_info_education_level: encodeURI(level === EducationLevel.M4 ? educationLevelKey.v1 : educationLevelKey.v2), |
| 80 | + }, |
| 81 | + ], |
| 82 | + }, |
| 83 | + }, |
| 84 | + orderBy: { |
| 85 | + std_total_score: { |
| 86 | + std_total_score: "desc", |
| 87 | + }, |
| 88 | + }, |
| 89 | + include: { |
| 90 | + std_total_score: true, |
| 91 | + std_status: true, |
| 92 | + std_info: true, |
| 93 | + }, |
| 94 | + take: 5, |
| 95 | + skip: 20, |
| 96 | + }); |
| 97 | + |
| 98 | + return getTopScore; |
| 99 | + } catch (e) { |
| 100 | + this.logger.error(e); |
| 101 | + if (e instanceof HttpException) { |
| 102 | + throw e; |
| 103 | + } |
| 104 | + |
| 105 | + throw new InternalServerErrorException(e); |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments