finsh user,car,driver pages also add validator layer to app build trip page

This commit is contained in:
m7amedez5511
2026-06-18 16:51:31 +03:00
parent a23d21f222
commit c1b77f89cd
45 changed files with 4128 additions and 1690 deletions

View File

@@ -1,5 +1,3 @@
import { get, post, patch, del } from "./api";
import type {
Car,

View File

@@ -4,7 +4,7 @@
// - src/routes/driver.route.js → /api/v1/driver/...
// - src/routes/driver_report.route.js → /api/v1/drivers/:id/reports/daily
import { get, post, put, del } from "./api";
import { get, post, put, del, patch } from "./api";
import type {
Driver,
DriverListResponse,
@@ -81,7 +81,7 @@ export const driverService = {
* Requires: "update-driver" permission
*/
update: (id: string, payload: UpdateDriverPayload, token: string | null) =>
put<{ data: Driver }>(`driver/${id}`, payload, token),
patch<{ data: Driver }>(`driver/${id}`, payload, token),
/**
* DELETE /driver/:id
@@ -100,10 +100,10 @@ export const driverService = {
* Requires: "generate-driver-report" permission
*/
getDailyReport: (id: string, date: string, token: string | null) =>
get<DriverReportResponse>(
`driver/${id}/reports/daily?date=${encodeURIComponent(date)}`,
token,
),
get<DriverReportResponse>(
`drivers/${id}/reports/daily?date=${encodeURIComponent(date)}`,
token,
),
// ── Image upload (multipart) ──────────────────────────────────────────────

114
services/trip.service.ts Normal file
View File

@@ -0,0 +1,114 @@
// services/trip.service.ts
// All API calls for the Trip module.
// Endpoints extracted from: trip_api_reference.html → /api/v1/trips/...
// Follows the exact same pattern as services/driver.service.ts.
import { get, post, patch, del } from "./api";
import type {
Trip,
TripListResponse,
TripDetailResponse,
TripReportResponse,
CreateTripPayload,
UpdateTripPayload,
TripListParams,
} from "@/types/trip";
/** Build a query string for list endpoints */
function buildQuery(
params: Record<string, string | number | undefined>,
): string {
const entries = Object.entries(params).filter(
([, v]) => v !== undefined && v !== "",
);
if (!entries.length) return "";
return (
"?" +
entries
.map(([k, v]) => `${k}=${encodeURIComponent(String(v))}`)
.join("&")
);
}
export const tripService = {
/**
* GET /trips
* Fetch paginated + filterable + searchable trip list.
* Requires: "read-trip" permission
*/
getAll: (params: TripListParams = {}, token: string | null) =>
get<TripListResponse>(`trips${buildQuery({ ...params })}`, token),
/**
* GET /trips/archived
* Fetch soft-deleted (archived) trips. Same filters as getAll.
* Requires: "read-deleted-trip" permission
*/
getArchived: (params: TripListParams = {}, token: string | null) =>
get<TripListResponse>(`trips/archived${buildQuery({ ...params })}`, token),
/**
* GET /trips/archived/:id
* Fetch a single archived trip with full details.
* Requires: "read-deleted-trip" permission
*/
getArchivedById: (id: string, token: string | null) =>
get<TripDetailResponse>(`trips/archived/${id}`, token),
/**
* GET /trips/:id
* Fetch a single trip with full driver/car details.
* Requires: "read-trip" permission
*/
getById: (id: string, token: string | null) =>
get<TripDetailResponse>(`trips/${id}`, token),
/**
* POST /trips
* Create a new trip.
* Side effect: driver & car flip to status "InTrip" automatically.
* Requires: "create-trip" permission
*/
create: (payload: CreateTripPayload, token: string | null) =>
post<{ data: Trip }>("trips", payload, token),
/**
* PATCH /trips/:id
* Update trip data / status / reassign driver or car.
* Side effects:
* - changing driverId: old driver -> Active, new driver -> InTrip
* - changing carId: same logic for cars
* - status Completed/Cancelled: driver & car both -> Active
* Requires: "update-trip" permission
*/
update: (id: string, payload: UpdateTripPayload, token: string | null) =>
patch<{ data: Trip }>(`trips/${id}`, payload, token),
/**
* DELETE /trips/:id
* Soft-delete a trip (isDeleted=true, deletedAt=now()). 204 No Content.
* Requires: "delete-trip" permission
*/
delete: (id: string, token: string | null) =>
del<void>(`trips/${id}`, token),
// ── Reports ───────────────────────────────────────────────────────────────
/**
* GET /trips/:id/reports
* Generate the Trip Manifest report (HTML, saved server-side).
* Returns { reportUrl }
* Requires: "generate-trip-report" permission
*/
getReport: (id: string, token: string | null) =>
get<TripReportResponse>(`trips/${id}/reports`, token),
/**
* GET /trips/:id/reports/client/:clientId
* Generate a client-filtered version of the trip report.
* Returns { reportUrl }
* Requires: "generate-trip-report" permission
*/
getClientReport: (id: string, clientId: string, token: string | null) =>
get<TripReportResponse>(`trips/${id}/reports/client/${clientId}`, token),
};