Skip to content

Commit

Permalink
feat: change max price behavior
Browse files Browse the repository at this point in the history
Supersedes #2057

This changes the way lookups are returned by checking if
the item is in stock first, then checking if the item
is within the max price range (if specified).
  • Loading branch information
jef committed Apr 9, 2021
1 parent b412146 commit 9b02c36
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
4 changes: 3 additions & 1 deletion src/logger.ts
Expand Up @@ -143,7 +143,9 @@ export const Print = {
'✖ ' +
buildProductString(link, store, true) +
' :: ' +
chalk.yellow(`PRICE ${link.price ?? ''} EXCEEDS LIMIT ${maxPrice}`)
chalk.yellow(
`IN STOCK, PRICE ${link.price ?? ''} EXCEEDS LIMIT ${maxPrice}`
)
);
}

Expand Down
39 changes: 28 additions & 11 deletions src/store/lookup.ts
Expand Up @@ -407,6 +407,26 @@ 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 lookupCardInStock(store: Store, page: Page, link: Link) {
const baseOptions: Selector = {
requireVisible: false,
Expand Down Expand Up @@ -438,17 +458,6 @@ async function lookupCardInStock(store: Store, page: Page, link: Link) {
}
}

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;
}
}

if (link.labels?.inStock) {
const options = {
...baseOptions,
Expand All @@ -460,6 +469,10 @@ async function lookupCardInStock(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 @@ -473,6 +486,10 @@ async function lookupCardInStock(store: Store, page: Page, link: Link) {
logger.info(Print.outOfStock(link, store, true));
return false;
}

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

return true;
Expand Down

0 comments on commit 9b02c36

Please sign in to comment.