changes
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
/**
|
||||
* Technician Location Services
|
||||
*
|
||||
* 1. Background logger -- logs GPS every 5 minutes during working hours.
|
||||
* 1. Background logger -- logs GPS every 5 minutes while the tech is clocked in.
|
||||
* 2. getLocation() -- returns a Promise that resolves to {latitude, longitude, accuracy}.
|
||||
* If the user denies permission or the request times out a blocking modal is shown
|
||||
* and the promise is rejected.
|
||||
@@ -11,9 +11,10 @@
|
||||
'use strict';
|
||||
|
||||
var INTERVAL_MS = 5 * 60 * 1000;
|
||||
var STORE_OPEN_HOUR = 9;
|
||||
var STORE_CLOSE_HOUR = 18;
|
||||
var CLOCK_CHECK_MS = 60 * 1000; // check clock status every 60s
|
||||
var locationTimer = null;
|
||||
var clockCheckTimer = null;
|
||||
var isClockedIn = false;
|
||||
var permissionDenied = false;
|
||||
|
||||
// =====================================================================
|
||||
@@ -137,21 +138,38 @@
|
||||
window.openGoogleMapsNav = openGoogleMapsNav;
|
||||
|
||||
// =====================================================================
|
||||
// BACKGROUND LOGGER
|
||||
// BACKGROUND LOGGER (tied to clock-in / clock-out status)
|
||||
// =====================================================================
|
||||
|
||||
function isWorkingHours() {
|
||||
var now = new Date();
|
||||
var hour = now.getHours();
|
||||
return hour >= STORE_OPEN_HOUR && hour < STORE_CLOSE_HOUR;
|
||||
}
|
||||
|
||||
function isTechnicianPortal() {
|
||||
return window.location.pathname.indexOf('/my/technician') !== -1;
|
||||
}
|
||||
|
||||
function checkClockStatus() {
|
||||
fetch('/my/technician/clock-status', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ jsonrpc: '2.0', method: 'call', params: {} }),
|
||||
})
|
||||
.then(function (r) { return r.json(); })
|
||||
.then(function (data) {
|
||||
var wasClocked = isClockedIn;
|
||||
isClockedIn = !!(data.result && data.result.clocked_in);
|
||||
if (isClockedIn && !wasClocked) {
|
||||
// Just clocked in — start tracking immediately
|
||||
startLocationTimer();
|
||||
} else if (!isClockedIn && wasClocked) {
|
||||
// Just clocked out — stop tracking
|
||||
stopLocationTimer();
|
||||
}
|
||||
})
|
||||
.catch(function () {
|
||||
/* network error: keep current state */
|
||||
});
|
||||
}
|
||||
|
||||
function logLocation() {
|
||||
if (!isWorkingHours() || document.hidden || !navigator.geolocation) return;
|
||||
if (!isClockedIn || document.hidden || !navigator.geolocation) return;
|
||||
|
||||
getLocation().then(function (coords) {
|
||||
fetch('/my/technician/location/log', {
|
||||
@@ -181,16 +199,32 @@
|
||||
});
|
||||
}
|
||||
|
||||
function startLocationTimer() {
|
||||
if (locationTimer) return; // already running
|
||||
logLocation(); // immediate first log
|
||||
locationTimer = setInterval(logLocation, INTERVAL_MS);
|
||||
}
|
||||
|
||||
function stopLocationTimer() {
|
||||
if (locationTimer) {
|
||||
clearInterval(locationTimer);
|
||||
locationTimer = null;
|
||||
}
|
||||
}
|
||||
|
||||
function startLocationLogging() {
|
||||
if (!isTechnicianPortal()) return;
|
||||
logLocation();
|
||||
locationTimer = setInterval(logLocation, INTERVAL_MS);
|
||||
|
||||
// Check clock status immediately, then every 60s
|
||||
checkClockStatus();
|
||||
clockCheckTimer = setInterval(checkClockStatus, CLOCK_CHECK_MS);
|
||||
|
||||
// Pause/resume on tab visibility
|
||||
document.addEventListener('visibilitychange', function () {
|
||||
if (document.hidden) {
|
||||
if (locationTimer) { clearInterval(locationTimer); locationTimer = null; }
|
||||
} else {
|
||||
logLocation();
|
||||
if (!locationTimer) { locationTimer = setInterval(logLocation, INTERVAL_MS); }
|
||||
stopLocationTimer();
|
||||
} else if (isClockedIn) {
|
||||
startLocationTimer();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user