Spaces:
Build error
Build error
File size: 7,462 Bytes
f8ff91f 7515f43 fdaf912 2483bce fdaf912 4071fe2 fdaf912 4071fe2 fdaf912 4071fe2 fdaf912 2483bce fdaf912 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
export const revalidate = 10;
import { createClient } from '@supabase/supabase-js';
import { NextRequest, NextResponse } from "next/server";
// GET request to retrieve the user's collections data from the database
export async function GET(request: NextRequest) {
const { pathname, origin } = request.nextUrl;
const signinPage = new URL('/sign-in', origin);
// 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');
// Create a new Supabase client
const supabaseAuth = createClient(
process.env.SUPABASE_URL ?? '',
process.env.SUPABASE_SERVICE_ROLE_KEY ?? '',
{ db: { schema: 'next_auth' } },
);
const supabase = createClient(
process.env.SUPABASE_URL ?? '',
process.env.SUPABASE_SERVICE_ROLE_KEY ?? '',
{ db: { schema: 'public' } },
);
// Retrieve the user's ID from the session token
const { data: sessionData, error: sessionError } = await supabaseAuth
.from('sessions')
.select('userId')
.eq('sessionToken', session?.value)
.single();
const userId = sessionData?.userId;
if (sessionError) {
console.error('Error fetching session from database:', sessionError.message);
return NextResponse.redirect(signinPage.href, { status: 302 });
}
// Retrieve the user's collections id and data from the database
const { data: userCollections, error: userCollErr } = await supabase
.from('collections')
.select('collection_id, display_name, description, created_at')
.eq('id', userId);
if (userCollErr) {
console.error('Error fetching user collection data from database:', userCollErr.message);
return NextResponse.redirect(signinPage.href, { status: 302 });
}
return NextResponse.json({ userCollections: userCollections });
}
// POST request to insert the user's collection data into the database
export async function POST(request: NextRequest) {
// Create a new Supabase client
const supabaseAuth = createClient(
process.env.SUPABASE_URL ?? '',
process.env.SUPABASE_SERVICE_ROLE_KEY ?? '',
{ db: { schema: 'next_auth' } },
);
const supabase = createClient(
process.env.SUPABASE_URL ?? '',
process.env.SUPABASE_SERVICE_ROLE_KEY ?? '',
{ db: { schema: 'public' } },
);
// 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');
// Retrieve the collection data from the request body
const { display_name, description } = await request?.json();
// Retrieve the user's ID from the session token
const { data: sessionData, error: sessionError } = await supabaseAuth
.from('sessions')
.select('userId')
.eq('sessionToken', session?.value)
.single();
const userId = sessionData?.userId;
if (sessionError) {
console.error('Error fetching session from database:', sessionError.message);
return NextResponse.json({ error: sessionError.message }, { status: 500 });
}
// Insert the collection data into the database and return the data
const { data: insertData, error: insertError } = await supabase
.from('collections')
.insert([{ id: userId, display_name, description }])
.select('collection_id');
if (insertError) {
console.error('Error inserting user collection data into database:', insertError.message);
return NextResponse.json({ error: insertError.message }, { status: 500 });
}
// console.log('Collection data inserted:', insertData);
return NextResponse.json({ message: 'Collection data inserted successfully.', collectionId: insertData[0].collection_id });
}
// DELETE request to delete the user's collection data from the database
export async function DELETE(request: NextRequest) {
// Create a new Supabase client
const supabaseAuth = createClient(
process.env.SUPABASE_URL ?? '',
process.env.SUPABASE_SERVICE_ROLE_KEY ?? '',
{ db: { schema: 'next_auth' } },
);
const supabase = createClient(
process.env.SUPABASE_URL ?? '',
process.env.SUPABASE_SERVICE_ROLE_KEY ?? '',
{ db: { schema: 'public' } },
);
// 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');
// Retrieve the authorization token from the request headers
let authorization = request.headers.get('Authorization');
// Public API key
let api_key = null;
// If no session, use the public API key
if (authorization === null || authorization === undefined || authorization.includes('undefined')) {
console.log('No authorization token found, using public API key');
api_key = process.env.BACKEND_API_KEY as string;
authorization = null; // Clear the authorization token
}
// Create default delete_vecs variable
let is_delete_vecs = true;
// Retrieve the collection_id from the request body
const { collection_id, delete_vecs } = await request?.json();
// if delete_vecs is not undefined, take its value
if (delete_vecs !== undefined) {
is_delete_vecs = delete_vecs;
}
// Retrieve the user's ID from the session token
const { data: sessionData, error: sessionError } = await supabaseAuth
.from('sessions')
.select('userId')
.eq('sessionToken', session?.value)
.single();
const userId = sessionData?.userId;
if (sessionError) {
console.error('Error fetching session from database:', sessionError.message);
return NextResponse.json({ error: sessionError.message }, { status: 500 });
}
if (is_delete_vecs === true) {
// Delete the vector collection from the vecs schema via POST request to Backend API
const deleteVecsResponse = await fetch(`${process.env.DELETE_SINGLE_COLLECTION_API}?collection_id=${collection_id}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': authorization,
'X-API-Key': api_key,
} as any,
body: JSON.stringify({ collection_id: collection_id }),
});
if (!deleteVecsResponse.ok) {
console.error('Error deleting', collection_id, 'from vecs schema:', deleteVecsResponse.statusText);
return NextResponse.json({ error: deleteVecsResponse.statusText }, { status: deleteVecsResponse.status });
}
}
// Delete the collection data from the database
const { data: deleteData, error: deleteError } = await supabase
.from('collections')
.delete()
.eq('id', userId)
.eq('collection_id', collection_id);
if (deleteError) {
console.error('Error deleting', collection_id, ' from database:', deleteError.message);
return NextResponse.json({ error: deleteError.message }, { status: 500 });
}
// console.log('Delete', collection_id, ':', deleteData, 'deleteVecsResponse:', deleteVecsResponse);
return NextResponse.json({ message: 'Collection data deleted successfully.' });
}
|