import {DateTime} from 'luxon' CheckoutComplete.getInitialProps = async function({ctx: {query: {session_id}, axios}}){ const {data: orders} = await axios.get('/api/orders') const mostRecentOrder = orders.sort(sortOrders)[0] return {order: mostRecentOrder} } export default function CheckoutComplete({order}){ return (
{JSON.stringify(order, null, 2)}) } function parsePaymentTime({payment}){ if(typeof payment.time === 'string') payment.time = DateTime.fromISO(payment.time) return payment.time } function sortTransactions(a,b){ const timeA = parsePaymentTime(a) const timeB = parsePaymentTime(b) return timeB.diff(timeA).as('seconds') } function sortOrders(a,b){ const timePaidA = parsePaymentTime(a.transactions.sort(sortTransactions)[0]) const timePaidB = parsePaymentTime(b.transactions.sort(sortTransactions)[0]) return timePaidB.diff(timePaidA).as('seconds') }