calculations

This commit is contained in:
Edison
2025-11-03 13:35:35 +08:00
parent 7231310f93
commit 7e37230894
2 changed files with 23 additions and 6 deletions
+6 -5
View File
@@ -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);
+17 -1
View File
@@ -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');