Files
edr-platform/apps/edr-freight-web/portal/src/services/invoices.service.ts
2026-06-29 13:54:14 +00:00

90 lines
2.2 KiB
TypeScript

import type { Freight } from "@edr/types";
import { URL_CONSTANTS } from "@/constants/URLS";
import { client } from "../utils/api";
import type { InitiateResponse } from "./payments.service";
const B = URL_CONSTANTS.BILLING;
export interface InvoiceCompany {
id: string;
name: string;
}
export interface InvoiceCompanyProfile {
id: string;
type: string;
reference: string | null;
}
/**
* A customer-facing invoice row, as returned by `GET /billing/my-invoices`.
* Mirrors the freight `Invoice` entity (the shared `Freight.IInvoice` type is
* stale and intentionally not reused here).
*/
export interface PortalInvoice {
id: string;
invoiceNumber: string;
totalAmount: number;
currency: string;
status: Freight.InvoiceStatus;
/** Originating subsystem: booking / warehouse / demurrage. */
source: string;
sourceId: string;
/** What the invoice bills for (e.g. PREPAID). */
type: string;
issuedAt: string | null;
dueAt: string;
createdAt: string;
company?: InvoiceCompany;
companyProfile?: InvoiceCompanyProfile;
}
export interface InvoiceLine {
id: string;
chargeType: string;
description?: string | null;
quantity: number;
unitRate: number;
amount: number;
currency: string;
}
export interface PortalInvoiceDetail extends PortalInvoice {
lines: InvoiceLine[];
}
export interface PayInvoicePayload {
method?: string;
platform?: "web" | "mobile";
payerAccount?: string;
returnUrl?: string;
failureUrl?: string;
}
export const invoicesService = {
/** Every invoice billed to the signed-in customer's company, newest first. */
listMy: async (): Promise<PortalInvoice[]> => {
const { data } = await client.get(B.MY_INVOICES);
return data.data ?? data;
},
/** One of the customer's invoices, with its line items. */
get: async (id: string): Promise<PortalInvoiceDetail> => {
const { data } = await client.get(B.MY_INVOICE_BY_ID(id));
return data.data ?? data;
},
/** Initiate gateway payment for an open invoice; returns the client action. */
pay: async (
id: string,
payload: PayInvoicePayload = {},
): Promise<InitiateResponse> => {
const { data } = await client.post(B.PAY_INVOICE(id), {
platform: "web",
...payload,
});
return data.data ?? data;
},
};