From fa8e1b68bdd0dc2559b0ea6f2c2b3ce843f05139 Mon Sep 17 00:00:00 2001 From: Maurice Date: Mon, 23 Nov 2026 14:55:32 +0100 Subject: [PATCH] fix(mail-ingest): set an inbox entry aside once its booking is saved through Fill in yourself The entry stayed pending after the prefilled booking was saved, kept its Add action and counted in the badge, so it could be added a second time. --- .../MailIngest/useMailPrefillLink.test.ts | 18 ++++++++++++++- .../MailIngest/useMailPrefillLink.ts | 19 +++++++++++---- .../pages/tripPlanner/useBookingEdits.test.ts | 23 ++++++++++++++++++- .../src/pages/tripPlanner/useBookingEdits.ts | 13 +++++++++++ 4 files changed, 67 insertions(+), 6 deletions(-) diff --git a/client/src/components/MailIngest/useMailPrefillLink.test.ts b/client/src/components/MailIngest/useMailPrefillLink.test.ts index 0d0213463..493d67218 100644 --- a/client/src/components/MailIngest/useMailPrefillLink.test.ts +++ b/client/src/components/MailIngest/useMailPrefillLink.test.ts @@ -1,10 +1,11 @@ -// FE-MAIL-PREFILL-001 to FE-MAIL-PREFILL-002: "Fill in yourself" opens the booking form in the trip. +// FE-MAIL-PREFILL-001 to FE-MAIL-PREFILL-003: "Fill in yourself" opens the booking form in the trip. import { act, renderHook, waitFor } from '@testing-library/react'; import type { BookingImportPreviewItem } from '@trek/shared'; import { createElement, type ReactNode } from 'react'; import { MemoryRouter, useSearchParams } from 'react-router'; import { beforeEach, describe, expect, it, vi } from 'vitest'; import { TranslationProvider } from '../../i18n/TranslationContext'; +import { useMailIngestStore } from '../../store/mailIngestStore'; const repo = vi.hoisted(() => ({ prefill: vi.fn() })); const toast = vi.hoisted(() => ({ success: vi.fn(), error: vi.fn() })); @@ -56,4 +57,19 @@ describe('useMailPrefillLink', () => { await waitFor(() => expect(startImportReview).toHaveBeenCalledTimes(2)); expect(result.current.searchParams.get('mailPrefill')).toBeNull(); }); + + it('FE-MAIL-PREFILL-003: a saved booking sets the entry aside, and a refused one reads the inbox again', async () => { + const act$ = vi.fn().mockResolvedValue({}); + const loadInbox = vi.fn().mockResolvedValue(undefined); + useMailIngestStore.setState({ act: act$, loadInbox }); + const { startImportReview } = setup(); + await waitFor(() => expect(startImportReview).toHaveBeenCalledTimes(1)); + const onSaved = startImportReview.mock.calls[0][3] as () => void; + onSaved(); + expect(act$).toHaveBeenCalledWith(12, 'discard'); + expect(loadInbox).not.toHaveBeenCalled(); + act$.mockRejectedValueOnce(new Error('decided already')); + onSaved(); + await waitFor(() => expect(loadInbox).toHaveBeenCalledTimes(1)); + }); }); diff --git a/client/src/components/MailIngest/useMailPrefillLink.ts b/client/src/components/MailIngest/useMailPrefillLink.ts index 11d4d3d25..d214b2999 100644 --- a/client/src/components/MailIngest/useMailPrefillLink.ts +++ b/client/src/components/MailIngest/useMailPrefillLink.ts @@ -3,14 +3,16 @@ import { useEffect, useRef } from 'react'; import type { SetURLSearchParams } from 'react-router'; import { useTranslation } from '../../i18n'; import { mailIngestRepo } from '../../repo/mailIngestRepo'; +import { useMailIngestStore } from '../../store/mailIngestStore'; import { useToast } from '../shared/Toast'; /** * "Fill in yourself" from the mail inbox lands in the trip as ?mailPrefill=: * once the trip has loaded, the booking form opens with what the mail held, the * same per-item review a file import uses, and the parameter leaves the address - * so a reload does not open it again. Desktop and phone share the planner hook - * this runs in. + * so a reload does not open it again. Once the booking is saved the entry is set + * aside, so it leaves the inbox instead of offering to add it a second time. + * Desktop and phone share the planner hook this runs in. */ export function useMailPrefillLink({ searchParams, @@ -21,7 +23,12 @@ export function useMailPrefillLink({ searchParams: URLSearchParams; setSearchParams: SetURLSearchParams; loaded: boolean; - startImportReview: (items: BookingImportPreviewItem[], files?: File[], kind?: 'transports' | 'bookings') => void; + startImportReview: ( + items: BookingImportPreviewItem[], + files?: File[], + kind?: 'transports' | 'bookings', + onSaved?: () => void + ) => void; }): void { const { t } = useTranslation(); const toast = useToast(); @@ -48,7 +55,11 @@ export function useMailPrefillLink({ mailIngestRepo .prefill(id) .then(({ item }) => { - startImportReview([item], [], isTransportReservationType(item.type) ? 'transports' : 'bookings'); + startImportReview([item], [], isTransportReservationType(item.type) ? 'transports' : 'bookings', () => { + const inbox = useMailIngestStore.getState(); + // Decided elsewhere meanwhile, or offline: a fresh read shows what is left. + void inbox.act(id, 'discard').catch(() => inbox.loadInbox()); + }); }) .catch(() => { // An entry decided meanwhile has nothing to prefill: the trip stays open, and the person hears why. diff --git a/client/src/pages/tripPlanner/useBookingEdits.test.ts b/client/src/pages/tripPlanner/useBookingEdits.test.ts index 3c6b05889..8fa224763 100644 --- a/client/src/pages/tripPlanner/useBookingEdits.test.ts +++ b/client/src/pages/tripPlanner/useBookingEdits.test.ts @@ -1,4 +1,4 @@ -// FE-TP-BOOKING-001 to FE-TP-BOOKING-016 +// FE-TP-BOOKING-001 to FE-TP-BOOKING-017 // // Booking writes and the review before an import is saved, driven straight through // useBookingEdits. Every write goes to a recorded stand-in for the store's actions and @@ -355,6 +355,27 @@ describe('useBookingEdits', () => { await waitFor(() => expect(fixed.setTripAccommodations).toHaveBeenLastCalledWith([])) }) + it('FE-TP-BOOKING-017: a review started with a saved hook hears of the saved booking once, a skipped one never', async () => { + const { result } = renderEdits() + const onSaved = vi.fn() + act(() => { result.current.startImportReview(items(hotelItem), [], 'bookings', onSaved) }) + await act(async () => { await result.current.handleSaveReservation({ title: 'Ryokan', type: 'hotel' } as never) }) + expect(onSaved).toHaveBeenCalledTimes(1) + await act(async () => { await result.current.handleSaveReservation({ title: 'Ryokan', type: 'hotel' } as never) }) + expect(onSaved).toHaveBeenCalledTimes(1) + + const onTransport = vi.fn() + act(() => { result.current.startImportReview(items(flightItem), [], 'transports', onTransport) }) + await act(async () => { await result.current.handleSaveTransport({ title: 'NRT to CDG', type: 'flight' }) }) + expect(onTransport).toHaveBeenCalledTimes(1) + + const onSkipped = vi.fn() + act(() => { result.current.startImportReview(items(hotelItem), [], 'bookings', onSkipped) }) + act(() => { result.current.advanceImportReview() }) + await act(async () => { await result.current.handleSaveReservation({ title: 'Other', type: 'hotel' } as never) }) + expect(onSkipped).not.toHaveBeenCalled() + }) + it('FE-TP-BOOKING-012: the bridge hands this trip\'s finished import to the review, from memory or from IndexedDB', async () => { const file = new File(['x'], 'mail.pdf') useBackgroundTasksStore.setState({ diff --git a/client/src/pages/tripPlanner/useBookingEdits.ts b/client/src/pages/tripPlanner/useBookingEdits.ts index f3f35a90c..e165fc0ba 100644 --- a/client/src/pages/tripPlanner/useBookingEdits.ts +++ b/client/src/pages/tripPlanner/useBookingEdits.ts @@ -57,6 +57,14 @@ export function useBookingEdits(options: BookingEditsOptions) { // 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') + // Told when a booking of this review is saved, for the mail inbox entry it came from. + const importSavedRef = useRef<(() => void) | null>(null) + + const importSaved = () => { + const saved = importSavedRef.current + importSavedRef.current = null + saved?.() + } // A save is done once the editor's own part (travellers, files) is done too: // only then does it say so and close. Until then the editor stays open, so a @@ -109,6 +117,7 @@ export function useBookingEdits(options: BookingEditsOptions) { accommodationRepo.list(tripId).then(d => setTripAccommodations(d.accommodations || [])).catch(() => {}) } await afterSave?.(r) + if (!editingReservation) importSaved() toast.success(t(editingReservation ? 'trip.toast.reservationUpdated' : 'trip.toast.reservationAdded')) setShowReservationModal(false) if (editingReservation) setEditingReservation(null) @@ -132,6 +141,7 @@ export function useBookingEdits(options: BookingEditsOptions) { if (data.create_budget_entry) await tripActions.loadBudgetItems?.(tripId) } await afterSave?.(r) + if (!editingTransport) importSaved() toast.success(t(editingTransport ? 'trip.toast.reservationUpdated' : 'trip.toast.reservationAdded')) setShowTransportModal(false) setEditingTransport(null) @@ -224,10 +234,12 @@ export function useBookingEdits(options: BookingEditsOptions) { items: BookingImportPreviewItem[], sourceFiles: File[] = [], kind: 'transports' | 'bookings' = 'bookings', + onSaved: (() => void) | null = null, ) => { if (!items.length) return importSourceFilesRef.current = sourceFiles importKindRef.current = kind + importSavedRef.current = onSaved importQueueRef.current = items.slice(1) setImportReviewActive(true) openImportItem(items[0]) @@ -288,6 +300,7 @@ export function useBookingEdits(options: BookingEditsOptions) { return } importQueueRef.current = [] + importSavedRef.current = null setImportReviewActive(false) setShowReservationModal(false); setEditingReservation(null); setReservationPrefill(null) setShowTransportModal(false); setEditingTransport(null); setTransportPrefill(null); setTransportModalDayId(null)