feat(*): Add ability to filter by game and region

This commit is contained in:
Josh Creek
2024-07-14 20:51:05 +01:00
parent 62c03f41eb
commit 0285725b6d
3 changed files with 126 additions and 16 deletions
+35 -2
View File
@@ -6,7 +6,9 @@ class CombinedDataRepository {
async findCombinedData(
page: number = 1,
limit: number = 20,
enableForms: boolean = true
enableForms: boolean = true,
region: string = '',
game: string = ''
): Promise<CombinedData[]> {
const skip = (page - 1) * limit;
const pipeline: any[] = [
@@ -43,6 +45,22 @@ class CombinedDataRepository {
});
}
if (region.length > 0) {
pipeline.unshift({
$match: {
regionToCatchIn: region
}
});
}
if (game.length > 0) {
pipeline.unshift({
$match: {
gamesToCatchIn: { $in: [game] }
}
});
}
const combinedData: CombinedData[] = await PokedexEntryModel.aggregate(pipeline).exec();
// Filter out entries with no catch records
@@ -54,13 +72,28 @@ class CombinedDataRepository {
}));
}
async countCombinedData(enableForms: boolean): Promise<number> {
async countCombinedData(enableForms: boolean, region: string, game: string): Promise<number> {
const filter: any = {};
if (!enableForms) {
filter['boxPlacement.box'] = { $ne: null };
}
if (region.length > 0) {
filter['regionToCatchIn'] = region;
}
if (game.length > 0) {
// Check if gamesToCatchIn is not already defined in filter
if (!filter['gamesToCatchIn']) {
filter['gamesToCatchIn'] = [];
}
// Add the game to the filter if it's not already included
if (!filter['gamesToCatchIn'].includes(game)) {
filter['gamesToCatchIn'].push(game);
}
}
return PokedexEntryModel.countDocuments(filter).exec();
}
}