Skip to content

Commit 1f14967

Browse files
authored
Merge pull request #35 from game-node-app/dev
Dev
2 parents da911fe + 4d7d23e commit 1f14967

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/game/game-repository/game-repository.service.ts

+23-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { HttpException, HttpStatus, Injectable, Logger } from "@nestjs/common";
22
import { InjectRepository } from "@nestjs/typeorm";
33
import { Game } from "./entities/game.entity";
4-
import { DataSource, In, Repository } from "typeorm";
4+
import { DataSource, FindOptionsRelations, In, Repository } from "typeorm";
55
import { GameGenre } from "./entities/game-genre.entity";
66
import { GamePlatform } from "./entities/game-platform.entity";
77
import { GameTheme } from "./entities/game-theme.entity";
@@ -29,6 +29,7 @@ export type TAllowedResource = keyof typeof resourceToEntityMap;
2929
@Injectable()
3030
export class GameRepositoryService {
3131
private readonly logger = new Logger(GameRepositoryService.name);
32+
private readonly maximumAllowedRelationsQuery = 3;
3233

3334
/**
3435
* @param dataSource
@@ -40,10 +41,29 @@ export class GameRepositoryService {
4041
private readonly gameRepository: Repository<Game>,
4142
) {}
4243

44+
private validateMaximumRelations(
45+
relations: FindOptionsRelations<Game> | undefined,
46+
) {
47+
if (!relations) return;
48+
const totalQueriedEntries = Object.entries(relations).filter(
49+
([key, value]) => {
50+
// E.g.: genres: true
51+
return key != undefined && value;
52+
},
53+
).length;
54+
if (totalQueriedEntries > this.maximumAllowedRelationsQuery) {
55+
throw new HttpException(
56+
`For performance reasons, queries with more than ${this.maximumAllowedRelationsQuery} relations are not allowed.`,
57+
HttpStatus.BAD_REQUEST,
58+
);
59+
}
60+
}
61+
4362
async findOneById(
4463
id: number,
4564
dto?: GameRepositoryFindOneDto,
4665
): Promise<Game> {
66+
this.validateMaximumRelations(dto?.relations);
4767
const game = await this.gameRepository.findOne({
4868
where: {
4969
id,
@@ -64,6 +84,8 @@ export class GameRepositoryService {
6484
) {
6585
throw new HttpException("Invalid query.", HttpStatus.BAD_REQUEST);
6686
}
87+
this.validateMaximumRelations(dto?.relations);
88+
6789
const games = await this.gameRepository.find({
6890
where: {
6991
id: In(dto?.gameIds),

src/notifications/notifications.controller.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import { fromPromise } from "rxjs/internal/observable/innerFrom";
2424
import { PaginationInterceptor } from "../interceptor/pagination.interceptor";
2525
import { PaginatedNotificationAggregationDto } from "./dto/paginated-notification-aggregation.dto";
2626

27-
const NOTIFICATIONS_CHECK_INTERVAL = 5000;
27+
const NOTIFICATIONS_CHECK_INTERVAL = 20000;
2828

2929
@Controller("notifications")
3030
@ApiTags("notifications")

0 commit comments

Comments
 (0)