Skip to content

Commit

Permalink
Merge branch 'master' into sf-key-accesskey
Browse files Browse the repository at this point in the history
  • Loading branch information
psybers authored Oct 26, 2024
2 parents 37d9218 + c9e6d78 commit 851f99d
Show file tree
Hide file tree
Showing 19 changed files with 947 additions and 207 deletions.
3 changes: 3 additions & 0 deletions src/app-gocardless/bank-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ export const BANKS_WITH_LIMITED_HISTORY = [
'CESKA_SPORITELNA_LONG_GIBACZPX',
'COOP_EKRDEE22',
'DOTS_HYEEIT22',
'FINECO_FEBIITM2XXX',
'FINECO_UK_FEBIITM2XXX',
'HYPE_BUSINESS_HYEEIT22',
'HYPE_HYEEIT2',
'ILLIMITY_ITTPIT2M',
Expand All @@ -90,6 +92,7 @@ export const BANKS_WITH_LIMITED_HISTORY = [
'LUMINOR_RIKOLV2X',
'MEDICINOSBANK_MDBALT22XXX',
'NORDEA_NDEADKKK',
'N26_NTSBDEB1',
'OPYN_BITAITRRB2B',
'PAYTIPPER_PAYTITM1',
'REVOLUT_REVOLT21',
Expand Down
6 changes: 3 additions & 3 deletions src/app-gocardless/banks/integration-bank.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default {
accessValidForDays: 90,

normalizeAccount(account) {
console.log(
console.debug(
'Available account properties for new institution integration',
{ account: JSON.stringify(account) },
);
Expand Down Expand Up @@ -66,15 +66,15 @@ export default {
},

sortTransactions(transactions = []) {
console.log(
console.debug(
'Available (first 10) transactions properties for new integration of institution in sortTransactions function',
{ top10Transactions: JSON.stringify(transactions.slice(0, 10)) },
);
return sortByBookingDateOrValueDate(transactions);
},

calculateStartingBalance(sortedTransactions = [], balances = []) {
console.log(
console.debug(
'Available (first 10) transactions properties for new integration of institution in calculateStartingBalance function',
{
balances: JSON.stringify(balances),
Expand Down
2 changes: 1 addition & 1 deletion src/app-gocardless/banks/tests/integration-bank.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('IntegrationBank', () => {
let consoleSpy;

beforeEach(() => {
consoleSpy = jest.spyOn(console, 'log');
consoleSpy = jest.spyOn(console, 'debug');
});

describe('normalizeAccount', () => {
Expand Down
125 changes: 125 additions & 0 deletions src/app-gocardless/tests/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,130 @@ describe('utils', () => {
},
]);
});

it('should sort by valueDate if bookingDate is missing', () => {
const transactions = [
{
valueDate: '2023-01-01',
transactionAmount: mockTransactionAmount,
},
{
valueDate: '2023-01-20',
transactionAmount: mockTransactionAmount,
},
{
valueDate: '2023-01-10',
transactionAmount: mockTransactionAmount,
},
];
expect(sortByBookingDateOrValueDate(transactions)).toEqual([
{
valueDate: '2023-01-20',
transactionAmount: mockTransactionAmount,
},
{
valueDate: '2023-01-10',
transactionAmount: mockTransactionAmount,
},
{
valueDate: '2023-01-01',
transactionAmount: mockTransactionAmount,
},
]);
});

it('should use bookingDate primarily even if bookingDateTime is on an other date', () => {
const transactions = [
{
bookingDate: '2023-01-01',
bookingDateTime: '2023-01-01T00:00:00Z',
transactionAmount: mockTransactionAmount,
},
{
bookingDate: '2023-01-10',
bookingDateTime: '2023-01-01T12:00:00Z',
transactionAmount: mockTransactionAmount,
},
{
bookingDate: '2023-01-01',
bookingDateTime: '2023-01-01T12:00:00Z',
transactionAmount: mockTransactionAmount,
},
{
bookingDate: '2023-01-10',
bookingDateTime: '2023-01-01T00:00:00Z',
transactionAmount: mockTransactionAmount,
},
];
expect(sortByBookingDateOrValueDate(transactions)).toEqual([
{
bookingDate: '2023-01-10',
bookingDateTime: '2023-01-01T12:00:00Z',
transactionAmount: mockTransactionAmount,
},
{
bookingDate: '2023-01-10',
bookingDateTime: '2023-01-01T00:00:00Z',
transactionAmount: mockTransactionAmount,
},
{
bookingDate: '2023-01-01',
bookingDateTime: '2023-01-01T12:00:00Z',
transactionAmount: mockTransactionAmount,
},
{
bookingDate: '2023-01-01',
bookingDateTime: '2023-01-01T00:00:00Z',
transactionAmount: mockTransactionAmount,
},
]);
});

it('should sort on booking date if value date is widely off', () => {
const transactions = [
{
bookingDate: '2023-01-01',
valueDateTime: '2023-01-31T00:00:00Z',
transactionAmount: mockTransactionAmount,
},
{
bookingDate: '2023-01-02',
valueDateTime: '2023-01-02T12:00:00Z',
transactionAmount: mockTransactionAmount,
},
{
bookingDate: '2023-01-30',
valueDateTime: '2023-01-01T12:00:00Z',
transactionAmount: mockTransactionAmount,
},
{
bookingDate: '2023-01-30',
valueDateTime: '2023-01-01T00:00:00Z',
transactionAmount: mockTransactionAmount,
},
];
expect(sortByBookingDateOrValueDate(transactions)).toEqual([
{
bookingDate: '2023-01-30',
valueDateTime: '2023-01-01T12:00:00Z',
transactionAmount: mockTransactionAmount,
},
{
bookingDate: '2023-01-30',
valueDateTime: '2023-01-01T00:00:00Z',
transactionAmount: mockTransactionAmount,
},
{
bookingDate: '2023-01-02',
valueDateTime: '2023-01-02T12:00:00Z',
transactionAmount: mockTransactionAmount,
},
{
bookingDate: '2023-01-01',
valueDateTime: '2023-01-31T00:00:00Z',
transactionAmount: mockTransactionAmount,
},
]);
});
});
});
39 changes: 34 additions & 5 deletions src/app-gocardless/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,40 @@ export const printIban = (account) => {
}
};

const compareDates = (
/** @type {string | number | Date | undefined} */ a,
/** @type {string | number | Date | undefined} */ b,
) => {
if (a == null && b == null) {
return 0;
} else if (a == null) {
return 1;
} else if (b == null) {
return -1;
}

return +new Date(a) - +new Date(b);
};

/**
* @type {(function(*, *): number)[]}
*/
const compareFunctions = [
(a, b) => compareDates(a.bookingDate, b.bookingDate),
(a, b) => compareDates(a.bookingDateTime, b.bookingDateTime),
(a, b) => compareDates(a.valueDate, b.valueDate),
(a, b) => compareDates(a.valueDateTime, b.valueDateTime),
];

export const sortByBookingDateOrValueDate = (transactions = []) =>
transactions.sort(
(a, b) =>
+new Date(b.bookingDate || b.valueDate) -
+new Date(a.bookingDate || a.valueDate),
);
transactions.sort((a, b) => {
for (const sortFunction of compareFunctions) {
const result = sortFunction(b, a);
if (result !== 0) {
return result;
}
}
return 0;
});

export const amountToInteger = (n) => Math.round(n * 100);
35 changes: 20 additions & 15 deletions src/app-simplefin/app-simplefin.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,13 @@ app.post(
}
}
}
} catch (error) {
} catch {
invalidToken(res);
return;
}

const now = new Date();
const startDate = new Date(now.getFullYear(), now.getMonth(), 1);
const endDate = new Date(now.getFullYear(), now.getMonth() + 1, 1);

try {
const accounts = await getAccounts(accessKey, startDate, endDate);
const accounts = await getAccounts(accessKey, null, null, true);

res.send({
status: 'ok',
Expand Down Expand Up @@ -317,7 +313,12 @@ function normalizeDate(date) {
return (date.valueOf() - date.getTimezoneOffset() * 60 * 1000) / 1000;
}

async function getAccounts(accessKey, startDate, endDate) {
async function getAccounts(
accessKey,
startDate,
endDate,
noTransactions = false,
) {
const sfin = parseAccessKey(accessKey);
const options = {
headers: {
Expand All @@ -327,16 +328,20 @@ async function getAccounts(accessKey, startDate, endDate) {
},
};
const params = [];
let queryString = '';
if (startDate) {
params.push(`start-date=${normalizeDate(startDate)}`);
}
if (endDate) {
params.push(`end-date=${normalizeDate(endDate)}`);
}
if (!noTransactions) {
if (startDate) {
params.push(`start-date=${normalizeDate(startDate)}`);
}
if (endDate) {
params.push(`end-date=${normalizeDate(endDate)}`);
}

params.push(`pending=1`);
params.push(`pending=1`);
} else {
params.push(`balances-only=1`);
}

let queryString = '';
if (params.length > 0) {
queryString += '?' + params.join('&');
}
Expand Down
Loading

0 comments on commit 851f99d

Please sign in to comment.