Refactor DeviceUuidService and NativeServicesManager for simplified device handling

- Removed detailed device registration and heartbeat logic from NativeServicesManager.
- Simplified device UUID retrieval and validation in NativeServicesManager.
- Streamlined DeviceUuidService by removing unnecessary methods and logging.
- Updated WorkerDashboardView to directly interact with location service instead of background location service.
- Enhanced clock status synchronization with location service in WorkerDashboardView.
This commit is contained in:
sudomarcma
2025-07-08 18:18:28 +08:00
parent 4e81e5d761
commit acb0947908
9 changed files with 246 additions and 1129 deletions
+2 -49
View File
@@ -12,13 +12,10 @@ import mysql from 'mysql2/promise'
import dotenv from 'dotenv'
import bcrypt from 'bcrypt'
import jwt from 'jsonwebtoken'
// --- FIX START ---
// Import only the required functions from turf
import { point, polygon, booleanPointInPolygon, pointToLineDistance } from '@turf/turf'
// --- FIX END ---
// Helper function to validate device for user with simplified workers table approach
async function validateDeviceForUser(userId, deviceUuid, db) {
try {
// Step 1: Get user's current registered device UUID from workers table
@@ -116,7 +113,7 @@ async function logSecurityAlert(userId, alertType, alertData, db) {
}
// Helper function to register a new device for user (simplified for workers table)
async function registerDeviceForUser(userId, deviceUuid, deviceInfo, db) {
async function registerDeviceForUser(userId, deviceUuid, db) {
try {
// Check if device is already registered to another user
const [otherUserRows] = await db.execute(
@@ -129,7 +126,6 @@ async function registerDeviceForUser(userId, deviceUuid, deviceInfo, db) {
await logSecurityAlert(userId, 'device_registration_conflict', {
attempted_device_uuid: deviceUuid,
conflicting_user: otherUserRows[0].username,
device_info: deviceInfo,
message: 'Attempted to register device already assigned to another user'
}, db)
@@ -1005,49 +1001,6 @@ const geofence = polygon([
}
})
// Device Heartbeat Endpoint (Simplified - no timestamp tracking)
app.post('/api/device/heartbeat', authenticateJWT, async (req, res) => {
try {
const { userId, deviceUuid } = req.body
if (!userId || !deviceUuid) {
return res.status(400).json({ message: 'User ID and device UUID are required.' })
}
// Validate device registration (simplified check)
const [userRows] = await db.execute(
'SELECT device_uuid FROM workers WHERE id = ?',
[userId]
)
if (userRows.length === 0) {
return res.status(404).json({ message: 'User not found.' })
}
const registeredDeviceUuid = userRows[0].device_uuid
if (!registeredDeviceUuid) {
return res.status(400).json({ message: 'No device registered for this user.' })
}
if (registeredDeviceUuid !== deviceUuid) {
// Log security alert for heartbeat from unauthorized device
await logSecurityAlert(userId, 'unauthorized_heartbeat', {
registered_device_uuid: registeredDeviceUuid,
attempted_device_uuid: deviceUuid,
message: 'Heartbeat attempt from unauthorized device'
}, db)
return res.status(403).json({ message: 'Device not authorized for heartbeat.' })
}
// Device is valid - heartbeat accepted (no data storage needed)
res.json({ message: 'Heartbeat accepted' })
} catch (error) {
console.error('Device heartbeat error:', error)
res.status(500).json({ message: 'Database error during heartbeat validation.' })
}
})
// Security Check Endpoint
app.post('/api/security/check', authenticateJWT, async (req, res) => {