From 88ceb24a636b223010f7dee6ffc9ec789f783217 Mon Sep 17 00:00:00 2001 From: Yee Kit Date: Sun, 19 May 2024 17:51:24 +0800 Subject: [PATCH 1/2] Updated to new table name --- .../collections-requests/approve/route.ts | 28 ++++---- .../collections-requests/reject/route.ts | 18 ++--- .../api/admin/collections-requests/route.ts | 7 +- frontend/app/api/admin/collections/route.ts | 41 +++++++++++ frontend/app/api/profile/route.ts | 8 +-- .../api/user/collections-requests/route.ts | 68 +++++++++---------- frontend/app/api/user/collections/route.ts | 4 +- .../ui/admin/admin-manage-collections.tsx | 14 ++-- .../app/components/ui/query/query-manage.tsx | 36 +++++----- 9 files changed, 134 insertions(+), 90 deletions(-) diff --git a/frontend/app/api/admin/collections-requests/approve/route.ts b/frontend/app/api/admin/collections-requests/approve/route.ts index 81081cb..cbf5724 100644 --- a/frontend/app/api/admin/collections-requests/approve/route.ts +++ b/frontend/app/api/admin/collections-requests/approve/route.ts @@ -1,7 +1,7 @@ import { createClient } from '@supabase/supabase-js'; import { NextRequest, NextResponse } from "next/server"; -// POST request to approve the user's public collections request status in the database (Used by admin) +// POST request to approve the user's collections request status in the database (Used by admin) export async function POST(request: NextRequest) { // Create a new Supabase client const supabase = createClient( @@ -13,31 +13,31 @@ export async function POST(request: NextRequest) { // Retrieve the collection_id from the request body const { collection_id, is_make_public } = await request?.json(); - // Update the user's public collections request data in the database, set is_pending = false - const { data: updatedUserPubCollectionsReq, error: updatedUserPubCollReqErr } = await supabase - .from('public_collections_requests') + // Update the user's collections request data in the database, set is_pending = false + const { data: updatedUserCollectionsReq, error: updatedUserCollReqErr } = await supabase + .from('collections_requests') .update({ is_pending: false, is_approved: true }) .eq('collection_id', collection_id); - if (updatedUserPubCollReqErr) { - console.error('Error updating user public collections request data in database:', updatedUserPubCollReqErr.message); - return NextResponse.json({ error: updatedUserPubCollReqErr.message }, { status: 500 }); + if (updatedUserCollReqErr) { + console.error('Error updating user collections request data in database:', updatedUserCollReqErr.message); + return NextResponse.json({ error: updatedUserCollReqErr.message }, { status: 500 }); } // Update the user's collections data in the database, set is_public = true console.log('is_public:', is_make_public); - const { data: updatedUserPubCollections, error: updatedUserPubCollErr } = await supabase + const { data: updatedUserCollections, error: updatedUserCollErr } = await supabase .from('collections') .update({ is_public: is_make_public}) .eq('collection_id', collection_id); - if (updatedUserPubCollErr) { - console.error('Error updating user public collections data in database:', updatedUserPubCollErr.message); - return NextResponse.json({ error: updatedUserPubCollErr.message }, { status: 500 }); + if (updatedUserCollErr) { + console.error('Error updating user public collections data in database:', updatedUserCollErr.message); + return NextResponse.json({ error: updatedUserCollErr.message }, { status: 500 }); } - // console.log('Admin: User Public Collections Requests:', userPubCollectionsReq); - // console.log('Admin: User Public Collections:', userPubCollections); + // console.log('Admin: Approved User Collections Request:', userPubCollectionsReq); + // console.log('Admin: Updated User Collections:', userPubCollections); - return NextResponse.json({ updatedUserPubCollectionsReq, updatedUserPubCollections }); + return NextResponse.json({ updatedUserCollectionsReq, updatedUserCollections }); } diff --git a/frontend/app/api/admin/collections-requests/reject/route.ts b/frontend/app/api/admin/collections-requests/reject/route.ts index 14b25e5..c6186c0 100644 --- a/frontend/app/api/admin/collections-requests/reject/route.ts +++ b/frontend/app/api/admin/collections-requests/reject/route.ts @@ -1,7 +1,7 @@ import { createClient } from '@supabase/supabase-js'; import { NextRequest, NextResponse } from "next/server"; -// POST request to reject the user's public collections request status in the database (Used by admin) +// POST request to reject the user's collections request status in the database (Used by admin) export async function POST(request: NextRequest) { // Create a new Supabase client const supabase = createClient( @@ -13,18 +13,18 @@ export async function POST(request: NextRequest) { // Retrieve the collection_id from the request body const { collection_id } = await request?.json(); - // Update the user's public collections request data in the database, set is_pending = false - const { data: updatedUserPubCollectionsReq, error: updatedUserPubCollErr } = await supabase - .from('public_collections_requests') + // Update the user's collections request data in the database, set is_pending = false + const { data: updatedUserCollectionsReq, error: updatedUserCollErr } = await supabase + .from('collections_requests') .update({ is_pending: false, is_approved: false}) .eq('collection_id', collection_id); - if (updatedUserPubCollErr) { - console.error('Error updating user public collections request data in database:', updatedUserPubCollErr.message); - return NextResponse.json({ error: updatedUserPubCollErr.message }, { status: 500 }); + if (updatedUserCollErr) { + console.error('Error updating user collections request data in database:', updatedUserCollErr.message); + return NextResponse.json({ error: updatedUserCollErr.message }, { status: 500 }); } - // console.log('User Public Collections Requests:', userPubCollectionsReq); + // console.log('Admin: Reject User Collections Requests:', updatedUserCollectionsReq); - return NextResponse.json({ updatedUserPubCollectionsReq }); + return NextResponse.json({ updatedUserCollectionsReq }); } diff --git a/frontend/app/api/admin/collections-requests/route.ts b/frontend/app/api/admin/collections-requests/route.ts index 36e001d..2e16412 100644 --- a/frontend/app/api/admin/collections-requests/route.ts +++ b/frontend/app/api/admin/collections-requests/route.ts @@ -1,6 +1,7 @@ import { createClient } from '@supabase/supabase-js'; import { NextRequest, NextResponse } from "next/server"; +// GET request to retrieve the collections requests data from the database export async function GET(request: NextRequest) { // Create a new Supabase client const supabase = createClient( @@ -11,16 +12,16 @@ export async function GET(request: NextRequest) { // Retrieve the public collections requests data from the database const { data: pubCollectionsReq, error: pubCollErr } = await supabase - .from('public_collections_requests') + .from('collections_requests') .select('collection_id, is_make_public, is_pending, is_approved, created_at, updated_at, collections (collection_id, id, display_name, description, is_public, users (id, name, email))') .eq('is_pending', true); if (pubCollErr) { - console.error('Error fetching public collection request data from database:', pubCollErr.message); + console.error('Error fetching collection request data from database:', pubCollErr.message); return NextResponse.json({ error: pubCollErr.message }, { status: 500 }); } - console.log('Collections Request:', pubCollectionsReq); + // console.log('Collections Request:', pubCollectionsReq); return NextResponse.json({ pubCollectionsReq: pubCollectionsReq }); } diff --git a/frontend/app/api/admin/collections/route.ts b/frontend/app/api/admin/collections/route.ts index 8060db7..5926cc8 100644 --- a/frontend/app/api/admin/collections/route.ts +++ b/frontend/app/api/admin/collections/route.ts @@ -24,3 +24,44 @@ export async function GET(request: NextRequest) { return NextResponse.json({ collections: collections }); } + +// PUT request to update the collection data in the database +export async function PUT(request: NextRequest) { + // Create a new Supabase client + const supabase = createClient( + process.env.SUPABASE_URL ?? '', + process.env.SUPABASE_SERVICE_ROLE_KEY ?? '', + { db: { schema: 'public' } }, + ); + + // Retrieve the collection ID from the request body + const { collection_id, is_public } = await request.json(); + + // Update the collection data in the database + const { data, error } = await supabase + .from('collections') + .update({ is_public: is_public }) + .match({ collection_id }); + + if (error) { + console.error('Error updating collection data in database:', error.message); + return NextResponse.json({ error: error.message }, { status: 500 }); + } + + // console.log('Updated collection:', data); + + // Delete the collection requests data in the database (Since it is manually updated by Admin) + const { data: delData, error: delError } = await supabase + .from('collection_requests') + .delete() + .match({ collection_id }); + + if (delError) { + console.error('Error deleting collection requests data in database:', delError.message); + return NextResponse.json({ error: delError.message }, { status: 500 }); + } + + // console.log('Deleted collection requests:', delData); + + return NextResponse.json({ message: 'Collection updated successfully' }); +} \ No newline at end of file diff --git a/frontend/app/api/profile/route.ts b/frontend/app/api/profile/route.ts index da11d40..2119849 100644 --- a/frontend/app/api/profile/route.ts +++ b/frontend/app/api/profile/route.ts @@ -127,10 +127,10 @@ export async function DELETE(request: NextRequest) { // Convert collectionIds to an array of strings with only the collection_id values const collectionIdsArray = collectionIds.map((collection: any) => collection.collection_id); - console.log('collectionIdsArray:', collectionIdsArray); + // console.log('collectionIdsArray:', collectionIdsArray); // Log the request body before sending the POST request - console.log('Request Body:', JSON.stringify({ collection_ids: collectionIdsArray })); + // console.log('Request Body:', JSON.stringify({ collection_ids: collectionIdsArray })); // Delete the user's collection data from vecs schema via POST request to Backend API const deleteVecsResponse = await fetch(`${process.env.DELETE_MULTI_COLLECTION_API}`, { @@ -148,7 +148,7 @@ export async function DELETE(request: NextRequest) { return NextResponse.json({ error: deleteVecsResponse.statusText }, { status: deleteVecsResponse.status }); } - // Delete the user's profile data from users table in next_auth schema (and all related data via cascaded delete for tables in both publicand next_auth schema) + // Delete the user's profile data from users table in next_auth schema (and all related data via cascaded delete for tables in both public and next_auth schema) const { data: deletedUserData, error: deleteError } = await supabaseAuth .from('users') .delete() @@ -161,7 +161,7 @@ export async function DELETE(request: NextRequest) { return NextResponse.json({ error: deleteError.message }, { status: 500 }); } - console.log('deletedUserData:', deletedUserData, 'collectionIds:', collectionIdsArray, 'deleteVecsResponse:', deleteVecsResponse); + // console.log('deletedUserData:', deletedUserData, 'collectionIds:', collectionIdsArray, 'deleteVecsResponse:', deleteVecsResponse); return NextResponse.json({ deletedUserData }); } diff --git a/frontend/app/api/user/collections-requests/route.ts b/frontend/app/api/user/collections-requests/route.ts index f41dac1..5b17f04 100644 --- a/frontend/app/api/user/collections-requests/route.ts +++ b/frontend/app/api/user/collections-requests/route.ts @@ -1,7 +1,7 @@ import { createClient } from '@supabase/supabase-js'; import { NextRequest, NextResponse } from "next/server"; -// GET request to retrieve the user's public collections requests data from the database +// GET request to retrieve the user's collections requests data from the database export async function GET(request: NextRequest) { // Retrieve the session token from the request cookies const session = request.cookies.get('next-auth.session-token') || request.cookies.get('__Secure-next-auth.session-token'); @@ -33,23 +33,23 @@ export async function GET(request: NextRequest) { return NextResponse.json({ error: sessionError.message }, { status: 500 }); } - // Retrieve the user's collections and public collections requests data via inner join from the database - const { data: userPubCollectionsReq, error: userPubCollErr } = await supabase + // Retrieve the user's collections and collections requests data via inner join from the database + const { data: userCollectionsReq, error: userCollErr } = await supabase .from('collections') - .select('collection_id, display_name, description, is_public, created_at, public_collections_requests (collection_id, is_make_public, is_pending, is_approved, created_at, updated_at)') + .select('collection_id, display_name, description, is_public, created_at, collections_requests (collection_id, is_make_public, is_pending, is_approved, created_at, updated_at)') .eq('id', userId); - if (userPubCollErr) { - console.error('Error fetching user public collections requests data from database:', userPubCollErr.message); - return NextResponse.json({ error: userPubCollErr.message }, { status: 500 }); + if (userCollErr) { + console.error('Error fetching user collections requests data from database:', userCollErr.message); + return NextResponse.json({ error: userCollErr.message }, { status: 500 }); } - // console.log('User Public Collections Requests:', userPubCollectionsReq.map(item => item.public_collections_requests)); + // console.log('User Collections Requests:', userCollectionsReq); - return NextResponse.json({ userPubCollectionsReq: userPubCollectionsReq }); + return NextResponse.json({ userCollectionsReq: userCollectionsReq }); } -// POST request to insert the user's public collections request data into the database if not exist (Used by user) +// POST request to insert the user's collections request data into the database if not exist (Used by user) export async function POST(request: NextRequest) { // Create a new Supabase client const supabase = createClient( @@ -62,21 +62,21 @@ export async function POST(request: NextRequest) { const { collection_id, is_make_public } = await request?.json(); // Insert the user's public collections request data into the database - const { data: newUserPubCollectionsReq, error: newUserPubCollErr } = await supabase - .from('public_collections_requests') + const { data: newUserCollectionsReq, error: newUserCollErr } = await supabase + .from('collections_requests') .insert([{ collection_id, is_make_public }]); - if (newUserPubCollErr) { - console.error('Error inserting user public collections request data into database:', newUserPubCollErr.message); - return NextResponse.json({ error: newUserPubCollErr.message }, { status: 500 }); + if (newUserCollErr) { + console.error('Error inserting user collections request data into database:', newUserCollErr.message); + return NextResponse.json({ error: newUserCollErr.message }, { status: 500 }); } - // console.log('User Public Collections Requests:', userPubCollectionsReq); + // console.log('Insert User Collections Requests:', userCollectionsReq); - return NextResponse.json({ newUserPubCollectionsReq }); + return NextResponse.json({ newUserCollectionsReq }); } -// PUT request to update the user's public collections request data in the database (Used by user) +// PUT request to update the user's collections request data in the database (Used by user) export async function PUT(request: NextRequest) { // Create a new Supabase client const supabase = createClient( @@ -89,22 +89,22 @@ export async function PUT(request: NextRequest) { const { collection_id, is_make_public } = await request?.json(); // Update the user's public collections request data in the database, set is_pending = true - const { data: updatedUserPubCollectionsReq, error: updatedUserPubCollErr } = await supabase - .from('public_collections_requests') + const { data: updatedUserCollectionsReq, error: updatedUserCollErr } = await supabase + .from('collections_requests') .update({ is_make_public: is_make_public, is_pending: true, is_approved: false }) .eq('collection_id', collection_id); - if (updatedUserPubCollErr) { - console.error('Error updating user public collections request data in database:', updatedUserPubCollErr.message); - return NextResponse.json({ error: updatedUserPubCollErr.message }, { status: 500 }); + if (updatedUserCollErr) { + console.error('Error updating user collections request data in database:', updatedUserCollErr.message); + return NextResponse.json({ error: updatedUserCollErr.message }, { status: 500 }); } - // console.log('User Public Collections Requests:', userPubCollectionsReq); + // console.log('Update User Collections Requests:', userCollectionsReq); - return NextResponse.json({ updatedUserPubCollectionsReq }); + return NextResponse.json({ updatedUserCollectionsReq }); } -// DELETE request to delete the user's public collections request data from the database +// DELETE request to delete the user's collections request data from the database export async function DELETE(request: NextRequest) { // Create a new Supabase client const supabase = createClient( @@ -116,18 +116,18 @@ export async function DELETE(request: NextRequest) { // Retrieve the collection_id from the request body const { collection_id } = await request?.json(); - // Delete the user's public collections request data from the database - const { data: deletedUserPubCollectionsReq, error: deletedUserPubCollErr } = await supabase - .from('public_collections_requests') + // Delete the user's collections request data from the database + const { data: deletedUserCollectionsReq, error: deletedUserCollErr } = await supabase + .from('collections_requests') .delete() .eq('collection_id', collection_id); - if (deletedUserPubCollErr) { - console.error('Error deleting user public collections request data from database:', deletedUserPubCollErr.message); - return NextResponse.json({ error: deletedUserPubCollErr.message }, { status: 500 }); + if (deletedUserCollErr) { + console.error('Error deleting user collections request data from database:', deletedUserCollErr.message); + return NextResponse.json({ error: deletedUserCollErr.message }, { status: 500 }); } - // console.log('User Public Collections Requests:', userPubCollectionsReq); + // console.log('Delete User Collections Requests:', userCollectionsReq); - return NextResponse.json({ deletedUserPubCollectionsReq }); + return NextResponse.json({ deletedUserCollectionsReq }); } \ No newline at end of file diff --git a/frontend/app/api/user/collections/route.ts b/frontend/app/api/user/collections/route.ts index b31e640..33ec48b 100644 --- a/frontend/app/api/user/collections/route.ts +++ b/frontend/app/api/user/collections/route.ts @@ -95,7 +95,7 @@ export async function POST(request: NextRequest) { return NextResponse.json({ error: insertError.message }, { status: 500 }); } - console.log('Collection data inserted:', insertData); + // console.log('Collection data inserted:', insertData); return NextResponse.json({ message: 'Collection data inserted successfully.', collectionId: insertData[0].collection_id }); } @@ -177,7 +177,7 @@ export async function DELETE(request: NextRequest) { return NextResponse.json({ error: deleteError.message }, { status: 500 }); } - console.log('Delete', collection_id, ':', deleteData, 'deleteVecsResponse:', deleteVecsResponse); + // console.log('Delete', collection_id, ':', deleteData, 'deleteVecsResponse:', deleteVecsResponse); return NextResponse.json({ message: 'Collection data deleted successfully.' }); } diff --git a/frontend/app/components/ui/admin/admin-manage-collections.tsx b/frontend/app/components/ui/admin/admin-manage-collections.tsx index 697b230..704dd00 100644 --- a/frontend/app/components/ui/admin/admin-manage-collections.tsx +++ b/frontend/app/components/ui/admin/admin-manage-collections.tsx @@ -60,7 +60,7 @@ export default function AdminManageCollections() { const handleMakePrivate = async (collectionId: string) => { // Show confirmation dialog Swal.fire({ - title: 'Private Request Confirmation', + title: 'Request Confirmation', text: "Are you sure you want to make this collection Private?", icon: 'question', showCancelButton: true, @@ -71,13 +71,14 @@ export default function AdminManageCollections() { }).then((result) => { if (result.isConfirmed) { // if user confirms, send request to server - fetch(`/api/admin/collections-requests/reject`, { - method: 'POST', + fetch(`/api/admin/collections`, { + method: 'PUT', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ collection_id: collectionId, + is_public: false, // Set collection to Private }), }).then(async (response) => { if (!response.ok) { @@ -120,7 +121,7 @@ export default function AdminManageCollections() { const handleMakePublic = async (collectionId: string) => { // Show confirmation dialog Swal.fire({ - title: 'Public Request Confirmation', + title: 'Request Confirmation', text: "Are you sure you want to make this collection Public?", icon: 'question', showCancelButton: true, @@ -131,13 +132,14 @@ export default function AdminManageCollections() { }).then((result) => { if (result.isConfirmed) { // if user confirms, send request to server - fetch(`/api/admin/collections-requests/approve`, { - method: 'POST', + fetch(`/api/admin/collections`, { + method: 'PUT', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ collection_id: collectionId, + is_public: true, // Set collection to Public }), }).then(async (response) => { if (!response.ok) { diff --git a/frontend/app/components/ui/query/query-manage.tsx b/frontend/app/components/ui/query/query-manage.tsx index e2d3d1f..0210bb3 100644 --- a/frontend/app/components/ui/query/query-manage.tsx +++ b/frontend/app/components/ui/query/query-manage.tsx @@ -15,7 +15,7 @@ export default function QueryCollectionManage() { const { data: session, status } = useSession(); const supabaseAccessToken = session?.supabaseAccessToken; - // Retrieve the user's collections and public collections requests data from the database + // Retrieve the user's collections and collections requests data from the database const getUserCollectionsandRequests = async () => { setIsLoading(true); // Fetch the user's public collection requests from the API @@ -30,14 +30,14 @@ export default function QueryCollectionManage() { ) .then((response) => response.json()) .then((data) => { - const publicCollectionsRequests = data.userPubCollectionsReq; - // console.log('Public Collections Requests:', publicCollectionsRequests); + const userCollectionsRequests = data.userCollectionsReq; + console.log('User Collections Requests:', userCollectionsRequests); // Sort the collections by created date in descending order (oldest first) - publicCollectionsRequests.sort((a: any, b: any) => new Date(a.created_at).getTime() - new Date(b.created_at).getTime()); - // Extract the collection data from the public collections requests - const updatedCollections = publicCollectionsRequests.map((collection: any) => { - // Check if the collection has any public collection requests - if (collection.public_collections_requests.length === 0) { + userCollectionsRequests.sort((a: any, b: any) => new Date(a.created_at).getTime() - new Date(b.created_at).getTime()); + // Extract the collection data from the collections requests + const updatedCollections = userCollectionsRequests.map((collection: any) => { + // Check if the collection has any collection requests + if (collection.collections_requests.length === 0) { // If not, return the collection data with no other details return { collection_id: collection.collection_id, @@ -49,17 +49,17 @@ export default function QueryCollectionManage() { }; } else { - // If the collection has public collection requests, return the collection data with the request status and dates + // If the collection has collection requests, return the collection data with the request status and dates return { collection_id: collection.collection_id, display_name: collection.display_name, description: collection.description, created_at: new Date(collection.created_at).toLocaleDateString('en-GB', { year: 'numeric', month: 'short', day: 'numeric', hour: 'numeric', minute: 'numeric', second: 'numeric', hour12: true }), isPublic: collection.is_public, - requestType: collection.public_collections_requests[0].is_make_public ? 'Public' : 'Private', - requestStatus: collection.public_collections_requests[0].is_pending ? '⏳Pending' : collection.public_collections_requests[0].is_approved ? '✅Approved' : '❌Rejected', - requestDate: new Date(collection.public_collections_requests[0].created_at).toLocaleDateString('en-GB', { year: 'numeric', month: 'short', day: 'numeric', hour: 'numeric', minute: 'numeric', second: 'numeric', hour12: true }), - updatedRequestDate: new Date(collection.public_collections_requests[0].updated_at).toLocaleDateString('en-GB', { year: 'numeric', month: 'short', day: 'numeric', hour: 'numeric', minute: 'numeric', second: 'numeric', hour12: true }), + requestType: collection.collections_requests[0].is_make_public ? 'Public' : 'Private', + requestStatus: collection.collections_requests[0].is_pending ? '⏳Pending' : collection.collections_requests[0].is_approved ? '✅Approved' : '❌Rejected', + requestDate: new Date(collection.collections_requests[0].created_at).toLocaleDateString('en-GB', { year: 'numeric', month: 'short', day: 'numeric', hour: 'numeric', minute: 'numeric', second: 'numeric', hour12: true }), + updatedRequestDate: new Date(collection.collections_requests[0].updated_at).toLocaleDateString('en-GB', { year: 'numeric', month: 'short', day: 'numeric', hour: 'numeric', minute: 'numeric', second: 'numeric', hour12: true }), }; } }); @@ -68,7 +68,7 @@ export default function QueryCollectionManage() { setIsLoading(false); }) .catch((error) => { - console.error("Error fetching user public collection requests:", error); + console.error("Error fetching user collection requests:", error); setIsLoading(false); return false; }); @@ -76,7 +76,7 @@ export default function QueryCollectionManage() { } - // Fetch the user's collections and public collections requests data from the database on component mount + // Fetch the user's collections and collections requests data from the database on component mount useEffect(() => { getUserCollectionsandRequests(); }, []); @@ -531,7 +531,7 @@ export default function QueryCollectionManage() { {collection.requestStatus === '⏳Pending' ? ( @@ -539,7 +539,7 @@ export default function QueryCollectionManage() { collection.isPublic ? (