From 18980fbe97af163c822e6856a086777e6e9e3993 Mon Sep 17 00:00:00 2001 From: Kevin Cai Date: Wed, 1 Nov 2023 15:55:25 -0700 Subject: [PATCH 01/23] Cart Backend WIP --- src/api/supabase/queries/tests/user_test.ts | 83 +++++++-- src/api/supabase/queries/user_queries.ts | 197 ++++++++++++++------ src/app/page.tsx | 18 +- src/schema/schema.ts | 14 +- 4 files changed, 209 insertions(+), 103 deletions(-) diff --git a/src/api/supabase/queries/tests/user_test.ts b/src/api/supabase/queries/tests/user_test.ts index 37a19898..3c2b0d8c 100644 --- a/src/api/supabase/queries/tests/user_test.ts +++ b/src/api/supabase/queries/tests/user_test.ts @@ -4,38 +4,87 @@ import { fetchUserData, fetchUserByUUID, - addUserAddress, + fetchUserCart, + incrementCartItemByOne, + decrementCartItemByOne, + decrementCartItem, } from '../user_queries'; -export async function testFetchUserData() { +export async function runFetchUserData() { try { const result = await fetchUserData(); - console.log('Fetch Data Result:', result); + console.log('fetchUserData Result:', result); } catch (error) { - console.error('Test Fetch Data Error:', error); + console.error('Error in fetchUserData:', error); } } -export async function testFetchUserByUUID() { - const uuid = '3b4a1317-b9ea-4cbd-95d7-e959aa80d1ea'; // Replace with a valid user ID +export async function runFetchUserByUUID() { + const testUUID = 'aeaf5f6c-a8bc-41b8-9850-5fb11e1b6dea'; try { - const result = await fetchUserByUUID(uuid); - console.log('Fetch User by UUID Result:', result); + const result = await fetchUserByUUID(testUUID); + console.log('fetchUserByUUID Result:', result); } catch (error) { - console.error('Test Fetch User by UUID Error:', error); + console.error('Error in fetchUserByUUID:', error); } } -export async function testAddUserAddress() { - const uuid = '3b4a1317-b9ea-4cbd-95d7-e959aa80d1ea'; // Replace with a valid user ID - const newStreet = '123 New Street'; - const newCity = 'New City'; - const newZipcode = '12345'; +export async function runFetchUserCart() { + const testUserId = 'aeaf5f6c-a8bc-41b8-9850-5fb11e1b6dea'; + try { + const result = await fetchUserCart(testUserId); + console.log('fetchUserCart Result:', result); + } catch (error) { + console.error('Error in fetchUserCart:', error); + } +} + +export async function runIncrementCartItemByOne() { + const testUserId = 'aeaf5f6c-a8bc-41b8-9850-5fb11e1b6dea'; + const testItemId = '10'; + try { + await incrementCartItemByOne(testUserId, testItemId); + const result = await fetchUserCart(testUserId); + console.log('fetchUserCart Result_1:', result); + console.log('incrementCartItemByOne executed successfully.'); + } catch (error) { + console.error('Error in incrementCartItemByOne:', error); + } +} + +export async function runDecrementCartItemByOne() { + const testUserId = 'aeaf5f6c-a8bc-41b8-9850-5fb11e1b6dea'; + const testItemId = '10'; + try { + await decrementCartItemByOne(testUserId, testItemId); + const result = await fetchUserCart(testUserId); + console.log('fetchUserCart Result_1:', result); + console.log('incrementCartItemByOne executed successfully.'); + } catch (error) { + console.error('Error in incrementCartItemByOne:', error); + } +} +export async function fullCartTest() { + const testUserId = '4a934844-76fa-4a1a-80d7-fa00597398e1'; + const testItemId = '10'; try { - const result = await addUserAddress(uuid, newStreet, newCity, newZipcode); - console.log('Add User Address Result:', result); + const result = await fetchUserByUUID(testUserId); + console.log('fetchUserData Result:', result); + await incrementCartItemByOne(testUserId, testItemId); + await incrementCartItemByOne(testUserId, testItemId); + await incrementCartItemByOne(testUserId, testItemId); + let result_1 = await fetchUserCart(testUserId); + console.log('fetchUserCart Result_1:', result_1); + // await decrementCartItemByOne(testUserId, testItemId); + result_1 = await fetchUserCart(testUserId); + console.log('fetchUserCart Result_2:', result_1); + // await decrementCartItem(testUserId, testItemId, 6); + + // result = await fetchUserCart(testUserId); + // console.log('fetchUserCart Result_2:', result); } catch (error) { - console.error('Test Add User Address Error:', error); + console.error('Error in incrementCartItemByOne:', error); } } + diff --git a/src/api/supabase/queries/user_queries.ts b/src/api/supabase/queries/user_queries.ts index 22807404..8f73f572 100644 --- a/src/api/supabase/queries/user_queries.ts +++ b/src/api/supabase/queries/user_queries.ts @@ -16,86 +16,163 @@ const supabaseApiKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY; // Initialize the Supabase client const supabase = createClient(supabaseUrl ?? '', supabaseApiKey ?? ''); -export async function fetchUserData(): Promise< - PostgrestSingleResponse | { data: never[]; error: PostgrestError } -> { - try { - const { data: users, error } = await supabase.from('users').select('*'); +export async function fetchUserData() + { + const { data , error }: {data:User[] | null, error: PostgrestError| null} = await supabase.from('profiles').select('*'); if (error) { - console.error('Error fetching data:', error); - return { data: [], error }; + throw new Error(`An error occured when trying to read profiles: ${error}`); + } else { + return data; } - - return { data: users } as PostgrestSingleResponse; - } catch (error) { - console.error('Error:', error); - throw error; - } } export async function fetchUserByUUID( uuid: string, -): Promise> { - try { - const { data: user, error } = await supabase - .from('Users') +) { + + const { data , error }: {data:User | null, error: PostgrestError| null} = await supabase + .from('profiles') .select('*') .eq('user_id', uuid) .single(); + if (error) { + throw new Error(`An error occured when trying to read profiles: ${error}`); + } else { + return data; + } + } + + export async function fetchUserCart(userId: string): Promise> { + const { data, error }: { data: User | null, error: PostgrestError | null } = await supabase + .from('profiles') + .select('*') + .eq('user_id', userId).single(); + if (error) { - console.error('Error fetching user data:', error); + throw new Error(`An error occurred when trying to fetch the cart: ${error.message}`); + } else if (!data) { + throw new Error("No user found with the specified user_id."); } + + return data.cart; + } + +export async function updateCart(userId: string, currentCart: Record) { + console.log(currentCart); + const { data, error } = await supabase + .from('users') + .update({ cart: currentCart }) + .eq('user_id', userId); + + if (error) { + throw new Error(`An error occurred when trying to update the cart: ${error.message}`); + } +} + +export async function incrementCartItem(userId: string, itemId: string, n: number) { + // First, fetch the current cart for the user + const { data, error }: { data: User | null, error: PostgrestError | null } = await supabase + .from('profiles') + .select('*') + .eq('user_id', userId).single(); - return user; - } catch (error) { - console.error('Error:', error); - throw error; + if (error) { + throw new Error(`An error occurred when trying to fetch the cart: ${error.message}`); + } else if (!data) { + throw new Error("No user found with the specified user_id."); } + // console.log(data); + const currentCart = data.cart; + console.log(currentCart); + // Increment the item's quantity by n or set it to n if not present + currentCart[itemId] = (currentCart[itemId] || 0) + n; + console.log(currentCart); + // Use the updateCart function to update the cart in the database + await updateCart(userId, currentCart); } -export async function addUserAddress( - uuid: string, - newStreet: string, - newCity: string, - newZipcode: string, -): Promise> { - try { - const { data: existingUser, error: selectError } = await supabase - .from('Users') - .select('street, city, zipcode') - .eq('user_id', uuid) - .single(); +export async function incrementCartItemByOne(userId: string, itemId: string) { + return incrementCartItem(userId, itemId, 1); +} - if (selectError) { - console.error('Error selecting user data:', selectError); - throw selectError; - } +export async function decrementCartItem(userId: string, itemId: string, n: number) { + // First, fetch the current cart for the user + const { data, error }: { data: User[] | null, error: PostgrestError | null } = await supabase + .from('profiles') + .select('*') + .eq('user_id', userId); - // Append new values to the arrays - const updatedStreet = [...(existingUser?.street || []), newStreet]; - const updatedCity = [...(existingUser?.city || []), newCity]; - const updatedZipcode = [...(existingUser?.zipcode || []), newZipcode]; - - const { data, error } = await supabase - .from('Users') - .update({ - street: updatedStreet, - city: updatedCity, - zipcode: updatedZipcode, - }) - .eq('user_id', uuid) - .single(); + if (error) { + throw new Error(`An error occurred when trying to fetch the cart: ${error.message}`); + } else if (!data || data.length === 0) { + throw new Error("No user found with the specified user_id."); + } - if (error) { - console.error('Error updating user data:', error); - throw error; - } + const currentCart = data[0].cart; - return { data, error: null, status: 200, statusText: 'OK', count: 1 }; - } catch (error) { - console.error('Error:', error); - throw error; + // Decrement the item's quantity by n or remove it if it's 0 or below + if (currentCart[itemId]) { + currentCart[itemId] -= n; + + if (currentCart[itemId] <= 0) { + delete currentCart[itemId]; + } } + + // Use the updateCart function to update the cart in the database + await updateCart(userId, currentCart); +} + +export async function decrementCartItemByOne(userId: string, itemId: string) { + return decrementCartItem(userId, itemId, 1); } + + + + +// export async function addUserAddress( +// uuid: string, +// newStreet: string, +// newCity: string, +// newZipcode: string, +// ): Promise> { +// try { +// const { data: existingUser, error: selectError } = await supabase +// .from('profiles') +// .select('street, city, zipcode') +// .eq('user_id', uuid) +// .single(); + +// if (selectError) { +// console.error('Error selecting user data:', selectError); +// throw selectError; +// } + +// // Append new values to the arrays +// const updatedStreet = [...(existingUser?.street || []), newStreet]; +// const updatedCity = [...(existingUser?.city || []), newCity]; +// const updatedZipcode = [...(existingUser?.zipcode || []), newZipcode]; + +// const { data, error } = await supabase +// .from('profiles') +// .update({ +// street: updatedStreet, +// city: updatedCity, +// zipcode: updatedZipcode, +// }) +// .eq('user_id', uuid) +// .single(); + +// if (error) { +// console.error('Error updating user data:', error); +// throw error; +// } + +// return { data, error: null, status: 200, statusText: 'OK', count: 1 }; +// } catch (error) { +// console.error('Error:', error); +// throw error; +// } +// } diff --git a/src/app/page.tsx b/src/app/page.tsx index a2b349a5..216dcee7 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -2,9 +2,7 @@ import Link from 'next/link'; import { - testFetchUserData, - testFetchUserByUUID, - testAddUserAddress, + fullCartTest } from '../api/supabase/queries/tests/user_test'; import { testFetchOrderByUUID, @@ -24,19 +22,7 @@ import { } from '../api/supabase/queries/tests/pickup_test'; export default function Checkout() { - testFetchUserData(); - // testFetchUserByUUID(); - // testAddUserAddress(); - // testFetchOrderByUUID(); - // testFetchOrders(); - // testGetOrderById(); - // testToggleOrderProgress(); - // testFetchProducts(); - // testFetchProductByName(); - // testFetchPickupData(); - // testFetchPickupTimesByUUID(); - // testUpdateAllOrdersProgressToTrue(); - + fullCartTest(); return (
Login diff --git a/src/schema/schema.ts b/src/schema/schema.ts index c0a294be..2132d8e3 100644 --- a/src/schema/schema.ts +++ b/src/schema/schema.ts @@ -6,16 +6,15 @@ export type User = { pet_information: string; user_id: string; // UUID order_option: boolean; - address: number; // index of the address the street, city, and zipcode to build the address - street: string[]; - city: string[]; - zipcode: string[]; + creaed_at: string; // timestamp with time zone not null default now(); + address_id: string; // UUID + cart: Record; // JSONB with item as key and quantity as value }; export type Order = { id: number; // bigint generated by default as identity user_id: string; // UUID not null - cart: number; // bigint[] null + cart: Record; // JSONB with item as key and quantity as value status: string; // bigint null pickup_time: number; // bigint null }; @@ -37,8 +36,3 @@ export type Product = { updated_at: string; // timestamp with time zone not null default now(); }; -export type Cart = { - id: number; // bigint generated by default as identity - user_id: string; // UUID not null - product_id: Record; // JSONB with item as key and quantity as value -}; From 3b7ac519035095a5c32e1242867c438fc7cd0258 Mon Sep 17 00:00:00 2001 From: Kevin Cai Date: Wed, 1 Nov 2023 16:31:52 -0700 Subject: [PATCH 02/23] WIP --- src/api/supabase/queries/user_queries.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/api/supabase/queries/user_queries.ts b/src/api/supabase/queries/user_queries.ts index 8f73f572..c20defb1 100644 --- a/src/api/supabase/queries/user_queries.ts +++ b/src/api/supabase/queries/user_queries.ts @@ -63,7 +63,7 @@ export async function updateCart(userId: string, currentCart: Record Date: Wed, 1 Nov 2023 17:33:27 -0700 Subject: [PATCH 03/23] Cart Done --- src/api/supabase/queries/tests/user_test.ts | 10 +- src/api/supabase/queries/user_queries.ts | 135 +++++++++++--------- src/app/page.tsx | 20 ++- 3 files changed, 92 insertions(+), 73 deletions(-) diff --git a/src/api/supabase/queries/tests/user_test.ts b/src/api/supabase/queries/tests/user_test.ts index 3c2b0d8c..a5b62227 100644 --- a/src/api/supabase/queries/tests/user_test.ts +++ b/src/api/supabase/queries/tests/user_test.ts @@ -71,20 +71,20 @@ export async function fullCartTest() { try { const result = await fetchUserByUUID(testUserId); console.log('fetchUserData Result:', result); + await incrementCartItemByOne(testUserId, testItemId); await incrementCartItemByOne(testUserId, testItemId); await incrementCartItemByOne(testUserId, testItemId); let result_1 = await fetchUserCart(testUserId); console.log('fetchUserCart Result_1:', result_1); - // await decrementCartItemByOne(testUserId, testItemId); + await decrementCartItemByOne(testUserId, testItemId); result_1 = await fetchUserCart(testUserId); console.log('fetchUserCart Result_2:', result_1); - // await decrementCartItem(testUserId, testItemId, 6); + await decrementCartItem(testUserId, testItemId, 6); - // result = await fetchUserCart(testUserId); - // console.log('fetchUserCart Result_2:', result); + const result_3 = await fetchUserCart(testUserId); + console.log('fetchUserCart Result_3:', result_3); } catch (error) { console.error('Error in incrementCartItemByOne:', error); } } - diff --git a/src/api/supabase/queries/user_queries.ts b/src/api/supabase/queries/user_queries.ts index c20defb1..6e3969d6 100644 --- a/src/api/supabase/queries/user_queries.ts +++ b/src/api/supabase/queries/user_queries.ts @@ -16,79 +16,87 @@ const supabaseApiKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY; // Initialize the Supabase client const supabase = createClient(supabaseUrl ?? '', supabaseApiKey ?? ''); -export async function fetchUserData() - { - const { data , error }: {data:User[] | null, error: PostgrestError| null} = await supabase.from('profiles').select('*'); - - if (error) { - throw new Error(`An error occured when trying to read profiles: ${error}`); - } else { - return data; - } -} +export async function fetchUserData() { + const { data, error }: { data: User[] | null; error: PostgrestError | null } = + await supabase.from('profiles').select('*'); -export async function fetchUserByUUID( - uuid: string, -) { + if (error) { + throw new Error(`An error occured when trying to read profiles: ${error}`); + } else { + return data; + } +} - const { data , error }: {data:User | null, error: PostgrestError| null} = await supabase - .from('profiles') - .select('*') - .eq('user_id', uuid) - .single(); - - if (error) { - throw new Error(`An error occured when trying to read profiles: ${error}`); - } else { - return data; - } +export async function fetchUserByUUID(uuid: string) { + const { data, error } = await supabase + .from('profiles') + .select('*') + .eq('user_id', uuid) + .single(); + if (error) { + throw new Error(`An error occured when trying to read profiles: ${error}`); + } else { + return data; } +} - export async function fetchUserCart(userId: string): Promise> { - const { data, error }: { data: User | null, error: PostgrestError | null } = await supabase - .from('profiles') - .select('*') - .eq('user_id', userId).single(); - - if (error) { - throw new Error(`An error occurred when trying to fetch the cart: ${error.message}`); - } else if (!data) { - throw new Error("No user found with the specified user_id."); - } - - return data.cart; +export async function fetchUserCart( + userId: string, +): Promise> { + const { data, error }: { data: User | null; error: PostgrestError | null } = + await supabase.from('profiles').select('*').eq('user_id', userId).single(); + + if (error) { + throw new Error( + `An error occurred when trying to fetch the cart: ${error.message}`, + ); + } else if (!data) { + throw new Error('No user found with the specified user_id.'); } -export async function updateCart(userId: string, currentCart: Record) { - console.log(currentCart); - const { data, error } = await supabase - .from('users') - .update({ "cart": currentCart }) + return data.cart; +} + +export async function updateCart( + userId: string, + currentCart: Record, +) { + + const { data, error } = await supabase + .from('profiles') + .update({ cart: currentCart }) .eq('user_id', userId); if (error) { - throw new Error(`An error occurred when trying to update the cart: ${error.message}`); - } + throw new Error( + `An error occurred when trying to update the cart: ${error.message}`, + ); + } } -export async function incrementCartItem(userId: string, itemId: string, n: number) { +export async function incrementCartItem( + userId: string, + itemId: string, + n: number, +) { + // First, fetch the current cart for the user - const { data, error }: { data: User | null, error: PostgrestError | null } = await supabase - .from('profiles') - .select('*') - .eq('user_id', userId).single(); + const { data, error }: { data: User | null; error: PostgrestError | null } = + await supabase.from('profiles').select('*').eq('user_id', userId).single(); if (error) { - throw new Error(`An error occurred when trying to fetch the cart: ${error.message}`); + throw new Error( + `An error occurred when trying to fetch the cart: ${error.message}`, + ); } else if (!data) { - throw new Error("No user found with the specified user_id."); + throw new Error('No user found with the specified user_id.'); } // console.log(data); const currentCart = data.cart; - console.log(currentCart); + // Increment the item's quantity by n or set it to n if not present currentCart[itemId] = (currentCart[itemId] || 0) + n; - console.log(currentCart); + // console.log(currentCart); // Use the updateCart function to update the cart in the database await updateCart(userId, currentCart); } @@ -97,17 +105,21 @@ export async function incrementCartItemByOne(userId: string, itemId: string) { return incrementCartItem(userId, itemId, 1); } -export async function decrementCartItem(userId: string, itemId: string, n: number) { +export async function decrementCartItem( + userId: string, + itemId: string, + n: number, +) { // First, fetch the current cart for the user - const { data, error }: { data: User[] | null, error: PostgrestError | null } = await supabase - .from('profiles') - .select('*') - .eq('user_id', userId); + const { data, error }: { data: User[] | null; error: PostgrestError | null } = + await supabase.from('profiles').select('*').eq('user_id', userId); if (error) { - throw new Error(`An error occurred when trying to fetch the cart: ${error.message}`); + throw new Error( + `An error occurred when trying to fetch the cart: ${error.message}`, + ); } else if (!data || data.length === 0) { - throw new Error("No user found with the specified user_id."); + throw new Error('No user found with the specified user_id.'); } const currentCart = data[0].cart; @@ -129,9 +141,6 @@ export async function decrementCartItemByOne(userId: string, itemId: string) { return decrementCartItem(userId, itemId, 1); } - - - // export async function addUserAddress( // uuid: string, // newStreet: string, diff --git a/src/app/page.tsx b/src/app/page.tsx index 216dcee7..a46ba71e 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -1,9 +1,10 @@ /* eslint-disable @typescript-eslint/no-unused-vars */ +'use client'; + +import React, { useEffect } from 'react'; import Link from 'next/link'; -import { - fullCartTest -} from '../api/supabase/queries/tests/user_test'; +import { fullCartTest } from '../api/supabase/queries/tests/user_test'; import { testFetchOrderByUUID, testFetchOrders, @@ -21,8 +22,17 @@ import { testFetchPickupTimesByUUID, } from '../api/supabase/queries/tests/pickup_test'; -export default function Checkout() { - fullCartTest(); +export const revalidate = 10; + +export default async function Checkout() { + useEffect(() => { + async function testEverything() { + await fullCartTest(); + } + testEverything(); + }); + + // await fullCartTest(); return (
Login From b7e0116df872e2a654285c2a9ad984ea96be123f Mon Sep 17 00:00:00 2001 From: Kevin Cai Date: Wed, 1 Nov 2023 18:50:39 -0700 Subject: [PATCH 04/23] Done --- src/api/supabase/queries/tests/user_test.ts | 12 ++++++------ src/api/supabase/queries/user_queries.ts | 2 -- src/schema/schema.ts | 1 - 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/src/api/supabase/queries/tests/user_test.ts b/src/api/supabase/queries/tests/user_test.ts index a5b62227..e720d4c9 100644 --- a/src/api/supabase/queries/tests/user_test.ts +++ b/src/api/supabase/queries/tests/user_test.ts @@ -75,15 +75,15 @@ export async function fullCartTest() { await incrementCartItemByOne(testUserId, testItemId); await incrementCartItemByOne(testUserId, testItemId); await incrementCartItemByOne(testUserId, testItemId); - let result_1 = await fetchUserCart(testUserId); - console.log('fetchUserCart Result_1:', result_1); + let result1 = await fetchUserCart(testUserId); + console.log('fetchUserCart Result_1:', result1); await decrementCartItemByOne(testUserId, testItemId); - result_1 = await fetchUserCart(testUserId); - console.log('fetchUserCart Result_2:', result_1); + result1 = await fetchUserCart(testUserId); + console.log('fetchUserCart Result_2:', result1); await decrementCartItem(testUserId, testItemId, 6); - const result_3 = await fetchUserCart(testUserId); - console.log('fetchUserCart Result_3:', result_3); + const result3 = await fetchUserCart(testUserId); + console.log('fetchUserCart Result_3:', result3); } catch (error) { console.error('Error in incrementCartItemByOne:', error); } diff --git a/src/api/supabase/queries/user_queries.ts b/src/api/supabase/queries/user_queries.ts index 6e3969d6..f9180e22 100644 --- a/src/api/supabase/queries/user_queries.ts +++ b/src/api/supabase/queries/user_queries.ts @@ -61,7 +61,6 @@ export async function updateCart( userId: string, currentCart: Record, ) { - const { data, error } = await supabase .from('profiles') .update({ cart: currentCart }) @@ -79,7 +78,6 @@ export async function incrementCartItem( itemId: string, n: number, ) { - // First, fetch the current cart for the user const { data, error }: { data: User | null; error: PostgrestError | null } = await supabase.from('profiles').select('*').eq('user_id', userId).single(); diff --git a/src/schema/schema.ts b/src/schema/schema.ts index 2132d8e3..4338e25d 100644 --- a/src/schema/schema.ts +++ b/src/schema/schema.ts @@ -35,4 +35,3 @@ export type Product = { photo: string; // text null; updated_at: string; // timestamp with time zone not null default now(); }; - From 796bbecf807f156fb74e088b608cd00b6c731923 Mon Sep 17 00:00:00 2001 From: Kevin Cai Date: Wed, 1 Nov 2023 19:59:38 -0700 Subject: [PATCH 05/23] Done --- src/styles/fonts.tsx | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/styles/fonts.tsx b/src/styles/fonts.tsx index 417ebd83..956d3377 100644 --- a/src/styles/fonts.tsx +++ b/src/styles/fonts.tsx @@ -1,17 +1,17 @@ import styled from 'styled-components/native'; export const Heading1 = styled.Text` - font-family: public-sans; - font-size: 40px; - font-style: normal; - font-weight: 700; - line-height: normal; + font-family: public-sans; + font-size: 40px; + font-style: normal; + font-weight: 700; + line-height: normal; `; export const Heading4 = styled.Text` - font-family: public-sans; - font-size: 15px; - font-style: normal; - font-weight: 400; - line-height: normal; -`; \ No newline at end of file + font-family: public-sans; + font-size: 15px; + font-style: normal; + font-weight: 400; + line-height: normal; +`; From d3545700f2a41f63274ae69d70adbb135a55ee59 Mon Sep 17 00:00:00 2001 From: Kevin Cai Date: Fri, 3 Nov 2023 16:12:08 -0700 Subject: [PATCH 06/23] prettier --- src/schema/schema.ts | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/src/schema/schema.ts b/src/schema/schema.ts index 1c610b2d..d2d57b36 100644 --- a/src/schema/schema.ts +++ b/src/schema/schema.ts @@ -5,27 +5,16 @@ export type User = { first_name: string; last_name: string; pet_information: string; -<<<<<<< HEAD - user_id: string; // UUID order_option: boolean; creaed_at: string; // timestamp with time zone not null default now(); address_id: string; // UUID cart: Record; // JSONB with item as key and quantity as value -======= - delivery_allowed: boolean; - created_at: Date; // timestamp of when record was created - cart_id: string; // UUID - address_id: string; // UUID ->>>>>>> 3a6696360a9e7f891c0ff66245612853965f12cf }; export type Order = { id: number; // bigint generated by default as identity user_id: string; // UUID not null -<<<<<<< HEAD cart: Record; // JSONB with item as key and quantity as value -======= ->>>>>>> 3a6696360a9e7f891c0ff66245612853965f12cf status: string; // bigint null pickup_time: number; // bigint null }; From 142e6c3145c2b4c92dc903eb964599dc09c535bc Mon Sep 17 00:00:00 2001 From: kevinjcai <75354434+kevinjcai@users.noreply.github.com> Date: Fri, 3 Nov 2023 17:04:51 -0700 Subject: [PATCH 07/23] Delete src/app/page.tsx --- src/app/page.tsx | 41 ----------------------------------------- 1 file changed, 41 deletions(-) delete mode 100644 src/app/page.tsx diff --git a/src/app/page.tsx b/src/app/page.tsx deleted file mode 100644 index a46ba71e..00000000 --- a/src/app/page.tsx +++ /dev/null @@ -1,41 +0,0 @@ -/* eslint-disable @typescript-eslint/no-unused-vars */ - -'use client'; - -import React, { useEffect } from 'react'; -import Link from 'next/link'; -import { fullCartTest } from '../api/supabase/queries/tests/user_test'; -import { - testFetchOrderByUUID, - testFetchOrders, - testGetOrderById, - testToggleOrderProgress, - // eslint-disable-next-line @typescript-eslint/no-unused-vars - testUpdateAllOrdersProgressToTrue, -} from '../api/supabase/queries/tests/order_test'; -import { - testFetchProducts, - testFetchProductByName, -} from '../api/supabase/queries/tests/product_test'; -import { - testFetchPickupData, - testFetchPickupTimesByUUID, -} from '../api/supabase/queries/tests/pickup_test'; - -export const revalidate = 10; - -export default async function Checkout() { - useEffect(() => { - async function testEverything() { - await fullCartTest(); - } - testEverything(); - }); - - // await fullCartTest(); - return ( -
- Login -
- ); -} From 73d0e225e185deeb1711476c427ee5f8f38bc182 Mon Sep 17 00:00:00 2001 From: Kevin Cai Date: Fri, 3 Nov 2023 17:12:14 -0700 Subject: [PATCH 08/23] Vercel Deploy Fix --- src/app/page.tsx | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 src/app/page.tsx diff --git a/src/app/page.tsx b/src/app/page.tsx new file mode 100644 index 00000000..11302e15 --- /dev/null +++ b/src/app/page.tsx @@ -0,0 +1,41 @@ +/* eslint-disable @typescript-eslint/no-unused-vars */ + +'use client'; + +import React, { useEffect } from 'react'; +import Link from 'next/link'; +import { fullCartTest } from '../api/supabase/queries/tests/user_test'; +import { + testFetchOrderByUUID, + testFetchOrders, + testGetOrderById, + testToggleOrderProgress, + // eslint-disable-next-line @typescript-eslint/no-unused-vars + testUpdateAllOrdersProgressToTrue, +} from '../api/supabase/queries/tests/order_test'; +import { + testFetchProducts, + testFetchProductByName, +} from '../api/supabase/queries/tests/product_test'; +import { + testFetchPickupData, + testFetchPickupTimesByUUID, +} from '../api/supabase/queries/tests/pickup_test'; + +export const revalidate = 10; + +export default async function Checkout() { + // useEffect(() => { + // async function testEverything() { + // await fullCartTest(); + // } + // testEverything(); + // }); + + // await fullCartTest(); + return ( +
+ Login +
+ ); +} From bb9ac4ceebea0805812943adead0351b971030a1 Mon Sep 17 00:00:00 2001 From: Kevin Cai Date: Fri, 3 Nov 2023 17:23:26 -0700 Subject: [PATCH 09/23] console log comment --- src/api/supabase/queries/tests/user_test.ts | 26 ++++++++++----------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/api/supabase/queries/tests/user_test.ts b/src/api/supabase/queries/tests/user_test.ts index e720d4c9..b83a5022 100644 --- a/src/api/supabase/queries/tests/user_test.ts +++ b/src/api/supabase/queries/tests/user_test.ts @@ -44,9 +44,9 @@ export async function runIncrementCartItemByOne() { const testItemId = '10'; try { await incrementCartItemByOne(testUserId, testItemId); - const result = await fetchUserCart(testUserId); - console.log('fetchUserCart Result_1:', result); - console.log('incrementCartItemByOne executed successfully.'); + // const result = await fetchUserCart(testUserId); + // console.log('fetchUserCart Result_1:', result); + // console.log('incrementCartItemByOne executed successfully.'); } catch (error) { console.error('Error in incrementCartItemByOne:', error); } @@ -57,9 +57,9 @@ export async function runDecrementCartItemByOne() { const testItemId = '10'; try { await decrementCartItemByOne(testUserId, testItemId); - const result = await fetchUserCart(testUserId); - console.log('fetchUserCart Result_1:', result); - console.log('incrementCartItemByOne executed successfully.'); + // const result = await fetchUserCart(testUserId); + // console.log('fetchUserCart Result_1:', result); + // console.log('incrementCartItemByOne executed successfully.'); } catch (error) { console.error('Error in incrementCartItemByOne:', error); } @@ -70,20 +70,20 @@ export async function fullCartTest() { const testItemId = '10'; try { const result = await fetchUserByUUID(testUserId); - console.log('fetchUserData Result:', result); + // console.log('fetchUserData Result:', result); await incrementCartItemByOne(testUserId, testItemId); await incrementCartItemByOne(testUserId, testItemId); await incrementCartItemByOne(testUserId, testItemId); - let result1 = await fetchUserCart(testUserId); - console.log('fetchUserCart Result_1:', result1); + const result1 = await fetchUserCart(testUserId); + // console.log('fetchUserCart Result_1:', result1); await decrementCartItemByOne(testUserId, testItemId); - result1 = await fetchUserCart(testUserId); - console.log('fetchUserCart Result_2:', result1); + // result1 = await fetchUserCart(testUserId); + // console.log('fetchUserCart Result_2:', result1); await decrementCartItem(testUserId, testItemId, 6); - const result3 = await fetchUserCart(testUserId); - console.log('fetchUserCart Result_3:', result3); + // const result3 = await fetchUserCart(testUserId); + // console.log('fetchUserCart Result_3:', result3); } catch (error) { console.error('Error in incrementCartItemByOne:', error); } From ddfab4225f0be1f678a3c42da3bad343f1137554 Mon Sep 17 00:00:00 2001 From: kevinjcai <75354434+kevinjcai@users.noreply.github.com> Date: Sat, 4 Nov 2023 14:13:37 -0700 Subject: [PATCH 10/23] Update user_test.ts --- src/api/supabase/queries/tests/user_test.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/api/supabase/queries/tests/user_test.ts b/src/api/supabase/queries/tests/user_test.ts index b83a5022..35ce443d 100644 --- a/src/api/supabase/queries/tests/user_test.ts +++ b/src/api/supabase/queries/tests/user_test.ts @@ -13,7 +13,7 @@ import { export async function runFetchUserData() { try { const result = await fetchUserData(); - console.log('fetchUserData Result:', result); + // console.log('fetchUserData Result:', result); } catch (error) { console.error('Error in fetchUserData:', error); } @@ -23,7 +23,7 @@ export async function runFetchUserByUUID() { const testUUID = 'aeaf5f6c-a8bc-41b8-9850-5fb11e1b6dea'; try { const result = await fetchUserByUUID(testUUID); - console.log('fetchUserByUUID Result:', result); + // console.log('fetchUserByUUID Result:', result); } catch (error) { console.error('Error in fetchUserByUUID:', error); } @@ -33,7 +33,7 @@ export async function runFetchUserCart() { const testUserId = 'aeaf5f6c-a8bc-41b8-9850-5fb11e1b6dea'; try { const result = await fetchUserCart(testUserId); - console.log('fetchUserCart Result:', result); + // console.log('fetchUserCart Result:', result); } catch (error) { console.error('Error in fetchUserCart:', error); } From 212c42bbe966f8b412f6a22b3beb21a4fe3f3d1b Mon Sep 17 00:00:00 2001 From: kevinjcai <75354434+kevinjcai@users.noreply.github.com> Date: Sat, 4 Nov 2023 16:36:09 -0700 Subject: [PATCH 11/23] Update user_test.ts --- src/api/supabase/queries/tests/user_test.ts | 90 --------------------- 1 file changed, 90 deletions(-) diff --git a/src/api/supabase/queries/tests/user_test.ts b/src/api/supabase/queries/tests/user_test.ts index 35ce443d..e69de29b 100644 --- a/src/api/supabase/queries/tests/user_test.ts +++ b/src/api/supabase/queries/tests/user_test.ts @@ -1,90 +0,0 @@ -/* eslint-disable no-console */ -// - -import { - fetchUserData, - fetchUserByUUID, - fetchUserCart, - incrementCartItemByOne, - decrementCartItemByOne, - decrementCartItem, -} from '../user_queries'; - -export async function runFetchUserData() { - try { - const result = await fetchUserData(); - // console.log('fetchUserData Result:', result); - } catch (error) { - console.error('Error in fetchUserData:', error); - } -} - -export async function runFetchUserByUUID() { - const testUUID = 'aeaf5f6c-a8bc-41b8-9850-5fb11e1b6dea'; - try { - const result = await fetchUserByUUID(testUUID); - // console.log('fetchUserByUUID Result:', result); - } catch (error) { - console.error('Error in fetchUserByUUID:', error); - } -} - -export async function runFetchUserCart() { - const testUserId = 'aeaf5f6c-a8bc-41b8-9850-5fb11e1b6dea'; - try { - const result = await fetchUserCart(testUserId); - // console.log('fetchUserCart Result:', result); - } catch (error) { - console.error('Error in fetchUserCart:', error); - } -} - -export async function runIncrementCartItemByOne() { - const testUserId = 'aeaf5f6c-a8bc-41b8-9850-5fb11e1b6dea'; - const testItemId = '10'; - try { - await incrementCartItemByOne(testUserId, testItemId); - // const result = await fetchUserCart(testUserId); - // console.log('fetchUserCart Result_1:', result); - // console.log('incrementCartItemByOne executed successfully.'); - } catch (error) { - console.error('Error in incrementCartItemByOne:', error); - } -} - -export async function runDecrementCartItemByOne() { - const testUserId = 'aeaf5f6c-a8bc-41b8-9850-5fb11e1b6dea'; - const testItemId = '10'; - try { - await decrementCartItemByOne(testUserId, testItemId); - // const result = await fetchUserCart(testUserId); - // console.log('fetchUserCart Result_1:', result); - // console.log('incrementCartItemByOne executed successfully.'); - } catch (error) { - console.error('Error in incrementCartItemByOne:', error); - } -} - -export async function fullCartTest() { - const testUserId = '4a934844-76fa-4a1a-80d7-fa00597398e1'; - const testItemId = '10'; - try { - const result = await fetchUserByUUID(testUserId); - // console.log('fetchUserData Result:', result); - - await incrementCartItemByOne(testUserId, testItemId); - await incrementCartItemByOne(testUserId, testItemId); - await incrementCartItemByOne(testUserId, testItemId); - const result1 = await fetchUserCart(testUserId); - // console.log('fetchUserCart Result_1:', result1); - await decrementCartItemByOne(testUserId, testItemId); - // result1 = await fetchUserCart(testUserId); - // console.log('fetchUserCart Result_2:', result1); - await decrementCartItem(testUserId, testItemId, 6); - - // const result3 = await fetchUserCart(testUserId); - // console.log('fetchUserCart Result_3:', result3); - } catch (error) { - console.error('Error in incrementCartItemByOne:', error); - } -} From a16961e1eb9a06ed16ab38c961409295d61d0886 Mon Sep 17 00:00:00 2001 From: kevinjcai <75354434+kevinjcai@users.noreply.github.com> Date: Sat, 4 Nov 2023 16:51:33 -0700 Subject: [PATCH 12/23] Update user_test.ts --- src/api/supabase/queries/tests/user_test.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/api/supabase/queries/tests/user_test.ts b/src/api/supabase/queries/tests/user_test.ts index e69de29b..faa74708 100644 --- a/src/api/supabase/queries/tests/user_test.ts +++ b/src/api/supabase/queries/tests/user_test.ts @@ -0,0 +1,17 @@ +/* eslint-disable no-console */ +// + +import { + fetchUserData, + fetchUserByUUID, + fetchUserCart, + incrementCartItemByOne, + decrementCartItemByOne, + decrementCartItem, +} from '../user_queries'; + +export async function runFetchUserData() { + + const result = await fetchUserData(); + +} From 26b0acbdba98102adc88e5a211c4aa0d19888d3b Mon Sep 17 00:00:00 2001 From: kevinjcai <75354434+kevinjcai@users.noreply.github.com> Date: Sat, 4 Nov 2023 16:52:51 -0700 Subject: [PATCH 13/23] Update user_queries.ts --- src/api/supabase/queries/user_queries.ts | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/api/supabase/queries/user_queries.ts b/src/api/supabase/queries/user_queries.ts index f9180e22..e144a212 100644 --- a/src/api/supabase/queries/user_queries.ts +++ b/src/api/supabase/queries/user_queries.ts @@ -89,12 +89,12 @@ export async function incrementCartItem( } else if (!data) { throw new Error('No user found with the specified user_id.'); } - // console.log(data); + const currentCart = data.cart; // Increment the item's quantity by n or set it to n if not present currentCart[itemId] = (currentCart[itemId] || 0) + n; - // console.log(currentCart); + // Use the updateCart function to update the cart in the database await updateCart(userId, currentCart); } @@ -153,7 +153,6 @@ export async function decrementCartItemByOne(userId: string, itemId: string) { // .single(); // if (selectError) { -// console.error('Error selecting user data:', selectError); // throw selectError; // } @@ -173,13 +172,10 @@ export async function decrementCartItemByOne(userId: string, itemId: string) { // .single(); // if (error) { -// console.error('Error updating user data:', error); // throw error; // } // return { data, error: null, status: 200, statusText: 'OK', count: 1 }; // } catch (error) { -// console.error('Error:', error); -// throw error; // } // } From aa5ac70b1f018208ba8e44068fe79643344c90c8 Mon Sep 17 00:00:00 2001 From: kevinjcai <75354434+kevinjcai@users.noreply.github.com> Date: Sun, 5 Nov 2023 16:09:53 -0800 Subject: [PATCH 14/23] Update user_test.ts --- src/api/supabase/queries/tests/user_test.ts | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/api/supabase/queries/tests/user_test.ts b/src/api/supabase/queries/tests/user_test.ts index faa74708..eca7395d 100644 --- a/src/api/supabase/queries/tests/user_test.ts +++ b/src/api/supabase/queries/tests/user_test.ts @@ -3,11 +3,7 @@ import { fetchUserData, - fetchUserByUUID, - fetchUserCart, - incrementCartItemByOne, - decrementCartItemByOne, - decrementCartItem, + } from '../user_queries'; export async function runFetchUserData() { From 4c9b2adc3dfed2cb33847ba413d358b521074e4b Mon Sep 17 00:00:00 2001 From: kevinjcai <75354434+kevinjcai@users.noreply.github.com> Date: Sun, 5 Nov 2023 16:15:39 -0800 Subject: [PATCH 15/23] Update user_test.ts --- src/api/supabase/queries/tests/user_test.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/api/supabase/queries/tests/user_test.ts b/src/api/supabase/queries/tests/user_test.ts index eca7395d..190c3ff0 100644 --- a/src/api/supabase/queries/tests/user_test.ts +++ b/src/api/supabase/queries/tests/user_test.ts @@ -5,8 +5,7 @@ import { fetchUserData, } from '../user_queries'; - -export async function runFetchUserData() { +async default function runFetchUserData() { const result = await fetchUserData(); From 25ecea32ab06abccb7091251e92d4339108c436d Mon Sep 17 00:00:00 2001 From: kevinjcai <75354434+kevinjcai@users.noreply.github.com> Date: Sun, 5 Nov 2023 16:18:16 -0800 Subject: [PATCH 16/23] Update user_test.ts --- src/api/supabase/queries/tests/user_test.ts | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/api/supabase/queries/tests/user_test.ts b/src/api/supabase/queries/tests/user_test.ts index 190c3ff0..091d1fd1 100644 --- a/src/api/supabase/queries/tests/user_test.ts +++ b/src/api/supabase/queries/tests/user_test.ts @@ -1,12 +1,6 @@ -/* eslint-disable no-console */ -// -import { - fetchUserData, - -} from '../user_queries'; async default function runFetchUserData() { - const result = await fetchUserData(); + const result = 5 } From f02f4db91b7c430fe90c30d7c49d45acb9999527 Mon Sep 17 00:00:00 2001 From: kevinjcai <75354434+kevinjcai@users.noreply.github.com> Date: Sun, 5 Nov 2023 16:48:40 -0800 Subject: [PATCH 17/23] Update user_test.ts --- src/api/supabase/queries/tests/user_test.ts | 49 +++++++++++++++++++-- 1 file changed, 45 insertions(+), 4 deletions(-) diff --git a/src/api/supabase/queries/tests/user_test.ts b/src/api/supabase/queries/tests/user_test.ts index 091d1fd1..0384aa77 100644 --- a/src/api/supabase/queries/tests/user_test.ts +++ b/src/api/supabase/queries/tests/user_test.ts @@ -1,6 +1,47 @@ -async default function runFetchUserData() { - - const result = 5 - +/* eslint-disable no-console */ +// + +import { + fetchUserData, + fetchUserByUUID, + fetchFavoriteItems, + addToFavorites, + removeFromFavorites, +} from '../user_queries'; + +export async function runFetchUserData() { + try { + const result = await fetchUserData(); + console.log('fetchUserData Result:', result); + } catch (error) { + console.error('Error in fetchUserData:', error); + } +} + +export async function runFetchUserByUUID() { + const testUUID = 'aeaf5f6c-a8bc-41b8-9850-5fb11e1b6dea'; + try { + const result = await fetchUserByUUID(testUUID); + console.log('fetchUserByUUID Result:', result); + } catch (error) { + console.error('Error in fetchUserByUUID:', error); + } +} + +export async function fullFavItemTest() { + const testUserId = '4a934844-76fa-4a1a-80d7-fa00597398e1'; + const testItemId = '10'; + try { + const result = await fetchUserByUUID(testUserId); + console.log('fetchUserData Result:', result); + addToFavorites(testUserId, testItemId); + let result1 = await fetchFavoriteItems(testUserId); + console.log('fetchFavoriteItems Result:', result1); + removeFromFavorites(testUserId, testItemId); + result1 = await fetchFavoriteItems(testUserId); + console.log('fetchFavoriteItems Result:', result1); + } catch (error) { + console.error('Error in incrementCartItemByOne:', error); + } } From cc14ef95cef94e7dcf7612e6ed3a64a21e0d185b Mon Sep 17 00:00:00 2001 From: Kevin Cai Date: Sun, 5 Nov 2023 20:54:16 -0800 Subject: [PATCH 18/23] Done --- src/api/supabase/queries/tests/user_test.ts | 1 - src/api/supabase/queries/user_queries.ts | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/api/supabase/queries/tests/user_test.ts b/src/api/supabase/queries/tests/user_test.ts index 0384aa77..07532473 100644 --- a/src/api/supabase/queries/tests/user_test.ts +++ b/src/api/supabase/queries/tests/user_test.ts @@ -1,4 +1,3 @@ - /* eslint-disable no-console */ // diff --git a/src/api/supabase/queries/user_queries.ts b/src/api/supabase/queries/user_queries.ts index e144a212..5df9b46c 100644 --- a/src/api/supabase/queries/user_queries.ts +++ b/src/api/supabase/queries/user_queries.ts @@ -89,7 +89,7 @@ export async function incrementCartItem( } else if (!data) { throw new Error('No user found with the specified user_id.'); } - + const currentCart = data.cart; // Increment the item's quantity by n or set it to n if not present From 5d64a2813fa494cd2d69bf21c352777bb5e1554d Mon Sep 17 00:00:00 2001 From: Kevin Cai Date: Sun, 5 Nov 2023 20:59:04 -0800 Subject: [PATCH 19/23] Done --- src/api/supabase/queries/tests/user_test.ts | 46 --------------------- 1 file changed, 46 deletions(-) delete mode 100644 src/api/supabase/queries/tests/user_test.ts diff --git a/src/api/supabase/queries/tests/user_test.ts b/src/api/supabase/queries/tests/user_test.ts deleted file mode 100644 index 07532473..00000000 --- a/src/api/supabase/queries/tests/user_test.ts +++ /dev/null @@ -1,46 +0,0 @@ -/* eslint-disable no-console */ -// - -import { - fetchUserData, - fetchUserByUUID, - fetchFavoriteItems, - addToFavorites, - removeFromFavorites, -} from '../user_queries'; - -export async function runFetchUserData() { - try { - const result = await fetchUserData(); - console.log('fetchUserData Result:', result); - } catch (error) { - console.error('Error in fetchUserData:', error); - } -} - -export async function runFetchUserByUUID() { - const testUUID = 'aeaf5f6c-a8bc-41b8-9850-5fb11e1b6dea'; - try { - const result = await fetchUserByUUID(testUUID); - console.log('fetchUserByUUID Result:', result); - } catch (error) { - console.error('Error in fetchUserByUUID:', error); - } -} - -export async function fullFavItemTest() { - const testUserId = '4a934844-76fa-4a1a-80d7-fa00597398e1'; - const testItemId = '10'; - try { - const result = await fetchUserByUUID(testUserId); - console.log('fetchUserData Result:', result); - addToFavorites(testUserId, testItemId); - let result1 = await fetchFavoriteItems(testUserId); - console.log('fetchFavoriteItems Result:', result1); - removeFromFavorites(testUserId, testItemId); - result1 = await fetchFavoriteItems(testUserId); - console.log('fetchFavoriteItems Result:', result1); - } catch (error) { - console.error('Error in incrementCartItemByOne:', error); - } -} From 032bebac221c5bad35adabd6ec5d9c55e91ae707 Mon Sep 17 00:00:00 2001 From: Kevin Cai Date: Sun, 5 Nov 2023 20:59:29 -0800 Subject: [PATCH 20/23] Done --- src/app/page.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/src/app/page.tsx b/src/app/page.tsx index 11302e15..a6fd7d4d 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -4,7 +4,6 @@ import React, { useEffect } from 'react'; import Link from 'next/link'; -import { fullCartTest } from '../api/supabase/queries/tests/user_test'; import { testFetchOrderByUUID, testFetchOrders, From 42c37e7edfbf062d6d9a372afe9f71937bb32993 Mon Sep 17 00:00:00 2001 From: Kevin Cai Date: Wed, 8 Nov 2023 15:25:40 -0800 Subject: [PATCH 21/23] Comments Resolve Auth needed --- src/api/supabase/queries/tests/user_test.ts | 20 +++ src/api/supabase/queries/user_queries.ts | 135 +++----------------- src/app/page.tsx | 28 +--- src/schema/schema.ts | 2 +- 4 files changed, 40 insertions(+), 145 deletions(-) create mode 100644 src/api/supabase/queries/tests/user_test.ts diff --git a/src/api/supabase/queries/tests/user_test.ts b/src/api/supabase/queries/tests/user_test.ts new file mode 100644 index 00000000..6b5156dc --- /dev/null +++ b/src/api/supabase/queries/tests/user_test.ts @@ -0,0 +1,20 @@ +/* eslint-disable import/prefer-default-export */ +/* eslint-disable no-console */ +// + +import { + fetchUserByUUID, +} from '../user_queries'; + + + +export async function testFetchUserByUUID() { + const uuid = '3b4a1317-b9ea-4cbd-95d7-e959aa80d1ea'; // Replace with a valid user ID + try { + const result = await fetchUserByUUID(); + console.log('Fetch User by UUID Result:', result); + } catch (error) { + console.error('Test Fetch User by UUID Error:', error); + } +} + diff --git a/src/api/supabase/queries/user_queries.ts b/src/api/supabase/queries/user_queries.ts index 5df9b46c..dfbbe77a 100644 --- a/src/api/supabase/queries/user_queries.ts +++ b/src/api/supabase/queries/user_queries.ts @@ -1,10 +1,4 @@ -/* eslint-disable @typescript-eslint/no-unused-vars */ -/* eslint-disable no-console */ -// - import { - PostgrestSingleResponse, - PostgrestError, createClient, } from '@supabase/supabase-js'; import { User } from '../../../schema/schema'; @@ -16,52 +10,25 @@ const supabaseApiKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY; // Initialize the Supabase client const supabase = createClient(supabaseUrl ?? '', supabaseApiKey ?? ''); -export async function fetchUserData() { - const { data, error }: { data: User[] | null; error: PostgrestError | null } = - await supabase.from('profiles').select('*'); - - if (error) { - throw new Error(`An error occured when trying to read profiles: ${error}`); - } else { - return data; - } -} -export async function fetchUserByUUID(uuid: string) { - const { data, error } = await supabase - .from('profiles') - .select('*') - .eq('user_id', uuid) - .single(); - if (error) { - throw new Error(`An error occured when trying to read profiles: ${error}`); - } else { - return data; +export async function fetchUserByUUID() { + const val = (await supabase.auth.getUser()).data.user?.user_metadata; + if (val) { + return val; } + throw new Error('User not found'); } -export async function fetchUserCart( - userId: string, -): Promise> { - const { data, error }: { data: User | null; error: PostgrestError | null } = - await supabase.from('profiles').select('*').eq('user_id', userId).single(); - - if (error) { - throw new Error( - `An error occurred when trying to fetch the cart: ${error.message}`, - ); - } else if (!data) { - throw new Error('No user found with the specified user_id.'); - } - +export async function fetchUserCart(): Promise> { + const data = await fetchUserByUUID(); return data.cart; } export async function updateCart( - userId: string, + currentCart: Record, ) { - const { data, error } = await supabase + const { error } = await supabase .from('profiles') .update({ cart: currentCart }) .eq('user_id', userId); @@ -74,55 +41,26 @@ export async function updateCart( } export async function incrementCartItem( - userId: string, + itemId: string, n: number, ) { - // First, fetch the current cart for the user - const { data, error }: { data: User | null; error: PostgrestError | null } = - await supabase.from('profiles').select('*').eq('user_id', userId).single(); - - if (error) { - throw new Error( - `An error occurred when trying to fetch the cart: ${error.message}`, - ); - } else if (!data) { - throw new Error('No user found with the specified user_id.'); - } - const currentCart = data.cart; + const currentCart = await fetchUserCart(userId); - // Increment the item's quantity by n or set it to n if not present currentCart[itemId] = (currentCart[itemId] || 0) + n; - // Use the updateCart function to update the cart in the database await updateCart(userId, currentCart); } -export async function incrementCartItemByOne(userId: string, itemId: string) { - return incrementCartItem(userId, itemId, 1); -} - export async function decrementCartItem( - userId: string, + itemId: string, n: number, ) { - // First, fetch the current cart for the user - const { data, error }: { data: User[] | null; error: PostgrestError | null } = - await supabase.from('profiles').select('*').eq('user_id', userId); - if (error) { - throw new Error( - `An error occurred when trying to fetch the cart: ${error.message}`, - ); - } else if (!data || data.length === 0) { - throw new Error('No user found with the specified user_id.'); - } + const currentCart = await fetchUserCart(userId); - const currentCart = data[0].cart; - - // Decrement the item's quantity by n or remove it if it's 0 or below if (currentCart[itemId]) { currentCart[itemId] -= n; @@ -131,51 +69,14 @@ export async function decrementCartItem( } } - // Use the updateCart function to update the cart in the database await updateCart(userId, currentCart); } +export async function incrementCartItemByOne(userId: string, itemId: string) { + await incrementCartItem(userId, itemId, 1); +} + export async function decrementCartItemByOne(userId: string, itemId: string) { - return decrementCartItem(userId, itemId, 1); + await decrementCartItem(userId, itemId, 1); } -// export async function addUserAddress( -// uuid: string, -// newStreet: string, -// newCity: string, -// newZipcode: string, -// ): Promise> { -// try { -// const { data: existingUser, error: selectError } = await supabase -// .from('profiles') -// .select('street, city, zipcode') -// .eq('user_id', uuid) -// .single(); - -// if (selectError) { -// throw selectError; -// } - -// // Append new values to the arrays -// const updatedStreet = [...(existingUser?.street || []), newStreet]; -// const updatedCity = [...(existingUser?.city || []), newCity]; -// const updatedZipcode = [...(existingUser?.zipcode || []), newZipcode]; - -// const { data, error } = await supabase -// .from('profiles') -// .update({ -// street: updatedStreet, -// city: updatedCity, -// zipcode: updatedZipcode, -// }) -// .eq('user_id', uuid) -// .single(); - -// if (error) { -// throw error; -// } - -// return { data, error: null, status: 200, statusText: 'OK', count: 1 }; -// } catch (error) { -// } -// } diff --git a/src/app/page.tsx b/src/app/page.tsx index a6fd7d4d..69e87373 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -4,34 +4,8 @@ import React, { useEffect } from 'react'; import Link from 'next/link'; -import { - testFetchOrderByUUID, - testFetchOrders, - testGetOrderById, - testToggleOrderProgress, - // eslint-disable-next-line @typescript-eslint/no-unused-vars - testUpdateAllOrdersProgressToTrue, -} from '../api/supabase/queries/tests/order_test'; -import { - testFetchProducts, - testFetchProductByName, -} from '../api/supabase/queries/tests/product_test'; -import { - testFetchPickupData, - testFetchPickupTimesByUUID, -} from '../api/supabase/queries/tests/pickup_test'; -export const revalidate = 10; - -export default async function Checkout() { - // useEffect(() => { - // async function testEverything() { - // await fullCartTest(); - // } - // testEverything(); - // }); - - // await fullCartTest(); +export default function Home() { return (
Login diff --git a/src/schema/schema.ts b/src/schema/schema.ts index d2d57b36..f7a64292 100644 --- a/src/schema/schema.ts +++ b/src/schema/schema.ts @@ -6,7 +6,7 @@ export type User = { last_name: string; pet_information: string; order_option: boolean; - creaed_at: string; // timestamp with time zone not null default now(); + created_at: string; // timestamp with time zone not null default now(); address_id: string; // UUID cart: Record; // JSONB with item as key and quantity as value }; From c9e00f3415de49401cb4a719e7b946969f5e970f Mon Sep 17 00:00:00 2001 From: Kevin Cai Date: Wed, 8 Nov 2023 15:53:54 -0800 Subject: [PATCH 22/23] prettier --- src/api/supabase/queries/tests/user_test.ts | 7 +-- src/api/supabase/queries/user_queries.ts | 47 ++++++--------------- src/app/page.tsx | 3 +- src/schema/schema.ts | 3 +- 4 files changed, 18 insertions(+), 42 deletions(-) diff --git a/src/api/supabase/queries/tests/user_test.ts b/src/api/supabase/queries/tests/user_test.ts index 2ac6a181..88eb256a 100644 --- a/src/api/supabase/queries/tests/user_test.ts +++ b/src/api/supabase/queries/tests/user_test.ts @@ -2,11 +2,7 @@ /* eslint-disable no-console */ // -import { - fetchUserByUUID, -} from '../user_queries'; - - +import { fetchUserByUUID } from '../user_queries'; export async function runFetchUserByUUID() { const testUUID = 'aeaf5f6c-a8bc-41b8-9850-5fb11e1b6dea'; @@ -17,4 +13,3 @@ export async function runFetchUserByUUID() { console.error('Error in fetchUserByUUID:', error); } } - diff --git a/src/api/supabase/queries/user_queries.ts b/src/api/supabase/queries/user_queries.ts index 69a7938b..430056e0 100644 --- a/src/api/supabase/queries/user_queries.ts +++ b/src/api/supabase/queries/user_queries.ts @@ -1,6 +1,4 @@ -import { - createClient, -} from '@supabase/supabase-js'; +import { createClient } from '@supabase/supabase-js'; import { User } from '../../../schema/schema'; // Replace these with your Supabase project URL and API key @@ -10,13 +8,12 @@ const supabaseApiKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY; // Initialize the Supabase client const supabase = createClient(supabaseUrl ?? '', supabaseApiKey ?? ''); - -export async function fetchUserByUUID() { - const val = (await supabase.auth.getUser()).data.user?.user_metadata; - if (val) { - return val; +export async function fetchUserByUUID(): Promise { + const { data, error } = await supabase.auth.getUser(); + if (error) { + throw new Error(`Error fetching user: ${error.message}`); } - throw new Error('User not found'); + return data.user.user_metadata as User; } export async function fetchUserCart(): Promise> { @@ -24,15 +21,10 @@ export async function fetchUserCart(): Promise> { return data.cart; } -export async function updateCart( - - currentCart: Record, -) { - - const { error } = await supabase - .from('profiles') - .update({ cart: currentCart }) - .eq('user_id', userId); +export async function updateCart(currentCart: Record) { + const { error } = await supabase.auth.updateUser({ + data: { cart: currentCart }, + }); if (error) { throw new Error( @@ -41,25 +33,15 @@ export async function updateCart( } } -export async function incrementCartItem( - - itemId: string, - n: number, -) { - - const currentCart = await fetchUserCart(userId); +export async function incrementCartItem(itemId: string, n: number) { + const currentCart = await fetchUserCart(); currentCart[itemId] = (currentCart[itemId] || 0) + n; - await updateCart(userId, currentCart); + await updateCart(currentCart); } -export async function decrementCartItem( - - itemId: string, - n: number, -) { - +export async function decrementCartItem(itemId: string, n: number) { const currentCart = await fetchUserCart(); if (currentCart[itemId]) { @@ -80,4 +62,3 @@ export async function incrementCartItemByOne(userId: string, itemId: string) { export async function decrementCartItemByOne(userId: string, itemId: string) { await decrementCartItem(itemId, 1); } - diff --git a/src/app/page.tsx b/src/app/page.tsx index 417f07fb..fdb0284a 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -22,11 +22,10 @@ import { testFetchPickupTimesByUUID, } from '../api/supabase/queries/tests/pickup_test'; - export default function Home() { return (
Login
); -} \ No newline at end of file +} diff --git a/src/schema/schema.ts b/src/schema/schema.ts index f7a64292..9acb06ad 100644 --- a/src/schema/schema.ts +++ b/src/schema/schema.ts @@ -5,10 +5,11 @@ export type User = { first_name: string; last_name: string; pet_information: string; - order_option: boolean; + delivery_allowed: boolean; created_at: string; // timestamp with time zone not null default now(); address_id: string; // UUID cart: Record; // JSONB with item as key and quantity as value + fav_items: Record; // JSONB with item as key and quantity as value }; export type Order = { From 32be4bc5dbc783b1777ae749460c8cbd140c4cbe Mon Sep 17 00:00:00 2001 From: Kevin Cai Date: Wed, 8 Nov 2023 15:59:18 -0800 Subject: [PATCH 23/23] compiler error --- src/api/supabase/queries/user_queries.ts | 1 + src/app/checkout/page.tsx | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/api/supabase/queries/user_queries.ts b/src/api/supabase/queries/user_queries.ts index 430056e0..b849ce8c 100644 --- a/src/api/supabase/queries/user_queries.ts +++ b/src/api/supabase/queries/user_queries.ts @@ -13,6 +13,7 @@ export async function fetchUserByUUID(): Promise { if (error) { throw new Error(`Error fetching user: ${error.message}`); } + return data.user.user_metadata as User; } diff --git a/src/app/checkout/page.tsx b/src/app/checkout/page.tsx index 4ad3c627..8ab193e3 100644 --- a/src/app/checkout/page.tsx +++ b/src/app/checkout/page.tsx @@ -23,7 +23,7 @@ export default function Checkout() { ) return; - const data = await fetchUserByUUID(sessionData.session.user.id as string); + const data = await fetchUserByUUID(); setDeliveryEnabled(data.delivery_allowed); })(); }, []);