refactor: migrate to use the central api obj

This commit is contained in:
Nathnael
2026-06-22 11:52:59 +00:00
parent 71db3a968e
commit 74e03f2067
8 changed files with 805 additions and 105 deletions

View File

@@ -37,8 +37,724 @@ import {
import type { BookingDetail } from "@/types/booking";
import type { IOverviewDashboard, OverviewRange } from "@/types/overview";
import { overviewService } from "./overview.service";
import {
cargoService,
type Cargo,
type DeliverCargoPayload,
} from "./cargoService";
import { warehouseService } from "./warehouse.service";
import type {
AllocationCriteria,
AllocationPreviewResult,
AllocationRule,
ArrivalQueueItem,
AutoLoadResult,
AutoUnloadArrivedResult,
AutoUnloadResult,
BookingScheduleView,
BulkDispatchResult,
BulkInspectPayload,
BulkInspectResult,
BulkReceivePayload,
BulkReceiveResult,
DeliverInventoryPayload,
EligibleBooking,
FeePreview,
FeeRule,
ImportTrain,
ImportTrainItem,
ImportUnloadedItem,
InspectionAttachment,
InspectionReport,
InspectionReportPayload,
InventoryFilter,
InventoryInquiryFilter,
InventoryInquiryResult,
InventoryMovement,
LoadableWagon,
LoadInventoryPayload,
LoadPassedExportResult,
MoveInventoryPayload,
PayInvoicePayload,
ReadyToLoadRow,
ReceiveInventoryPayload,
ReleaseOrderPayload,
ReserveInventoryPayload,
SaveAllocationRulePayload,
SaveFeeRulePayload,
SaveWarehousePayload,
SaveYardPayload,
SaveZonePayload,
Warehouse,
WarehouseActivityLog,
WarehouseDashboard,
WarehouseFeeInvoice,
WarehouseFilter,
WarehouseInventoryItem,
WarehouseInvoiceFilter,
WarehouseLoading,
WarehouseYard,
WarehouseZone,
} from "@/types/warehouse";
/** Query keys for inventory-lifecycle mutations that ripple across views. */
const INVENTORY_INVALIDATIONS: ReadonlyArray<readonly unknown[]> = [
["warehouse-inventory"],
["warehouse-loadings"],
["warehouses"],
];
export const api = {
warehouses: {
// ── Warehouses ─────────────────────────────────────────────────────────
list: endpoint<{ filter?: WarehouseFilter }, Warehouse[]>(
"warehouses",
"list",
({ filter }) => warehouseService.list(filter).then((r) => r.data),
),
getById: endpoint<{ id: string }, Warehouse>(
"warehouses",
"getById",
({ id }) => warehouseService.getById(id).then((r) => r.data),
),
dashboard: endpoint<void, WarehouseDashboard>("warehouses", "dashboard", () =>
warehouseService.dashboard().then((r) => r.data),
),
create: endpoint<SaveWarehousePayload, Warehouse>(
"warehouses",
"create",
(payload) => warehouseService.create(payload).then((r) => r.data),
undefined,
() => [["warehouses"]],
),
update: endpoint<
{ id: string; payload: Partial<SaveWarehousePayload> },
Warehouse
>(
"warehouses",
"update",
({ id, payload }) =>
warehouseService.update(id, payload).then((r) => r.data),
undefined,
() => [["warehouses"]],
),
// ── Yards ──────────────────────────────────────────────────────────────
listYards: endpoint<{ warehouseId: string }, WarehouseYard[]>(
"warehouses",
"listYards",
({ warehouseId }) =>
warehouseService.listYards(warehouseId).then((r) => r.data),
),
createYard: endpoint<
{ warehouseId: string; payload: SaveYardPayload },
WarehouseYard
>(
"warehouses",
"createYard",
({ warehouseId, payload }) =>
warehouseService.createYard(warehouseId, payload).then((r) => r.data),
undefined,
() => [["warehouses"]],
),
updateYard: endpoint<
{ id: string; payload: Partial<SaveYardPayload> },
WarehouseYard
>(
"warehouses",
"updateYard",
({ id, payload }) =>
warehouseService.updateYard(id, payload).then((r) => r.data),
undefined,
() => [["warehouses"], ["warehouse-yards"]],
),
// ── Zones ──────────────────────────────────────────────────────────────
listZones: endpoint<{ yardId: string }, WarehouseZone[]>(
"warehouses",
"listZones",
({ yardId }) => warehouseService.listZones(yardId).then((r) => r.data),
({ yardId }) => ["warehouse-yards", yardId, "zones"],
),
createZone: endpoint<
{ yardId: string; payload: SaveZonePayload },
WarehouseZone
>(
"warehouses",
"createZone",
({ yardId, payload }) =>
warehouseService.createZone(yardId, payload).then((r) => r.data),
undefined,
({ yardId }) => [["warehouse-yards", yardId, "zones"]],
),
updateZone: endpoint<
{ id: string; payload: Partial<SaveZonePayload> },
WarehouseZone
>(
"warehouses",
"updateZone",
({ id, payload }) =>
warehouseService.updateZone(id, payload).then((r) => r.data),
undefined,
() => [["warehouse-yards"]],
),
// ── Inventory (queries) ────────────────────────────────────────────────
listInventory: endpoint<
{ filter?: InventoryFilter },
WarehouseInventoryItem[]
>(
"warehouse-inventory",
"list",
({ filter }) => warehouseService.listInventory(filter).then((r) => r.data),
),
inquiry: endpoint<
{ filter: InventoryInquiryFilter },
InventoryInquiryResult[]
>(
"warehouse-inventory",
"inquiry",
({ filter }) => warehouseService.inquiry(filter).then((r) => r.data),
({ filter }) => ["warehouse-inventory", "inquiry", filter],
),
eligibleBookings: endpoint<void, EligibleBooking[]>(
"warehouse-inventory",
"eligible-bookings",
() => warehouseService.eligibleBookings().then((r) => r.data),
() => ["warehouse-inventory", "eligible-bookings"],
),
readyToLoadExport: endpoint<void, ReadyToLoadRow[]>(
"warehouse-inventory",
"ready-to-load-export",
() => warehouseService.readyToLoadExport().then((r) => r.data),
() => ["warehouse-inventory", "ready-to-load-export"],
),
loadedExport: endpoint<void, ReadyToLoadRow[]>(
"warehouse-inventory",
"loaded-export",
() => warehouseService.loadedExport().then((r) => r.data),
() => ["warehouse-inventory", "loaded-export"],
),
importArriveQueue: endpoint<void, ImportTrain[]>(
"warehouse-inventory",
"import-arrive-queue",
() => warehouseService.importArriveQueue().then((r) => r.data),
() => ["warehouse-inventory", "import-arrive-queue"],
),
importTrainItems: endpoint<{ scheduleId: string }, ImportTrainItem[]>(
"warehouse-inventory",
"import-train-items",
({ scheduleId }) =>
warehouseService.importTrainItems(scheduleId).then((r) => r.data),
({ scheduleId }) => ["warehouse-inventory", "import-train-items", scheduleId],
),
importUnloadedQueue: endpoint<void, ImportUnloadedItem[]>(
"warehouse-inventory",
"import-unloaded-queue",
() => warehouseService.importUnloadedQueue().then((r) => r.data),
() => ["warehouse-inventory", "import-unloaded-queue"],
),
importPickupReadyQueue: endpoint<void, ImportUnloadedItem[]>(
"warehouse-inventory",
"import-pickup-ready-queue",
() => warehouseService.importPickupReadyQueue().then((r) => r.data),
() => ["warehouse-inventory", "import-pickup-ready-queue"],
),
loadableWagons: endpoint<void, LoadableWagon[]>(
"warehouse",
"loadable-wagons",
() => warehouseService.loadableWagons().then((r) => r.data),
() => ["warehouse", "loadable-wagons"],
),
loadings: endpoint<
{ params?: { bookingId?: string; wagonId?: string } },
WarehouseLoading[]
>(
"warehouse-loadings",
"list",
({ params }) => warehouseService.loadings(params).then((r) => r.data),
({ params }) => ["warehouse-loadings", params ?? {}],
),
bookingSchedule: endpoint<{ bookingId: string }, BookingScheduleView>(
"warehouse",
"booking-schedule",
({ bookingId }) =>
warehouseService.bookingSchedule(bookingId).then((r) => r.data),
({ bookingId }) => ["warehouse", "booking-schedule", bookingId],
),
movements: endpoint<{ id: string }, InventoryMovement[]>(
"warehouse-inventory",
"movements",
({ id }) => warehouseService.movements(id).then((r) => r.data),
({ id }) => ["warehouse-inventory", id, "movements"],
),
activity: endpoint<{ id: string }, WarehouseActivityLog[]>(
"warehouse-inventory",
"activity",
({ id }) => warehouseService.activity(id).then((r) => r.data),
({ id }) => ["warehouse-inventory", id, "activity"],
),
arrivalQueue: endpoint<void, ArrivalQueueItem[]>(
"warehouse-inventory",
"arrival-queue",
() => warehouseService.arrivalQueue().then((r) => r.data),
() => ["warehouse-inventory", "arrival-queue"],
),
inspectionReports: endpoint<{ inventoryId: string }, InspectionReport[]>(
"warehouse-inventory",
"inspection-reports",
({ inventoryId }) =>
warehouseService.listInspectionReports(inventoryId).then((r) => r.data),
({ inventoryId }) =>
["warehouse-inventory", inventoryId, "inspection-reports"],
),
allocationRules: endpoint<void, AllocationRule[]>(
"warehouse-allocation-rules",
"list",
() => warehouseService.listAllocationRules().then((r) => r.data),
() => ["warehouse-allocation-rules"],
),
feeRules: endpoint<void, FeeRule[]>(
"warehouse-fee-rules",
"list",
() => warehouseService.listFeeRules().then((r) => r.data),
() => ["warehouse-fee-rules"],
),
feePreview: endpoint<{ inventoryId: string }, FeePreview[]>(
"warehouse-inventory",
"fee-preview",
({ inventoryId }) =>
warehouseService.feePreview(inventoryId).then((r) => r.data),
({ inventoryId }) => ["warehouse-inventory", inventoryId, "fee-preview"],
),
invoices: endpoint<{ filter?: WarehouseInvoiceFilter }, WarehouseFeeInvoice[]>(
"warehouse-fee-invoices",
"list",
({ filter }) => warehouseService.listInvoices(filter).then((r) => r.data),
({ filter }) => ["warehouse-fee-invoices", filter ?? {}],
),
invoice: endpoint<{ id: string }, WarehouseFeeInvoice>(
"warehouse-fee-invoices",
"detail",
({ id }) => warehouseService.getInvoice(id).then((r) => r.data),
({ id }) => ["warehouse-fee-invoices", "detail", id],
),
invoicesForInventory: endpoint<
{ inventoryId: string },
WarehouseFeeInvoice[]
>(
"warehouse-inventory",
"fee-invoices",
({ inventoryId }) =>
warehouseService.invoicesForInventory(inventoryId).then((r) => r.data),
({ inventoryId }) => ["warehouse-inventory", inventoryId, "fee-invoices"],
),
// ── Inventory (mutations) ──────────────────────────────────────────────
receiveInventory: endpoint<ReceiveInventoryPayload, WarehouseInventoryItem>(
"warehouse-inventory",
"receive",
(payload) => warehouseService.receiveInventory(payload).then((r) => r.data),
undefined,
() => [["warehouse-inventory"], ["warehouses"]],
),
store: endpoint<string, WarehouseInventoryItem>(
"warehouse-inventory",
"store",
(id) => warehouseService.store(id).then((r) => r.data),
undefined,
() => INVENTORY_INVALIDATIONS,
),
reserve: endpoint<ReserveInventoryPayload, WarehouseInventoryItem>(
"warehouse-inventory",
"reserve",
(payload) => warehouseService.reserve(payload).then((r) => r.data),
undefined,
() => INVENTORY_INVALIDATIONS,
),
markReadyForLoading: endpoint<string, WarehouseInventoryItem>(
"warehouse-inventory",
"mark-ready-for-loading",
(id) => warehouseService.markReadyForLoading(id).then((r) => r.data),
undefined,
() => INVENTORY_INVALIDATIONS,
),
load: endpoint<
{ id: string; payload: LoadInventoryPayload },
WarehouseInventoryItem
>(
"warehouse-inventory",
"load",
({ id, payload }) => warehouseService.load(id, payload).then((r) => r.data),
undefined,
() => INVENTORY_INVALIDATIONS,
),
dispatch: endpoint<string, WarehouseInventoryItem>(
"warehouse-inventory",
"dispatch",
(id) => warehouseService.dispatch(id).then((r) => r.data),
undefined,
() => INVENTORY_INVALIDATIONS,
),
move: endpoint<
{ id: string; payload: MoveInventoryPayload },
WarehouseInventoryItem
>(
"warehouse-inventory",
"move",
({ id, payload }) => warehouseService.move(id, payload).then((r) => r.data),
undefined,
() => INVENTORY_INVALIDATIONS,
),
markReadyForPickup: endpoint<string, WarehouseInventoryItem>(
"warehouse-inventory",
"mark-ready-for-pickup",
(id) => warehouseService.markReadyForPickup(id).then((r) => r.data),
undefined,
() => INVENTORY_INVALIDATIONS,
),
release: endpoint<
{ id: string; payload: ReleaseOrderPayload },
WarehouseInventoryItem
>(
"warehouse-inventory",
"release",
({ id, payload }) =>
warehouseService.release(id, payload).then((r) => r.data),
undefined,
() => INVENTORY_INVALIDATIONS,
),
deliver: endpoint<
{ id: string; payload: DeliverInventoryPayload },
WarehouseInventoryItem
>(
"warehouse-inventory",
"deliver",
({ id, payload }) =>
warehouseService.deliver(id, payload).then((r) => r.data),
undefined,
() => INVENTORY_INVALIDATIONS,
),
bulkReceive: endpoint<BulkReceivePayload, BulkReceiveResult>(
"warehouse-inventory",
"bulk-receive",
(payload) => warehouseService.receiveBulk(payload).then((r) => r.data),
undefined,
() => INVENTORY_INVALIDATIONS,
),
loadPassedExport: endpoint<void, LoadPassedExportResult>(
"warehouse-inventory",
"load-passed-export",
() => warehouseService.loadPassedExport().then((r) => r.data),
undefined,
() => INVENTORY_INVALIDATIONS,
),
bulkMarkInspected: endpoint<BulkInspectPayload, BulkInspectResult>(
"warehouse-inventory",
"bulk-mark-inspected",
(payload) => warehouseService.bulkMarkInspected(payload).then((r) => r.data),
undefined,
() => INVENTORY_INVALIDATIONS,
),
bulkDispatchExport: endpoint<string[], BulkDispatchResult>(
"warehouse-inventory",
"bulk-dispatch-export",
(inventoryIds) =>
warehouseService.bulkDispatchExport(inventoryIds).then((r) => r.data),
undefined,
() => INVENTORY_INVALIDATIONS,
),
autoUnloadArrivedBookings: endpoint<string, AutoUnloadArrivedResult>(
"warehouse-inventory",
"auto-unload-arrived-bookings",
(scheduleId) =>
warehouseService
.autoUnloadArrivedBookings(scheduleId)
.then((r) => r.data),
undefined,
() => INVENTORY_INVALIDATIONS,
),
autoUnloadArrived: endpoint<void, AutoUnloadResult>(
"warehouse-inventory",
"auto-unload-arrived",
() => warehouseService.autoUnloadArrived().then((r) => r.data),
undefined,
() => [["warehouse-inventory"], ["warehouses"]],
),
autoLoadReady: endpoint<void, AutoLoadResult>(
"warehouse-inventory",
"auto-load-ready",
() => warehouseService.autoLoadReady().then((r) => r.data),
undefined,
() => [["warehouse-inventory"], ["warehouses"]],
),
unloadBooking: endpoint<
{ bookingId: string; payload?: Record<string, unknown> },
WarehouseInventoryItem
>(
"warehouse-inventory",
"unload-booking",
({ bookingId, payload }) =>
warehouseService.unloadBooking(bookingId, payload).then((r) => r.data),
undefined,
() => [["warehouse-inventory"], ["warehouses"]],
),
createInspectionReport: endpoint<
{ inventoryId: string; payload: InspectionReportPayload },
InspectionReport
>(
"warehouse-inventory",
"create-inspection-report",
({ inventoryId, payload }) =>
warehouseService
.createInspectionReport(inventoryId, payload)
.then((r) => r.data),
undefined,
({ inventoryId }) => [
["warehouse-inventory", inventoryId, "inspection-reports"],
["warehouse-inventory"],
],
),
uploadInspectionAttachments: endpoint<
{ reportId: string; files: File[] },
InspectionAttachment[]
>(
"warehouse-inventory",
"upload-inspection-attachments",
({ reportId, files }) =>
warehouseService
.uploadInspectionAttachments(reportId, files)
.then((r) => r.data),
),
// ── Allocation + fee rules ─────────────────────────────────────────────
previewAllocation: endpoint<AllocationCriteria, AllocationPreviewResult | null>(
"warehouse-allocation-rules",
"preview",
(criteria) =>
warehouseService.previewAllocation(criteria).then((r) => r.data),
),
createAllocationRule: endpoint<SaveAllocationRulePayload, AllocationRule>(
"warehouse-allocation-rules",
"create",
(payload) =>
warehouseService.createAllocationRule(payload).then((r) => r.data),
undefined,
() => [["warehouse-allocation-rules"]],
),
updateAllocationRule: endpoint<
{ id: string; payload: Partial<SaveAllocationRulePayload> },
AllocationRule
>(
"warehouse-allocation-rules",
"update",
({ id, payload }) =>
warehouseService.updateAllocationRule(id, payload).then((r) => r.data),
undefined,
() => [["warehouse-allocation-rules"]],
),
deleteAllocationRule: endpoint<string, void>(
"warehouse-allocation-rules",
"delete",
(id) => warehouseService.deleteAllocationRule(id).then(() => undefined),
undefined,
() => [["warehouse-allocation-rules"]],
),
createFeeRule: endpoint<SaveFeeRulePayload, FeeRule>(
"warehouse-fee-rules",
"create",
(payload) => warehouseService.createFeeRule(payload).then((r) => r.data),
undefined,
() => [["warehouse-fee-rules"]],
),
updateFeeRule: endpoint<
{ id: string; payload: Partial<SaveFeeRulePayload> },
FeeRule
>(
"warehouse-fee-rules",
"update",
({ id, payload }) =>
warehouseService.updateFeeRule(id, payload).then((r) => r.data),
undefined,
() => [["warehouse-fee-rules"]],
),
deleteFeeRule: endpoint<string, void>(
"warehouse-fee-rules",
"delete",
(id) => warehouseService.deleteFeeRule(id).then(() => undefined),
undefined,
() => [["warehouse-fee-rules"]],
),
// ── Invoices ───────────────────────────────────────────────────────────
generateInvoice: endpoint<
{ inventoryId: string; confirmZero?: boolean },
WarehouseFeeInvoice
>(
"warehouse-fee-invoices",
"generate",
({ inventoryId, confirmZero }) =>
warehouseService
.generateInvoice(inventoryId, confirmZero)
.then((r) => r.data),
undefined,
() => [["warehouse-fee-invoices"], ["warehouse-inventory"]],
),
cancelInvoice: endpoint<string, WarehouseFeeInvoice>(
"warehouse-fee-invoices",
"cancel",
(id) => warehouseService.cancelInvoice(id).then((r) => r.data),
undefined,
() => [["warehouse-fee-invoices"], ["warehouse-inventory"]],
),
payInvoice: endpoint<
{ id: string; payload: PayInvoicePayload },
WarehouseFeeInvoice
>(
"warehouse-fee-invoices",
"pay",
({ id, payload }) =>
warehouseService.payInvoice(id, payload).then((r) => r.data),
undefined,
() => [["warehouse-fee-invoices"], ["warehouse-inventory"]],
),
gateClearance: endpoint<string, WarehouseInventoryItem>(
"warehouse-fee-invoices",
"gate-clearance",
(inventoryId) =>
warehouseService.gateClearance(inventoryId).then((r) => r.data),
undefined,
() => [["warehouse-fee-invoices"], ["warehouse-inventory"]],
),
},
cargoes: {
list: endpoint<void, Cargo[]>("cargoes", "list", () =>
cargoService.getAll().then((r) => r.data),
),
listByContainer: endpoint<{ containerId: string }, Cargo[]>(
"cargoes",
"listByContainer",
({ containerId }) =>
cargoService.getByContainer(containerId).then((r) => r.data),
),
getById: endpoint<{ id: string }, Cargo>("cargoes", "getById", ({ id }) =>
cargoService.getById(id).then((r) => r.data),
),
create: endpoint<Partial<Cargo>, Cargo>(
"cargoes",
"create",
(payload) => cargoService.create(payload).then((r) => r.data),
undefined,
() => [["cargoes"]],
),
update: endpoint<{ id: string; data: Partial<Cargo> }, Cargo>(
"cargoes",
"update",
({ id, data }) => cargoService.update(id, data).then((r) => r.data),
undefined,
() => [["cargoes"]],
),
remove: endpoint<string, void>(
"cargoes",
"remove",
(id) => cargoService.delete(id).then(() => undefined),
undefined,
() => [["cargoes"]],
),
load: endpoint<
{ id: string; quantity: number; weight: number; volume?: number },
Cargo
>(
"cargoes",
"load",
({ id, quantity, weight, volume }) =>
cargoService.load(id, quantity, weight, volume).then((r) => r.data),
undefined,
() => [["cargoes"]],
),
deliver: endpoint<{ id: string; payload?: DeliverCargoPayload }, Cargo>(
"cargoes",
"deliver",
({ id, payload }) =>
cargoService.deliver(id, payload).then((r) => r.data),
undefined,
() => [["cargoes"]],
),
unload: endpoint<{ id: string }, Cargo>(
"cargoes",
"unload",
({ id }) => cargoService.unload(id).then((r) => r.data),
undefined,
() => [["cargoes"]],
),
},
fileUploadSettings: {
list: endpoint<void, FileUploadSetting[]>(
"file-upload-settings",