97 lines
3.2 KiB
TypeScript
97 lines
3.2 KiB
TypeScript
import { Injectable } from '@nestjs/common';
|
|
import { PrismaClient, seasons, timetracker, user, ranks } from '@prisma/client';
|
|
import logger from 'src/logger/Logger';
|
|
import { TimeTrackerPredicate } from './timetracking.predicate';
|
|
import { SeasonInfo, TimeTrackerStats, UserStatsResponse } from '../models/aliases';
|
|
|
|
@Injectable()
|
|
export class DatabaseService {
|
|
// private host = process.env.MYSQL_HOST
|
|
// private port = process.env.MYSQL_PORT
|
|
// private credentials = {
|
|
// username: process.env.MYSQL_USERNAME,
|
|
// password: process.env.MYSQL_PASSWORD
|
|
// }
|
|
// private database = process.env.MYSQL_DATABASE
|
|
|
|
constructor(
|
|
private readonly prismaClient: PrismaClient,
|
|
private readonly timetrackerPredicate: TimeTrackerPredicate
|
|
) {}
|
|
|
|
public fetchSeasonInfos = async (seasonId: string): Promise<SeasonInfo> => {
|
|
let seasons
|
|
return this.prismaClient.seasons.findOne({
|
|
where: {
|
|
// eslint-disable-next-line @typescript-eslint/camelcase
|
|
season_id: Number(seasonId)
|
|
}
|
|
})
|
|
.then(result => seasons = result)
|
|
.then(this.fetchMaxSeasonId)
|
|
.then(result => {
|
|
seasons.maxSeasonId = result
|
|
return seasons
|
|
})
|
|
};
|
|
|
|
public fetchStats = async (seasonId: string): Promise<UserStatsResponse> => {
|
|
let response
|
|
return this.fetchSeasonInfos(seasonId)
|
|
.then(result => {
|
|
response = {
|
|
seasonId: result.season_id,
|
|
maxSeasonid: result.maxSeasonId,
|
|
dates: {
|
|
start: result.start_date,
|
|
end: result.end_date
|
|
}
|
|
}
|
|
return result.season_id.toString()
|
|
})
|
|
.then(this.fetchTimeTrackerStats)
|
|
.then(this.timetrackerPredicate.process)
|
|
.then(result => {
|
|
response.stats = result
|
|
return response
|
|
})
|
|
};
|
|
|
|
// public fetchStats = async (seasonId: string): Promise<TableEntry[]> => this.prismaClient.timetracker.findMany({
|
|
// include: {
|
|
// user: true,
|
|
// ranks: true
|
|
// },
|
|
// where: {
|
|
// // eslint-disable-next-line @typescript-eslint/camelcase
|
|
// season_id: Number(seasonId)
|
|
// }
|
|
// })
|
|
// .then(this.timetrackerPredicate.process);
|
|
|
|
fetchTimeTrackerStats: (string) => Promise<TimeTrackerStats[]> = (seasonId: string) => this.prismaClient.timetracker.findMany({
|
|
include: {
|
|
user: true,
|
|
ranks: true
|
|
},
|
|
where: {
|
|
// eslint-disable-next-line @typescript-eslint/camelcase
|
|
season_id: Number(seasonId)
|
|
}
|
|
})
|
|
|
|
fetchMaxSeasonId: () => Promise<number> = () => new Promise(
|
|
(resolve, reject) => {
|
|
this.prismaClient.seasons.aggregate({
|
|
max: {
|
|
// eslint-disable-next-line @typescript-eslint/camelcase
|
|
season_id: true
|
|
}
|
|
})
|
|
.then(result => result.max.season_id)
|
|
.then(resolve)
|
|
.catch(reject)
|
|
}
|
|
)
|
|
}
|