import type { Freight } from "@edr/types"; import { api as client } from "../auth/http"; import { unwrap } from "@/utils/endpoint"; import { URL_CONSTANTS } from "@/constants/URLS"; const X = URL_CONSTANTS.GL_EXCHANGE; export interface GlExchangeUpload { title: string; visibleToCustomer: boolean; file: File; } export interface GlExchangeEdit { title?: string; visibleToCustomer?: boolean; /** Optional replacement bytes — omit to keep the stored file. */ file?: File | null; } const multipart = { headers: { "Content-Type": "multipart/form-data" } }; /** GL Ethiopia ↔ GL Djibouti shared documents for one booking or contract. */ export const glExchangeService = { list: async (entityId: string): Promise => { const response = await client.get(X.FOR_ENTITY(entityId)); return (unwrap(response.data) ?? []) as Freight.GlExchangeDocument[]; }, upload: async ( entityId: string, input: GlExchangeUpload, ): Promise => { const form = new FormData(); form.append("file", input.file); form.append("title", input.title); form.append("visibleToCustomer", String(input.visibleToCustomer)); const response = await client.post(X.FOR_ENTITY(entityId), form, multipart); return unwrap(response.data) as Freight.GlExchangeDocument; }, update: async ( documentId: string, input: GlExchangeEdit, ): Promise => { const form = new FormData(); if (input.file) form.append("file", input.file); if (input.title != null) form.append("title", input.title); if (input.visibleToCustomer != null) { form.append("visibleToCustomer", String(input.visibleToCustomer)); } const response = await client.patch(X.DOCUMENT(documentId), form, multipart); return unwrap(response.data) as Freight.GlExchangeDocument; }, remove: async (documentId: string): Promise => { await client.delete(X.DOCUMENT(documentId)); }, };