geofence refinement
and manual failsafe
This commit is contained in:
1 parent
71ccea85d2
commit
c90f8cb1fa
18 files changed
+671
-14
No files matched your search
@@ -1,5 +1,6 @@
|
||||
import { apiClient } from "./client";
|
||||
import { API_ENDPOINTS } from "../config/api";
|
||||
import { isAxiosError } from "axios";
|
||||
|
||||
export interface Attendance {
|
||||
id: string;
|
||||
@@ -23,7 +24,9 @@ export const attendanceApi = {
|
||||
});
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
throw error;
|
||||
throw new Error(
|
||||
getAttendanceErrorMessage(error, "Failed to load attendance history."),
|
||||
);
|
||||
}
|
||||
},
|
||||
|
||||
@@ -31,30 +34,55 @@ export const attendanceApi = {
|
||||
type: string,
|
||||
token: string,
|
||||
location: AttendanceLocationPayload,
|
||||
fallbackRequested = false,
|
||||
): Promise<void> => {
|
||||
try {
|
||||
await apiClient.post(
|
||||
API_ENDPOINTS.ATTENDANCE.CHECK_IN,
|
||||
{ type, location },
|
||||
{ type, location, fallbackRequested },
|
||||
{ headers: { Authorization: `Bearer ${token}` } },
|
||||
);
|
||||
} catch (error) {
|
||||
throw error;
|
||||
throw new Error(
|
||||
getAttendanceErrorMessage(error, "Failed to start workout."),
|
||||
);
|
||||
}
|
||||
},
|
||||
|
||||
checkOut: async (
|
||||
token: string,
|
||||
location: AttendanceLocationPayload,
|
||||
fallbackRequested = false,
|
||||
): Promise<void> => {
|
||||
try {
|
||||
await apiClient.post(
|
||||
API_ENDPOINTS.ATTENDANCE.CHECK_OUT,
|
||||
{ location },
|
||||
{ location, fallbackRequested },
|
||||
{ headers: { Authorization: `Bearer ${token}` } },
|
||||
);
|
||||
} catch (error) {
|
||||
throw error;
|
||||
throw new Error(
|
||||
getAttendanceErrorMessage(error, "Failed to end workout."),
|
||||
);
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
function getAttendanceErrorMessage(error: unknown, fallback: string): string {
|
||||
if (isAxiosError(error)) {
|
||||
const payload = error.response?.data;
|
||||
|
||||
if (typeof payload === "string" && payload.trim()) {
|
||||
return payload;
|
||||
}
|
||||
|
||||
if (payload && typeof payload === "object") {
|
||||
const message = (payload as { error?: unknown }).error;
|
||||
if (typeof message === "string" && message.trim()) {
|
||||
return message;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return fallback;
|
||||
}
|
||||
Reference in new issue
Block a user