Skip to content

Commit

Permalink
refactor: simplify maxPrice logic
Browse files Browse the repository at this point in the history
  • Loading branch information
jef committed Apr 9, 2021
1 parent e006c48 commit a25b7f1
Showing 1 changed file with 8 additions and 25 deletions.
33 changes: 8 additions & 25 deletions src/store/lookup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,26 +407,6 @@ async function checkIsCloudflare(store: Store, page: Page, link: Link) {
return false;
}

async function isAboveMaxPrice(
store: Store,
page: Page,
link: Link,
options: Selector
): Promise<boolean> {
if (store.labels.maxPrice) {
const maxPrice = config.store.maxPrice.series[link.series];

link.price = await getPrice(page, store.labels.maxPrice, options);

if (link.price && link.price > maxPrice && maxPrice > 0) {
logger.info(Print.maxPrice(link, store, maxPrice, true));
return true;
}
}

return false;
}

async function isItemInStock(store: Store, page: Page, link: Link) {
const baseOptions: Selector = {
requireVisible: false,
Expand Down Expand Up @@ -469,10 +449,6 @@ async function isItemInStock(store: Store, page: Page, link: Link) {
logger.info(Print.outOfStock(link, store, true));
return false;
}

if (await isAboveMaxPrice(store, page, link, options)) {
return false;
}
}

if (store.labels.inStock) {
Expand All @@ -486,8 +462,15 @@ async function isItemInStock(store: Store, page: Page, link: Link) {
logger.info(Print.outOfStock(link, store, true));
return false;
}
}

if (await isAboveMaxPrice(store, page, link, options)) {
if (store.labels.maxPrice) {
const maxPrice = config.store.maxPrice.series[link.series];

link.price = await getPrice(page, store.labels.maxPrice, baseOptions);

if (link.price && link.price > maxPrice && maxPrice > 0) {
logger.info(Print.maxPrice(link, store, maxPrice, true));
return false;
}
}
Expand Down

0 comments on commit a25b7f1

Please sign in to comment.