Skip to content

Commit 4bf1dcf

Browse files
committed
allow demo users a radius around demo cities
1 parent 6d76477 commit 4bf1dcf

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

src/decorators/validate-lat-lng-user.decorator.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
import { Injectable, BadRequestException, ForbiddenException } from '@nestjs/common';
22

3+
// Helper to calculate distance between two lat/lng points in meters
4+
function haversineDistance(lat1: number, lng1: number, lat2: number, lng2: number): number {
5+
const toRad = (x: number) => x * Math.PI / 180;
6+
const R = 6371000; // meters
7+
const dLat = toRad(lat2 - lat1);
8+
const dLng = toRad(lng2 - lng1);
9+
const a =
10+
Math.sin(dLat / 2) * Math.sin(dLat / 2) +
11+
Math.cos(toRad(lat1)) * Math.cos(toRad(lat2)) *
12+
Math.sin(dLng / 2) * Math.sin(dLng / 2);
13+
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
14+
return R * c;
15+
}
16+
317
const demoCities = [{
418
"city": "New York",
519
"lat": 40.7128,
@@ -28,13 +42,17 @@ export function ValidateLatLngUser(): MethodDecorator {
2842
const request = args[0]; // Assuming the first argument is the request object
2943
const lat = request?.lat;
3044
const lng = request?.lng;
31-
const user = args[1]
45+
const user = args[1];
3246

3347
if (lat && lng && user?.isDemoAccount) {
34-
//check if lat / lng is found as a demo city
35-
const foundCity = demoCities.find(city => city.lat === lat && city.lng === lng);
36-
if (!foundCity) {
37-
throw new BadRequestException(`Demo accounts can only access demo cities. These are ${demoCities.map(city => JSON.stringify(city)).join(", ")}`);
48+
// Allow within 10,000 meters of any demo city
49+
const withinRadius = demoCities.some(city =>
50+
haversineDistance(lat, lng, city.lat, city.lng) <= 10000
51+
);
52+
if (!withinRadius) {
53+
throw new BadRequestException(
54+
`Demo accounts can only access locations within 10,000 meters of demo cities. Demo cities: ${demoCities.map(city => JSON.stringify(city)).join(", ")}`
55+
);
3856
}
3957
}
4058

0 commit comments

Comments
 (0)