mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-08-30 02:58:11 +00:00
- Implemented a method in to permanently delete wagons without history. - Added corresponding permissions for hard delete actions in . - Updated the UI components to include purge actions, ensuring they are only available to users with the appropriate permissions. - Created modals for confirming permanent deletions in and . - Enhanced API services to handle purge requests for locomotives, wagons, and routes. - Added tests for the purge functionality in both and services to ensure proper behavior and error handling.
115 lines
4.9 KiB
TypeScript
115 lines
4.9 KiB
TypeScript
import type { PaginatedResponse } from "@edr/types";
|
|
|
|
import { cargoService, type Cargo } from "@/services/cargoService";
|
|
import { containerService, type Container } from "@/services/containerService";
|
|
import {
|
|
locomotivesService,
|
|
type Locomotive,
|
|
type LocomotiveListFilters,
|
|
} from "@/services/locomotives.service";
|
|
import { trainService, type Train } from "@/services/trains.service";
|
|
import { wagonService, type Wagon, type WagonListFilters } from "@/services/wagon.service";
|
|
import { vehiclesService, type Vehicle, type VehicleListFilters } from "@/services/vehicles.service";
|
|
import { driversService, type Driver, type DriverListFilters } from "@/services/drivers.service";
|
|
import type { FleetResourceSlug } from "@/pages/fleet/config/resources";
|
|
|
|
export type FleetRecord = Locomotive | Train | Wagon | Container | Cargo | Vehicle | Driver;
|
|
|
|
export type FleetListFilters = WagonListFilters & LocomotiveListFilters & VehicleListFilters & DriverListFilters;
|
|
|
|
const listHandlers: Record<
|
|
FleetResourceSlug,
|
|
(filters?: FleetListFilters) => Promise<FleetRecord[]>
|
|
> = {
|
|
locomotives: (filters) => locomotivesService.getAll(filters ?? {}).then((r) => r.data),
|
|
trains: () => trainService.getAll().then((r) => r.data),
|
|
wagons: (filters) => wagonService.listAll(filters ?? {}),
|
|
containers: () => containerService.getAll().then((r) => r.data),
|
|
cargoes: () => cargoService.getAll().then((r) => r.data),
|
|
vehicles: (filters) => vehiclesService.getAll(filters ?? {}).then((r) => r.data),
|
|
drivers: (filters) => driversService.getAll(filters ?? {}).then((r) => r.data),
|
|
};
|
|
|
|
/**
|
|
* Server-paginated slugs. Everything else still lists in full and pages in the
|
|
* browser — add an entry here once its API grows a `/paged` endpoint.
|
|
*/
|
|
const pagedHandlers: Partial<
|
|
Record<FleetResourceSlug, (filters: FleetListFilters) => Promise<PaginatedResponse<FleetRecord>>>
|
|
> = {
|
|
locomotives: (filters) => locomotivesService.getPaged(filters).then((r) => r.data),
|
|
wagons: (filters) => wagonService.getAll(filters).then((r) => r.data),
|
|
};
|
|
|
|
export const isFleetServerPaginated = (slug: FleetResourceSlug): boolean =>
|
|
slug in pagedHandlers;
|
|
|
|
const createHandlers: Record<FleetResourceSlug, (data: Record<string, unknown>) => Promise<unknown>> = {
|
|
locomotives: (data) => locomotivesService.create(data),
|
|
trains: (data) => trainService.create(data),
|
|
wagons: (data) => wagonService.create(data),
|
|
containers: (data) => containerService.create(data),
|
|
cargoes: (data) => cargoService.create(data),
|
|
vehicles: (data) => vehiclesService.create(data),
|
|
drivers: (data) => driversService.create(data),
|
|
};
|
|
|
|
const updateHandlers: Record<
|
|
FleetResourceSlug,
|
|
(id: string, data: Record<string, unknown>) => Promise<unknown>
|
|
> = {
|
|
locomotives: (id, data) => locomotivesService.update(id, data),
|
|
trains: (id, data) => trainService.update(id, data),
|
|
wagons: (id, data) => wagonService.update(id, data),
|
|
containers: (id, data) => containerService.update(id, data),
|
|
cargoes: (id, data) => cargoService.update(id, data),
|
|
vehicles: (id, data) => vehiclesService.update(id, data),
|
|
drivers: (id, data) => driversService.update(id, data),
|
|
};
|
|
|
|
const removeHandlers: Record<FleetResourceSlug, (id: string) => Promise<unknown>> = {
|
|
locomotives: (id) => locomotivesService.decommission(id),
|
|
trains: (id) => trainService.delete(id),
|
|
wagons: (id) => wagonService.delete(id),
|
|
containers: (id) => containerService.delete(id),
|
|
cargoes: (id) => cargoService.delete(id),
|
|
vehicles: (id) => vehiclesService.delete(id),
|
|
drivers: (id) => driversService.delete(id),
|
|
};
|
|
|
|
/**
|
|
* Permanent purge, only for the two slugs that expose it. Everything else stays
|
|
* soft-delete/decommission only, so there is deliberately no entry here.
|
|
*/
|
|
const purgeHandlers: Partial<
|
|
Record<FleetResourceSlug, (id: string) => Promise<unknown>>
|
|
> = {
|
|
locomotives: (id) => locomotivesService.purge(id),
|
|
wagons: (id) => wagonService.purge(id),
|
|
};
|
|
|
|
/** True when the slug supports an irreversible purge. */
|
|
export const isFleetPurgeable = (slug: FleetResourceSlug) =>
|
|
slug in purgeHandlers;
|
|
|
|
export const fleetService = {
|
|
list: (slug: FleetResourceSlug, filters?: FleetListFilters) => listHandlers[slug](filters),
|
|
/** Only for slugs in `pagedHandlers` — guard with `isFleetServerPaginated`. */
|
|
listPaged: (slug: FleetResourceSlug, filters: FleetListFilters = {}) => {
|
|
const handler = pagedHandlers[slug];
|
|
if (!handler) throw new Error(`Fleet resource "${slug}" has no paginated list endpoint`);
|
|
return handler(filters);
|
|
},
|
|
create: (slug: FleetResourceSlug, data: Record<string, unknown>) => createHandlers[slug](data),
|
|
update: (slug: FleetResourceSlug, id: string, data: Record<string, unknown>) =>
|
|
updateHandlers[slug](id, data),
|
|
remove: (slug: FleetResourceSlug, id: string) => removeHandlers[slug](id),
|
|
purge: (slug: FleetResourceSlug, id: string) => {
|
|
const handler = purgeHandlers[slug];
|
|
if (!handler) {
|
|
throw new Error(`Fleet resource "${slug}" cannot be permanently deleted`);
|
|
}
|
|
return handler(id);
|
|
},
|
|
};
|