Files
edr-platform/apps/edr-freight-web/backoffice/src/services/glExchange.service.ts

61 lines
2.0 KiB
TypeScript

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<Freight.GlExchangeDocument[]> => {
const response = await client.get(X.FOR_ENTITY(entityId));
return (unwrap(response.data) ?? []) as Freight.GlExchangeDocument[];
},
upload: async (
entityId: string,
input: GlExchangeUpload,
): Promise<Freight.GlExchangeDocument> => {
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<Freight.GlExchangeDocument> => {
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<void> => {
await client.delete(X.DOCUMENT(documentId));
},
};