Skip to content

Commit

Permalink
Merge pull request #205 from eshaham/fix-leumi-card-possible-results
Browse files Browse the repository at this point in the history
Fix leumi card possible results
  • Loading branch information
eshaham authored Jun 10, 2019
2 parents d52d607 + d638886 commit f457d19
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 17 deletions.
35 changes: 22 additions & 13 deletions src/scrapers/base-scraper-with-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,29 @@ const VIEWPORT_WIDTH = 1024;
const VIEWPORT_HEIGHT = 768;
const OK_STATUS = 200;

function getKeyByValue(object, value) {
return Object.keys(object).find((key) => {
const compareTo = object[key];
let result = false;

result = compareTo.find((item) => {
if (item instanceof RegExp) {
return item.test(value);
async function getKeyByValue(object, value) {
const keys = Object.keys(object);
for (const key of keys) {
const conditions = object[key];

for (const condition of conditions) {
let result = false;

if (condition instanceof RegExp) {
result = condition.test(value);
} else if (typeof condition === 'function') {
result = await condition();
} else {
result = value.toLowerCase() === condition.toLowerCase();
}

return value.toLowerCase() === item.toLowerCase();
});
if (result) {
return Promise.resolve(key);
}
}
}

return !!result;
});
return Promise.resolve(LOGIN_RESULT.UNKNOWN_ERROR);
}

function handleLoginResult(scraper, loginResult) {
Expand All @@ -32,6 +40,7 @@ function handleLoginResult(scraper, loginResult) {
scraper.emitProgress(SCRAPE_PROGRESS_TYPES.LOGIN_SUCCESS);
return { success: true };
case LOGIN_RESULT.INVALID_PASSWORD:
case LOGIN_RESULT.UNKNOWN_ERROR:
scraper.emitProgress(SCRAPE_PROGRESS_TYPES.LOGIN_FAILED);
return {
success: false,
Expand Down Expand Up @@ -132,7 +141,7 @@ class BaseScraperWithBrowser extends BaseScraper {
}

const current = await getCurrentUrl(this.page, true);
const loginResult = getKeyByValue(loginOptions.possibleResults, current);
const loginResult = await getKeyByValue(loginOptions.possibleResults, current);
return handleLoginResult(this, loginResult);
}

Expand Down
17 changes: 13 additions & 4 deletions src/scrapers/leumi-card.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,14 @@ const TWO_MONTHS_POSTPONED_TYPE_NAME = 'דחוי חודשיים';
const MONTHLY_CHARGE_PLUS_INTEREST_TYPE_NAME = 'חודשי + ריבית';
const CREDIT_TYPE_NAME = 'קרדיט';

const INVALID_DETAILS_SELECTOR = '#popupWrongDetails';
const LOGIN_ERROR_SELECTOR = '#popupCardHoldersLoginError';

function redirectOrDialog(page) {
return Promise.race([
waitForRedirect(page, 20000, false, [BASE_WELCOME_URL, `${BASE_WELCOME_URL}/`]),
waitUntilElementFound(page, '#popupWrongDetails', true),
waitUntilElementFound(page, INVALID_DETAILS_SELECTOR, true),
waitUntilElementFound(page, LOGIN_ERROR_SELECTOR, true),
]);
}

Expand Down Expand Up @@ -311,11 +315,16 @@ async function fetchTransactions(browser, options, navigateToFunc) {
return allResults;
}

function getPossibleLoginResults() {
function getPossibleLoginResults(page) {
const urls = {};
urls[LOGIN_RESULT.SUCCESS] = [`${BASE_WELCOME_URL}/homepage/personal`];
urls[LOGIN_RESULT.CHANGE_PASSWORD] = [`${BASE_ACTIONS_URL}/Anonymous/Login/PasswordExpired.aspx`];
urls[LOGIN_RESULT.INVALID_PASSWORD] = [`${BASE_ACTIONS_URL}/Anonymous/Login/CardHoldersLogin.aspx`];
urls[LOGIN_RESULT.INVALID_PASSWORD] = [async () => {
return elementPresentOnPage(page, INVALID_DETAILS_SELECTOR);
}];
urls[LOGIN_RESULT.UNKNOWN_ERROR] = [async () => {
return elementPresentOnPage(page, LOGIN_ERROR_SELECTOR);
}];
return urls;
}

Expand All @@ -339,7 +348,7 @@ class LeumiCardScraper extends BaseScraperWithBrowser {
}
},
postAction: async () => redirectOrDialog(this.page),
possibleResults: getPossibleLoginResults(),
possibleResults: getPossibleLoginResults(this.page),
};
}

Expand Down

0 comments on commit f457d19

Please sign in to comment.