chore: type updates on the billing service

This commit is contained in:
Nathnael
2026-06-29 08:28:12 +00:00
parent 1da5af94ab
commit 36711eace4
2 changed files with 76 additions and 30 deletions

View File

@@ -33,8 +33,20 @@ function generateInput(overrides: Record<string, unknown> = {}) {
companyProfileId: "profile-1",
currency: "ETB",
lines: [
{ chargeType: "RAIL_FREIGHT", description: "Rail freight", quantity: 2, unitRate: 500, amount: 1000 },
{ chargeType: "HAZARD_SURCHARGE", description: "Hazard surcharge", quantity: 2, unitRate: 250, amount: 500 },
{
chargeType: "RAIL_FREIGHT",
description: "Rail freight",
quantity: 2,
unitRate: 500,
amount: 1000,
},
{
chargeType: "HAZARD_SURCHARGE",
description: "Hazard surcharge",
quantity: 2,
unitRate: 250,
amount: 500,
},
],
...overrides,
};
@@ -52,10 +64,17 @@ describe("BillingService.generateInvoice", () => {
manager = makeManager(savedLines);
events = makeEvents();
dataSource = {
transaction: jest.fn().mockImplementation((cb: (mg: unknown) => unknown) => cb(manager)),
transaction: jest
.fn()
.mockImplementation((cb: (mg: unknown) => unknown) => cb(manager)),
manager,
};
service = new BillingService(dataSource as never, {} as never, {} as never, events as never);
service = new BillingService(
dataSource as never,
{} as never,
{} as never,
events as never,
);
});
it("creates a PENDING invoice with one line per input line", async () => {
@@ -122,18 +141,31 @@ describe("BillingService.markInvoiceAsPaid", () => {
);
expect(events.emit).toHaveBeenCalledWith(
"booking.invoice.paid",
expect.objectContaining({ invoiceId: "inv-1", status: Freight.InvoiceStatus.Paid, paymentId: "pay-1" }),
expect.objectContaining({
invoiceId: "inv-1",
status: Freight.InvoiceStatus.Paid,
paymentId: "pay-1",
}),
);
});
it("is a no-op (no event) when the invoice is already paid", async () => {
const paid = { id: "inv-1", status: Freight.InvoiceStatus.Paid, source: "booking" };
const paid = {
id: "inv-1",
status: Freight.InvoiceStatus.Paid,
source: "booking",
};
const mg = {
findOne: jest.fn().mockResolvedValue(paid),
update: jest.fn().mockResolvedValue(undefined),
};
const events = makeEvents();
const service = new BillingService({ manager: mg } as never, {} as never, {} as never, events as never);
const service = new BillingService(
{ manager: mg } as never,
{} as never,
{} as never,
events as never,
);
await service.markInvoiceAsPaid("inv-1", "pay-1", mg as never);
@@ -155,12 +187,16 @@ describe("BillingService.settlePayable", () => {
update: jest.fn().mockResolvedValue(undefined),
};
const events = makeEvents();
const service = new BillingService({ manager: mg } as never, {} as never, {} as never, events as never);
const service = new BillingService(
{ manager: mg } as never,
{} as never,
{} as never,
events as never,
);
const settled = await service.settlePayable(
Freight.InvoiceSource.Booking,
"booking-1",
Freight.InvoiceType.Prepaid,
"pay-1",
mg as never,
);
@@ -171,7 +207,10 @@ describe("BillingService.settlePayable", () => {
{ id: "inv-1" },
{ status: Freight.InvoiceStatus.Paid, paymentId: "pay-1" },
);
expect(events.emit).toHaveBeenCalledWith("booking.invoice.paid", expect.anything());
expect(events.emit).toHaveBeenCalledWith(
"booking.invoice.paid",
expect.anything(),
);
});
it("is a no-op (returns null) when the source has no open invoice", async () => {
@@ -180,12 +219,16 @@ describe("BillingService.settlePayable", () => {
update: jest.fn().mockResolvedValue(undefined),
};
const events = makeEvents();
const service = new BillingService({ manager: mg } as never, {} as never, {} as never, events as never);
const service = new BillingService(
{ manager: mg } as never,
{} as never,
{} as never,
events as never,
);
const settled = await service.settlePayable(
Freight.InvoiceSource.Booking,
"booking-1",
Freight.InvoiceType.Prepaid,
"pay-1",
mg as never,
);