diff --git a/app/api/user/update-profile/route.ts b/app/api/user/update-profile/route.ts index 8d3feee..ef5e36b 100644 --- a/app/api/user/update-profile/route.ts +++ b/app/api/user/update-profile/route.ts @@ -26,6 +26,23 @@ export async function POST(req: Request) { // Start a transaction to ensure data consistency const result = await prisma.$transaction(async (prisma) => { + + if (profile.promptAnswers) { + const profileData = await prisma.profile.findUnique({ + where: { + userId: session.user.id, + }, + }); + console.log('profileData:', profileData); + + const deleted = await prisma.promptAnswer.deleteMany({ + where: { + profileId: profileData?.id, + }, + }); + console.log('Deleted prompt answers:', deleted); + } + // First, update/create the profile const updatedUser = await prisma.user.update({ where: {email: session.user.email}, @@ -54,7 +71,7 @@ export async function POST(req: Request) { } as const; async function handleFeatures(features, attribute: string, profileAttribute: string, idName: string) { - // Process interests if any + // Add new features if (features.length > 0 && updatedUser.profile) { // First, find or create all features console.log('profile', profileAttribute, profileAttribute); diff --git a/app/complete-profile/page.tsx b/app/complete-profile/page.tsx index e3098c9..89feaba 100644 --- a/app/complete-profile/page.tsx +++ b/app/complete-profile/page.tsx @@ -7,6 +7,7 @@ import Image from 'next/image'; import {ConflictStyle, Gender, PersonalityType} from "@prisma/client"; import {parseImage} from "@/lib/client/media"; import {DeleteProfileButton} from "@/lib/client/profile"; +import PromptAnswer from '@/components/ui/PromptAnswer'; export default function CompleteProfile() { return ( @@ -16,6 +17,11 @@ export default function CompleteProfile() { ); } +interface Prompt { + prompt: string + answer: string +} + function RegisterComponent() { const searchParams = useSearchParams(); const redirect = searchParams.get('redirect') || '/'; @@ -29,6 +35,7 @@ function RegisterComponent() { const [introversion, setIntroversion] = useState(null); const [personalityType, setPersonalityType] = useState(''); const [conflictStyle, setConflictStyle] = useState(''); + const [promptAnswers, setPromptAnswers] = useState({}); const [image, setImage] = useState(''); const [key, setKey] = useState(''); const [images, setImages] = useState([]); @@ -87,6 +94,9 @@ function RegisterComponent() { setGender(profile.gender || ''); setPersonalityType(profile.personalityType || null); setConflictStyle(profile.conflictStyle || ''); + if (profile.promptAnswers) { + setPromptAnswers(Object.fromEntries(profile.promptAnswers.map(item => [item.prompt, item.answer]))); + } setIntroversion(profile.introversion || null); if (profile.birthYear) { setAge(new Date().getFullYear() - profile.birthYear); @@ -231,6 +241,9 @@ function RegisterComponent() { setIsSubmitting(true); setError(''); + const promptAnswersList = Object.entries(promptAnswers).map(([prompt, answer]) => ({ prompt, answer })); + console.log('promptAnswersList', promptAnswersList) + console.log('submit image', key); console.log('submit images', keys); @@ -244,6 +257,7 @@ function RegisterComponent() { introversion, personalityType: personalityType as PersonalityType, conflictStyle: conflictStyle as ConflictStyle, + promptAnswers: {create: promptAnswersList}, images: keys, }, ...(key && {image: key}), @@ -440,7 +454,7 @@ function RegisterComponent() {