Spaces:
Sleeping
Sleeping
File size: 1,359 Bytes
05c5ed5 99774e8 05c5ed5 | 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 | import { getSession } from "auth/server";
import { UserPreferencesZodSchema } from "app-types/user";
import { userRepository } from "lib/db/repository";
import { NextResponse } from "next/server";
export async function GET() {
try {
const session = await getSession();
if (!session?.user?.id) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
const preferences = await userRepository.getPreferences(session.user.id);
return NextResponse.json(preferences ?? {});
} catch (error: any) {
return NextResponse.json(
{ error: error.message || "Failed to get preferences" },
{ status: 500 },
);
}
}
export async function PUT(request: Request) {
try {
const session = await getSession();
if (!session?.user?.id) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
const json = await request.json();
const preferences = UserPreferencesZodSchema.parse(json);
const updatedUser = await userRepository.updatePreferences(
session.user.id,
preferences as any,
);
return NextResponse.json({
success: true,
preferences: updatedUser.preferences,
});
} catch (error: any) {
return NextResponse.json(
{ error: error.message || "Failed to update preferences" },
{ status: 500 },
);
}
}
|