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.

44 lines
1.1 KiB
JavaScript

const pg = require('../pg')
const joinjs = require('join-js').default;
const debug = require('debug')('sos:db:address')
const mappings = require('../mappings')
const easypost = new (require('@easypost/api'))(process.env.EASYPOST_API_KEY);
const address = module.exports = {}
address.create = async (name, street1, street2, city, state, zip, country) => {
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?
}
const query = {
text: 'select * from sos.create_address($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)',
values: [
epAddress.name,
epAddress.company,
epAddress.street1,
epAddress.street2,
epAddress.city,
epAddress.state,
epAddress.zip,
epAddress.country,
epAddress.phone,
epAddress.id
]
}
debug(query)
const {rows} = await pg.query(query)
return joinjs.map(rows, mappings, 'addressMap', 'address_')[0]
}