Factor out user find in db

This commit is contained in:
MartinBraquet
2025-08-02 22:50:18 +02:00
parent 7205245d29
commit 4fcf445f0b
2 changed files with 15 additions and 51 deletions

View File

@@ -2,6 +2,7 @@ import {prisma} from "@/lib/server/prisma";
import {NextResponse} from "next/server";
import {getSession} from "@/lib/server/auth";
import {notFound, redirect} from "next/navigation";
import {retrieveUser} from "@/lib/server/db-utils";
export async function GET() {
@@ -11,35 +12,16 @@ export async function GET() {
if (!session?.user?.email)
redirect('/login');
const user = await prisma.user.findUnique({
where: {email: session.user.email},
include: {
profile: {
include: {
intellectualInterests: {
include: {
interest: true
},
},
desiredConnections: {
include: {
connection: true
},
},
coreValues: {
include: {
value: true
},
},
}
}
}
});
const id = session.user.id;
const user = await retrieveUser(id);
if (!user) {
notFound();
return new NextResponse(JSON.stringify({ error: "User not found" }), {
status: 404,
headers: { "Content-Type": "application/json" },
});
}
console.log('user', user);
return NextResponse.json(user);
}

View File

@@ -2,6 +2,7 @@ import { NextResponse } from "next/server";
import { prisma } from "@/lib/server/prisma";
import { getServerSession } from "next-auth/next";
import { authOptions } from "@/lib/server/auth";
import {retrieveUser} from "@/lib/server/db-utils";
// Handler for GET /api/profiles/[id]
export async function GET(
@@ -12,38 +13,16 @@ export async function GET(
const params = await context.params;
const { id } = params;
const cacheStrategy = { swr: 60, ttl: 60, tags: ["profiles_id"] };
const user = await prisma.user.findUnique({
where: { id },
select: {
id: true,
name: true,
// email: true,
image: true,
createdAt: true,
profile: {
include: {
intellectualInterests: { include: { interest: true } },
causeAreas: { include: { causeArea: true } },
coreValues: { include: { value: true } },
desiredConnections: { include: { connection: true } },
promptAnswers: true,
},
},
},
cacheStrategy: cacheStrategy,
});
const user = await retrieveUser(id)
// If user not found, return 404
if (!user) {
return new NextResponse(JSON.stringify({ error: "User not found" }), {
return new NextResponse(JSON.stringify({error: "User not found"}), {
status: 404,
headers: { "Content-Type": "application/json" },
headers: {"Content-Type": "application/json"},
});
}
console.log("Fetched user profile:", user);
return new NextResponse(JSON.stringify(user), {
status: 200,
headers: { "Content-Type": "application/json" },
@@ -96,6 +75,9 @@ export async function DELETE(
prisma.profileInterest.deleteMany({
where: { profileId: id },
}),
prisma.profileValue.deleteMany({
where: { profileId: id },
}),
// Delete cause areas
prisma.profileCauseArea.deleteMany({
where: { profileId: id },