You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
1.5 KiB
JavaScript
55 lines
1.5 KiB
JavaScript
const router = module.exports = require('express-promise-router')()
|
|
const easypost = new (require('@easypost/api'))(process.env.EASYPOST_API_KEY);
|
|
const parseJSON = require('body-parser').json()
|
|
const db = require('../db')
|
|
|
|
const validate = require('./middleware/validators')
|
|
|
|
router.use(require('./middleware/ensureCart'))
|
|
|
|
router.put('/', async (req, res) => {
|
|
const order = await db.order.create(req.cart.uuid);
|
|
res.json(order)
|
|
})
|
|
|
|
router.get('/current', async (req, res) => {
|
|
const order = await db.order.findForCart(req.cart.uuid);
|
|
res.json(order)
|
|
})
|
|
|
|
router.post('/current/address', parseJSON, validate.address, validate.handleApiError, async (req, res) => {
|
|
const order = await db.order.findForCart(req.cart.uuid);
|
|
if(!order) throw new Error("Unable to find current order");
|
|
|
|
const {name, street1, street2, city, state, zip} = req.body;
|
|
|
|
const epAddress = new easypost.Address({
|
|
name, street1, street2, city, state, zip,
|
|
verify: ['delivery']
|
|
})
|
|
|
|
await epAddress.save()
|
|
|
|
const {success} = epAddress.verifications.delivery
|
|
if(!success){
|
|
// TODO: Send back an error and ask them to double-check?
|
|
}
|
|
|
|
const address = await db.address.create(
|
|
epAddress.name,
|
|
epAddress.company,
|
|
epAddress.street1,
|
|
epAddress.street2,
|
|
epAddress.city,
|
|
epAddress.state,
|
|
epAddress.zip,
|
|
epAddress.country,
|
|
epAddress.phone,
|
|
epAddress.id
|
|
)
|
|
|
|
const updatedOrder = await db.order.addAddress(order.uuid, address.uuid)
|
|
|
|
res.json(updatedOrder)
|
|
})
|