diff --git a/src/interactions/customerInteraction.ts b/src/interactions/customerInteraction.ts index 5ddb8e6..f1c4f2c 100644 --- a/src/interactions/customerInteraction.ts +++ b/src/interactions/customerInteraction.ts @@ -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 { diff --git a/src/interactions/statementInteractionCAMT.ts b/src/interactions/statementInteractionCAMT.ts index 3644f23..33e1f26 100644 --- a/src/interactions/statementInteractionCAMT.ts +++ b/src/interactions/statementInteractionCAMT.ts @@ -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 `` // and checks if it contains the attribute encoding="UTF-8". @@ -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); diff --git a/src/tests/partedResponse.test.ts b/src/tests/partedResponse.test.ts index 5b0bdd9..f1e8e88 100644 --- a/src/tests/partedResponse.test.ts +++ b/src/tests/partedResponse.test.ts @@ -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) => + `` + + `${id}`; + 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 = ''; + 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', () => {