ai rec works

as intended, needs refinments
This commit is contained in:
dimitar committed 2025-11-24 17:57:35 +01:00
1 parent 0183a8ea6a
commit 3ab9908588
21 files changed
+1579 -696

No files matched your search

+45 -21
View File
@@ -12,12 +12,21 @@ export async function GET(request: Request) {
const { searchParams } = new URL(request.url)
const targetUserId = searchParams.get('userId')
if (!targetUserId) {
return new NextResponse('User ID is required', { status: 400 })
}
const db = await getDatabase()
// If no userId provided, check if staff and return all recommendations
if (!targetUserId) {
const currentUser = await db.getUserById(currentUserId)
const isStaff = currentUser?.role === 'admin' || currentUser?.role === 'superAdmin' || currentUser?.role === 'trainer'
if (!isStaff) {
return new NextResponse('User ID is required', { status: 400 })
}
const recommendations = await db.getAllRecommendations()
return NextResponse.json({ recommendations })
}
// Check permissions: Users can view their own, Admins/Trainers can view anyone's
if (currentUserId !== targetUserId) {
const currentUser = await db.getUserById(currentUserId)
@@ -52,21 +61,37 @@ export async function POST(request: Request) {
}
const body = await request.json()
const { userId, type, content, status } = body
const { userId, fitnessProfileId, recommendationText, activityPlan, dietPlan, status, type, content } = body
if (!userId || !type || !content) {
return new NextResponse('Missing required fields', { status: 400 })
// Handle AI Plan (Legacy/Specific)
if (recommendationText && activityPlan && dietPlan && fitnessProfileId) {
const recommendation = await db.createRecommendation({
id: crypto.randomUUID(),
userId,
fitnessProfileId,
type: 'ai_plan',
content: recommendationText,
activityPlan,
dietPlan,
status: status || 'pending'
})
return NextResponse.json(recommendation)
}
const recommendation = await db.createRecommendation({
id: crypto.randomUUID(),
userId,
type,
content,
status: status || 'pending'
})
// Handle User Goal (Generic)
if (type && content) {
const recommendation = await db.createRecommendation({
id: crypto.randomUUID(),
userId,
type,
content,
status: status || 'pending'
})
return NextResponse.json(recommendation)
}
return NextResponse.json('Missing required fields', { status: 400 })
return NextResponse.json(recommendation)
} catch (error) {
console.error('Error creating recommendation:', error)
return new NextResponse('Internal Server Error', { status: 500 })
@@ -81,7 +106,7 @@ export async function PUT(request: Request) {
}
const body = await request.json()
const { id, status, content } = body
const { id, status, recommendationText, activityPlan, dietPlan, content } = body
if (!id) {
return new NextResponse('Recommendation ID is required', { status: 400 })
@@ -89,13 +114,12 @@ export async function PUT(request: Request) {
const db = await getDatabase()
// Users can update status (e.g. mark as completed), Staff can update content too
// Ideally we'd check ownership for status update, but for now let's allow it if they have the ID
// A stricter check would be: fetch recommendation, check if userId matches currentUserId OR if currentUser is staff
const updated = await db.updateRecommendation(id, {
...(status && { status }),
...(content && { content })
...(recommendationText && { content: recommendationText }), // Map legacy field
...(content && { content }),
...(activityPlan && { activityPlan }),
...(dietPlan && { dietPlan })
})
return NextResponse.json(updated)