Skip to content

Commit

Permalink
refactor: use logstream notification for in_stock logging too
Browse files Browse the repository at this point in the history
  • Loading branch information
Guillaume Malette committed Feb 26, 2021
1 parent a73a18a commit dde6c7e
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/notification/link_poll_event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type LinkPollError = {result: 'failure'} & (
| OutOfStock
| BannedSeller
);
type LinkPollSuccess = {result: 'in_stock'};
type LinkPollSuccess = {result: 'in_stock'; url: string};

export type LinkPollEvent = (LinkPollError | LinkPollSuccess) & {
link: Link;
Expand Down
4 changes: 3 additions & 1 deletion src/notification/logstream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ function assertNever(x: never): never {

export function sendLogstream(pollEvent: LinkPollEvent) {
const {link, store} = pollEvent;
if (pollEvent.result === 'failure') {
if (pollEvent.result === 'in_stock') {
logger.info(`${Print.inStock(link, store, true)}\n${pollEvent.url}`);
} else {
switch (pollEvent.failureReason) {
case 'captcha':
logger.warn(Print.captcha(link, store, true));
Expand Down
4 changes: 2 additions & 2 deletions src/store/lookup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {filterStoreLink} from './filter';
import open from 'open';
import {processBackoffDelay} from './model/helpers/backoff';
import useProxy from '@doridian/puppeteer-page-proxy';
import {LinkPollEvent} from "../notification/link_poll_event";
import {LinkPollEvent} from '../notification/link_poll_event';

const inStock: Record<string, boolean> = {};

Expand Down Expand Up @@ -331,7 +331,6 @@ async function lookupCard(
if (await lookupCardInStock(store, page, link, sendNotification)) {
const givenUrl =
link.cartUrl && config.store.autoAddToCart ? link.cartUrl : link.url;
logger.info(`${Print.inStock(link, store, true)}\n${givenUrl}`);

if (config.browser.open) {
await (link.openCartAction === undefined
Expand All @@ -341,6 +340,7 @@ async function lookupCard(

sendNotification({
result: 'in_stock',
url: givenUrl,
link,
store,
});
Expand Down
2 changes: 1 addition & 1 deletion test/functional/test-notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const store: Store = {
/**
* Send test email.
*/
sendNotification({result: 'in_stock', link, store});
sendNotification({result: 'in_stock', url: 'test:url', link, store});

/**
* Open browser.
Expand Down

0 comments on commit dde6c7e

Please sign in to comment.