Skip to content

Commit

Permalink
Support ofx INVSTMTMSGSRSV1 (actualbudget#1876)
Browse files Browse the repository at this point in the history
* Support ofx INVSTMTMSGSRSV1

* Release notes
  • Loading branch information
joel-jeremy authored Nov 11, 2023
1 parent 30c7024 commit 353e7be
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 19 deletions.
26 changes: 13 additions & 13 deletions packages/desktop-client/src/components/modals/ImportTransactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -767,11 +767,11 @@ export default function ImportTransactions({ modalProps, options }) {

useEffect(() => {
const fileType = getFileType(options.filename);
const parseOptions = getParseOptions(
fileType,
{ delimiter, hasHeaderRow },
{ fallbackMissingPayeeToMemo },
);
const parseOptions = getParseOptions(fileType, {
delimiter,
hasHeaderRow,
fallbackMissingPayeeToMemo,
});

parse(options.filename, parseOptions);
}, [parseTransactions, options.filename]);
Expand Down Expand Up @@ -819,11 +819,11 @@ export default function ImportTransactions({ modalProps, options }) {
});

const fileType = getFileType(res[0]);
const parseOptions = getParseOptions(
fileType,
{ delimiter, hasHeaderRow },
{ fallbackMissingPayeeToMemo },
);
const parseOptions = getParseOptions(fileType, {
delimiter,
hasHeaderRow,
fallbackMissingPayeeToMemo,
});

parse(res[0], parseOptions);
}
Expand Down Expand Up @@ -1192,12 +1192,12 @@ export default function ImportTransactions({ modalProps, options }) {
);
}

function getParseOptions(fileType, csvOptions, ofxOptions) {
function getParseOptions(fileType, options = {}) {
if (fileType === 'csv') {
const { delimiter, hasHeaderRow } = csvOptions;
const { delimiter, hasHeaderRow } = options;
return { delimiter, hasHeaderRow };
} else if (isOfxFile(fileType)) {
const { fallbackMissingPayeeToMemo } = ofxOptions;
const { fallbackMissingPayeeToMemo } = options;
return { fallbackMissingPayeeToMemo };
}
return {};
Expand Down
52 changes: 46 additions & 6 deletions packages/loot-core/src/server/accounts/ofx2json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,54 @@ async function parseXml(content) {

function getStmtTrn(data) {
const ofx = data?.['OFX'];
const isCc = ofx?.['CREDITCARDMSGSRSV1'] != null;
const msg = isCc ? ofx?.['CREDITCARDMSGSRSV1'] : ofx?.['BANKMSGSRSV1'];
const stmtTrnRs = msg?.[`${isCc ? 'CC' : ''}STMTTRNRS`];
const stmtRs = stmtTrnRs?.[`${isCc ? 'CC' : ''}STMTRS`];
const bankTranList = stmtRs?.['BANKTRANLIST'];
if (ofx?.['CREDITCARDMSGSRSV1'] != null) {
return getCcStmtTrn(ofx);
} else if (ofx?.['INVSTMTMSGSRSV1'] != null) {
return getInvStmtTrn(ofx);
} else {
return getBankStmtTrn(ofx);
}
}

function getBankStmtTrn(ofx) {
const msg = ofx?.['BANKMSGSRSV1'];
const stmtTrnRs = msg?.['STMTTRNRS'];
const stmtRs = stmtTrnRs?.['STMTRS'];
const tranList = stmtRs?.['BANKTRANLIST'];
// Could be an array or a single object.
// xml2js serializes single item to an object and multiple to an array.
const stmtTrn = tranList?.['STMTTRN'];

if (!Array.isArray(stmtTrn)) {
return [stmtTrn];
}
return stmtTrn;
}

function getCcStmtTrn(ofx) {
const msg = ofx?.['CREDITCARDMSGSRSV1'];
const stmtTrnRs = msg?.['CCSTMTTRNRS'];
const stmtRs = stmtTrnRs?.['CCSTMTRS'];
const tranList = stmtRs?.['BANKTRANLIST'];
// Could be an array or a single object.
// xml2js serializes single item to an object and multiple to an array.
const stmtTrn = bankTranList?.['STMTTRN'];
const stmtTrn = tranList?.['STMTTRN'];

if (!Array.isArray(stmtTrn)) {
return [stmtTrn];
}
return stmtTrn;
}

function getInvStmtTrn(ofx) {
const msg = ofx?.['INVSTMTMSGSRSV1'];
const stmtTrnRs = msg?.['INVSTMTTRNRS'];
const stmtRs = stmtTrnRs?.['INVSTMTRS'];
const tranList = stmtRs?.['INVTRANLIST'];
// Could be an array or a single object.
// xml2js serializes single item to an object and multiple to an array.
const stmtTrn = tranList?.['INVBANKTRAN']?.flatMap(t => t?.['STMTTRN']);

if (!Array.isArray(stmtTrn)) {
return [stmtTrn];
}
Expand Down
6 changes: 6 additions & 0 deletions upcoming-release-notes/1876.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Enhancements
authors: [joel-jeremy]
---

Support import of ofx transactions of type INVSTMTMSGSRSV1

0 comments on commit 353e7be

Please sign in to comment.