rework vehicle handling
This commit is contained in:
@@ -2,47 +2,31 @@ import { api } from './api';
|
||||
import type {
|
||||
FahrzeugListItem,
|
||||
FahrzeugDetail,
|
||||
FahrzeugPruefung,
|
||||
FahrzeugWartungslog,
|
||||
VehicleStats,
|
||||
InspectionAlert,
|
||||
CreateFahrzeugPayload,
|
||||
UpdateFahrzeugPayload,
|
||||
UpdateStatusPayload,
|
||||
CreatePruefungPayload,
|
||||
CreateWartungslogPayload,
|
||||
} from '../types/vehicle.types';
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Internal: unwrap the standard { success, data } envelope
|
||||
// ---------------------------------------------------------------------------
|
||||
async function unwrap<T>(promise: ReturnType<typeof api.get<{ success: boolean; data: T }>>): Promise<T> {
|
||||
async function unwrap<T>(
|
||||
promise: ReturnType<typeof api.get<{ success: boolean; data: T }>>
|
||||
): Promise<T> {
|
||||
const response = await promise;
|
||||
return response.data.data;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Vehicle API Service
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export const vehiclesApi = {
|
||||
|
||||
// ── Fleet overview ──────────────────────────────────────────────────────────
|
||||
|
||||
/** Fetch all vehicles with their next inspection badge data */
|
||||
async getAll(): Promise<FahrzeugListItem[]> {
|
||||
return unwrap(api.get<{ success: boolean; data: FahrzeugListItem[] }>('/api/vehicles'));
|
||||
},
|
||||
|
||||
/** Dashboard KPI stats */
|
||||
async getStats(): Promise<VehicleStats> {
|
||||
return unwrap(api.get<{ success: boolean; data: VehicleStats }>('/api/vehicles/stats'));
|
||||
},
|
||||
|
||||
/**
|
||||
* Upcoming and overdue inspection alerts.
|
||||
* @param daysAhead How many days to look ahead (default 30, max 365).
|
||||
*/
|
||||
async getAlerts(daysAhead = 30): Promise<InspectionAlert[]> {
|
||||
return unwrap(
|
||||
api.get<{ success: boolean; data: InspectionAlert[] }>(
|
||||
@@ -51,15 +35,10 @@ export const vehiclesApi = {
|
||||
);
|
||||
},
|
||||
|
||||
// ── Vehicle detail ──────────────────────────────────────────────────────────
|
||||
|
||||
/** Full vehicle detail including inspection history and maintenance log */
|
||||
async getById(id: string): Promise<FahrzeugDetail> {
|
||||
return unwrap(api.get<{ success: boolean; data: FahrzeugDetail }>(`/api/vehicles/${id}`));
|
||||
},
|
||||
|
||||
// ── CRUD ────────────────────────────────────────────────────────────────────
|
||||
|
||||
async create(payload: CreateFahrzeugPayload): Promise<FahrzeugDetail> {
|
||||
const response = await api.post<{ success: boolean; data: FahrzeugDetail }>(
|
||||
'/api/vehicles',
|
||||
@@ -80,29 +59,10 @@ export const vehiclesApi = {
|
||||
await api.delete(`/api/vehicles/${id}`);
|
||||
},
|
||||
|
||||
/** Live status change — Socket.IO event is emitted server-side in Tier 3 */
|
||||
async updateStatus(id: string, payload: UpdateStatusPayload): Promise<void> {
|
||||
await api.patch(`/api/vehicles/${id}/status`, payload);
|
||||
},
|
||||
|
||||
// ── Inspections ─────────────────────────────────────────────────────────────
|
||||
|
||||
async getPruefungen(id: string): Promise<FahrzeugPruefung[]> {
|
||||
return unwrap(
|
||||
api.get<{ success: boolean; data: FahrzeugPruefung[] }>(`/api/vehicles/${id}/pruefungen`)
|
||||
);
|
||||
},
|
||||
|
||||
async addPruefung(id: string, payload: CreatePruefungPayload): Promise<FahrzeugPruefung> {
|
||||
const response = await api.post<{ success: boolean; data: FahrzeugPruefung }>(
|
||||
`/api/vehicles/${id}/pruefungen`,
|
||||
payload
|
||||
);
|
||||
return response.data.data;
|
||||
},
|
||||
|
||||
// ── Maintenance log ─────────────────────────────────────────────────────────
|
||||
|
||||
async getWartungslog(id: string): Promise<FahrzeugWartungslog[]> {
|
||||
return unwrap(
|
||||
api.get<{ success: boolean; data: FahrzeugWartungslog[] }>(`/api/vehicles/${id}/wartung`)
|
||||
|
||||
Reference in New Issue
Block a user