@@ -10,20 +10,7 @@ export async function checkIfPointInRegion(req: Request, res: Response) {
10
10
}
11
11
12
12
try {
13
- // Build a GeoJSON Point for the query (lon/lat order!).
14
- const point = {
15
- type : 'Point' as const ,
16
- coordinates : [ longitude , latitude ] ,
17
- } ;
18
-
19
- // Ask MongoDB if any `boundary` polygon intersects this point.
20
- const matchingRegion = await SanctionedRegion . findOne ( {
21
- boundary : {
22
- $geoIntersects : {
23
- $geometry : point
24
- }
25
- }
26
- } ) . exec ( ) ;
13
+ const matchingRegion = await checkInSanctionedRegion ( longitude , latitude ) ;
27
14
28
15
const isRestricted = ! ! matchingRegion ;
29
16
logger . info ( `User at [${ latitude } ,${ longitude } ] is ${ isRestricted ? '' : 'not ' } in a restricted zone.` ) ;
@@ -35,4 +22,22 @@ export async function checkIfPointInRegion(req: Request, res: Response) {
35
22
logger . error ( 'Error checking sanctioned location:' , error ) ;
36
23
return res . status ( 500 ) . json ( { error : 'Internal server error.' } ) ;
37
24
}
38
- }
25
+ }
26
+
27
+ async function checkInSanctionedRegion ( longitude : number , latitude : number ) {
28
+ // Build a GeoJSON Point for the query (lon/lat order!).
29
+ const point = {
30
+ type : 'Point' as const ,
31
+ coordinates : [ longitude , latitude ] ,
32
+ } ;
33
+
34
+ // Ask MongoDB if any `boundary` polygon intersects this point.
35
+ const matchingRegion = await SanctionedRegion . findOne ( {
36
+ boundary : {
37
+ $geoIntersects : {
38
+ $geometry : point
39
+ }
40
+ }
41
+ } ) . exec ( ) ;
42
+ return matchingRegion ;
43
+ }
0 commit comments