Skip to content

Commit

Permalink
Merge pull request #35 from game-node-app/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Lamarcke authored Mar 7, 2024
2 parents da911fe + 4d7d23e commit 1f14967
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
24 changes: 23 additions & 1 deletion src/game/game-repository/game-repository.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { HttpException, HttpStatus, Injectable, Logger } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { Game } from "./entities/game.entity";
import { DataSource, In, Repository } from "typeorm";
import { DataSource, FindOptionsRelations, In, Repository } from "typeorm";
import { GameGenre } from "./entities/game-genre.entity";
import { GamePlatform } from "./entities/game-platform.entity";
import { GameTheme } from "./entities/game-theme.entity";
Expand Down Expand Up @@ -29,6 +29,7 @@ export type TAllowedResource = keyof typeof resourceToEntityMap;
@Injectable()
export class GameRepositoryService {
private readonly logger = new Logger(GameRepositoryService.name);
private readonly maximumAllowedRelationsQuery = 3;

/**
* @param dataSource
Expand All @@ -40,10 +41,29 @@ export class GameRepositoryService {
private readonly gameRepository: Repository<Game>,
) {}

private validateMaximumRelations(
relations: FindOptionsRelations<Game> | undefined,
) {
if (!relations) return;
const totalQueriedEntries = Object.entries(relations).filter(
([key, value]) => {
// E.g.: genres: true
return key != undefined && value;
},
).length;
if (totalQueriedEntries > this.maximumAllowedRelationsQuery) {
throw new HttpException(
`For performance reasons, queries with more than ${this.maximumAllowedRelationsQuery} relations are not allowed.`,
HttpStatus.BAD_REQUEST,
);
}
}

async findOneById(
id: number,
dto?: GameRepositoryFindOneDto,
): Promise<Game> {
this.validateMaximumRelations(dto?.relations);
const game = await this.gameRepository.findOne({
where: {
id,
Expand All @@ -64,6 +84,8 @@ export class GameRepositoryService {
) {
throw new HttpException("Invalid query.", HttpStatus.BAD_REQUEST);
}
this.validateMaximumRelations(dto?.relations);

const games = await this.gameRepository.find({
where: {
id: In(dto?.gameIds),
Expand Down
2 changes: 1 addition & 1 deletion src/notifications/notifications.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { fromPromise } from "rxjs/internal/observable/innerFrom";
import { PaginationInterceptor } from "../interceptor/pagination.interceptor";
import { PaginatedNotificationAggregationDto } from "./dto/paginated-notification-aggregation.dto";

const NOTIFICATIONS_CHECK_INTERVAL = 5000;
const NOTIFICATIONS_CHECK_INTERVAL = 20000;

@Controller("notifications")
@ApiTags("notifications")
Expand Down

0 comments on commit 1f14967

Please sign in to comment.