feature(database-connection): Add TimeTrackerPredicate and PrismaService to providers
This commit is contained in:
@@ -1,17 +1,19 @@
|
|||||||
import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common';
|
import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common';
|
||||||
import { AppController } from './app.controller';
|
import { AppController } from './app.controller';
|
||||||
import { AppService } from './app.service';
|
import { AppService } from './app.service';
|
||||||
import { SinusBotService } from "./services/sinusbot.service";
|
import { SinusBotService } from './services/sinusbot.service';
|
||||||
import { DatabaseService } from './database/database.service';
|
import { DatabaseService } from './database/database.service';
|
||||||
import { LoggerMiddleware } from './logger.middleware';
|
import { LoggerMiddleware } from './logger.middleware';
|
||||||
|
import { PrismaService } from './prisma/prisma.service';
|
||||||
|
import { TimeTrackerPredicate } from './database/timetracking.predicate';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [],
|
imports: [],
|
||||||
controllers: [AppController],
|
controllers: [AppController],
|
||||||
providers: [AppService, SinusBotService, DatabaseService],
|
providers: [AppService, SinusBotService, DatabaseService, PrismaService, TimeTrackerPredicate],
|
||||||
})
|
})
|
||||||
export class AppModule implements NestModule {
|
export class AppModule implements NestModule {
|
||||||
configure(consumer: MiddlewareConsumer): any {
|
configure(consumer: MiddlewareConsumer): any {
|
||||||
consumer.apply(LoggerMiddleware).forRoutes('*')
|
consumer.apply(LoggerMiddleware).forRoutes('*');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,96 +1,100 @@
|
|||||||
import { Injectable } from '@nestjs/common';
|
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 { TimeTrackerPredicate } from './timetracking.predicate';
|
||||||
import { SeasonInfo, TimeTrackerStats, UserStatsResponse } from '../models/aliases';
|
import { SeasonInfo, TimeTrackerStats, UserStatsResponse } from '../models/aliases';
|
||||||
|
import { PrismaService } from '../prisma/prisma.service';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class DatabaseService {
|
export class DatabaseService {
|
||||||
// private host = process.env.MYSQL_HOST
|
// private host = process.env.MYSQL_HOST
|
||||||
// private port = process.env.MYSQL_PORT
|
// private port = process.env.MYSQL_PORT
|
||||||
// private credentials = {
|
// private credentials = {
|
||||||
// username: process.env.MYSQL_USERNAME,
|
// username: process.env.MYSQL_USERNAME,
|
||||||
// password: process.env.MYSQL_PASSWORD
|
// password: process.env.MYSQL_PASSWORD
|
||||||
// }
|
// }
|
||||||
// private database = process.env.MYSQL_DATABASE
|
// private database = process.env.MYSQL_DATABASE
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private readonly prismaClient: PrismaClient,
|
private readonly prismaClient: PrismaService,
|
||||||
private readonly timetrackerPredicate: TimeTrackerPredicate
|
private readonly timetrackerPredicate: TimeTrackerPredicate,
|
||||||
) {}
|
) {
|
||||||
|
}
|
||||||
|
|
||||||
public fetchSeasonInfos = async (seasonId: string): Promise<SeasonInfo> => {
|
public fetchSeasonInfos = async (seasonId: string): Promise<SeasonInfo> => {
|
||||||
let seasons
|
let seasons;
|
||||||
return this.prismaClient.seasons.findOne({
|
return this.prismaClient.seasons
|
||||||
where: {
|
.findOne({
|
||||||
// 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: {
|
where: {
|
||||||
// eslint-disable-next-line @typescript-eslint/camelcase
|
// eslint-disable-next-line @typescript-eslint/camelcase
|
||||||
season_id: Number(seasonId)
|
season_id: Number(seasonId),
|
||||||
}
|
},
|
||||||
})
|
})
|
||||||
|
.then(result => (seasons = result))
|
||||||
|
.then(this.fetchMaxSeasonId)
|
||||||
|
.then(result => {
|
||||||
|
seasons.maxSeasonId = result;
|
||||||
|
return seasons;
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
fetchMaxSeasonId: () => Promise<number> = () => new Promise(
|
public fetchStats = async (seasonId: string): Promise<UserStatsResponse> => {
|
||||||
(resolve, reject) => {
|
let response;
|
||||||
this.prismaClient.seasons.aggregate({
|
return this.fetchSeasonInfos(seasonId)
|
||||||
max: {
|
.then(result => {
|
||||||
// eslint-disable-next-line @typescript-eslint/camelcase
|
response = {
|
||||||
season_id: true
|
seasonId: result.season_id,
|
||||||
}
|
maxSeasonid: result.maxSeasonId,
|
||||||
})
|
dates: {
|
||||||
.then(result => result.max.season_id)
|
start: result.start_date,
|
||||||
.then(resolve)
|
end: result.end_date,
|
||||||
.catch(reject)
|
},
|
||||||
}
|
};
|
||||||
)
|
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);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
18
backend/src/prisma/prisma.service.spec.ts
Normal file
18
backend/src/prisma/prisma.service.spec.ts
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
import { Test, TestingModule } from '@nestjs/testing';
|
||||||
|
import { PrismaService } from './prisma.service';
|
||||||
|
|
||||||
|
describe('PrismaService', () => {
|
||||||
|
let service: PrismaService;
|
||||||
|
|
||||||
|
beforeEach(async () => {
|
||||||
|
const module: TestingModule = await Test.createTestingModule({
|
||||||
|
providers: [PrismaService],
|
||||||
|
}).compile();
|
||||||
|
|
||||||
|
service = module.get<PrismaService>(PrismaService);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should be defined', () => {
|
||||||
|
expect(service).toBeDefined();
|
||||||
|
});
|
||||||
|
});
|
||||||
14
backend/src/prisma/prisma.service.ts
Normal file
14
backend/src/prisma/prisma.service.ts
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
import { Injectable, OnModuleInit, OnModuleDestroy } from '@nestjs/common';
|
||||||
|
import { PrismaClient } from '@prisma/client';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class PrismaService extends PrismaClient
|
||||||
|
implements OnModuleInit, OnModuleDestroy {
|
||||||
|
async onModuleInit() {
|
||||||
|
await this.$connect();
|
||||||
|
}
|
||||||
|
|
||||||
|
async onModuleDestroy() {
|
||||||
|
await this.$disconnect();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user