forked from 4GeeksAcademy/Proyecto-Final-RRHH
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfichajeService.js
More file actions
55 lines (43 loc) · 1.53 KB
/
fichajeService.js
File metadata and controls
55 lines (43 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
const API_URL = import.meta.env.VITE_BACKEND_URL;
// Fichar entrada
export const ficharEntrada = async () => {
const token = localStorage.getItem("jwt-token");
if (!token) throw new Error("Sin token");
const res = await fetch(`${API_URL}/api/fichaje/entrada`, {
method: "POST",
headers: {
Authorization: "Bearer " + token,
"Content-Type": "application/json",
},
});
if (!res.ok) throw new Error("Error al fichar entrada");
const data = await res.json(); // devuelve { horaEntrada: "...", id: ... }
return data;
};
// Fichar salida
export const ficharSalida = async () => {
const token = localStorage.getItem("jwt-token");
if (!token) throw new Error("Sin token");
const res = await fetch(`${API_URL}/api/fichaje/salida`, {
method: "POST",
headers: {
Authorization: "Bearer " + token,
"Content-Type": "application/json",
},
});
if (!res.ok) throw new Error("Error al fichar salida");
const data = await res.json(); // devuelve { horaSalida: "...", id: ... }
return data;
};
// Obtener fichaje activo (para temporizador)
export const obtenerFichajeActivo = async () => {
const token = localStorage.getItem("jwt-token");
if (!token) return null;
const res = await fetch(`${API_URL}/api/fichaje/activo`, {
headers: { Authorization: "Bearer " + token },
});
if (res.status === 204) return null; // no hay fichaje activo
if (!res.ok) throw new Error("Error al obtener fichaje activo");
const data = await res.json(); // { id: ..., horaEntrada: "..." }
return data;
};