|
|
@ -4,6 +4,8 @@ const debug = require('debug')('sos:db:order')
|
|
|
|
const mappings = require('../mappings')
|
|
|
|
const mappings = require('../mappings')
|
|
|
|
const dbUtil = require('../util')
|
|
|
|
const dbUtil = require('../util')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const config = require('./config')
|
|
|
|
|
|
|
|
|
|
|
|
const {DateTime} = require('luxon')
|
|
|
|
const {DateTime} = require('luxon')
|
|
|
|
const easypost = new (require('@easypost/api'))(process.env.EASYPOST_API_KEY);
|
|
|
|
const easypost = new (require('@easypost/api'))(process.env.EASYPOST_API_KEY);
|
|
|
|
|
|
|
|
|
|
|
@ -45,6 +47,16 @@ order.findAllForSession = async function(session_uuid) {
|
|
|
|
return joinjs.map(rows, mappings, 'orderMap', 'order_');
|
|
|
|
return joinjs.map(rows, mappings, 'orderMap', 'order_');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
order.findForTransaction = transaction_uuid =>
|
|
|
|
|
|
|
|
dbUtil.executeFunction({
|
|
|
|
|
|
|
|
name: 'find_order_for_transaction',
|
|
|
|
|
|
|
|
params: [
|
|
|
|
|
|
|
|
transaction_uuid
|
|
|
|
|
|
|
|
],
|
|
|
|
|
|
|
|
returnType: 'order',
|
|
|
|
|
|
|
|
single: true
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
order.addAddress = async function (transaction, address){
|
|
|
|
order.addAddress = async function (transaction, address){
|
|
|
|
// Get parcel size
|
|
|
|
// Get parcel size
|
|
|
|
const parcel = {
|
|
|
|
const parcel = {
|
|
|
@ -75,24 +87,59 @@ order.addAddress = async function (transaction, address){
|
|
|
|
if(!price)
|
|
|
|
if(!price)
|
|
|
|
throw new Error("Unable to estimate price");
|
|
|
|
throw new Error("Unable to estimate price");
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: Update with real tax rate
|
|
|
|
// Add up tax for all the items
|
|
|
|
const tax = epShipment.to_address.state === 'UT'
|
|
|
|
const tax = epShipment.to_address.state !== 'UT'
|
|
|
|
? 200
|
|
|
|
? null
|
|
|
|
: null
|
|
|
|
: transaction.cart.items
|
|
|
|
|
|
|
|
// For each item type, create an array of length n (number of items of that type in cart)
|
|
|
|
|
|
|
|
// where each index is initialized to the item tax amount
|
|
|
|
|
|
|
|
.map(({item, count}) => new Array(count).fill(item.tax_rate * item.price_cents / 100))
|
|
|
|
|
|
|
|
// Flatten to just be all tax amounts
|
|
|
|
|
|
|
|
.flat()
|
|
|
|
|
|
|
|
// Sum all the numbers
|
|
|
|
|
|
|
|
.reduce((a,c) => (a+c))
|
|
|
|
|
|
|
|
|
|
|
|
// Update database
|
|
|
|
// Update database
|
|
|
|
const query = {
|
|
|
|
const query = {
|
|
|
|
text: 'select * from sos.add_address_to_order($1, $2, $3, $4)',
|
|
|
|
text: 'select * from sos.add_address_to_order($1, $2, $3)',
|
|
|
|
values: [transaction.uuid, address.uuid, price, tax]
|
|
|
|
values: [transaction.uuid, address.uuid, price]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
debug(query)
|
|
|
|
debug(query)
|
|
|
|
const {rows} = await pg.query(query)
|
|
|
|
await pg.query(query)
|
|
|
|
|
|
|
|
|
|
|
|
return joinjs.map(rows, mappings, 'orderMap', 'order_')[0];
|
|
|
|
// Update tax
|
|
|
|
|
|
|
|
return await order.updateTax(transaction.uuid)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
order.updateTax = async function(transaction_uuid){
|
|
|
|
|
|
|
|
const _order = await order.findForTransaction(transaction_uuid)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(!_order.address)
|
|
|
|
|
|
|
|
throw new Error("Order has no address");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(_order.address.state !== 'UT'){
|
|
|
|
|
|
|
|
debug("Skipping tax for state: " + _order.address.state);
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const {item_total_price, coupon_effective_discount} = _order.transactions.find(t => t.uuid === transaction_uuid)
|
|
|
|
|
|
|
|
const itemPriceWithDiscount = item_total_price - (coupon_effective_discount || 0)
|
|
|
|
|
|
|
|
const taxRate = await config.getTaxRate()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const computedTax = Math.round(itemPriceWithDiscount * taxRate / 100)
|
|
|
|
|
|
|
|
return await dbUtil.executeFunction({
|
|
|
|
|
|
|
|
name: 'add_tax_to_transaction',
|
|
|
|
|
|
|
|
params: [
|
|
|
|
|
|
|
|
transaction_uuid,
|
|
|
|
|
|
|
|
computedTax
|
|
|
|
|
|
|
|
],
|
|
|
|
|
|
|
|
returnType: 'order',
|
|
|
|
|
|
|
|
single: true
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
order.addCoupon = function(transaction, coupon) {
|
|
|
|
order.addCoupon = async function(transaction, coupon) {
|
|
|
|
// Check coupon validity
|
|
|
|
// Check coupon validity
|
|
|
|
const couponExpireTime = DateTime.fromJSDate(coupon.valid_until)
|
|
|
|
const couponExpireTime = DateTime.fromJSDate(coupon.valid_until)
|
|
|
|
if(couponExpireTime.diffNow().as('seconds') < 0)
|
|
|
|
if(couponExpireTime.diffNow().as('seconds') < 0)
|
|
|
@ -133,7 +180,7 @@ order.addCoupon = function(transaction, coupon) {
|
|
|
|
// Sum the item costs for total discount
|
|
|
|
// Sum the item costs for total discount
|
|
|
|
.reduce((a,c) => (a+c))
|
|
|
|
.reduce((a,c) => (a+c))
|
|
|
|
|
|
|
|
|
|
|
|
return dbUtil.executeFunction({
|
|
|
|
await dbUtil.executeFunction({
|
|
|
|
name: 'add_coupon_to_transaction',
|
|
|
|
name: 'add_coupon_to_transaction',
|
|
|
|
params: [
|
|
|
|
params: [
|
|
|
|
transaction.uuid,
|
|
|
|
transaction.uuid,
|
|
|
@ -143,6 +190,9 @@ order.addCoupon = function(transaction, coupon) {
|
|
|
|
returnType: 'order',
|
|
|
|
returnType: 'order',
|
|
|
|
single: true
|
|
|
|
single: true
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Update tax
|
|
|
|
|
|
|
|
return await order.updateTax(transaction.uuid)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
order.addPayment = async function(transaction, paymentIntent){
|
|
|
|
order.addPayment = async function(transaction, paymentIntent){
|
|
|
|