diff --git a/src/api/supabase/queries/cart_queries.ts b/src/api/supabase/queries/cart_queries.ts index bb731e8d..bc5b1662 100644 --- a/src/api/supabase/queries/cart_queries.ts +++ b/src/api/supabase/queries/cart_queries.ts @@ -208,3 +208,9 @@ export async function fetchCartItemsWithQuantity(): Promise< return fetchedProducts; } + +export async function fetchCartIdFromUser() { + const user = await fetchUser(); + const cartID = user.cart_id; + return cartID; +} diff --git a/src/api/supabase/queries/order_queries.ts b/src/api/supabase/queries/order_queries.ts index fdd4e246..d431d3fa 100644 --- a/src/api/supabase/queries/order_queries.ts +++ b/src/api/supabase/queries/order_queries.ts @@ -1,7 +1,7 @@ /* eslint-disable no-console */ // -import { Order, OrderProduct, Product } from '../../../schema/schema'; +import { Order, OrderProduct, OrderStatus, Product } from '../../../schema/schema'; import { fetchUser } from './user_queries'; import { fetchProductByID } from './product_queries'; import supabase from '../createClient'; @@ -33,17 +33,19 @@ export async function createOrder() { .insert({ user_id: user.id }) .select('*') .single(); - + console.log(order); if (error) { throw new Error(`Error creating order: ${error.message}`); } + console.log(order.id) await supabase - .from('users') + .from('profiles') .update({ cart_id: order.id }) .match({ id: user.id }); } + /** * gets all orders by user id and sorted it by creation data * @param Order[] - An array of Order objects. @@ -68,7 +70,7 @@ export async function fetchOrdersByUser(): Promise { .from('order') .select('*') .eq('user_id', userId) - .neq('status', 'In Progress'); + .neq('order_status', 'In Progress'); if (error) { throw new Error(`Error fetching orders for user: ${error.message}`); @@ -228,3 +230,36 @@ export async function updateCartPickupId(pickupId: number) { .update({ pickup_time_id: pickupId }) .eq('id', cartId); } + +/* Update the status of an order */ +export async function updateOrderStatus(orderId: number, orderStatus: OrderStatus) { + await supabase + .from('order') + .update({order_status: orderStatus}) + .eq('id', orderId); +} +/** + * Gets user's most recent order + * @returns Promise - The most recent Order object, or throws an error if no order is found. + */ + export async function fetchCartIdFromUserfetchMostRecentOrderByUser(): Promise { + const user = await fetchUser(); + const userId = user.id; + const { data, error } = await supabase + .from('order') + .select('*') + .eq('user_id', userId) + .order('created_at', { ascending: false }) // Order by creation date in descending order + .limit(1); // Limit to only one order + + if (error) { + throw new Error(`Error fetching most recent order for user: ${error.message}`); + } + + if (data.length === 0) { + throw new Error('No orders found for the user.'); + } + + // Return the first order in the data array + return data[0]; +} \ No newline at end of file diff --git a/src/app/delivery/page.tsx b/src/app/delivery/page.tsx index c482880b..dd3a4d95 100644 --- a/src/app/delivery/page.tsx +++ b/src/app/delivery/page.tsx @@ -8,11 +8,12 @@ import { } from '@/api/supabase/queries/user_queries'; import BackButton from '../../components/BackButton/BackButton'; import { + fetchCartIdFromUser, fetchCartItemsWithQuantity, totalNumberOfItemsInCart, } from '../../api/supabase/queries/cart_queries'; import { Heading1, Normal700Text } from '../../styles/fonts'; -import { ProductWithQuantity, User, Address } from '../../schema/schema'; +import { ProductWithQuantity, User, Address, OrderStatus } from '../../schema/schema'; import OrderSummary from '../../components/OrderSummaryFolder/OrderSummary'; import NavBar from '../../components/NavBarFolder/NavBar'; import { @@ -24,6 +25,7 @@ import { BackButtonDiv, OutterDiv, } from './styles'; +import { createOrder, fetchCurrentOrdersByUser, updateOrderStatus } from '@/api/supabase/queries/order_queries'; export default function App() { const [numberOfItems, setNumberOfItems] = useState(0); @@ -68,7 +70,16 @@ export default function App() { router.push('/orderConfirmationDelivery')} + onClick={async () => { + const firstOrder = (await fetchCartIdFromUser()); + await updateOrderStatus(firstOrder, OrderStatus.Submitted); + await createOrder(); + const newestOrder = (await fetchCartIdFromUser()); + await updateOrderStatus(newestOrder, OrderStatus.inProgress); + console.log(newestOrder) + router.push('/orderConfirmationDelivery') + } + } > Place Order diff --git a/src/app/orderConfirmationPickUp/page.tsx b/src/app/orderConfirmationPickUp/page.tsx index 892bd14b..03f73d5b 100644 --- a/src/app/orderConfirmationPickUp/page.tsx +++ b/src/app/orderConfirmationPickUp/page.tsx @@ -4,7 +4,7 @@ import { useState, useEffect } from 'react'; import { fetchUser } from '@/api/supabase/queries/user_queries'; import { fetchPickupTimesByID } from '@/api/supabase/queries/pickup_queries'; -import { fetchCurrentOrdersByUser } from '@/api/supabase/queries/order_queries'; +import { fetchCurrentOrdersByUser, getOrderById } from '@/api/supabase/queries/order_queries'; import { Body2Bold, Body2, Heading3Bold } from '@/styles/fonts'; import { fetchCartItemsWithQuantity } from '../../api/supabase/queries/cart_queries'; @@ -26,11 +26,15 @@ import { } from './styles'; import { Product, User, Pickup } from '../../schema/schema'; +import { useSearchParams } from 'next/navigation'; export default function OrderConfirmationPickUp() { const [Cart, setCart] = useState([]); const [user, setUser] = useState(); const [pickupTime, setPickupTime] = useState(); + const searchParams = useSearchParams(); + const orderIDFromSearch = searchParams.get('orderID'); + console.log(orderIDFromSearch); useEffect(() => { async function fetchProducts() { @@ -41,8 +45,8 @@ export default function OrderConfirmationPickUp() { async function setUserDetails() { const fetchedUser = await fetchUser(); setUser(fetchedUser); - const currOrder = await fetchCurrentOrdersByUser(); - const pickup = await fetchPickupTimesByID(currOrder[0].pickup_time_id); + const currOrder = await getOrderById(Number(orderIDFromSearch)); + const pickup = await fetchPickupTimesByID(currOrder.pickup_time_id); setPickupTime(pickup); } diff --git a/src/app/orderPage/page.tsx b/src/app/orderPage/page.tsx index 068bf974..6d2d0fd9 100644 --- a/src/app/orderPage/page.tsx +++ b/src/app/orderPage/page.tsx @@ -89,7 +89,7 @@ export default function OrderPage() { {formatDate(order?.created_at)} {' '} - {order?.status}{' '} + {order?.order_status}{' '} diff --git a/src/app/pickup/page.tsx b/src/app/pickup/page.tsx index c45c4715..998736b6 100644 --- a/src/app/pickup/page.tsx +++ b/src/app/pickup/page.tsx @@ -4,15 +4,16 @@ import { ArrowLeft } from 'react-feather'; import { fetchUser } from '@/api/supabase/queries/user_queries'; import { + fetchCartIdFromUser, fetchCartItemsWithQuantity, totalNumberOfItemsInCart, } from '@/api/supabase/queries/cart_queries'; import { useState, useEffect } from 'react'; -import { useRouter } from 'next/navigation'; +import { useRouter, useSearchParams } from 'next/navigation'; import { Heading4Bold } from '@/styles/fonts'; import { fetchNRecentPickupTimes } from '@/api/supabase/queries/pickup_queries'; -import { updateCartPickupId } from '@/api/supabase/queries/order_queries'; -import { Pickup, User, ProductWithQuantity } from '@/schema/schema'; +import { updateCartPickupId, updateOrderStatus, createOrder } from '@/api/supabase/queries/order_queries'; +import { Pickup, User, ProductWithQuantity, OrderStatus } from '@/schema/schema'; import OrderSummary from '../../components/OrderSummaryFolder/OrderSummary'; import NavBar from '../../components/NavBarFolder/NavBar'; import { @@ -152,7 +153,14 @@ export default function Pickup() { onClick={async () => { if (selectedPickupIndex !== 0) { await updateCartPickupId(selectedPickupIndex); // TODO double check if this is correct - router.push('/orderConfirmationPickUp'); + const firstOrder = (await fetchCartIdFromUser()); + await updateOrderStatus(firstOrder, OrderStatus.Submitted); + await createOrder(); + const newestOrder = (await fetchCartIdFromUser()); + console.log(newestOrder); + await updateOrderStatus(newestOrder, OrderStatus.inProgress); + router.push(`/orderConfirmationPickUp?${firstOrder}`); + } else { // TODO handle the case where they didn't select a time! } @@ -164,4 +172,4 @@ export default function Pickup() { ); -} +} \ No newline at end of file diff --git a/src/app/profileScreen/page.tsx b/src/app/profileScreen/page.tsx index 5c3cc6e4..26484163 100644 --- a/src/app/profileScreen/page.tsx +++ b/src/app/profileScreen/page.tsx @@ -171,19 +171,19 @@ function OrderHistorySection(props: { Orders: Order[] }) { if (firstOrderProducts.length > 0) { let backgroundColor = 'transparent'; - if (Orders[0].status === 'Submitted') { + if (Orders[0].order_status === 'Submitted') { backgroundColor = '#CEE8BE'; - } else if (Orders[0].status === 'Rejected') { + } else if (Orders[0].order_status === 'Rejected') { backgroundColor = '#FFDDDD'; - } else if (Orders[0].status === 'Confirmed') { + } else if (Orders[0].order_status === 'Confirmed') { backgroundColor = '#C7DDFF'; } let icon; - if (Orders[0].status === 'Submitted') { + if (Orders[0].order_status === 'Submitted') { icon = ; - } else if (Orders[0].status === 'Rejected') { + } else if (Orders[0].order_status === 'Rejected') { icon = ; - } else if (Orders[0].status === 'Confirmed') { + } else if (Orders[0].order_status === 'Confirmed') { icon = ; } else { icon = null; @@ -230,7 +230,7 @@ function OrderHistorySection(props: { Orders: Order[] }) { > {icon} - {Orders[0].status} + {Orders[0].order_status} diff --git a/src/components/OrderHistory/OrderHistoryBox.tsx b/src/components/OrderHistory/OrderHistoryBox.tsx index 329a921c..eaa42466 100644 --- a/src/components/OrderHistory/OrderHistoryBox.tsx +++ b/src/components/OrderHistory/OrderHistoryBox.tsx @@ -53,7 +53,7 @@ export default function OrderDetailsWithProducts({ diff --git a/src/components/OrderHistory/OrderHistoryText.tsx b/src/components/OrderHistory/OrderHistoryText.tsx index ab5fd7ac..ba1c6737 100644 --- a/src/components/OrderHistory/OrderHistoryText.tsx +++ b/src/components/OrderHistory/OrderHistoryText.tsx @@ -40,7 +40,7 @@ export default function OrderDetails(props: OrderDetailsProps) { const queryString = querystring.stringify({ orderID }); router.push(`/orderPage?${queryString}`); }; - if (order.status === OrderStatus.Rejected) { + if (order.order_status === OrderStatus.Rejected) { return (
); } - if (order.status === OrderStatus.Complete) { + if (order.order_status === OrderStatus.Complete) { return (
); } - if (order.status === OrderStatus.Submitted) { + if (order.order_status === OrderStatus.Submitted) { return (