Send shipping notifications

main
Ashelyn Dawn 4 years ago
parent fa160b26eb
commit c5ae39fd62

@ -6,6 +6,7 @@ const easypost = new (require('@easypost/api'))(process.env.EASYPOST_API_KEY);
const ensureAdmin = require('./middleware/ensureAdmin') const ensureAdmin = require('./middleware/ensureAdmin')
const ensureCart = require('./middleware/ensureCart') const ensureCart = require('./middleware/ensureCart')
const ensureUser = require('./middleware/ensureUser') const ensureUser = require('./middleware/ensureUser')
const email = require('../utils/email')
const validate = require('./middleware/validators') 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) => { 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 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) res.json(order)
}) })
@ -208,5 +213,8 @@ router.post('/:uuid/ship/easypost', ensureAdmin, parseJSON, async (req, res) =>
req.body.weight req.body.weight
) )
const user = await db.order.getUser(order.uuid)
await email.sendShippingNotification(user, order)
res.json(order) res.json(order)
}) })

@ -3,6 +3,7 @@ const joinjs = require('join-js').default;
const debug = require('debug')('sos:db:order') const debug = require('debug')('sos:db:order')
const mappings = require('../mappings') const mappings = require('../mappings')
const dbUtil = require('../util') const dbUtil = require('../util')
const user = require('./user')
const config = require('./config') const config = require('./config')
@ -53,6 +54,19 @@ order.setUser = (order_uuid, user_uuid) => dbUtil.executeFunction({
single: true 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) { order.findAllForSession = async function(session_uuid) {
const query = { const query = {
text: 'select * from sos.find_orders_for_session($1)', text: 'select * from sos.find_orders_for_session($1)',
@ -311,4 +325,4 @@ order.shipEasyPost = async ( uuid, length, width, height, weight ) => {
returnType: 'order', returnType: 'order',
single: true single: true
}) })
} }

@ -1,5 +1,6 @@
const db = require('../db') const db = require('../db')
const sendgrid = require('@sendgrid/mail') const sendgrid = require('@sendgrid/mail')
const {DateTime} = require('luxon')
sendgrid.setApiKey(process.env.SENDGRID_KEY) sendgrid.setApiKey(process.env.SENDGRID_KEY)
const email = module.exports = {} const email = module.exports = {}
@ -58,4 +59,25 @@ email.sendNoSuchAccount = async email => {
} }
await sendgrid.send(msg) 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')
} }
Loading…
Cancel
Save