mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 00:20:07 +00:00
fix(booking-import): stop writing an unreadable type down as a hotel (#2076)
When neither the keyword table nor the model produced a type, the router wrote "hotel" and cast whatever string it got to FlatType without checking it. A hotel is a booking type, so a ferry voucher imported from the Transport tab opened the booking form, which offers six chips and not one transport among them, and "other" was the only honest pick left. An unrecognised string got through the cast too and then vanished silently in nuExtractToKiReservations. The type is validated against FLAT_TYPES now. When it cannot be read the item is still extracted with the lodging shape so it survives mapping, but it says so, and the tie-breaker #2080 added finally has something to fire on: isUnplaceableItem only ever matched a type outside both form's vocabularies, and the server can only emit its own eight, so on a real document it never matched at all. The tab travels with the job instead of sitting in component state. The parse deliberately outlives navigation and reload and the review is triggered by the global widget on any page, so by the time it ran the state had remounted back to its default and the context was gone.
This commit is contained in:
@@ -10,6 +10,9 @@ interface BookingImportModalProps {
|
||||
isOpen: boolean
|
||||
onClose: () => void
|
||||
tripId: number
|
||||
/** The tab this import was started from, carried into the job so the review
|
||||
* can pick the right form for an item whose type had to be guessed (#2076). */
|
||||
kind?: 'transports' | 'bookings'
|
||||
}
|
||||
|
||||
const ACCEPTED_EXTS = ['.eml', '.pdf', '.pkpass', '.html', '.htm', '.txt']
|
||||
@@ -22,7 +25,7 @@ const MAX_FILES = 5
|
||||
* (progress over the WebSocket). When it finishes, the trip page opens the per-item
|
||||
* review flow — so the user can navigate and keep editing while it works.
|
||||
*/
|
||||
export default function BookingImportModal({ isOpen, onClose, tripId }: BookingImportModalProps) {
|
||||
export default function BookingImportModal({ isOpen, onClose, tripId, kind }: BookingImportModalProps) {
|
||||
const { t } = useTranslation()
|
||||
const addTask = useBackgroundTasksStore((s) => s.addTask)
|
||||
const fileInputRef = useRef<HTMLInputElement>(null)
|
||||
@@ -100,7 +103,7 @@ export default function BookingImportModal({ isOpen, onClose, tripId }: BookingI
|
||||
// Keep the uploaded files so the review can attach each source document to its booking —
|
||||
// in memory for the immediate path, and in IndexedDB so it survives a reload mid-parse.
|
||||
await saveImportFiles(jobId, files)
|
||||
addTask({ id: jobId, tripId: String(tripId), label: files.map((f) => f.name).join(', '), total: files.length, files, mode })
|
||||
addTask({ id: jobId, tripId: String(tripId), label: files.map((f) => f.name).join(', '), total: files.length, files, mode, kind })
|
||||
handleClose()
|
||||
} catch (err: any) {
|
||||
setError(err?.response?.data?.error ?? t('reservations.import.error'))
|
||||
|
||||
@@ -52,9 +52,16 @@ export function isTransportItem(item: BookingImportPreviewItem): boolean {
|
||||
}
|
||||
|
||||
/**
|
||||
* Neither form can express this type, so neither is obviously right. The tab the
|
||||
* user started the import from breaks the tie (#2076).
|
||||
* Neither form is obviously right, so the tab the user started the import from
|
||||
* breaks the tie (#2076).
|
||||
*
|
||||
* Two cases. A type neither form can express, and a type the extractor filled in
|
||||
* rather than read. The second is the one that actually occurs: the server can
|
||||
* only ever emit its eight known types, so the first branch never fires on a real
|
||||
* document, and a ferry voucher the model could not classify arrived as a
|
||||
* placeholder 'hotel' with no way to tell it from a real one.
|
||||
*/
|
||||
export function isUnplaceableItem(item: BookingImportPreviewItem): boolean {
|
||||
if (item.type_guessed === true) return true
|
||||
return !TRANSPORT_TYPES.has(item.type) && !BOOKING_TYPES.has(item.type)
|
||||
}
|
||||
|
||||
@@ -126,7 +126,7 @@ export default function MTripSheets({ planner, shell }: MTripSheetsProps) {
|
||||
/>
|
||||
)}
|
||||
|
||||
<BookingImportModal isOpen={planner.showBookingImport} onClose={() => planner.setShowBookingImport(false)} tripId={tripId} />
|
||||
<BookingImportModal isOpen={planner.showBookingImport} onClose={() => planner.setShowBookingImport(false)} tripId={tripId} kind={planner.bookingImportKind} />
|
||||
<AirTrailImportModal isOpen={planner.showAirTrailImport} onClose={() => planner.setShowAirTrailImport(false)} tripId={tripId} pushUndo={planner.pushUndo} />
|
||||
|
||||
{/* Trip edit + share/members, opened from the Mehr sheet. */}
|
||||
|
||||
@@ -252,7 +252,7 @@ function TripPlannerPageDesktop(): React.ReactElement | null {
|
||||
prefillCoords, setPrefillCoords, editingAssignmentId, setEditingAssignmentId,
|
||||
showTripForm, setShowTripForm, showMembersModal, setShowMembersModal,
|
||||
showReservationModal, setShowReservationModal, editingReservation, setEditingReservation,
|
||||
showBookingImport, setShowBookingImport, setBookingImportKind, bookingImportAvailable,
|
||||
showBookingImport, setShowBookingImport, bookingImportKind, setBookingImportKind, bookingImportAvailable,
|
||||
airTrailAvailable, showAirTrailImport, setShowAirTrailImport,
|
||||
bookingForAssignmentId, setBookingForAssignmentId,
|
||||
showTransportModal, setShowTransportModal, editingTransport, setEditingTransport,
|
||||
@@ -877,7 +877,7 @@ function TripPlannerPageDesktop(): React.ReactElement | null {
|
||||
</Suspense>
|
||||
</ErrorBoundary>
|
||||
)}
|
||||
<BookingImportModal isOpen={showBookingImport} onClose={() => setShowBookingImport(false)} tripId={tripId} />
|
||||
<BookingImportModal isOpen={showBookingImport} onClose={() => setShowBookingImport(false)} tripId={tripId} kind={bookingImportKind} />
|
||||
<AirTrailImportModal isOpen={showAirTrailImport} onClose={() => setShowAirTrailImport(false)} tripId={tripId} pushUndo={pushUndo} />
|
||||
<ConfirmDialog
|
||||
isOpen={!!deletePlaceId}
|
||||
|
||||
@@ -1642,8 +1642,10 @@ describe('useTripPlanner — booking import review', () => {
|
||||
const { result } = await renderPlanner()
|
||||
const odd = { type: 'shuttle-voucher', title: 'Airport transfer' }
|
||||
|
||||
act(() => { result.current.setBookingImportKind('transports') })
|
||||
act(() => { result.current.startImportReview([odd] as never) })
|
||||
// The tab now travels with the review rather than living in component state:
|
||||
// the parse outlives navigation and reload, and the review is triggered by the
|
||||
// global widget, so by then the state has remounted back to its default.
|
||||
act(() => { result.current.startImportReview([odd] as never, [], 'transports') })
|
||||
|
||||
expect(result.current.showTransportModal).toBe(true)
|
||||
expect(result.current.showReservationModal).toBe(false)
|
||||
@@ -1654,8 +1656,33 @@ describe('useTripPlanner — booking import review', () => {
|
||||
const { result } = await renderPlanner()
|
||||
const odd = { type: 'shuttle-voucher', title: 'Airport transfer' }
|
||||
|
||||
act(() => { result.current.setBookingImportKind('bookings') })
|
||||
act(() => { result.current.startImportReview([odd] as never) })
|
||||
act(() => { result.current.startImportReview([odd] as never, [], 'bookings') })
|
||||
|
||||
expect(result.current.showReservationModal).toBe(true)
|
||||
expect(result.current.showTransportModal).toBe(false)
|
||||
})
|
||||
|
||||
// The case that actually occurs. The server only ever emits its eight known
|
||||
// types, so 'shuttle-voucher' above never reaches a real import: a document the
|
||||
// model could not classify arrived as a placeholder 'hotel', which IS a booking
|
||||
// type, so the tie-breaker never ran and the tab lost every time (#2076).
|
||||
it('FE-TP-HOOK-112b: a guessed hotel opens the transport form when the import began there', async () => {
|
||||
seedTrip()
|
||||
const { result } = await renderPlanner()
|
||||
const guessed = { type: 'hotel', title: 'Airport transfer', type_guessed: true }
|
||||
|
||||
act(() => { result.current.startImportReview([guessed] as never, [], 'transports') })
|
||||
|
||||
expect(result.current.showTransportModal).toBe(true)
|
||||
expect(result.current.showReservationModal).toBe(false)
|
||||
})
|
||||
|
||||
it('FE-TP-HOOK-112c: a real hotel from the same tab still opens the booking form', async () => {
|
||||
seedTrip()
|
||||
const { result } = await renderPlanner()
|
||||
const real = { type: 'hotel', title: 'Ryokan' }
|
||||
|
||||
act(() => { result.current.startImportReview([real] as never, [], 'transports') })
|
||||
|
||||
expect(result.current.showReservationModal).toBe(true)
|
||||
expect(result.current.showTransportModal).toBe(false)
|
||||
|
||||
@@ -274,6 +274,11 @@ export function useTripPlanner() {
|
||||
const importQueueRef = useRef<BookingImportPreviewItem[]>([])
|
||||
// The files this import was parsed from, so each reviewed booking can attach its source doc.
|
||||
const importSourceFilesRef = useRef<File[]>([])
|
||||
// The tab the items under review came from. A ref, not the bookingImportKind
|
||||
// state: the parse outlives navigation and reload, and the review is triggered
|
||||
// by the global widget, so by then the state has remounted back to its default.
|
||||
// The value comes off the persisted job (#2076).
|
||||
const importKindRef = useRef<'transports' | 'bookings'>('bookings')
|
||||
// Manual route planning: off by default, toggled from the day-plan footer. Mode
|
||||
// is per-session and selects which travel time the connectors show — either a
|
||||
// built-in OSRM profile or a plugin route profile ('plugin:<id>/<profile>').
|
||||
@@ -933,7 +938,7 @@ export function useTripPlanner() {
|
||||
const srcName = item.source?.fileName
|
||||
const srcFile = srcName ? importSourceFilesRef.current.find(f => f.name === srcName) : undefined
|
||||
if (srcFile) draft._sourceFiles = [srcFile]
|
||||
if (isTransportItem(item) || (isUnplaceableItem(item) && bookingImportKind === 'transports')) {
|
||||
if (isTransportItem(item) || (isUnplaceableItem(item) && importKindRef.current === 'transports')) {
|
||||
setShowReservationModal(false); setEditingReservation(null); setReservationPrefill(null)
|
||||
setEditingTransport(null); setTransportModalDayId(null)
|
||||
setTransportPrefill(draft); setShowTransportModal(true)
|
||||
@@ -944,9 +949,14 @@ export function useTripPlanner() {
|
||||
}
|
||||
}
|
||||
|
||||
const startImportReview = (items: BookingImportPreviewItem[], sourceFiles: File[] = []) => {
|
||||
const startImportReview = (
|
||||
items: BookingImportPreviewItem[],
|
||||
sourceFiles: File[] = [],
|
||||
kind: 'transports' | 'bookings' = 'bookings',
|
||||
) => {
|
||||
if (!items.length) return
|
||||
importSourceFilesRef.current = sourceFiles
|
||||
importKindRef.current = kind
|
||||
importQueueRef.current = items.slice(1)
|
||||
setImportReviewActive(true)
|
||||
openImportItem(items[0])
|
||||
@@ -967,12 +977,13 @@ export function useTripPlanner() {
|
||||
const items = task.items
|
||||
const jobId = task.id
|
||||
const inMemory = task.sourceFiles
|
||||
const kind = task.kind ?? 'bookings'
|
||||
dismissBgTask(jobId)
|
||||
// Prefer the in-memory files (immediate path); after a reload they live in IndexedDB.
|
||||
void (async () => {
|
||||
const files = inMemory && inMemory.length ? inMemory : await getImportFiles(jobId)
|
||||
deleteImportFiles(jobId)
|
||||
startImportReview(items, files)
|
||||
startImportReview(items, files, kind)
|
||||
})()
|
||||
}
|
||||
}, [bgTasks, tripId, startImportReview, dismissBgTask])
|
||||
|
||||
@@ -32,11 +32,19 @@ export interface BackgroundImportTask {
|
||||
sourceFiles?: File[]
|
||||
/** Mode this run used — the widget only offers an AI retry if it wasn't already one. Not persisted. */
|
||||
mode?: BookingImportMode
|
||||
/**
|
||||
* Which tab the import was started from. It decides which form an item opens
|
||||
* when its type is guessed (#2076), and unlike the other transient flags it IS
|
||||
* persisted: the parse deliberately outlives navigation and reload, and the
|
||||
* review is triggered by the global widget on any page, so a component-local
|
||||
* value is gone by the time it is needed.
|
||||
*/
|
||||
kind?: 'transports' | 'bookings'
|
||||
}
|
||||
|
||||
interface BackgroundTasksState {
|
||||
tasks: BackgroundImportTask[]
|
||||
addTask: (task: { id: string; tripId: string; label: string; total: number; files?: File[]; mode?: BookingImportMode }) => void
|
||||
addTask: (task: { id: string; tripId: string; label: string; total: number; files?: File[]; mode?: BookingImportMode; kind?: 'transports' | 'bookings' }) => void
|
||||
setProgress: (id: string, tripId: string, done: number, total: number) => void
|
||||
setDone: (id: string, tripId: string, items: BookingImportPreviewItem[], warnings: string[]) => void
|
||||
setError: (id: string, tripId: string, error: string) => void
|
||||
@@ -63,7 +71,7 @@ export const useBackgroundTasksStore = create<BackgroundTasksState>()(
|
||||
|
||||
return {
|
||||
tasks: [],
|
||||
addTask: ({ id, tripId, label, total, files, mode }) => upsert(id, tripId, { label, total, status: 'running', done: 0, sourceFiles: files, mode }),
|
||||
addTask: ({ id, tripId, label, total, files, mode, kind }) => upsert(id, tripId, { label, total, status: 'running', done: 0, sourceFiles: files, mode, kind }),
|
||||
setProgress: (id, tripId, done, total) => upsert(id, tripId, { done, total, status: 'running' }),
|
||||
setDone: (id, tripId, items, warnings) => upsert(id, tripId, { status: 'done', items, warnings, done: items?.length ?? 0 }),
|
||||
setError: (id, tripId, error) => upsert(id, tripId, { status: 'error', error }),
|
||||
@@ -81,7 +89,9 @@ export const useBackgroundTasksStore = create<BackgroundTasksState>()(
|
||||
partialize: (state) => ({
|
||||
tasks: state.tasks
|
||||
.filter((t) => !t.consumed && t.status !== 'error')
|
||||
.map((t) => ({ id: t.id, tripId: t.tripId, label: t.label, status: t.status, done: t.done, total: t.total })),
|
||||
// `kind` rides along: it is not a review flag but the context the review
|
||||
// needs, and without it a reload silently falls back to 'bookings'.
|
||||
.map((t) => ({ id: t.id, tripId: t.tripId, label: t.label, status: t.status, done: t.done, total: t.total, kind: t.kind })),
|
||||
}),
|
||||
},
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user