File size: 5,748 Bytes
f8ff91f
7515f43
fdaf912
 
 
2483bce
fdaf912
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2483bce
 
fdaf912
b6915e6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fdaf912
 
2483bce
 
 
fdaf912
 
2483bce
fdaf912
2483bce
fdaf912
 
2483bce
fdaf912
 
 
 
 
 
 
 
 
 
 
 
2483bce
 
fdaf912
 
2483bce
 
 
fdaf912
 
2483bce
fdaf912
2483bce
fdaf912
 
2483bce
fdaf912
 
 
 
 
 
 
 
 
 
 
 
2483bce
 
fdaf912
 
 
2483bce
 
 
fdaf912
 
2483bce
fdaf912
2483bce
fdaf912
 
2483bce
fdaf912
 
 
 
 
 
 
 
 
 
 
2483bce
 
 
fdaf912
 
 
2483bce
 
 
fdaf912
 
2483bce
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
export const revalidate = 10;

import { createClient } from '@supabase/supabase-js';
import { NextRequest, NextResponse } from "next/server";

// 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');

    // 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.json({ error: sessionError.message }, { status: 500 });
    }

    // 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,
            collections_requests (
                collection_id,
                is_make_public,
                is_pending,
                is_approved,
                created_at,
                updated_at
            )
        `)
        .eq('id', userId);

    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 Collections Requests:', userCollectionsReq);

    return NextResponse.json({ userCollectionsReq: userCollectionsReq });
}

// 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(
        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_make_public } = await request?.json();

    // Insert the user's public collections request data into the database
    const { data: newUserCollectionsReq, error: newUserCollErr } = await supabase
        .from('collections_requests')
        .insert([{ collection_id, is_make_public }]);

    if (newUserCollErr) {
        console.error('Error inserting user collections request data into database:', newUserCollErr.message);
        return NextResponse.json({ error: newUserCollErr.message }, { status: 500 });
    }

    // console.log('Insert User Collections Requests:', userCollectionsReq);

    return NextResponse.json({ newUserCollectionsReq });
}

// 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(
        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_make_public } = await request?.json();

    // Update the user's public collections request data in the database, set is_pending = true
    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 (updatedUserCollErr) {
        console.error('Error updating user collections request data in database:', updatedUserCollErr.message);
        return NextResponse.json({ error: updatedUserCollErr.message }, { status: 500 });
    }

    // console.log('Update User Collections Requests:', userCollectionsReq);

    return NextResponse.json({ updatedUserCollectionsReq });
}

// 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(
        process.env.SUPABASE_URL ?? '',
        process.env.SUPABASE_SERVICE_ROLE_KEY ?? '',
        { db: { schema: 'public' } },
    );

    // Retrieve the collection_id from the request body
    const { collection_id } = await request?.json();

    // 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 (deletedUserCollErr) {
        console.error('Error deleting user collections request data from database:', deletedUserCollErr.message);
        return NextResponse.json({ error: deletedUserCollErr.message }, { status: 500 });
    }

    // console.log('Delete User Collections Requests:', userCollectionsReq);

    return NextResponse.json({ deletedUserCollectionsReq });
}