|
1 | 1 | import { Injectable, BadRequestException, ForbiddenException } from '@nestjs/common'; |
2 | 2 |
|
| 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 | + |
3 | 17 | const demoCities = [{ |
4 | 18 | "city": "New York", |
5 | 19 | "lat": 40.7128, |
@@ -28,13 +42,17 @@ export function ValidateLatLngUser(): MethodDecorator { |
28 | 42 | const request = args[0]; // Assuming the first argument is the request object |
29 | 43 | const lat = request?.lat; |
30 | 44 | const lng = request?.lng; |
31 | | - const user = args[1] |
| 45 | + const user = args[1]; |
32 | 46 |
|
33 | 47 | 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 | + ); |
38 | 56 | } |
39 | 57 | } |
40 | 58 |
|
|
0 commit comments