|
| 1 | +import type { Schedule, DashboardMetrics, Task } from "@/store"; |
| 2 | +import { MOCK_SCHEDULES, MOCK_METRICS } from "@/lib/constants"; |
| 3 | + |
| 4 | +export const getSchedules = async (): Promise<Schedule[]> => { |
| 5 | + try { |
| 6 | + // Simulate API call - in real app this would be: |
| 7 | + // return await api("/schedules").get().json() |
| 8 | + |
| 9 | + await new Promise((resolve) => setTimeout(resolve, 600)); |
| 10 | + return MOCK_SCHEDULES; |
| 11 | + } catch (error) { |
| 12 | + console.error("Failed to fetch schedules:", error); |
| 13 | + throw error; |
| 14 | + } |
| 15 | +}; |
| 16 | + |
| 17 | +export const getScheduleById = async (id: string): Promise<Schedule | null> => { |
| 18 | + try { |
| 19 | + // Simulate API call - in real app this would be: |
| 20 | + // return await api(`/schedules/${id}`).get().json() |
| 21 | + |
| 22 | + await new Promise((resolve) => setTimeout(resolve, 400)); |
| 23 | + const schedule = MOCK_SCHEDULES.find((s) => s.id === id); |
| 24 | + return schedule || null; |
| 25 | + } catch (error) { |
| 26 | + console.error("Failed to fetch schedule:", error); |
| 27 | + throw error; |
| 28 | + } |
| 29 | +}; |
| 30 | + |
| 31 | +export const getDashboardMetrics = async (): Promise<DashboardMetrics> => { |
| 32 | + try { |
| 33 | + // Simulate API call - in real app this would be: |
| 34 | + // return await api("/dashboard/metrics").get().json() |
| 35 | + |
| 36 | + await new Promise((resolve) => setTimeout(resolve, 300)); |
| 37 | + return MOCK_METRICS; |
| 38 | + } catch (error) { |
| 39 | + console.error("Failed to fetch metrics:", error); |
| 40 | + throw error; |
| 41 | + } |
| 42 | +}; |
| 43 | + |
| 44 | +export const updateScheduleStatus = async ( |
| 45 | + id: string, |
| 46 | + status: Schedule["status"] |
| 47 | +): Promise<Schedule> => { |
| 48 | + try { |
| 49 | + // Simulate API call - in real app this would be: |
| 50 | + // return await api(`/schedules/${id}/status`).put({ status }).json() |
| 51 | + |
| 52 | + await new Promise((resolve) => setTimeout(resolve, 500)); |
| 53 | + const schedule = MOCK_SCHEDULES.find((s) => s.id === id); |
| 54 | + if (!schedule) throw new Error("Schedule not found"); |
| 55 | + |
| 56 | + return { ...schedule, status }; |
| 57 | + } catch (error) { |
| 58 | + console.error("Failed to update schedule status:", error); |
| 59 | + throw error; |
| 60 | + } |
| 61 | +}; |
| 62 | + |
| 63 | +export const updateTaskStatus = async ( |
| 64 | + scheduleId: string, |
| 65 | + taskId: number, |
| 66 | + status: Task["status"], |
| 67 | + reason?: string |
| 68 | +): Promise<Task> => { |
| 69 | + try { |
| 70 | + // Simulate API call - in real app this would be: |
| 71 | + // return await api(`/schedules/${scheduleId}/tasks/${taskId}`).put({ status, reason }).json() |
| 72 | + |
| 73 | + await new Promise((resolve) => setTimeout(resolve, 400)); |
| 74 | + const schedule = MOCK_SCHEDULES.find((s) => s.id === scheduleId); |
| 75 | + if (!schedule) throw new Error("Schedule not found"); |
| 76 | + |
| 77 | + const task = schedule.tasks.find((t) => t.id === taskId); |
| 78 | + if (!task) throw new Error("Task not found"); |
| 79 | + |
| 80 | + return { ...task, status, reason }; |
| 81 | + } catch (error) { |
| 82 | + console.error("Failed to update task status:", error); |
| 83 | + throw error; |
| 84 | + } |
| 85 | +}; |
0 commit comments