diff --git a/backend/src/gps/gps.service.ts b/backend/src/gps/gps.service.ts index d6f8bc2..e689b3e 100644 --- a/backend/src/gps/gps.service.ts +++ b/backend/src/gps/gps.service.ts @@ -162,8 +162,10 @@ export class GpsService implements OnModuleInit { throw new BadRequestException('Driver is already enrolled for GPS tracking'); } - // Generate unique device identifier (no special characters for better compatibility) - const deviceIdentifier = `vipdriver${driverId.replace(/-/g, '').slice(0, 8)}`; + // Generate unique device identifier (lowercase alphanumeric only for compatibility) + const deviceIdentifier = `vipdriver${driverId.replace(/-/g, '').slice(0, 8)}`.toLowerCase(); + + this.logger.log(`Enrolling driver ${driver.name} with device identifier: ${deviceIdentifier}`); // Create device in Traccar const traccarDevice = await this.traccarClient.createDevice( @@ -172,12 +174,16 @@ export class GpsService implements OnModuleInit { driver.phone || undefined, ); + // Use the uniqueId returned by Traccar (in case it was modified) + const actualDeviceId = traccarDevice.uniqueId; + this.logger.log(`Traccar returned device with uniqueId: ${actualDeviceId}`); + // Create GPS device record (consent pre-approved by HR at hiring) await this.prisma.gpsDevice.create({ data: { driverId, traccarDeviceId: traccarDevice.id, - deviceIdentifier, + deviceIdentifier: actualDeviceId, // Use what Traccar actually stored consentGiven: true, consentGivenAt: new Date(), }, @@ -194,7 +200,7 @@ GPS Tracking Setup Instructions for ${driver.name}: - Android: https://play.google.com/store/apps/details?id=org.traccar.client 2. Open the app and configure: - - Device identifier: ${deviceIdentifier} + - Device identifier: ${actualDeviceId} - Server URL: ${serverUrl} - Frequency: ${settings.updateIntervalSeconds} seconds - Location accuracy: High @@ -226,7 +232,7 @@ Note: GPS tracking is only active during shift hours (${settings.shiftStartHour} return { success: true, - deviceIdentifier, + deviceIdentifier: actualDeviceId, // Return what Traccar actually stored serverUrl, instructions, signalMessageSent, diff --git a/backend/src/gps/traccar-client.service.ts b/backend/src/gps/traccar-client.service.ts index b842dbd..119e352 100644 --- a/backend/src/gps/traccar-client.service.ts +++ b/backend/src/gps/traccar-client.service.ts @@ -180,13 +180,21 @@ export class TraccarClientService implements OnModuleInit { * Create a new device in Traccar */ async createDevice(name: string, uniqueId: string, phone?: string): Promise { + // Sanitize uniqueId - trim whitespace, lowercase, remove any non-alphanumeric chars + const sanitizedUniqueId = uniqueId.trim().toLowerCase().replace(/[^a-z0-9]/g, ''); + + this.logger.log(`Creating Traccar device: name="${name}", uniqueId="${sanitizedUniqueId}"`); + const device = await this.request('post', '/api/devices', { name, - uniqueId, - phone, + uniqueId: sanitizedUniqueId, + phone: phone || null, category: 'person', + disabled: false, // Explicitly enable the device }); + this.logger.log(`Traccar device created: id=${device.id}, uniqueId="${device.uniqueId}", disabled=${device.disabled}`); + // Link device to all admin users so they can see it await this.linkDeviceToAllAdmins(device.id);