From dab9c228adfec9c64c6c8bb232f7ae00622fac0e Mon Sep 17 00:00:00 2001 From: Ashelyn Dawn Date: Sat, 6 Feb 2021 15:05:14 -0700 Subject: [PATCH] Configurable shipping from address --- db/mappings/config.js | 1 + db/models/address.js | 1 + db/models/order.js | 12 +++++------- db/sql/1-tables.sql | 3 ++- db/sql/2-views.sql | 1 + 5 files changed, 10 insertions(+), 8 deletions(-) diff --git a/db/mappings/config.js b/db/mappings/config.js index c779e12..5bbd125 100644 --- a/db/mappings/config.js +++ b/db/mappings/config.js @@ -7,5 +7,6 @@ module.exports = [{ ], associations: [ {name: 'modified_by', mapId: 'userMap', columnPrefix: 'user_'}, + {name: 'shipping_from', mapId: 'addressMap', columnPrefix: 'address_'} ] }] diff --git a/db/models/address.js b/db/models/address.js index fe8ef2e..d3ec392 100644 --- a/db/models/address.js +++ b/db/models/address.js @@ -19,6 +19,7 @@ address.create = async (name, street1, street2, city, state, zip, country) => { const {success} = epAddress.verifications.delivery if (!success) { // TODO: Send back an error? + console.error(`Could not verify address: ${epAddress.id}`) } const query = { diff --git a/db/models/order.js b/db/models/order.js index 70075d9..5ea095d 100644 --- a/db/models/order.js +++ b/db/models/order.js @@ -283,17 +283,15 @@ order.setDelivery = (uuid, description, deliveryDate) => order.shipEasyPost = async ( uuid, length, width, height, weight ) => { // Retrieve address const {address} = await order.findByUUID(uuid) + const {shipping_from} = await config.getLatestConfig() + + if(!shipping_from?.easypost_id) + throw new Error("Cannot ship - no from address set in config") // Create shipment const epShipment = new easypost.Shipment({ to_address: address.easypost_id, - from_address: { - street1: '11381 N. Sampson Drive', - city: 'Highland', - state: 'UT', - zip: '84003', - country: 'US' - }, + from_address: shipping_from.easypost_id, parcel: {length, width, height, weight} }) diff --git a/db/sql/1-tables.sql b/db/sql/1-tables.sql index e50f4fc..f584fc0 100644 --- a/db/sql/1-tables.sql +++ b/db/sql/1-tables.sql @@ -272,7 +272,8 @@ create table sos."config" ( config_uuid uuid primary key default uuid_generate_v4(), config_date_updated timestamptz not null default now(), config_updated_by uuid references sos."user" (user_uuid), - config_default_tax_percent numeric(8,6) not null default 7.250 + config_default_tax_percent numeric(8,6) not null default 7.250, + config_shipping_from uuid references sos."address" (address_uuid) ); insert into sos."config" default values; diff --git a/db/sql/2-views.sql b/db/sql/2-views.sql index 84ffe4e..a8c3738 100644 --- a/db/sql/2-views.sql +++ b/db/sql/2-views.sql @@ -147,6 +147,7 @@ create or replace view sos.v_order as create or replace view sos.v_config as select * from sos."config" left join sos."user" on config_updated_by = user_uuid + left join sos."address" on config_shipping_from = address_uuid where config_date_updated = (select max(config_date_updated) from sos."config"); create or replace view sos.v_stockchange as