Skip to content

Commit f14f63d

Browse files
committed
chore: code style
1 parent 8aa97ca commit f14f63d

File tree

8 files changed

+35
-64
lines changed

8 files changed

+35
-64
lines changed

server/src/models/Gym.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,9 @@ class Gym extends Model {
136136
const latCol = isMad ? 'latitude' : 'lat'
137137
const lonCol = isMad ? 'longitude' : 'lon'
138138
const idCol = isMad ? 'gym.gym_id' : 'id'
139-
const manualId =
140-
typeof onlyManualId === 'string' || typeof onlyManualId === 'number'
141-
? onlyManualId
142-
: null
143139

144140
applyManualIdFilter(query, {
145-
manualId,
141+
manualId: onlyManualId,
146142
latColumn: latCol,
147143
lonColumn: lonCol,
148144
idColumn: idCol,

server/src/models/Nest.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,9 @@ class Nest extends Model {
2828
static async getAll(perms, args, { polygon }) {
2929
const { areaRestrictions } = perms
3030
const { minLat, minLon, maxLat, maxLon, filters } = args
31-
const manualId =
32-
typeof filters.onlyManualId === 'string' ||
33-
typeof filters.onlyManualId === 'number'
34-
? filters.onlyManualId
35-
: null
3631
const query = this.query().select(['*', 'nest_id AS id'])
3732
applyManualIdFilter(query, {
38-
manualId,
33+
manualId: filters.onlyManualId,
3934
latColumn: 'lat',
4035
lonColumn: 'lon',
4136
idColumn: 'nest_id',

server/src/models/Pokemon.js

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ const config = require('@rm/config')
1414
const { getAreaSql } = require('../utils/getAreaSql')
1515
const { filterRTree } = require('../utils/filterRTree')
1616
const { fetchJson } = require('../utils/fetchJson')
17-
const { applyManualIdFilter } = require('../utils/manualFilter')
17+
const {
18+
applyManualIdFilter,
19+
normalizeManualId,
20+
} = require('../utils/manualFilter')
1821
const {
1922
IV_CALC,
2023
LEVEL_CALC,
@@ -147,15 +150,11 @@ class Pokemon extends Model {
147150
if (!noPokemonSelect) return []
148151
}
149152

150-
const manualIdRaw =
151-
typeof args.filters.onlyManualId === 'string' ||
152-
typeof args.filters.onlyManualId === 'number'
153-
? args.filters.onlyManualId
154-
: null
155-
156153
const query = this.query()
157154

158-
let manualId = null
155+
const manualIdFilter = normalizeManualId(args.filters.onlyManualId)
156+
157+
let manualId = manualIdFilter
159158

160159
const pokemonIds = []
161160
const pokemonForms = []
@@ -184,7 +183,7 @@ class Pokemon extends Model {
184183
isMad ? this.knex().fn.now() : ts,
185184
)
186185
manualId = applyManualIdFilter(query, {
187-
manualId: manualIdRaw,
186+
manualId: manualIdFilter,
188187
latColumn: isMad ? 'pokemon.latitude' : 'lat',
189188
lonColumn: isMad ? 'pokemon.longitude' : 'lon',
190189
idColumn: isMad ? 'pokemon.encounter_id' : 'id',
@@ -256,8 +255,6 @@ class Pokemon extends Model {
256255
if (!getAreaSql(query, areaRestrictions, onlyAreas, isMad, 'pokemon')) {
257256
return []
258257
}
259-
} else {
260-
manualId = manualIdRaw
261258
}
262259

263260
const filters = mem
@@ -687,11 +684,7 @@ class Pokemon extends Model {
687684
const { isMad, hasSize, hasHeight, mem, secret, httpAuth } = ctx
688685
const ts = Math.floor(Date.now() / 1000)
689686
const { filterMap, globalFilter } = this.getFilters(perms, args, ctx)
690-
const manualIdRaw =
691-
typeof args.filters.onlyManualId === 'string' ||
692-
typeof args.filters.onlyManualId === 'number'
693-
? args.filters.onlyManualId
694-
: null
687+
const manualIdFilter = normalizeManualId(args.filters.onlyManualId)
695688
const queryLimits = config.getSafe('api.queryLimits')
696689

697690
if (!perms.iv && !perms.pvp) {
@@ -732,7 +725,7 @@ class Pokemon extends Model {
732725
return []
733726
}
734727
const manualId = applyManualIdFilter(query, {
735-
manualId: manualIdRaw,
728+
manualId: manualIdFilter,
736729
latColumn: isMad ? 'pokemon.latitude' : 'lat',
737730
lonColumn: isMad ? 'pokemon.longitude' : 'lon',
738731
idColumn: isMad ? 'pokemon.encounter_id' : 'id',

server/src/models/Pokestop.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,6 @@ class Pokestop extends Model {
139139
const ts = Math.floor(Date.now() / 1000)
140140
const { queryLimits, stopValidDataLimit, hideOldPokestops } =
141141
config.getSafe('api')
142-
const manualId =
143-
typeof args.filters.onlyManualId === 'string' ||
144-
typeof args.filters.onlyManualId === 'number'
145-
? args.filters.onlyManualId
146-
: null
147-
148142
const {
149143
lures: lurePerms,
150144
quests: questPerms,
@@ -191,7 +185,7 @@ class Pokestop extends Model {
191185
}
192186
Pokestop.joinIncident(query, hasMultiInvasions, isMad, multiInvasionMs)
193187
applyManualIdFilter(query, {
194-
manualId,
188+
manualId: args.filters.onlyManualId,
195189
latColumn: isMad ? 'latitude' : 'lat',
196190
lonColumn: isMad ? 'longitude' : 'lon',
197191
idColumn: isMad ? 'pokestop.pokestop_id' : 'id',

server/src/models/Portal.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,9 @@ class Portal extends Model {
2929
maxLat,
3030
maxLon,
3131
} = args
32-
const manualId =
33-
typeof args.filters.onlyManualId === 'string' ||
34-
typeof args.filters.onlyManualId === 'number'
35-
? args.filters.onlyManualId
36-
: null
3732
const query = this.query()
3833
applyManualIdFilter(query, {
39-
manualId,
34+
manualId: args.filters.onlyManualId,
4035
latColumn: 'lat',
4136
lonColumn: 'lon',
4237
idColumn: 'id',

server/src/models/Route.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,6 @@ class Route extends Model {
4545
const ts =
4646
getEpoch() - config.getSafe('api.routeUpdateLimit') * 24 * 60 * 60
4747
const distanceInMeters = (onlyDistance || [0.5, 100]).map((x) => x * 1000)
48-
const manualId =
49-
typeof args.filters.onlyManualId === 'string' ||
50-
typeof args.filters.onlyManualId === 'number'
51-
? args.filters.onlyManualId
52-
: null
53-
5448
const startLatitude = isMad ? 'start_poi_latitude' : 'start_lat'
5549
const startLongitude = isMad ? 'start_poi_longitude' : 'start_lon'
5650
const distanceMeters = isMad ? 'route_distance_meters' : 'distance_meters'
@@ -61,8 +55,8 @@ class Route extends Model {
6155
const query = this.query().select(
6256
isMad ? GET_MAD_ALL_SELECT : GET_ALL_SELECT,
6357
)
64-
applyManualIdFilter(query, {
65-
manualId,
58+
const manualId = applyManualIdFilter(query, {
59+
manualId: args.filters.onlyManualId,
6660
latColumn: startLatitude,
6761
lonColumn: startLongitude,
6862
idColumn,

server/src/models/Station.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,6 @@ class Station extends Model {
3131
onlyGmaxStationed,
3232
} = args.filters
3333
const ts = getEpoch()
34-
const manualId =
35-
typeof args.filters.onlyManualId === 'string' ||
36-
typeof args.filters.onlyManualId === 'number'
37-
? args.filters.onlyManualId
38-
: null
39-
4034
const select = [
4135
'id',
4236
'name',
@@ -49,7 +43,7 @@ class Station extends Model {
4943

5044
const query = this.query()
5145
applyManualIdFilter(query, {
52-
manualId,
46+
manualId: args.filters.onlyManualId,
5347
latColumn: 'lat',
5448
lonColumn: 'lon',
5549
idColumn: 'id',

server/src/utils/manualFilter.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
// @ts-check
22

3+
/**
4+
* @param {unknown} manualId
5+
* @returns {string | number | null}
6+
*/
7+
function normalizeManualId(manualId) {
8+
if (
9+
manualId === undefined ||
10+
manualId === null ||
11+
manualId === '' ||
12+
(typeof manualId !== 'string' && typeof manualId !== 'number')
13+
) {
14+
return null
15+
}
16+
return manualId
17+
}
18+
319
/**
420
* @param {import('objection').QueryBuilder} query
521
* @param {{
@@ -20,13 +36,7 @@ function applyManualIdFilter(query, options) {
2036
bounds,
2137
} = options
2238

23-
const manualId =
24-
rawManual !== undefined &&
25-
rawManual !== null &&
26-
rawManual !== '' &&
27-
(typeof rawManual === 'string' || typeof rawManual === 'number')
28-
? rawManual
29-
: null
39+
const manualId = normalizeManualId(rawManual)
3040

3141
if (manualId !== null) {
3242
query.where((builder) => {
@@ -44,4 +54,4 @@ function applyManualIdFilter(query, options) {
4454
return manualId
4555
}
4656

47-
module.exports = { applyManualIdFilter }
57+
module.exports = { applyManualIdFilter, normalizeManualId }

0 commit comments

Comments
 (0)