2023-02-06 11:59:23 -06:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
|
|
|
import { getServerSession } from "next-auth/next";
|
|
|
|
import { authOptions } from "pages/api/auth/[...nextauth]";
|
2023-02-08 15:11:33 -06:00
|
|
|
import { prisma } from "@/lib/db";
|
2023-02-06 11:59:23 -06:00
|
|
|
|
|
|
|
type Data = {
|
2023-02-08 15:11:33 -06:00
|
|
|
response: object[] | string;
|
2023-02-06 11:59:23 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
export default async function handler(
|
|
|
|
req: NextApiRequest,
|
|
|
|
res: NextApiResponse<Data>
|
|
|
|
) {
|
|
|
|
const session = await getServerSession(req, res, authOptions);
|
|
|
|
|
2023-02-08 15:11:33 -06:00
|
|
|
if (!session?.user?.email) {
|
|
|
|
return res.status(401).json({ response: "You must be logged in." });
|
2023-02-06 11:59:23 -06:00
|
|
|
}
|
|
|
|
|
2023-02-08 15:11:33 -06:00
|
|
|
const email: string = session.user.email;
|
2023-02-06 11:59:23 -06:00
|
|
|
|
2023-02-08 15:11:33 -06:00
|
|
|
const findCollection = await prisma.user.findFirst({
|
|
|
|
where: {
|
|
|
|
email: email,
|
|
|
|
},
|
|
|
|
include: {
|
2023-02-13 18:09:13 -06:00
|
|
|
collections: true,
|
2023-02-08 15:11:33 -06:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const collections = findCollection?.collections.map((e) => {
|
2023-02-13 18:09:13 -06:00
|
|
|
return { id: e.id, name: e.name, createdAt: e.createdAt };
|
2023-02-08 15:11:33 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
// console.log(session?.user?.email);
|
|
|
|
|
|
|
|
return res.status(200).json({
|
|
|
|
response: collections || [],
|
2023-02-06 11:59:23 -06:00
|
|
|
});
|
|
|
|
}
|