diff --git a/api/orders.js b/api/orders.js index fbe19bc..eb531b2 100644 --- a/api/orders.js +++ b/api/orders.js @@ -10,3 +10,8 @@ router.put('/', async (req, res) => { const order = await db.order.create(req.cart.uuid); res.json(order) }) + +router.get('/current', async (req, res) => { + const order = await db.order.findForCart(req.cart.uuid); + res.json(order) +}) diff --git a/db/models/order.js b/db/models/order.js index 303daef..a65d6a9 100644 --- a/db/models/order.js +++ b/db/models/order.js @@ -16,3 +16,15 @@ order.create = async function(cart_uuid){ const {rows} = await pg.query(query) return joinjs.map(rows, mappings, 'orderMap', 'order_')[0]; } + +order.findForCart = async function(cart_uuid) { + const query = { + text: 'select * from sos.find_order_for_cart($1)', + values: [cart_uuid] + } + + debug(query) + + const {rows} = await pg.query(query) + return joinjs.map(rows, mappings, 'orderMap', 'order_')[0]; +} diff --git a/db/sql/1-tables.sql b/db/sql/1-tables.sql index 932a288..d221f38 100644 --- a/db/sql/1-tables.sql +++ b/db/sql/1-tables.sql @@ -174,6 +174,7 @@ create table sos."transaction" ( constraint transaction_cannot_complete_without_shipping check (transaction_payment_state != 'completed' or transaction_shipping_price is not null), constraint transaction_no_discount_without_coupon check (transaction_coupon_effective_discount = 0 or transaction_coupon_uuid is not null) -- TODO: Partial index on cart_uuid to disallow two "started" transactions with the same cart + -- TODO: Partial index on order_uuid to disallow two "started" transactions with the same order ); create table sos."payment" ( diff --git a/db/sql/3-functions.sql b/db/sql/3-functions.sql index a24cd3a..73ac3d3 100644 --- a/db/sql/3-functions.sql +++ b/db/sql/3-functions.sql @@ -479,3 +479,18 @@ begin ) where transaction_cart_uuid = _cart_uuid and transaction_payment_state = 'started'; end; $function$; + +create or replace function sos.find_order_for_cart(_cart_uuid uuid) + returns setof sos.v_order + language plpgsql +as $function$ +declare + _order_uuid uuid; +begin + select transaction_order_uuid into _order_uuid + from sos."transaction" + where transaction_cart_uuid = _cart_uuid + and transaction_payment_state = 'started'; + + return query select * from sos.v_order where order_uuid = _order_uuid; +end; $function$; diff --git a/pages/store/cart.js b/pages/store/cart.js index c3c3e61..4e243b1 100644 --- a/pages/store/cart.js +++ b/pages/store/cart.js @@ -1,4 +1,5 @@ import React from 'react' +import Router from 'next/router' import Link from 'next/link' import axios from 'axios' import Head from 'next/head' @@ -20,9 +21,10 @@ export default function Cart(){ setCart(data) } + // We can't let the form controller do this as it shouldn't actually pass a body const handleCreateTransaction = async () => { - const {data: order} = await axios.put(`/api/orders`) - console.log(order) + await axios.put(`/api/orders`) + Router.push('/store/checkout') } return ( diff --git a/pages/store/checkout/index.js b/pages/store/checkout/index.js new file mode 100644 index 0000000..8fdfc50 --- /dev/null +++ b/pages/store/checkout/index.js @@ -0,0 +1,15 @@ +CheckoutSummary.getInitialProps = async function({ctx: {axios}}){ + const {data: order} = await axios.get(`/api/orders/current`) + return {order} +} + +export default function CheckoutSummary({order}){ + return ( + <> +

Checkout

+
+        {JSON.stringify(order, null, 2)}
+      
+ + ) +}