Frontend can retrieve current order

main
Ashelyn Dawn 5 years ago
parent 9f270aeed5
commit f409186bf9

@ -10,3 +10,8 @@ router.put('/', async (req, res) => {
const order = await db.order.create(req.cart.uuid); const order = await db.order.create(req.cart.uuid);
res.json(order) res.json(order)
}) })
router.get('/current', async (req, res) => {
const order = await db.order.findForCart(req.cart.uuid);
res.json(order)
})

@ -16,3 +16,15 @@ order.create = async function(cart_uuid){
const {rows} = await pg.query(query) const {rows} = await pg.query(query)
return joinjs.map(rows, mappings, 'orderMap', 'order_')[0]; 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];
}

@ -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_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) 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 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" ( create table sos."payment" (

@ -479,3 +479,18 @@ begin
) where transaction_cart_uuid = _cart_uuid ) where transaction_cart_uuid = _cart_uuid
and transaction_payment_state = 'started'; and transaction_payment_state = 'started';
end; $function$; 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$;

@ -1,4 +1,5 @@
import React from 'react' import React from 'react'
import Router from 'next/router'
import Link from 'next/link' import Link from 'next/link'
import axios from 'axios' import axios from 'axios'
import Head from 'next/head' import Head from 'next/head'
@ -20,9 +21,10 @@ export default function Cart(){
setCart(data) setCart(data)
} }
// We can't let the form controller do this as it shouldn't actually pass a body
const handleCreateTransaction = async () => { const handleCreateTransaction = async () => {
const {data: order} = await axios.put(`/api/orders`) await axios.put(`/api/orders`)
console.log(order) Router.push('/store/checkout')
} }
return ( return (

@ -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 (
<>
<h2>Checkout</h2>
<pre>
{JSON.stringify(order, null, 2)}
</pre>
</>
)
}
Loading…
Cancel
Save