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 { AppController } from './app.controller';
import { AppService } from './app.service';
import { SinusBotService } from "./services/sinusbot.service";
import { SinusBotService } from './services/sinusbot.service';
import { DatabaseService } from './database/database.service';
import { LoggerMiddleware } from './logger.middleware';
import { PrismaService } from './prisma/prisma.service';
import { TimeTrackerPredicate } from './database/timetracking.predicate';
@Module({
imports: [],
controllers: [AppController],
providers: [AppService, SinusBotService, DatabaseService],
providers: [AppService, SinusBotService, DatabaseService, PrismaService, TimeTrackerPredicate],
})
export class AppModule implements NestModule {
configure(consumer: MiddlewareConsumer): any {
consumer.apply(LoggerMiddleware).forRoutes('*')
consumer.apply(LoggerMiddleware).forRoutes('*');
}
}

View File

@@ -1,8 +1,7 @@
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';
import { PrismaService } from '../prisma/prisma.service';
@Injectable()
export class DatabaseService {
@@ -15,28 +14,30 @@ export class DatabaseService {
// private database = process.env.MYSQL_DATABASE
constructor(
private readonly prismaClient: PrismaClient,
private readonly timetrackerPredicate: TimeTrackerPredicate
) {}
private readonly prismaClient: PrismaService,
private readonly timetrackerPredicate: TimeTrackerPredicate,
) {
}
public fetchSeasonInfos = async (seasonId: string): Promise<SeasonInfo> => {
let seasons
return this.prismaClient.seasons.findOne({
let seasons;
return this.prismaClient.seasons
.findOne({
where: {
// eslint-disable-next-line @typescript-eslint/camelcase
season_id: Number(seasonId)
}
season_id: Number(seasonId),
},
})
.then(result => seasons = result)
.then(result => (seasons = result))
.then(this.fetchMaxSeasonId)
.then(result => {
seasons.maxSeasonId = result
return seasons
})
seasons.maxSeasonId = result;
return seasons;
});
};
public fetchStats = async (seasonId: string): Promise<UserStatsResponse> => {
let response
let response;
return this.fetchSeasonInfos(seasonId)
.then(result => {
response = {
@@ -44,17 +45,17 @@ export class DatabaseService {
maxSeasonid: result.maxSeasonId,
dates: {
start: result.start_date,
end: result.end_date
}
}
return result.season_id.toString()
end: result.end_date,
},
};
return result.season_id.toString();
})
.then(this.fetchTimeTrackerStats)
.then(this.timetrackerPredicate.process)
.then(result => {
response.stats = result
return response
})
response.stats = result;
return response;
});
};
// public fetchStats = async (seasonId: string): Promise<TableEntry[]> => this.prismaClient.timetracker.findMany({
@@ -69,28 +70,31 @@ export class DatabaseService {
// })
// .then(this.timetrackerPredicate.process);
fetchTimeTrackerStats: (string) => Promise<TimeTrackerStats[]> = (seasonId: string) => this.prismaClient.timetracker.findMany({
fetchTimeTrackerStats: (string) => Promise<TimeTrackerStats[]> = (
seasonId: string,
) =>
this.prismaClient.timetracker.findMany({
include: {
user: true,
ranks: true
ranks: true,
},
where: {
// eslint-disable-next-line @typescript-eslint/camelcase
season_id: Number(seasonId)
}
})
season_id: Number(seasonId),
},
});
fetchMaxSeasonId: () => Promise<number> = () => new Promise(
(resolve, reject) => {
this.prismaClient.seasons.aggregate({
fetchMaxSeasonId: () => Promise<number> = () =>
new Promise((resolve, reject) => {
this.prismaClient.seasons
.aggregate({
max: {
// eslint-disable-next-line @typescript-eslint/camelcase
season_id: true
}
season_id: true,
},
})
.then(result => result.max.season_id)
.then(resolve)
.catch(reject)
}
)
.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();
}
}