Files
property-match/src/hooks/useInquiries.ts
T
Benjamin SutterandClaude Opus 4.8 a8b54af0b8 feat(messages): unified conversation inbox — connect demand ↔ supply, offers reach the seeker
Phase 1 of the messaging rework: one conversation/thread model (Inquiry) is the single source of truth, read perspectivally by both sides.

- domain/inquiry: add kind (INQUIRY|OFFER), tenantOrgId (demand org), offeredPropertyIds
- provider/service: InquiryFilters gains tenantOrgId+kind; new createInquiry (demand→supply) & createOffer (supply→demand) materialize threads; addMessage bumps unread; reply carries senderType (tenant vs supply_user)
- hooks: useCreateInquiry, useCreateOffer; reply payload carries sender perspective
- demand: InquiryQuickDialog → createInquiry via provider (session-based tenant/org, no hardcode); Anfragen.tsx reads via React Query (tenantOrgId) with Gesendet/Erhalten tabs, replies via provider, shows offered properties for OFFER threads; inquiryStore reduced to dialog-only (removes Zustand server-data island)
- supply: OfferChatComposer send now materializes an OFFER conversation into the seeker's inbox (routed via latentNeed.tenantOrgId); Anfragencenter gains a "Gesendet" tab; ActiveInquiriesTab filters by perspective (org + kind)
- seed: merge demand inquiries with tenantOrgId=org-mobimo + 2 OFFER seeds; provider seeds from both sets

Closes the loop: a sent inquiry reaches the Verwaltung and a sent offer reaches the Nachfrager — both answerable in one thread.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-21 01:32:13 +02:00

79 lines
2.6 KiB
TypeScript

import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'
import { inquiryService } from '../services/inquiryService'
import type { InquiryFilters, CreateInquiryInput, CreateOfferInput } from '../provider/IInquiryProvider'
import type { Attachment, InquiryMessage } from '../domain/inquiry'
import { useToastStore } from '../stores/toastStore'
export function useActiveInquiries(filters?: InquiryFilters) {
return useQuery({
queryKey: ['inquiries', 'active', filters ?? {}],
queryFn: () => inquiryService.getActiveInquiries(filters),
select: (res) => res.data ?? [],
})
}
export function useInquiryById(id: string) {
return useQuery({
queryKey: ['inquiry', id],
queryFn: () => inquiryService.getInquiryById(id),
enabled: !!id,
select: (res) => res.data ?? null,
})
}
export function useSendInquiryReply() {
const qc = useQueryClient()
return useMutation({
mutationFn: ({
inquiryId,
payload,
}: {
inquiryId: string
payload: { subject?: string; body: string; attachments?: Attachment[]; senderType?: InquiryMessage['senderType']; senderName?: string }
}) => inquiryService.sendInquiryReply(inquiryId, payload),
onSuccess: (_data, { inquiryId }) => {
qc.invalidateQueries({ queryKey: ['inquiry', inquiryId] })
qc.invalidateQueries({ queryKey: ['inquiries'] })
},
onError: () => {
useToastStore.getState().showToast('Antwort konnte nicht gesendet werden.', 'error')
},
})
}
/** Demand→Supply: neue Anfrage erstellen (ersetzt die alte Zustand-Insel). */
export function useCreateInquiry() {
const qc = useQueryClient()
return useMutation({
mutationFn: (input: CreateInquiryInput) => inquiryService.createInquiry(input),
onSuccess: () => {
qc.invalidateQueries({ queryKey: ['inquiries'] })
},
onError: () => {
useToastStore.getState().showToast('Anfrage konnte nicht gesendet werden.', 'error')
},
})
}
/** Supply→Demand: Angebot als Konversation im Nachfrager-Postfach materialisieren. */
export function useCreateOffer() {
const qc = useQueryClient()
return useMutation({
mutationFn: (input: CreateOfferInput) => inquiryService.createOffer(input),
onSuccess: () => {
qc.invalidateQueries({ queryKey: ['inquiries'] })
},
})
}
export function useMarkThreadAsRead() {
const qc = useQueryClient()
return useMutation({
mutationFn: (id: string) => inquiryService.markThreadAsRead(id),
onSuccess: (_data, id) => {
qc.invalidateQueries({ queryKey: ['inquiry', id] })
qc.invalidateQueries({ queryKey: ['inquiries'] })
},
})
}