const db = require('../db') const sendgrid = require('@sendgrid/mail') const {DateTime} = require('luxon') sendgrid.setApiKey(process.env.SENDGRID_KEY) const email = module.exports = {} email.sendAccountConfirmation = async user => { const confirmUrl = await db.user.createLoginLink(user.uuid) const msg = { to: user.email, from: {email: 'registration@email.societyofsocks.us', name: 'Society of Socks'}, templateId: 'd-33407f1dd1b14b7b84dd779511039c95', dynamic_template_data: { confirmUrl: confirmUrl } }; await sendgrid.send(msg); } email.sendPasswordReset = async user => { const resetURL = await db.user.createPasswordReset(user.uuid) const msg = { to: user.email, from: {email: 'accounts@email.societyofsocks.us', name: 'Society of Socks'}, templateId: 'd-90d751cfc8cd4047be39c994ff9d0f5c', dynamic_template_data: { resetPasswordURL: resetURL } } await sendgrid.send(msg) } email.sendPasswordChanged = async user => { const msg = { to: user.email, from: {email: 'accounts@email.societyofsocks.us', name: 'Society of Socks'}, templateId: 'd-a619058859e44a8a9cf99d77bed2cd35', dynamic_template_data: { emailAddress: user.email } } await sendgrid.send(msg) } email.sendNoSuchAccount = async email => { const msg = { to: email, from: {email: 'accounts@email.societyofsocks.us', name: 'Society of Socks'}, templateId: 'd-34bb8b2a94264092a47d1f03999be836', dynamic_template_data: { emailAddress: email } } await sendgrid.send(msg) } email.sendShippingNotification = async (user, order) => { const msg = { to: user.email, from: {email: 'notifications@email.societyofsocks.us', name: 'Society of Socks'}, templateId: 'd-dde1867d5a80426bb08b9e9a078643ff', dynamic_template_data: { orderNum: order.number, shipmentMethod: 'USPS', shipmentDate: formatTime(order.delivery.date_shipped), trackingNum: order.delivery.tracking_number, trackingURL: `https://tools.usps.com/go/TrackConfirmAction?tLabels=${order.delivery.tracking_number}` } } await sendgrid.send(msg) } function formatTime(time){ return DateTime.fromJSDate(time).setZone('local').toFormat('LLLL dd') }