calculations
This commit is contained in:
@@ -95,12 +95,13 @@ export default function(db) {
|
||||
|
||||
router.use(authenticateJWT);
|
||||
// Definitive version with distance calculation and specific error messages
|
||||
router.post('/clock', async (req, res) => {
|
||||
try {
|
||||
const { userId, eventType, qrCodeValue, latitude, longitude, device_epoch_ms } = req.body;
|
||||
|
||||
// Definitive version with distance calculation and specific error messages
|
||||
router.post('/clock', async (req, res) => {
|
||||
try {
|
||||
const { userId, eventType, qrCodeValue, latitude, longitude } = req.body;
|
||||
const currentTimestamp = new Date().toISOString().slice(0, 19).replace('T', ' ');
|
||||
// Use the device's recorded instant if provided; fall back to server time.
|
||||
const ts = Number.isFinite(+device_epoch_ms) ? new Date(Number(device_epoch_ms)) : new Date();
|
||||
const currentTimestamp = ts.toISOString().slice(0, 19).replace('T', ' ');
|
||||
|
||||
// 1. Kill Switch Enforcement
|
||||
const clockingAllowed = await isClockingEnabled(db);
|
||||
|
||||
@@ -186,13 +186,29 @@ const fetchCurrentStatus = async () => {
|
||||
}
|
||||
}
|
||||
|
||||
const getTimeContext = () => {
|
||||
const tzName = Intl.DateTimeFormat().resolvedOptions().timeZone || 'UTC'
|
||||
const offsetMin = -new Date().getTimezoneOffset() // east of UTC => positive
|
||||
return { device_epoch_ms: Date.now(), tz_name: tzName, offset_min: offsetMin }
|
||||
}
|
||||
|
||||
const sendClockEvent = async (qrCodeValue, latitude, longitude) => {
|
||||
const eventType = isClockedIn.value ? 'clock_out' : 'clock_in'
|
||||
try {
|
||||
const payload = {
|
||||
userId,
|
||||
eventType,
|
||||
qrCodeValue,
|
||||
latitude,
|
||||
longitude,
|
||||
...getTimeContext() // new: device_epoch_ms, tz_name, offset_min
|
||||
}
|
||||
|
||||
await apiFetch('/api/clock', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ userId, eventType, qrCodeValue, latitude, longitude }),
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
|
||||
const newClockStatus = !isClockedIn.value
|
||||
isClockedIn.value = newClockStatus
|
||||
triggerOverlay(t(newClockStatus ? 'successClockIn' : 'successClockOut'), 'success');
|
||||
|
||||
Reference in New Issue
Block a user