Skip to content

Commit ebe70b7

Browse files
feat: expose sanctioned regions endpoint.
add endpoint to expose sanctioned regions for client to consume on demand.
1 parent ff5342f commit ebe70b7

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import {Request, Response} from "express";
2+
import logger from "../config/loggingConfig";
3+
import SanctionedRegion from "../models/misc/SanctionedRegion";
4+
5+
export async function checkIfPointInRegion(req: Request, res: Response) {
6+
const { latitude, longitude } = req.body;
7+
8+
if (typeof latitude !== 'number' || typeof longitude !== 'number') {
9+
return res.status(400).json({ error: 'Invalid coordinates provided.' });
10+
}
11+
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();
27+
28+
const isRestricted = !!matchingRegion;
29+
logger.info(`User at [${latitude},${longitude}] is ${isRestricted ? '' : 'not '}in a restricted zone.`);
30+
return res.status(200).json({
31+
message: `Sell center is set within a ${isRestricted ? 'restricted' : 'clear' } zone`,
32+
isRestricted
33+
});
34+
} catch (error) {
35+
logger.error('Error checking sanctioned location:', error);
36+
return res.status(500).json({ error: 'Internal server error.' });
37+
}
38+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import {Router} from "express";
2+
import { verifyAdminToken } from "../middlewares/verifyToken";
3+
import * as sanctionedRegionsController from "../controllers/sanctionedRegionsController"
4+
5+
const sanctionedRegionsRoutes = Router();
6+
7+
/**
8+
* @swagger
9+
* /api/v1/reports/check-in-sanctioned-region:
10+
* post:
11+
* tags:
12+
* - Report
13+
* summary: Check if a point is within a sanctioned region
14+
* requestBody:
15+
* required: true
16+
* content:
17+
* application/json:
18+
* schema:
19+
* type: object
20+
* required:
21+
* - latitude
22+
* - longitude
23+
* properties:
24+
* latitude:
25+
* type: number
26+
* example: -1.94995
27+
* longitude:
28+
* type: number
29+
* example: 30.0588
30+
* responses:
31+
* 200:
32+
* description: Indicates whether the point is in a sanctioned region
33+
* content:
34+
* application/json:
35+
* schema:
36+
* type: object
37+
* properties:
38+
* isRestricted:
39+
* type: boolean
40+
* example: true
41+
* 400:
42+
* description: Invalid coordinates provided
43+
* 500:
44+
* description: Internal server error
45+
*/
46+
sanctionedRegionsRoutes.post(
47+
"/check-in-sanctioned-region",
48+
// verifyAdminToken,
49+
sanctionedRegionsController.checkIfPointInRegion
50+
);
51+
52+
export default sanctionedRegionsRoutes;

src/utils/app.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import reviewFeedbackRoutes from "../routes/reviewFeedback.routes";
1616
import mapCenterRoutes from "../routes/mapCenter.routes";
1717
import reportRoutes from "../routes/report.routes";
1818
import toggleRoutes from "../routes/toggle.routes";
19+
import sanctionedRegionsRoutes from "../routes/sanctionedRegions.routes";
1920

2021
dotenv.config();
2122

@@ -45,6 +46,7 @@ app.use("/api/v1/review-feedback", reviewFeedbackRoutes);
4546
app.use("/api/v1/map-center", mapCenterRoutes);
4647
app.use("/api/v1/reports", reportRoutes);
4748
app.use("/api/v1/toggles", toggleRoutes);
49+
app.use("/api/v1/sanctioned-regions", sanctionedRegionsRoutes);
4850

4951
app.use("/", homeRoutes);
5052

0 commit comments

Comments
 (0)