Skip to content

Commit 5055f9d

Browse files
refactor: extract code to private function.
1 parent ebe70b7 commit 5055f9d

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

src/controllers/sanctionedRegionsController.ts

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,7 @@ export async function checkIfPointInRegion(req: Request, res: Response) {
1010
}
1111

1212
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);
2714

2815
const isRestricted = !!matchingRegion;
2916
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) {
3522
logger.error('Error checking sanctioned location:', error);
3623
return res.status(500).json({ error: 'Internal server error.' });
3724
}
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

Comments
 (0)