feature(database-connection): Add TimeTrackerPredicate and PrismaService to providers

This commit is contained in:
2021-01-12 14:14:48 +01:00
parent 4fe3d6a984
commit c514337fea
4 changed files with 126 additions and 88 deletions

View File

@@ -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('*');
} }
} }

View File

@@ -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);
});
} }

View 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();
});
});

View 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();
}
}