|
|
|
@ -4,6 +4,8 @@ const debug = require('debug')('sos:db:order')
|
|
|
|
|
const mappings = require('../mappings')
|
|
|
|
|
const dbUtil = require('../util')
|
|
|
|
|
|
|
|
|
|
const config = require('./config')
|
|
|
|
|
|
|
|
|
|
const {DateTime} = require('luxon')
|
|
|
|
|
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_');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
order.findForTransaction = transaction_uuid =>
|
|
|
|
|
dbUtil.executeFunction({
|
|
|
|
|
name: 'find_order_for_transaction',
|
|
|
|
|
params: [
|
|
|
|
|
transaction_uuid
|
|
|
|
|
],
|
|
|
|
|
returnType: 'order',
|
|
|
|
|
single: true
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
order.addAddress = async function (transaction, address){
|
|
|
|
|
// Get parcel size
|
|
|
|
|
const parcel = {
|
|
|
|
@ -75,24 +87,59 @@ order.addAddress = async function (transaction, address){
|
|
|
|
|
if(!price)
|
|
|
|
|
throw new Error("Unable to estimate price");
|
|
|
|
|
|
|
|
|
|
// TODO: Update with real tax rate
|
|
|
|
|
const tax = epShipment.to_address.state === 'UT'
|
|
|
|
|
? 200
|
|
|
|
|
: null
|
|
|
|
|
// Add up tax for all the items
|
|
|
|
|
const tax = epShipment.to_address.state !== 'UT'
|
|
|
|
|
? 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
|
|
|
|
|
const query = {
|
|
|
|
|
text: 'select * from sos.add_address_to_order($1, $2, $3, $4)',
|
|
|
|
|
values: [transaction.uuid, address.uuid, price, tax]
|
|
|
|
|
text: 'select * from sos.add_address_to_order($1, $2, $3)',
|
|
|
|
|
values: [transaction.uuid, address.uuid, price]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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.addCoupon = function(transaction, coupon) {
|
|
|
|
|
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 = async function(transaction, coupon) {
|
|
|
|
|
// Check coupon validity
|
|
|
|
|
const couponExpireTime = DateTime.fromJSDate(coupon.valid_until)
|
|
|
|
|
if(couponExpireTime.diffNow().as('seconds') < 0)
|
|
|
|
@ -133,7 +180,7 @@ order.addCoupon = function(transaction, coupon) {
|
|
|
|
|
// Sum the item costs for total discount
|
|
|
|
|
.reduce((a,c) => (a+c))
|
|
|
|
|
|
|
|
|
|
return dbUtil.executeFunction({
|
|
|
|
|
await dbUtil.executeFunction({
|
|
|
|
|
name: 'add_coupon_to_transaction',
|
|
|
|
|
params: [
|
|
|
|
|
transaction.uuid,
|
|
|
|
@ -143,6 +190,9 @@ order.addCoupon = function(transaction, coupon) {
|
|
|
|
|
returnType: 'order',
|
|
|
|
|
single: true
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Update tax
|
|
|
|
|
return await order.updateTax(transaction.uuid)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
order.addPayment = async function(transaction, paymentIntent){
|
|
|
|
|