Enhance check-in verification to ensure integer distance values

- Added a new function, `roundedDistanceMeters`, to round the calculated distance for NFC and GPS methods.
- Updated the `verifyTargetCheckIn` function to use the new rounding logic, ensuring distance values are integers.
- Added unit tests to verify that distance values returned for NFC check-ins are whole numbers, improving data consistency.
This commit is contained in:
shancheas
2026-09-01 23:48:12 +07:00
parent ec5f012d6f
commit 82e4a0cbf0
2 changed files with 28 additions and 2 deletions
@@ -74,6 +74,23 @@ describe('check-in-verification', () => {
expect(result.method).toBe('gps'); expect(result.method).toBe('gps');
expect(result.distanceMeters).toBeGreaterThan(100); expect(result.distanceMeters).toBeGreaterThan(100);
expect(Number.isInteger(result.distanceMeters)).toBe(true);
});
it('returns whole meters for NFC distance so it matches the integer column', () => {
const result = verifyBranchCheckIn(
target,
{
method: 'nfc',
nfcId: 'nfc-123',
latitude: -6.2005,
longitude: 106.8005,
},
100,
);
expect(result.distanceMeters).not.toBeNull();
expect(Number.isInteger(result.distanceMeters)).toBe(true);
}); });
it('accepts GPS when the target has no location if skipGpsValidation is set', () => { it('accepts GPS when the target has no location if skipGpsValidation is set', () => {
@@ -1,6 +1,15 @@
import { BadRequestException } from '@nestjs/common'; import { BadRequestException } from '@nestjs/common';
import { haversineDistanceMeters } from './geo-distance'; import { haversineDistanceMeters } from './geo-distance';
function roundedDistanceMeters(
lat1: number,
lon1: number,
lat2: number,
lon2: number,
): number {
return Math.round(haversineDistanceMeters(lat1, lon1, lat2, lon2));
}
export const CHECK_IN_METHODS = ['nfc', 'qr', 'gps'] as const; export const CHECK_IN_METHODS = ['nfc', 'qr', 'gps'] as const;
export type CheckInMethod = (typeof CHECK_IN_METHODS)[number]; export type CheckInMethod = (typeof CHECK_IN_METHODS)[number];
@@ -117,7 +126,7 @@ function verifyTargetCheckIn(
throw new BadRequestException(locationMissingMessage); throw new BadRequestException(locationMissingMessage);
} }
if (canMeasure) { if (canMeasure) {
distanceMeters = haversineDistanceMeters( distanceMeters = roundedDistanceMeters(
payload.latitude, payload.latitude,
payload.longitude, payload.longitude,
targetLatitude, targetLatitude,
@@ -135,7 +144,7 @@ function verifyTargetCheckIn(
distanceMeters: distanceMeters:
distanceMeters == null distanceMeters == null
? target.latitude != null && target.longitude != null ? target.latitude != null && target.longitude != null
? haversineDistanceMeters( ? roundedDistanceMeters(
payload.latitude, payload.latitude,
payload.longitude, payload.longitude,
target.latitude, target.latitude,