Skip to content

Commit

Permalink
Done NEed to run checkers
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinjcai committed Nov 2, 2023
1 parent d317c3d commit a0841a2
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 60 deletions.
44 changes: 26 additions & 18 deletions src/api/supabase/queries/tests/user_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,46 @@
import {
fetchUserData,
fetchUserByUUID,
addUserAddress,
fetchFavoriteItems,
addToFavorites,
removeFromFavorites,
} 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 fullFavItemTest() {
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);
addToFavorites(testUserId, testItemId);
let result_1 = await fetchFavoriteItems(testUserId);
console.log('fetchFavoriteItems Result:', result_1);
removeFromFavorites(testUserId, testItemId);
result_1 = await fetchFavoriteItems(testUserId);
console.log('fetchFavoriteItems Result:', result_1);

} catch (error) {
console.error('Test Add User Address Error:', error);
console.error('Error in incrementCartItemByOne:', error);
}
}
}
105 changes: 67 additions & 38 deletions src/api/supabase/queries/user_queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export async function fetchUserData(): Promise<
PostgrestSingleResponse<User[]> | { data: never[]; error: PostgrestError }
> {
try {
const { data: users, error } = await supabase.from('users').select('*');
const { data: users, error } = await supabase.from('profiles').select('*');

if (error) {
console.error('Error fetching data:', error);
Expand All @@ -39,7 +39,7 @@ export async function fetchUserByUUID(
): Promise<PostgrestSingleResponse<unknown>> {
try {
const { data: user, error } = await supabase
.from('Users')
.from('profiles')
.select('*')
.eq('user_id', uuid)
.single();
Expand All @@ -55,47 +55,76 @@ export async function fetchUserByUUID(
}
}

export async function addUserAddress(
uuid: string,
newStreet: string,
newCity: string,
newZipcode: string,
): Promise<PostgrestSingleResponse<unknown>> {
try {
const { data: existingUser, error: selectError } = await supabase
.from('Users')
.select('street, city, zipcode')
.eq('user_id', uuid)
export async function fetchFavoriteItems(userId: string): Promise<Record<string, number>> {
// Fetch fav_items for the specified user
const { data, error } = await supabase
.from('profiles')
.select('fav_items')
.eq('user_id', userId)
.single();

if (selectError) {
console.error('Error selecting user data:', selectError);
throw selectError;
}
if (error) {
throw new Error(`An error occurred when trying to fetch favorite items: ${error.message}`);
} else if (!data) {
throw new Error("No user found with the specified user_id.");
}

// 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)
return data.fav_items;
}

export async function addToFavorites(userId: string, productId: string) {
// First, fetch the current fav_items for the user
const { data, error } = await supabase
.from('profiles')
.select('fav_items')
.eq('user_id', userId)
.single();

if (error) {
console.error('Error updating user data:', error);
throw error;
}
if (error) {
throw new Error(`An error occurred when trying to fetch the user's favorite items: ${error.message}`);
}

return { data, error: null, status: 200, statusText: 'OK', count: 1 };
} catch (error) {
console.error('Error:', error);
throw error;
const currentFavItems = data?.fav_items || {};

// Add the product to fav_items or update its quantity
currentFavItems[productId] = (currentFavItems[productId] || 0);

// Now update the user's fav_items in the database
const updateResponse = await supabase
.from('profiles')
.update({ fav_items: currentFavItems })
.eq('user_id', userId);

if (updateResponse.error) {
throw new Error(`An error occurred when trying to update the user's favorite items: ${updateResponse.error.message}`);
}
}

// Function to remove a product from fav_items
export async function removeFromFavorites(userId: string, productId: string) {
// First, fetch the current fav_items for the user
const { data, error } = await supabase
.from('profiles')
.select('fav_items')
.eq('user_id', userId)
.single();

if (error) {
throw new Error(`An error occurred when trying to fetch the user's favorite items: ${error.message}`);
}

const currentFavItems = data?.fav_items || {};

// Remove the product from fav_items
delete currentFavItems[productId];
console.log(currentFavItems);
// Now update the user's fav_items in the database
const updateResponse = await supabase
.from('profiles')
.update({ fav_items: currentFavItems })
.eq('user_id', userId);

if (updateResponse.error) {
throw new Error(`An error occurred when trying to update the user's favorite items: ${updateResponse.error.message}`);
}
}
17 changes: 13 additions & 4 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
/* eslint-disable @typescript-eslint/no-unused-vars */

'use client';

import React, { useEffect } from 'react';

import Link from 'next/link';
import {
testFetchUserData,
testFetchUserByUUID,
testAddUserAddress,
fullFavItemTest
} from '../api/supabase/queries/tests/user_test';
import {
testFetchOrderByUUID,
Expand All @@ -24,7 +26,7 @@ import {
} from '../api/supabase/queries/tests/pickup_test';

export default function Checkout() {
testFetchUserData();
// testFetchUserData();
// testFetchUserByUUID();
// testAddUserAddress();
// testFetchOrderByUUID();
Expand All @@ -36,8 +38,15 @@ export default function Checkout() {
// testFetchPickupData();
// testFetchPickupTimesByUUID();
// testUpdateAllOrdersProgressToTrue();
useEffect(() => {
async function testEverything() {
await fullFavItemTest();
}
testEverything();
});

return (

<main>
<Link href="/login">Login</Link>
</main>
Expand Down

0 comments on commit a0841a2

Please sign in to comment.