Allow from address to be configurable

main
Ashelyn Dawn 3 years ago
parent ba2d2a6cea
commit f932fb6e4c

@ -0,0 +1,20 @@
const router = module.exports = require('express-promise-router')()
const parseJSON = require('body-parser').json()
const db = require('../db')
const ensureAdmin = require('./middleware/ensureAdmin')
const validate = require('./middleware/validators')
router.get('/', ensureAdmin, async (req, res) => {
const config = await db.config.getLatestConfig()
res.json(config)
})
router.post('/address', ensureAdmin, parseJSON, validate.addressWithPhone, async (req, res) => {
const {name, street1, street2, city, state, zip, country, phone} = req.body;
// Create address, update order
const address = await db.address.create(name, street1, street2, city, state, zip, country, phone)
const config = await db.config.setShippingAddress(address.uuid, req.user.uuid)
res.json(config)
})

@ -26,6 +26,7 @@ router.use('/orders/', require('./orders'))
router.use('/shipments/', require('./shipments'))
router.use('/email/', require('./email'))
router.use('/coupons/', require('./coupons'))
router.use('/config/', require('./config'))
router.use((req, res, next)=>{
const err = new Error('Not found')

@ -46,6 +46,11 @@ validators.address = [
body('zip').isString().isLength({min: 1}).withMessage('Postal code is required')
]
validators.addressWithPhone = [
...validators.address,
body('phone').isString().isLength({min: 1}).withMessage('Phone is required')
]
validators.coupon = [
body('code').isString().isLength({min: 3, max: 50}).withMessage('Coupon code is required')
]

@ -74,7 +74,7 @@ router.post('/current/address', ensureCart, parseJSON, validate.address, validat
const {name, street1, street2, city, state, zip, country} = req.body;
// Create address, update order
const address = await db.address.create(name, street1, street2, city, state, zip, country)
const address = await db.address.create(name, street1, street2, city, state, zip, country, null)
const order = await db.order.addAddress(currentTransaction, address)
res.json(order)

@ -25,6 +25,7 @@ export default function AdminNav({children}){
<NavItem icon="supervisor_account" href="/admin/users">Users</NavItem>
<NavItem icon="local_shipping" href="/admin/shipments">Shipments</NavItem>
<NavItem icon="payment" href="/admin/coupons">Coupons</NavItem>
<NavItem icon="settings" href="/admin/config">Config</NavItem>
</div>
<main className={'adminPage ' + styles.notNav}>
{children}

@ -8,9 +8,9 @@ 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) => {
address.create = async (name, street1, street2, city, state, zip, country, phone) => {
const epAddress = new easypost.Address({
name, street1, street2, city, state, zip, country,
name, street1, street2, city, state, zip, country, phone,
verify: ['delivery']
})

@ -2,6 +2,7 @@ const pg = require('../pg')
const joinjs = require('join-js').default;
const debug = require('debug')('sos:db:config')
const mappings = require('../mappings')
const dbUtil = require('../util')
const config = module.exports = {}
@ -15,3 +16,11 @@ config.getTaxRate = async () => {
const current = await config.getLatestConfig()
return parseFloat(current.default_tax_percent)
}
config.setShippingAddress = async (address_uuid, user_uuid) =>
dbUtil.executeFunction({
name: 'set_shipping_source',
params: [address_uuid, user_uuid],
returnType: 'config',
single: true,
})

@ -1228,3 +1228,26 @@ begin
return query select * from sos.v_user where user_uuid = _user_uuid;
end; $function$;
create or replace function sos.set_shipping_source(_address_uuid uuid, _user_uuid uuid)
returns setof sos.v_config
language plpgsql
as $function$
declare
_tax_percent numeric(8,6);
begin
select config_default_tax_percent into _tax_percent
from sos.v_config;
insert into sos.config (
config_default_tax_percent,
config_shipping_from,
config_updated_by
) values (
_tax_percent,
_address_uuid,
_user_uuid
);
return query select * from sos.v_config;
end; $function$;

@ -0,0 +1,48 @@
import router from 'next/router'
import Link from 'next/link'
import ActionBar from '~/components/admin/actionBar'
import {DateTime} from 'luxon'
import Table from '~/components/table'
AdminConfig.getInitialProps = async ({ctx}) => {
const {data: config} = await ctx.axios.get('/api/config')
return {config}
}
export default function AdminConfig({config}) {
return (
<>
<ActionBar title="Config"/>
<Table
columns={[
{name: 'Parameter', extractor: config => config.name},
{name: 'Value', extractor: config => config.value},
{name: '', extractor: config =>
config.href && (
<Link href={config.href}><a>Edit</a></Link>
)
}
]}
rows={[
{id: 'tax', name: 'UT Tax Percent', value: parseFloat(config.default_tax_percent) + '%'},
{id: 'address', name: 'Shipping Source Address', value: (() => {
if(!config.shipping_from)
return 'Unset'
const {name, street1} = config.shipping_from
return `${name}, ${street1}`.slice(0, 20) + '...'
})(), href: '/admin/config/shipping'},
]}
/>
{config.modified_by && (
<p>
Config last modified on
{' ' + DateTime.fromISO(config.date_updated).setZone('local').toFormat('LLL dd, yyyy') + ' '}
by {config.modified_by.email}
</p>
)}
</>
)
}

@ -0,0 +1,32 @@
import router from 'next/router'
import ActionBar from '~/components/admin/actionBar'
import {FormController, Input, Button} from '~/components/form'
ShippingFrom.getInitialProps = async ({ctx: {axios}}) => {
const {data: {shipping_from: address}} = await axios.get(`/api/config`)
return {address}
}
export default function ShippingFrom({address}) {
const afterSave = () => {
router.push(`/admin/config`)
}
return (
<>
<ActionBar title="Update Shipping Source Address"/>
<FormController url="/api/config/address" afterSubmit={afterSave}>
<Input initialValue={address?.name} name="name" validate={value=>value.length > 0}/>
<Input initialValue={address?.street1} label="Street (line 1)" name="street1" validate={value=>value.length > 0}/>
<Input initialValue={address?.street2} label="Street (line 2)" name="street2"/>
<Input initialValue={address?.city} label="City" name="city" validate={value=>value.length > 0}/>
<Input initialValue={address?.state} label="State / Province" name="state" validate={value=>value.length > 0}/>
<Input initialValue={address?.zip} label="Postal Code" name="zip" validate={value=>value.length > 0}/>
<Input initialValue={address?.country} label="Country" name="country" validate={value=>value.length > 0}/>
<Input initialValue={address?.phone} label="Phone" name="phone" validate={value=>value.length > 0}/>
<Button type="submit">Save Address</Button>
</FormController>
</>
)
}
Loading…
Cancel
Save