From c5425d8cb47f552d8e38f2dae580a89304eea092 Mon Sep 17 00:00:00 2001 From: Ashelyn Dawn Date: Mon, 27 Apr 2020 22:09:09 -0600 Subject: [PATCH] Packages can be marked hand delivered --- api/orders.js | 5 +++ db/models/order.js | 8 ++++ db/sql/3-functions.sql | 47 ++++++++++++++++++++++++ pages/admin/orders/[id]/index.js | 4 +- pages/admin/orders/[id]/ship/delivery.js | 26 +++++++++++++ pages/admin/orders/[id]/ship/tracking.js | 1 + 6 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 pages/admin/orders/[id]/ship/delivery.js diff --git a/api/orders.js b/api/orders.js index a3c67b1..30ba988 100644 --- a/api/orders.js +++ b/api/orders.js @@ -162,3 +162,8 @@ router.post('/:uuid/ship/tracking', ensureAdmin, parseJSON, async (req, res) => const order = await db.order.setTracking(req.params.uuid, req.body.code, req.body.date || null) res.json(order) }) + +router.post('/:uuid/ship/delivery', ensureAdmin, parseJSON, async (req, res) => { + const order = await db.order.setDelivery(req.params.uuid, req.body.description, req.body.date || null) + res.json(order) +}) diff --git a/db/models/order.js b/db/models/order.js index bdaaa9f..5cfbd16 100644 --- a/db/models/order.js +++ b/db/models/order.js @@ -250,3 +250,11 @@ order.setTracking = (uuid, trackingCode, shipDate) => returnType: 'order', single: true }) + + order.setDelivery = (uuid, description, deliveryDate) => + dbUtil.executeFunction({ + name: 'set_delivery_by_hand', + params: [uuid, description, deliveryDate], + returnType: 'order', + single: true + }) diff --git a/db/sql/3-functions.sql b/db/sql/3-functions.sql index 25cb61e..b9ee927 100644 --- a/db/sql/3-functions.sql +++ b/db/sql/3-functions.sql @@ -943,3 +943,50 @@ begin return query select * from sos.v_order where order_uuid = _order_uuid; end; $function$; + +create or replace function sos.set_delivery_by_hand(_order_uuid uuid, _description text, _date_delivered timestamptz) + returns setof sos.v_order + language plpgsql +as $function$ +declare + _delivery_uuid uuid; +begin + -- Ensure order has no delivery + select order_delivery_uuid into _delivery_uuid + from sos."order" where order_uuid = _order_uuid; + if _delivery_uuid is not null then + raise 'Order already has a delivery record'; + end if; + + -- Create delivery + insert into sos."delivery" ( + delivery_type + ) values ( + 'hand_delivered' + ) returning delivery_uuid into _delivery_uuid; + + -- Default date + if _date_delivered is null then + _date_delivered := now(); + end if; + + -- Create delivery subtype record + insert into sos."delivery_hand_delivered" ( + delivery_uuid, + delivery_type, + delivery_description, + delivery_date_delivered + ) values ( + _delivery_uuid, + 'hand_delivered', + _description, + _date_delivered + ); + + -- Update order + update sos."order" set + order_delivery_uuid = _delivery_uuid + where order_uuid = _order_uuid; + + return query select * from sos.v_order where order_uuid = _order_uuid; +end; $function$; diff --git a/pages/admin/orders/[id]/index.js b/pages/admin/orders/[id]/index.js index 27df8a8..c546d9b 100644 --- a/pages/admin/orders/[id]/index.js +++ b/pages/admin/orders/[id]/index.js @@ -15,10 +15,10 @@ export default function Order({order}){ return ( <> -

Order for {capitalizeName(order.address.name)}

diff --git a/pages/admin/orders/[id]/ship/delivery.js b/pages/admin/orders/[id]/ship/delivery.js new file mode 100644 index 0000000..e2c18cf --- /dev/null +++ b/pages/admin/orders/[id]/ship/delivery.js @@ -0,0 +1,26 @@ +import React, {useState} from 'react' +import Router from 'next/router' + +import ActionBar from '~/components/admin/actionBar' +import {FormController, DateInput, TextArea, Button} from '~/components/form' + +EnterDelivery.getInitialProps = async ({ctx: {axios, query: {id}}}) => { + const {data: order} = await axios.get(`/api/orders/${id}`) + return {order} +} + +export default function EnterDelivery({order}){ + return ( + <> + + + Router.push(`/admin/orders/${order.uuid}`)} url={`/api/orders/${order.uuid}/ship/delivery`}> +

Use this to enter delivery information for packages delivered in-person or at a con.

+

In case we ever have future corespondence with this person, please be sure to describe where, when, and to who you gave the package.

+