|
1 | 1 | // @ts-check |
2 | | -/* eslint-disable no-nested-ternary */ |
3 | 2 | const { Model } = require('objection') |
4 | 3 | const i18next = require('i18next') |
5 | 4 | const config = require('@rm/config') |
@@ -28,18 +27,29 @@ class Nest extends Model { |
28 | 27 | static async getAll(perms, args, { polygon }) { |
29 | 28 | const { areaRestrictions } = perms |
30 | 29 | const { minLat, minLon, maxLat, maxLon, filters } = args |
31 | | - const pokemon = [] |
32 | | - Object.keys(filters).forEach((pkmn) => { |
33 | | - if (!pkmn.startsWith('o')) { |
34 | | - pokemon.push(pkmn.split('-')[0]) |
35 | | - } |
36 | | - }) |
37 | 30 | const query = this.query() |
38 | 31 | .select(['*', 'nest_id AS id']) |
39 | | - .whereNotNull('pokemon_id') |
| 32 | + // .whereNotNull('pokemon_id') |
40 | 33 | .whereBetween('lat', [minLat, maxLat]) |
41 | 34 | .andWhereBetween('lon', [minLon, maxLon]) |
42 | | - .whereIn('pokemon_id', pokemon) |
| 35 | + |
| 36 | + const pokemon = [] |
| 37 | + if (filters.onlyPokemon) { |
| 38 | + Object.keys(filters).forEach((pkmn) => { |
| 39 | + if (!pkmn.startsWith('o')) { |
| 40 | + pokemon.push(pkmn.split('-')[0]) |
| 41 | + } |
| 42 | + }) |
| 43 | + } |
| 44 | + |
| 45 | + if (pokemon.length) { |
| 46 | + query.whereIn('pokemon_id', pokemon) |
| 47 | + } |
| 48 | + if (filters.onlyActive === 'inactive') { |
| 49 | + query.where('active', false) |
| 50 | + } else if (filters.onlyActive === 'active') { |
| 51 | + query.where('active', true) |
| 52 | + } |
43 | 53 | if ( |
44 | 54 | !config |
45 | 55 | .getSafe('defaultFilters.nests.avgFilter') |
@@ -91,24 +101,27 @@ class Nest extends Model { |
91 | 101 | static secondaryFilter(queryResults, filters, polygon) { |
92 | 102 | const returnedResults = [] |
93 | 103 | queryResults.forEach((pkmn) => { |
94 | | - if (pkmn.pokemon_form == 0 || pkmn.pokemon_form === null) { |
95 | | - const formId = |
96 | | - state.event.masterfile.pokemon[pkmn.pokemon_id].defaultFormId |
97 | | - if (formId) pkmn.pokemon_form = formId |
98 | | - } |
99 | | - if (filters[`${pkmn.pokemon_id}-${pkmn.pokemon_form}`]) { |
100 | | - pkmn.polygon_path = polygon |
101 | | - ? typeof pkmn.polygon === 'string' && pkmn.polygon |
102 | | - ? pkmn.polygon |
103 | | - : JSON.stringify( |
104 | | - pkmn.polygon || { type: 'Polygon', coordinates: [] }, |
105 | | - ) |
106 | | - : JSON.stringify({ |
107 | | - type: 'Polygon', |
108 | | - coordinates: JSON.parse(pkmn.polygon_path || '[]').map((line) => |
109 | | - line.map((point) => [point[1], point[0]]), |
110 | | - ), |
111 | | - }) |
| 104 | + pkmn.polygon_path = polygon |
| 105 | + ? typeof pkmn.polygon === 'string' && pkmn.polygon |
| 106 | + ? pkmn.polygon |
| 107 | + : JSON.stringify(pkmn.polygon || { type: 'Polygon', coordinates: [] }) |
| 108 | + : JSON.stringify({ |
| 109 | + type: 'Polygon', |
| 110 | + coordinates: JSON.parse(pkmn.polygon_path || '[]').map((line) => |
| 111 | + line.map((point) => [point[1], point[0]]), |
| 112 | + ), |
| 113 | + }) |
| 114 | + |
| 115 | + if (pkmn.pokemon_id && filters.onlyPokemon) { |
| 116 | + if (pkmn.pokemon_form == 0 || pkmn.pokemon_form === null) { |
| 117 | + const formId = |
| 118 | + state.event.masterfile.pokemon[pkmn.pokemon_id].defaultFormId |
| 119 | + if (formId) pkmn.pokemon_form = formId |
| 120 | + } |
| 121 | + if (filters[`${pkmn.pokemon_id}-${pkmn.pokemon_form}`]) { |
| 122 | + returnedResults.push(pkmn) |
| 123 | + } |
| 124 | + } else { |
112 | 125 | returnedResults.push(pkmn) |
113 | 126 | } |
114 | 127 | }) |
|
0 commit comments