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
6 changes: 6 additions & 0 deletions src/interactions/customerInteraction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ export interface ClientResponse {

export interface StatementResponse extends ClientResponse {
statements: Statement[];
/**
* The CAMT documents as received from the bank (decoded to a string), one entry per
* document. Only set for CAMT (HKCAZ) responses. Gives access to fields the parser does
* not map to `Transaction`, e.g. return information (`RtrInf`) or charges.
*/
rawCamt?: string[];
}

export abstract class CustomerInteraction {
Expand Down
4 changes: 4 additions & 0 deletions src/interactions/statementInteractionCAMT.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ export class StatementInteractionCAMT extends CustomerOrderInteraction {
try {
// Parse all CAMT messages (one per booking day) and combine statements
const allStatements: Statement[] = [];
// Assigned up front so the raw documents survive a parser failure.
const rawCamt: string[] = [];
clientResponse.rawCamt = rawCamt;
for (const camtMessage of camtMessages) {
// The regex looks for the XML declaration `<?xml ... ?>`
// and checks if it contains the attribute encoding="UTF-8".
Expand All @@ -74,6 +77,7 @@ export class StatementInteractionCAMT extends CustomerOrderInteraction {
xmlString = intermediateBuffer.toString('utf8');
}

rawCamt.push(xmlString);
const parser = new CamtParser(xmlString);
const statements = parser.parse();
allStatements.push(...statements);
Expand Down
30 changes: 30 additions & 0 deletions src/tests/partedResponse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,36 @@ describe('StatementInteractionCAMT with a parted response', () => {
).statements.flatMap((s) => s.transactions);
expect(transactions).toHaveLength(2);
});

it('exposes the raw CAMT document of every segment', () => {
const doc = (id: string) =>
`<?xml version="1.0"?><Document xmlns="urn:iso:std:iso:20022:tech:xsd:camt.052.001.08">` +
`<BkToCstmrAcctRpt><GrpHdr><MsgId>${id}</MsgId></GrpHdr></BkToCstmrAcctRpt></Document>`;
const message = Message.decode(`${hicazText(doc('A'))}${hicazText(doc('B'))}`);

const interaction = new StatementInteractionCAMT('123');
const clientResponse = { statements: [] } as unknown as { rawCamt?: string[] };
interaction.handleResponse(message, clientResponse as never);

expect(clientResponse.rawCamt).toEqual([doc('A'), doc('B')]);
});

it('keeps the raw CAMT documents when parsing fails', () => {
const broken = '<?xml version="1.0"?><Document><Unclosed></Document>';
const message = Message.decode(hicazText(broken));
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});

const interaction = new StatementInteractionCAMT('123');
const clientResponse = { statements: [] } as unknown as {
statements: unknown[];
rawCamt?: string[];
};
interaction.handleResponse(message, clientResponse as never);

expect(clientResponse.statements).toEqual([]);
expect(clientResponse.rawCamt).toEqual([broken]);
warn.mockRestore();
});
});

describe('several response segments in one bank message', () => {
Expand Down