diff --git a/api/orders.js b/api/orders.js index b579449..0c93cee 100644 --- a/api/orders.js +++ b/api/orders.js @@ -6,6 +6,7 @@ const easypost = new (require('@easypost/api'))(process.env.EASYPOST_API_KEY); const ensureAdmin = require('./middleware/ensureAdmin') const ensureCart = require('./middleware/ensureCart') const ensureUser = require('./middleware/ensureUser') +const email = require('../utils/email') const validate = require('./middleware/validators') @@ -191,6 +192,10 @@ router.post('/current/checkout/verify', ensureCart, parseJSON, async (req, res) router.post('/:uuid/ship/tracking', ensureAdmin, parseJSON, async (req, res) => { const order = await db.order.setTracking(req.params.uuid, req.body.code, req.body.date || null, req.body.price_cents) + + const user = await db.order.getUser(order.uuid) + await email.sendShippingNotification(user, order) + res.json(order) }) @@ -208,5 +213,8 @@ router.post('/:uuid/ship/easypost', ensureAdmin, parseJSON, async (req, res) => req.body.weight ) + const user = await db.order.getUser(order.uuid) + await email.sendShippingNotification(user, order) + res.json(order) }) diff --git a/db/models/order.js b/db/models/order.js index 5ea095d..183ef58 100644 --- a/db/models/order.js +++ b/db/models/order.js @@ -3,6 +3,7 @@ const joinjs = require('join-js').default; const debug = require('debug')('sos:db:order') const mappings = require('../mappings') const dbUtil = require('../util') +const user = require('./user') const config = require('./config') @@ -53,6 +54,19 @@ order.setUser = (order_uuid, user_uuid) => dbUtil.executeFunction({ single: true }) +order.getUser = async function(order_uuid){ + const query = { + text: 'select order_user_uuid from sos.order where order_uuid = $1', + values: [order_uuid] + } + + debug(query) + const {rows} = await pg.query(query) + + const user_uuid = rows[0]?.order_user_uuid + return user.findById(user_uuid) +} + order.findAllForSession = async function(session_uuid) { const query = { text: 'select * from sos.find_orders_for_session($1)', @@ -311,4 +325,4 @@ order.shipEasyPost = async ( uuid, length, width, height, weight ) => { returnType: 'order', single: true }) -} +} \ No newline at end of file diff --git a/utils/email.js b/utils/email.js index 6f514c9..8f3d143 100644 --- a/utils/email.js +++ b/utils/email.js @@ -1,5 +1,6 @@ const db = require('../db') const sendgrid = require('@sendgrid/mail') +const {DateTime} = require('luxon') sendgrid.setApiKey(process.env.SENDGRID_KEY) const email = module.exports = {} @@ -58,4 +59,25 @@ email.sendNoSuchAccount = async 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') } \ No newline at end of file