fix(roadtrip): keep the stay on each visit, not on the place (#3092)

A nullable day_assignments.stay_minutes holds the stay of one visit; a visit without one keeps reading the place's duration, so existing trips drive as before. REST PUT /trips/:tripId/assignments/:id/stay and the MCP tool set_assignment_stay write it with day_edit.
This commit is contained in:
Maurice
2026-11-14 17:48:57 +01:00
parent 535761dbd5
commit bac4599d64
40 changed files with 510 additions and 96 deletions
+20
View File
@@ -3,12 +3,14 @@ import { beforeEach, describe, expect, it, vi } from 'vitest'
import { assignmentRepo } from './assignmentRepo'
import { offlineDb, clearAll } from '../db/offlineDb'
import { saveAssignmentEndDay } from '../api/assignmentEndDay'
import { saveAssignmentStay } from '../api/assignmentStay'
import { isEffectivelyOffline } from '../sync/networkMode'
import { mutationQueue } from '../sync/mutationQueue'
import { apiClient, assignmentsApi } from '../api/client'
import type { Assignment, Day } from '../types'
vi.mock('../api/assignmentEndDay', () => ({ saveAssignmentEndDay: vi.fn() }))
vi.mock('../api/assignmentStay', () => ({ saveAssignmentStay: vi.fn() }))
vi.mock('../sync/networkMode', () => ({ isEffectivelyOffline: vi.fn(() => true) }))
vi.mock('../sync/authGate', () => ({ isAuthed: () => true }))
vi.mock('../api/client', () => ({ apiClient: { request: vi.fn() }, assignmentsApi: { updateTime: vi.fn(), updateNotes: vi.fn(), clearDay: vi.fn() } }))
@@ -51,6 +53,24 @@ describe('assignment day-end persistence', () => {
})
})
describe('assignment stay persistence (#3092)', () => {
it('stores the stay on the one visit offline with a replayable write', async () => {
await assignmentRepo.setStay(9, assignment, 0)
expect((await offlineDb.days.get(1))?.assignments?.[0]).toMatchObject({ id: 7, stay_minutes: 0 })
expect(await offlineDb.mutationQueue.toArray()).toEqual([expect.objectContaining({
url: '/trips/9/assignments/7/stay', method: 'PUT', body: { stay_minutes: 0 }, resource: 'assignments', entityId: 7,
})])
})
it('saves through the online API and caches what came back', async () => {
vi.mocked(isEffectivelyOffline).mockReturnValue(false)
vi.mocked(saveAssignmentStay).mockResolvedValue({ ...assignment, stay_minutes: 20 })
await assignmentRepo.setStay(9, assignment, 20)
expect(saveAssignmentStay).toHaveBeenCalledWith(9, 7, { stay_minutes: 20 })
expect((await offlineDb.days.get(1))?.assignments?.[0].stay_minutes).toBe(20)
})
})
describe('assignment time persistence', () => {
const times = { place_time: '07:00', end_time: null }
+46 -30
View File
@@ -1,5 +1,6 @@
import { assignmentSchema } from '@trek/shared'
import { saveAssignmentEndDay } from '../api/assignmentEndDay'
import { saveAssignmentStay } from '../api/assignmentStay'
import { assignmentsApi } from '../api/client'
import { offlineDb } from '../db/offlineDb'
import { cacheAssignment } from '../db/cacheAssignment'
@@ -32,6 +33,34 @@ async function cacheVisitFields(tripId: number, id: number, fields: Partial<Assi
}
/** The day's visits in the order given; the ones not named keep their place behind them. */
/**
* One field of a saved visit, written through its own route: sent straight away when
* nothing older to the visit waits in the queue, otherwise queued behind it with the
* cached row patched, so the planner shows the change offline as well.
*/
async function writeVisitField(
tripId: number | string,
assignment: Assignment,
write: { suffix: string; body: Record<string, unknown>; fields: Partial<Assignment>; send: () => Promise<Assignment> },
): Promise<Assignment> {
if (!(await mutationQueue.mustQueue('assignments', assignment.id))) {
const saved = await write.send()
await cacheAssignment(saved)
return saved
}
const updated = { ...assignment, ...write.fields }
await offlineDb.transaction('rw', offlineDb.days, offlineDb.mutationQueue, async () => {
await mutationQueue.enqueue({
id: generateUUID(), tripId: Number(tripId), method: 'PUT',
url: `/trips/${tripId}/assignments/${assignment.id}/${write.suffix}`,
body: write.body, resource: 'assignments', entityId: assignment.id,
})
await cacheAssignment(updated)
})
mutationQueue.sendSoon()
return updated
}
function inOrder(items: Assignment[], orderedIds: number[]): Assignment[] {
const rank = new Map(orderedIds.map((id, index) => [id, index]))
return items
@@ -237,42 +266,29 @@ export const assignmentRepo = {
},
async setEndDay(tripId: number | string, assignment: Assignment, endDay: boolean): Promise<Assignment> {
if (!(await mutationQueue.mustQueue('assignments', assignment.id))) {
const saved = await saveAssignmentEndDay(tripId, assignment.id, { end_day: endDay })
await cacheAssignment(saved)
return saved
}
const updated = { ...assignment, end_day: endDay }
await offlineDb.transaction('rw', offlineDb.days, offlineDb.mutationQueue, async () => {
await mutationQueue.enqueue({
id: generateUUID(), tripId: Number(tripId), method: 'PUT',
url: `/trips/${tripId}/assignments/${assignment.id}/end-day`,
body: { end_day: endDay }, resource: 'assignments', entityId: assignment.id,
})
await cacheAssignment(updated)
return await writeVisitField(tripId, assignment, {
suffix: 'end-day', body: { end_day: endDay }, fields: { end_day: endDay },
send: () => saveAssignmentEndDay(tripId, assignment.id, { end_day: endDay }),
})
},
/**
* How long the drive stands at this one visit (#3092), offline too. Null hands the
* visit back to the stay its place carries.
*/
async setStay(tripId: number | string, assignment: Assignment, stayMinutes: number | null): Promise<Assignment> {
return await writeVisitField(tripId, assignment, {
suffix: 'stay', body: { stay_minutes: stayMinutes }, fields: { stay_minutes: stayMinutes },
send: () => saveAssignmentStay(tripId, assignment.id, { stay_minutes: stayMinutes }),
})
mutationQueue.sendSoon()
return updated
},
/** Takes a stop out of the day's route or puts it back (#2532), offline too. */
async setRouteExcluded(tripId: number | string, assignment: Assignment, excluded: boolean): Promise<Assignment> {
if (!(await mutationQueue.mustQueue('assignments', assignment.id))) {
const saved = assignmentSchema.parse((await assignmentsApi.setRouteExcluded(tripId, assignment.id, excluded)).assignment)
await cacheAssignment(saved)
return saved
}
const updated = { ...assignment, route_excluded: excluded }
await offlineDb.transaction('rw', offlineDb.days, offlineDb.mutationQueue, async () => {
await mutationQueue.enqueue({
id: generateUUID(), tripId: Number(tripId), method: 'PUT',
url: `/trips/${tripId}/assignments/${assignment.id}/route`,
body: { excluded }, resource: 'assignments', entityId: assignment.id,
})
await cacheAssignment(updated)
return await writeVisitField(tripId, assignment, {
suffix: 'route', body: { excluded }, fields: { route_excluded: excluded },
send: async () => assignmentSchema.parse((await assignmentsApi.setRouteExcluded(tripId, assignment.id, excluded)).assignment),
})
mutationQueue.sendSoon()
return updated
},
/**