Skip to content

Commit

Permalink
Merge pull request #43 from eshaham/combine_installments
Browse files Browse the repository at this point in the history
add option to combine installments
  • Loading branch information
eshaham authored Dec 26, 2017
2 parents 5a0c191 + 238b1c9 commit 9775b5c
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 22 deletions.
51 changes: 51 additions & 0 deletions src/helpers/transactions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import moment from 'moment';
import { NORMAL_TXN_TYPE, INSTALLMENTS_TXN_TYPE } from '../constants';

function isNormalTransaction(txn) {
return txn.type === NORMAL_TXN_TYPE;
}

function isInstallmentTransaction(txn) {
return txn.type === INSTALLMENTS_TXN_TYPE;
}

function isNonInitialInstallmentTransaction(txn) {
return isInstallmentTransaction(txn) && txn.installments && txn.installments.number > 1;
}

function isInitialInstallmentTransaction(txn) {
return isInstallmentTransaction(txn) && txn.installments && txn.installments.number === 1;
}

export function fixInstallments(txns) {
return txns.map((txn) => {
const clonedTxn = Object.assign({}, txn);
if (isNonInitialInstallmentTransaction(clonedTxn)) {
const dateMoment = moment(clonedTxn.date);
const actualDateMoment = dateMoment.add(clonedTxn.installments.number - 1, 'month');
clonedTxn.date = actualDateMoment.toDate();
}
return clonedTxn;
});
}

export function sortTransactionsByDate(txns) {
const cloned = Array.from(txns);
cloned.sort((txn1, txn2) => {
if (txn1.date.getTime() === txn2.date.getTime()) {
return 0;
}
return txn1.date < txn2.date ? -1 : 1;
});

return cloned;
}

export function filterOldTransactions(txns, startMoment, combineInstallments) {
return txns.filter((txn) => {
const combineNeededAndInitialOrNormal =
combineInstallments && (isNormalTransaction(txn) || isInitialInstallmentTransaction(txn));
return (!combineInstallments && startMoment.isSameOrBefore(txn.processedDate)) ||
(combineNeededAndInitialOrNormal && startMoment.isSameOrBefore(txn.date));
});
}
28 changes: 17 additions & 11 deletions src/scrapers/isracard.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { BaseScraper, LOGIN_RESULT } from './base-scraper';
import { fetchGet, fetchPost } from '../helpers/fetch';
import { SCRAPE_PROGRESS_TYPES, NORMAL_TXN_TYPE, INSTALLMENTS_TXN_TYPE } from '../constants';
import getAllMonthMoments from '../helpers/dates';
import { fixInstallments, filterOldTransactions } from '../helpers/transactions';

const BASE_URL = 'https://digital.isracard.co.il';
const SERVICES_URL = `${BASE_URL}/services/ProxyRequestHandler.ashx`;
Expand Down Expand Up @@ -69,8 +70,8 @@ function getInstallmentsInfo(txn) {
}

return {
number: matches[0],
total: matches[1],
number: parseInt(matches[0], 10),
total: parseInt(matches[1], 10),
};
}

Expand All @@ -93,7 +94,7 @@ function convertTransactions(txns, processedDate) {
});
}

async function fetchTransactions(page, startMoment, monthMoment) {
async function fetchTransactions(page, options, startMoment, monthMoment) {
const accounts = await fetchAccounts(page, monthMoment);
const dataUrl = getTransactionsUrl(monthMoment);
const dataResult = await fetchGet(page, dataUrl);
Expand All @@ -102,22 +103,27 @@ async function fetchTransactions(page, startMoment, monthMoment) {
accounts.forEach((account) => {
const txnGroups = _.get(dataResult, `CardsTransactionsListBean.Index${account.index}.CurrentCardTransactions`);
if (txnGroups) {
let allTxs = [];
let allTxns = [];
txnGroups.forEach((txnGroup) => {
if (txnGroup.txnIsrael) {
const txns = convertTransactions(txnGroup.txnIsrael, account.processedDate);
allTxs.push(...txns);
allTxns.push(...txns);
}
if (txnGroup.txnAbroad) {
const txns = convertTransactions(txnGroup.txnAbroad, account.processedDate);
allTxs.push(...txns);
allTxns.push(...txns);
}
});
allTxs = allTxs.filter(txn => startMoment.isSameOrBefore(txn.date));

if (!options.combineInstallments) {
allTxns = fixInstallments(allTxns);
}
allTxns = filterOldTransactions(allTxns, startMoment, options.combineInstallments);

accountTxns[account.accountNumber] = {
accountNumber: account.accountNumber,
index: account.index,
txns: allTxs,
txns: allTxns,
};
}
});
Expand All @@ -127,10 +133,10 @@ async function fetchTransactions(page, startMoment, monthMoment) {
return null;
}

async function fetchAllTransactions(page, startMoment) {
async function fetchAllTransactions(page, options, startMoment) {
const allMonths = getAllMonthMoments(startMoment, true);
const results = await Promise.all(allMonths.map(async (monthMoment) => {
return fetchTransactions(page, startMoment, monthMoment);
return fetchTransactions(page, options, startMoment, monthMoment);
}));

const combinedTxns = {};
Expand Down Expand Up @@ -235,7 +241,7 @@ class IsracardScraper extends BaseScraper {
const startDate = this.options.startDate || defaultStartMoment.toDate();
const startMoment = moment.max(defaultStartMoment, moment(startDate));

const txnsResult = await fetchAllTransactions(this.page, startMoment);
const txnsResult = await fetchAllTransactions(this.page, this.options, startMoment);
if (!txnsResult) {
throw new Error('unknown error while fetching data');
}
Expand Down
21 changes: 10 additions & 11 deletions src/scrapers/leumi-card.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { waitForRedirect } from '../helpers/navigation';
import { waitUntilElementFound } from '../helpers/elements-interactions';
import { NORMAL_TXN_TYPE, INSTALLMENTS_TXN_TYPE, SHEKEL_CURRENCY } from '../constants';
import getAllMonthMoments from '../helpers/dates';
import { fixInstallments, sortTransactionsByDate, filterOldTransactions } from '../helpers/transactions';

const BASE_URL = 'https://online.leumi-card.co.il';
const DATE_FORMAT = 'DD/MM/YYYY';
Expand Down Expand Up @@ -77,8 +78,8 @@ function getInstallmentsInfo(comments) {
}

return {
number: matches[0],
total: matches[1],
number: parseInt(matches[0], 10),
total: parseInt(matches[1], 10),
};
}

Expand Down Expand Up @@ -192,15 +193,13 @@ async function fetchTransactions(page, options, accountNumber) {
const result = await fetchTransactionsForMonth(page);
allResults = addResult(allResults, result);

const allTxns = allResults[accountNumber];
allTxns.sort((txn1, txn2) => {
if (txn1.date.getTime() === txn2.date.getTime()) {
return 0;
}
return txn1.date < txn2.date ? -1 : 1;
});

return allTxns.filter(txn => startMoment.isSameOrBefore(txn.date));
let allTxns = allResults[accountNumber];
if (!options.combineInstallments) {
allTxns = fixInstallments(allTxns);
}
allTxns = sortTransactionsByDate(allTxns);
allTxns = filterOldTransactions(allTxns, startMoment, options.combineInstallments);
return allTxns;
}

async function getAccountData(page, options) {
Expand Down

0 comments on commit 9775b5c

Please sign in to comment.