fix(planner): places put on a day, day names, day notes and trip details changed offline wait in the queue and sync once the connection is back

The day plan wrote straight to the API, so offline a place dropped on a day
snapped back with "Error assigning place", a renamed day or a new day note
failed, and a moved stop, a leg's travel mode or who joins a visit were lost.
Visits (put on a day, taken off, reordered, moved, travellers, leg mode), the
day's name, notes text and travel mode, and the day notes (add, edit, delete,
move) now go through assignmentRepo, dayRepo and dayNoteRepo and the mutation
queue: the cached day changes at once, a visit or note made offline gets a
temporary id that the edits after it wait for, and an online write held behind
an older queued one joins the queue, since none of these carry a version
token. A new order of a day that names a visit made offline is sent with its
real id, or without it when its create failed. Desktop, the phone timeline and
note sheet all write through the store, so both are covered.

The replay reads each answer for what it names: the day answer keeps the
cached notes and visits, the participants answer lands on its visit, and a
visit the server refuses with 409 (a Tour already on that day) fails instead
of waiting as a conflict no retry can solve.

The trip's name, description, reminder and the archive flag wait in the queue
too, from the planner, the phone and the dashboard. What only the server can
do says so instead of failing: new dates, a day count, a currency or a cover,
a new trip, a copy or a delete, members, guests, share and invite links, and
a new order of the days, which moves the dates of the bookings.

Refs #2601
This commit is contained in:
Maurice
2026-10-12 20:17:57 +02:00
parent 92c48fba22
commit bdd8c08dd4
85 changed files with 1557 additions and 88 deletions
@@ -678,7 +678,7 @@ describe('useMPlanTimeline', () => {
const { result } = await renderTimeline(makePlanner())
await act(async () => { result.current.setLegMode(11, 'walking') })
expect(useTripStore.getState().assignments['2'].map(a => a.leg_transport_mode)).toEqual(['walking', undefined])
expect(assignmentsApi.updateTransport).toHaveBeenCalledWith(1, 11, 'walking')
await waitFor(() => expect(assignmentsApi.updateTransport).toHaveBeenCalledWith(1, 11, 'walking'))
})
it('FE-MOB-PLTL-042: reverts to the server state when the leg mode fails to save', async () => {
@@ -1,9 +1,10 @@
import { describe, it, expect, beforeEach, vi } from 'vitest';
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
import { http, HttpResponse } from 'msw';
import { useTripStore } from '../../../src/store/tripStore';
import { resetAllStores, seedStore } from '../../helpers/store';
import { buildPlace, buildAssignment } from '../../helpers/factories';
import { server } from '../../helpers/msw/server';
import { clearAll, offlineDb } from '../../../src/db/offlineDb';
vi.mock('../../../src/api/websocket', () => ({
connect: vi.fn(),
@@ -567,4 +568,63 @@ describe('assignmentsSlice', () => {
expect(useTripStore.getState().assignments).toEqual(next);
});
});
// #2601: offline a place goes on its day, comes off it, moves and is reordered
// through the queue instead of failing.
describe('offline', () => {
const setOnline = (v: boolean) => Object.defineProperty(navigator, 'onLine', { value: v, writable: true, configurable: true });
beforeEach(async () => { await clearAll(); setOnline(false); });
afterEach(async () => { setOnline(true); await clearAll(); });
async function queued(): Promise<[string, string][]> {
return (await offlineDb.mutationQueue.toArray())
.sort((a, b) => a.createdAt - b.createdAt)
.map(m => [m.method, m.url]);
}
it('FE-ASSIGN-OFF-001: a place put on a day offline stays there and waits in the queue', async () => {
const place = buildPlace({ id: 10, trip_id: 1 });
seedStore(useTripStore, { places: [place], assignments: { '1': [] } });
const visit = await useTripStore.getState().assignPlaceToDay(1, 1, 10);
expect(visit?.id).toBeLessThan(0);
expect(useTripStore.getState().assignments['1'].map(a => a.place_id)).toEqual([10]);
expect(await queued()).toEqual([['POST', '/trips/1/days/1/assignments']]);
});
it('FE-ASSIGN-OFF-002: removing, reordering and moving offline keep the change and queue it', async () => {
const a = buildAssignment({ id: 5, day_id: 1, order_index: 0 });
const b = buildAssignment({ id: 6, day_id: 1, order_index: 1 });
seedStore(useTripStore, { assignments: { '1': [a, b], '2': [] } });
await useTripStore.getState().reorderAssignments(1, 1, [6, 5]);
await useTripStore.getState().moveAssignment(1, 6, 1, 2);
await useTripStore.getState().removeAssignment(1, 1, 5);
expect(useTripStore.getState().assignments['1']).toEqual([]);
expect(useTripStore.getState().assignments['2'].map(x => x.id)).toEqual([6]);
expect(await queued()).toEqual([
['PUT', '/trips/1/days/1/assignments/reorder'],
['PUT', '/trips/1/assignments/6/move'],
['DELETE', '/trips/1/days/1/assignments/5'],
]);
});
it('FE-ASSIGN-OFF-003: a leg mode and the travellers chosen offline land on the visit', async () => {
seedStore(useTripStore, { assignments: { '2': [buildAssignment({ id: 5, day_id: 2 })] } });
await offlineDb.tripMembers.put({ tripId: 1, id: 4, username: 'Lena', avatar: null });
await useTripStore.getState().setAssignmentParticipants(1, 2, 5, [4]);
await useTripStore.getState().setAssignmentLegMode(1, 2, 5, 'driving');
const visit = useTripStore.getState().assignments['2'][0];
expect(visit.participants).toEqual([{ user_id: 4, username: 'Lena', avatar: null }]);
expect(visit.leg_transport_mode).toBe('driving');
expect(await queued()).toEqual([
['PUT', '/trips/1/assignments/5/participants'],
['PUT', '/trips/1/assignments/5/transport'],
]);
});
});
});
+47 -1
View File
@@ -1,9 +1,10 @@
import { describe, it, expect, beforeEach, vi } from 'vitest';
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
import { http, HttpResponse } from 'msw';
import { useTripStore } from '../../../src/store/tripStore';
import { resetAllStores, seedStore } from '../../helpers/store';
import { buildDay, buildDayNote } from '../../helpers/factories';
import { server } from '../../helpers/msw/server';
import { clearAll, offlineDb } from '../../../src/db/offlineDb';
vi.mock('../../../src/api/websocket', () => ({
connect: vi.fn(),
@@ -206,4 +207,49 @@ describe('dayNotesSlice', () => {
expect(updatedDay?.title).toBe('Day at the Beach');
});
});
// #2601: offline the plan writes wait in the queue instead of failing.
describe('offline', () => {
const setOnline = (v: boolean) => Object.defineProperty(navigator, 'onLine', { value: v, writable: true, configurable: true });
afterEach(() => setOnline(true));
it('FE-DAYNOTES-OFF-001: a note added offline stays on the day and waits in the queue', async () => {
await clearAll();
seedStore(useTripStore, { dayNotes: { '1': [] } });
setOnline(false);
const note = await useTripStore.getState().addDayNote(1, 1, { text: 'Lunch' });
expect(note.id).toBeLessThan(0);
expect(useTripStore.getState().dayNotes['1'].map(n => n.text)).toEqual(['Lunch']);
expect((await offlineDb.mutationQueue.toArray()).map(m => m.url)).toEqual(['/trips/1/days/1/notes']);
});
it('FE-DAYNOTES-OFF-002: a day renamed offline keeps the new name and waits in the queue', async () => {
await clearAll();
seedStore(useTripStore, { days: [buildDay({ id: 1, trip_id: 1, title: null })] });
setOnline(false);
await useTripStore.getState().updateDayTitle(1, 1, 'Arrival');
expect(useTripStore.getState().days[0].title).toBe('Arrival');
expect((await offlineDb.mutationQueue.toArray()).map(m => m.body)).toEqual([{ title: 'Arrival' }]);
});
it('FE-DAYNOTES-OFF-003: a note moved offline is created on the other day and deleted on its own, in that order', async () => {
await clearAll();
seedStore(useTripStore, { dayNotes: { '1': [buildDayNote({ id: 900, day_id: 1, text: 'Museum' })], '2': [] } });
setOnline(false);
await useTripStore.getState().moveDayNote(1, 1, 2, 900);
expect(useTripStore.getState().dayNotes['2'].map(n => n.text)).toEqual(['Museum']);
expect(useTripStore.getState().dayNotes['1']).toEqual([]);
const queued = (await offlineDb.mutationQueue.toArray()).sort((a, b) => a.createdAt - b.createdAt);
expect(queued.map(m => [m.method, m.url])).toEqual([
['POST', '/trips/1/days/2/notes'],
['DELETE', '/trips/1/days/1/notes/900'],
]);
});
});
});
@@ -0,0 +1,191 @@
/**
* mutationQueue replay of offline plan writes (#2601): a visit put on a day
* moves to its real id and the new order of the day follows it, a note does the
* same on its day, a day answer keeps the notes the cache holds, a trip answer
* keeps the counts of the list, and a visit the server refuses with 409 fails
* instead of waiting as a conflict.
*/
import 'fake-indexeddb/auto';
import { http, HttpResponse } from 'msw';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { clearAll, offlineDb } from '../../../src/db/offlineDb';
import { assignmentRepo } from '../../../src/repo/assignmentRepo';
import { dayNoteRepo } from '../../../src/repo/dayNoteRepo';
import { dayRepo } from '../../../src/repo/dayRepo';
import { tripRepo } from '../../../src/repo/tripRepo';
import { setAuthed } from '../../../src/sync/authGate';
import { mutationQueue } from '../../../src/sync/mutationQueue';
import { buildAssignment, buildDay, buildDayNote, buildPlace, buildTrip } from '../../helpers/factories';
import { server } from '../../helpers/msw/server';
function setOnline(v: boolean): void {
Object.defineProperty(navigator, 'onLine', { value: v, writable: true, configurable: true });
}
const place = buildPlace({ id: 300, trip_id: 4 });
beforeEach(async () => {
await clearAll();
mutationQueue._resetFlushing();
setAuthed(true);
setOnline(false);
await offlineDb.days.put(
buildDay({
id: 70,
trip_id: 4,
title: 'Old',
assignments: [
buildAssignment({ id: 500, day_id: 70, order_index: 0, place: buildPlace({ id: 301, trip_id: 4 }) }),
],
notes_items: [buildDayNote({ id: 900, day_id: 70 })],
})
);
});
afterEach(() => {
vi.restoreAllMocks();
setAuthed(false);
setOnline(true);
});
describe('mutationQueue.flush, plan', () => {
it('FE-SYNC-DAYS-001: a replayed visit takes its real id and the queued order names it', async () => {
const visit = await assignmentRepo.create(4, 70, place);
await assignmentRepo.reorder(4, 70, [visit.id, 500]);
await assignmentRepo.setLegMode(4, visit.id, 'driving');
const orders: unknown[] = [];
const legs: string[] = [];
server.use(
http.post('/api/trips/4/days/70/assignments', () =>
HttpResponse.json({ assignment: buildAssignment({ id: 520, day_id: 70, order_index: 1, place }) })
),
http.put('/api/trips/4/days/70/assignments/reorder', async ({ request }) => {
orders.push(await request.json());
return HttpResponse.json({ success: true });
}),
http.put('/api/trips/4/assignments/:id/transport', ({ params }) => {
legs.push(String(params.id));
return HttpResponse.json({
assignment: buildAssignment({ id: 520, day_id: 70, order_index: 0, leg_transport_mode: 'driving', place }),
});
})
);
setOnline(true);
await mutationQueue.flush();
expect(orders).toEqual([{ orderedIds: [520, 500] }]);
expect(legs).toEqual(['520']);
expect(await offlineDb.mutationQueue.count()).toBe(0);
const ids = (await offlineDb.days.get(70))?.assignments?.map((a) => a.id);
expect(ids).toContain(520);
expect(ids).not.toContain(visit.id);
});
it('FE-SYNC-DAYS-002: an order that names a visit whose create failed is sent without it', async () => {
const visit = await assignmentRepo.create(4, 70, place);
await assignmentRepo.reorder(4, 70, [visit.id, 500]);
const orders: unknown[] = [];
server.use(
http.post('/api/trips/4/days/70/assignments', () =>
HttpResponse.json({ error: 'Place not found' }, { status: 404 })
),
http.put('/api/trips/4/days/70/assignments/reorder', async ({ request }) => {
orders.push(await request.json());
return HttpResponse.json({ success: true });
})
);
setOnline(true);
await mutationQueue.flush();
expect(orders).toEqual([{ orderedIds: [500] }]);
expect((await offlineDb.days.get(70))?.assignments?.map((a) => a.id)).toEqual([500]);
});
it('FE-SYNC-DAYS-003: a visit refused with 409 fails, it does not wait as a conflict', async () => {
await assignmentRepo.create(4, 70, place);
server.use(
http.post('/api/trips/4/days/70/assignments', () =>
HttpResponse.json({ error: 'Tour is already assigned to this day' }, { status: 409 })
)
);
setOnline(true);
await mutationQueue.flush();
const [row] = await offlineDb.mutationQueue.toArray();
expect(row.status).toBe('failed');
expect((await offlineDb.days.get(70))?.assignments?.map((a) => a.id)).toEqual([500]);
});
it('FE-SYNC-DAYS-004: the participants answer lands on the visit it names', async () => {
await assignmentRepo.setParticipants(4, 500, [5]);
server.use(
http.put('/api/trips/4/assignments/500/participants', () =>
HttpResponse.json({ participants: [{ user_id: 5, username: 'tom', avatar: null }] })
)
);
setOnline(true);
await mutationQueue.flush();
expect((await offlineDb.days.get(70))?.assignments?.[0].participants).toEqual([
{ user_id: 5, username: 'tom', avatar: null },
]);
});
it('FE-SYNC-DAYS-005: a replayed note takes its real id and the queued edit follows it', async () => {
const { note } = await dayNoteRepo.create(4, 70, { text: 'Lunch' });
await dayNoteRepo.update(4, 70, note, { text: 'Brunch' });
const puts: string[] = [];
server.use(
http.post('/api/trips/4/days/70/notes', () =>
HttpResponse.json({ note: buildDayNote({ id: 901, day_id: 70, text: 'Lunch' }) })
),
http.put('/api/trips/4/days/70/notes/:id', ({ params }) => {
puts.push(String(params.id));
return HttpResponse.json({ note: buildDayNote({ id: 901, day_id: 70, text: 'Brunch' }) });
})
);
setOnline(true);
await mutationQueue.flush();
expect(puts).toEqual(['901']);
expect((await offlineDb.days.get(70))?.notes_items?.map((n) => [n.id, n.text])).toEqual([
[900, 'Test note'],
[901, 'Brunch'],
]);
});
it('FE-SYNC-DAYS-006: the day answer keeps the notes and visits the cache holds', async () => {
await dayRepo.update(4, 70, { title: 'Arrival' });
server.use(
http.put('/api/trips/4/days/70', () =>
HttpResponse.json({ day: { id: 70, trip_id: 4, title: 'Arrival', notes: null, assignments: [] } })
)
);
setOnline(true);
await mutationQueue.flush();
const day = await offlineDb.days.get(70);
expect(day?.title).toBe('Arrival');
expect(day?.notes_items?.map((n) => n.id)).toEqual([900]);
expect(day?.assignments?.map((a) => a.id)).toEqual([500]);
});
it('FE-SYNC-DAYS-007: a trip renamed offline syncs and keeps the counts of the cached row', async () => {
await offlineDb.trips.put({ ...buildTrip({ id: 4, title: 'Rome' }), place_count: 7 } as never);
await tripRepo.update(4, { title: 'Roma', start_date: buildTrip({ id: 4 }).start_date });
expect((await offlineDb.trips.get(4))?.title).toBe('Roma');
server.use(http.put('/api/trips/4', () => HttpResponse.json({ trip: { ...buildTrip({ id: 4 }), title: 'Roma' } })));
setOnline(true);
await mutationQueue.flush();
expect(await offlineDb.mutationQueue.count()).toBe(0);
expect(await offlineDb.trips.get(4)).toMatchObject({ title: 'Roma', place_count: 7 });
});
});