Scrum4Me/components/notifications/notifications-bridge.tsx
Madhura68 9e8f33b96e fix(m12): user can answer idea-questions — inline + bell support
Two gaps discovered during the first live grill-session of IDEA-002:
the agent posted a question, but the user had no UI to answer it.
1. Idea-questions only appeared on the Timeline-tab as read-only entries
2. Notifications-bell fetched + handled story-questions only

This fix:

**Inline answer-form in IdeaTimeline** (components/ideas/idea-timeline.tsx)
- Open questions now render an AnswerForm directly under the question text
- Multi-choice options become clickable buttons (one-click submit); free-text
  fallback via collapsed details/textarea
- Plain free-text questions render textarea + Verzend
- Calls existing answerQuestion server-action; toast + router.refresh on success

**Notifications-bell extended for idea-questions**
- stores/notifications-store.ts: NotificationQuestion → discriminated union
  (kind: 'story' | 'idea'); forYouCount treats idea-questions as always-for-you
  (idea is strictly user_id-only — only the owner sees them)
- components/notifications/notifications-bridge.tsx: parallel fetch of
  story-questions (productAccessFilter) + idea-questions (idea.user_id ===
  session.userId); merged + sorted by created_at
- components/notifications/notifications-sheet.tsx: renders idea_code/title
  for kind='idea'
- components/notifications/answer-modal.tsx: header + open-link branch on
  kind (idea → /ideas/[id]?tab=timeline; story → existing /sprint link)
- lib/realtime/use-notifications-realtime.ts: idea-question events also
  trigger close+reconnect on 'open' (loads fresh detail) and remove(id) on
  non-open — same pattern story-questions already use
- components/shared/notifications-bell.tsx: badge counts idea-questions as
  for-you regardless of assignee

**Security gap closed (actions/questions.ts answerQuestion)**
Before: accepted any answer if user has product-access.
After: idea-questions require idea.user_id === session.userId; story-
questions keep the existing productAccessFilter path. (Prisma 7 rejects
\`{ not: null }\` in WHERE; routing happens app-level after a single fetch.)

Tests: 546/546 still green.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-05 13:05:39 +02:00

110 lines
3.6 KiB
TypeScript

// ST-1105: Mount-component voor de notifications-realtime hook (M11).
//
// Server Component dat de initial open-questions fetch't met
// productAccessFilter en doorgeeft aan een minimal client-island; client opent
// daarna de SSE-stream voor live updates.
import { prisma } from '@/lib/prisma'
import { productAccessFilter } from '@/lib/product-access'
import { NotificationsRealtimeMount } from './notifications-realtime-mount'
import type { NotificationQuestion } from '@/stores/notifications-store'
interface NotificationsBridgeProps {
userId: string
}
export async function NotificationsBridge({ userId }: NotificationsBridgeProps) {
const products = await prisma.product.findMany({
where: { archived: false, ...productAccessFilter(userId) },
select: { id: true },
})
const productIds = products.map((p) => p.id)
const openQuestions =
productIds.length === 0
? []
: await prisma.claudeQuestion.findMany({
where: {
status: 'open',
expires_at: { gt: new Date() },
product_id: { in: productIds },
// Skip idea-questions (story_id NULL): they have a separate
// realtime channel and aren't shown in this product-scoped bell.
// Narrowing happens in the flatMap below — Prisma 7 rejects
// `story_id: { not: null }` at runtime.
},
orderBy: { created_at: 'desc' },
take: 100,
select: {
id: true,
product_id: true,
story_id: true,
task_id: true,
question: true,
options: true,
created_at: true,
expires_at: true,
story: { select: { code: true, title: true, assignee_id: true } },
},
})
const storyQuestions: NotificationQuestion[] = openQuestions.flatMap((q) => {
if (!q.story || q.story_id === null) return []
return [{
kind: 'story' as const,
id: q.id,
product_id: q.product_id,
story_id: q.story_id,
task_id: q.task_id,
story_code: q.story.code,
story_title: q.story.title,
assignee_id: q.story.assignee_id,
question: q.question,
options: Array.isArray(q.options) ? (q.options as string[]) : null,
created_at: q.created_at.toISOString(),
expires_at: q.expires_at.toISOString(),
}]
})
// M12 hotfix: idea-questions ook in de bel. user_id-only scope (idee is privé).
const ideaOpenQuestions = await prisma.claudeQuestion.findMany({
where: {
status: 'open',
expires_at: { gt: new Date() },
idea: { user_id: userId },
},
orderBy: { created_at: 'desc' },
take: 100,
select: {
id: true,
product_id: true,
idea_id: true,
question: true,
options: true,
created_at: true,
expires_at: true,
idea: { select: { id: true, code: true, title: true } },
},
})
const ideaQuestions: NotificationQuestion[] = ideaOpenQuestions.flatMap((q) => {
if (!q.idea || q.idea_id === null) return []
return [{
kind: 'idea' as const,
id: q.id,
product_id: q.product_id,
idea_id: q.idea_id,
idea_code: q.idea.code,
idea_title: q.idea.title,
question: q.question,
options: Array.isArray(q.options) ? (q.options as string[]) : null,
created_at: q.created_at.toISOString(),
expires_at: q.expires_at.toISOString(),
}]
})
const initial: NotificationQuestion[] = [...storyQuestions, ...ideaQuestions]
.sort((a, b) => (a.created_at < b.created_at ? 1 : -1))
return <NotificationsRealtimeMount initial={initial} />
}