Skip to content

Commit

Permalink
feat: include screenshot for emails + sms notifications (#144)
Browse files Browse the repository at this point in the history
  • Loading branch information
whoisjordangarcia committed Sep 21, 2020
1 parent 0f6e570 commit 7191e03
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 5 deletions.
12 changes: 11 additions & 1 deletion src/notification/email.ts
Expand Up @@ -2,6 +2,7 @@ import nodemailer from 'nodemailer';
import Mail from 'nodemailer/lib/mailer';
import {Config} from '../config';
import {Logger} from '../logger';
import {Link} from '../store/model';

const email = Config.notifications.email;
const subject = 'NVIDIA - BUY NOW';
Expand All @@ -20,9 +21,18 @@ const mailOptions: Mail.Options = {
subject
};

export function sendEmail(cartUrl: string) {
export function sendEmail(cartUrl: string, link: Link) {
mailOptions.text = cartUrl;

if (link.screenshot) {
mailOptions.attachments = [
{
filename: link.screenshot,
path: `./${link.screenshot}`
}
];
}

transporter.sendMail(mailOptions, (error, info) => {
if (error) {
Logger.error(error);
Expand Down
4 changes: 2 additions & 2 deletions src/notification/notification.ts
Expand Up @@ -12,7 +12,7 @@ const notifications = Config.notifications;

export function sendNotification(cartUrl: string, link: Link) {
if (notifications.email.username && notifications.email.password) {
sendEmail(cartUrl);
sendEmail(cartUrl, link);
}

if (notifications.slack.channel && notifications.slack.token) {
Expand All @@ -30,7 +30,7 @@ export function sendNotification(cartUrl: string, link: Link) {
if (notifications.phone.number) {
const carrier = notifications.phone.carrier.toLowerCase();
if (carrier && notifications.phone.availableCarriers.has(carrier)) {
sendSMS(cartUrl);
sendSMS(cartUrl, link);
}
}

Expand Down
12 changes: 11 additions & 1 deletion src/notification/sms.ts
Expand Up @@ -2,6 +2,7 @@ import nodemailer from 'nodemailer';
import Mail from 'nodemailer/lib/mailer';
import {Config} from '../config';
import {Logger} from '../logger';
import {Link} from '../store/model';

const subject = 'NVIDIA - BUY NOW';
const [email, phone] = [Config.notifications.email, Config.notifications.phone];
Expand All @@ -20,9 +21,18 @@ const mailOptions: Mail.Options = {
subject
};

export function sendSMS(text: string) {
export function sendSMS(text: string, link: Link) {
mailOptions.text = text;

if (link.screenshot) {
mailOptions.attachments = [
{
filename: link.screenshot,
path: `./${link.screenshot}`
}
];
}

transporter.sendMail(mailOptions, (error, info) => {
if (error) {
Logger.error(error);
Expand Down
3 changes: 2 additions & 1 deletion src/store/lookup.ts
Expand Up @@ -84,7 +84,8 @@ async function lookup(browser: Browser, store: Store) {

if (Config.page.capture) {
Logger.debug('ℹ saving screenshot');
await page.screenshot({path: `success-${Date.now()}.png`});
link.screenshot = `success-${Date.now()}.png`;
await page.screenshot({path: link.screenshot});
}

const givenUrl = link.cartUrl ? link.cartUrl : link.url;
Expand Down
1 change: 1 addition & 0 deletions src/store/model/store.ts
Expand Up @@ -4,6 +4,7 @@ export interface Link {
model: string;
series: string;
url: string;
screenshot?: string;
}

export interface Labels {
Expand Down

0 comments on commit 7191e03

Please sign in to comment.