Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions src/handlers/update-xero-bank-transaction.handler.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { BankTransaction } from "xero-node";

const authenticateMock = vi.fn();
const getBankTransactionMock = vi.fn();
const updateBankTransactionMock = vi.fn();
const getClientHeadersMock = vi.fn(() => ({ "x-test": "header" }));

vi.mock("../clients/xero-client.js", () => ({
xeroClient: {
tenantId: "tenant-123",
authenticate: authenticateMock,
accountingApi: {
getBankTransaction: getBankTransactionMock,
updateBankTransaction: updateBankTransactionMock,
},
},
}));

vi.mock("../helpers/get-client-headers.js", () => ({
getClientHeaders: getClientHeadersMock,
}));

describe("updateXeroBankTransaction", () => {
beforeEach(() => {
vi.clearAllMocks();
});

it("sends a minimal update payload when line items are provided", async () => {
const existingBankTransaction: Partial<BankTransaction> = {
bankTransactionID: "btx-123",
reference: "old-ref",
date: "2026-06-01",
lineItems: [
{
description: "Old item",
quantity: 1,
unitAmount: 10,
accountCode: "200",
taxType: "NONE",
},
],
status: BankTransaction.StatusEnum.AUTHORISED,
};

const newLineItems = [
{
description: "Updated item",
quantity: 2,
unitAmount: 25,
accountCode: "310",
taxType: "OUTPUT2",
},
];

getBankTransactionMock.mockResolvedValue({
body: { bankTransactions: [existingBankTransaction] },
});
updateBankTransactionMock.mockResolvedValue({
body: {
bankTransactions: [
{
bankTransactionID: "btx-123",
lineItems: newLineItems,
},
],
},
});

const { updateXeroBankTransaction } = await import(
"./update-xero-bank-transaction.handler.js"
);

const result = await updateXeroBankTransaction(
"btx-123",
undefined,
undefined,
newLineItems,
"new-ref",
"2026-06-12",
);

expect(result.isError).toBe(false);
expect(authenticateMock).toHaveBeenCalledTimes(1);
expect(updateBankTransactionMock).toHaveBeenCalledWith(
"tenant-123",
"btx-123",
{
bankTransactions: [
{
lineItems: newLineItems,
reference: "new-ref",
date: "2026-06-12",
},
],
},
undefined,
undefined,
{ "x-test": "header" },
);

const payload = updateBankTransactionMock.mock.calls[0][2].bankTransactions[0];
expect(payload).not.toHaveProperty("status");
expect(payload).not.toHaveProperty("currencyCode");
expect(payload).not.toHaveProperty("bankTransactionID");
});
});
16 changes: 6 additions & 10 deletions src/handlers/update-xero-bank-transaction.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,18 @@ async function getBankTransaction(bankTransactionId: string): Promise<BankTransa

async function updateBankTransaction(
bankTransactionId: string,
existingBankTransaction: BankTransaction,
type?: BankTransactionType,
contactId?: string,
lineItems?: BankTransactionLineItem[],
reference?: string,
date?: string
): Promise<BankTransaction | undefined> {
const bankTransaction: BankTransaction = {
...existingBankTransaction,
bankTransactionID: bankTransactionId,
type: type ? BankTransaction.TypeEnum[type] : existingBankTransaction.type,
contact: contactId ? { contactID: contactId } : existingBankTransaction.contact,
lineItems: lineItems ? lineItems : existingBankTransaction.lineItems,
reference: reference ? reference : existingBankTransaction.reference,
date: date ? date : existingBankTransaction.date
...(type ? { type: BankTransaction.TypeEnum[type] } : {}),
...(contactId ? { contact: { contactID: contactId } } : {}),
...(lineItems ? { lineItems } : {}),
...(reference ? { reference } : {}),
...(date ? { date } : {}),
};

const response = await xeroClient.accountingApi.updateBankTransaction(
Expand Down Expand Up @@ -75,7 +72,6 @@ export async function updateXeroBankTransaction(

const updatedBankTransaction = await updateBankTransaction(
bankTransactionId,
existingBankTransaction,
type,
contactId,
lineItems,
Expand All @@ -99,4 +95,4 @@ export async function updateXeroBankTransaction(
error: formatError(error),
};
}
}
}