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
@@ -89,6 +89,42 @@ describe('Create assignment', () => {
await request(app).put(url).send({ end_day: true }).expect(401);
});
it('ASSIGN-STAY: two visits of one place keep their own stay, the other falls back to the place (#3092)', async () => {
const { user } = createUser(testDb);
const { trip, day, place } = setupAssignmentFixtures(user.id);
await updateRows(orm, Places, { id: place.id }, { duration_minutes: 60 });
const cookie = authCookie(user.id);
const plan = `/api/trips/${trip.id}/days/${day.id}/assignments`;
const first = (await request(app).post(plan).set('Cookie', cookie).send({ place_id: place.id }).expect(201)).body
.assignment;
const second = (await request(app).post(plan).set('Cookie', cookie).send({ place_id: place.id }).expect(201)).body
.assignment;
expect(first.stay_minutes).toBeNull();
const url = `/api/trips/${trip.id}/assignments/${second.id}/stay`;
const changed = await request(app).put(url).set('Cookie', cookie).send({ stay_minutes: 0 }).expect(200);
expect(changed.body.assignment).toMatchObject({ id: second.id, stay_minutes: 0 });
// The other visit and the place itself are untouched.
const listed = await request(app).get(`/api/trips/${trip.id}/days`).set('Cookie', cookie).expect(200);
const visits = listed.body.days[0].assignments as Array<{ id: number; stay_minutes: number | null }>;
expect(visits.find((a) => a.id === first.id)?.stay_minutes).toBeNull();
expect(visits.find((a) => a.id === second.id)?.stay_minutes).toBe(0);
expect((await findRows(orm, Places, { id: place.id }))[0]?.duration_minutes).toBe(60);
await request(app).put(url).set('Cookie', cookie).send({ stay_minutes: -5 }).expect(400);
await request(app).put(url).set('Cookie', cookie).send({ stay_minutes: 1.5 }).expect(400);
const foreign = createTrip(testDb, user.id);
await request(app)
.put(`/api/trips/${foreign.id}/assignments/${second.id}/stay`)
.set('Cookie', cookie)
.send({ stay_minutes: 10 })
.expect(404);
const reset = await request(app).put(url).set('Cookie', cookie).send({ stay_minutes: null }).expect(200);
expect(reset.body.assignment.stay_minutes).toBeNull();
await request(app).put(url).send({ stay_minutes: 10 }).expect(401);
});
it('ASSIGN-001 — POST creates assignment linking place to day', async () => {
const { user } = createUser(testDb);
const { trip, day, place } = setupAssignmentFixtures(user.id);
@@ -227,6 +227,13 @@ export const plan: CaseGroup = {
rest: { method: 'put', path: (s) => `${assignmentPath(s)}/end-day`, body: () => ({ end_day: true }) },
expect: ['assignment:updated'],
},
{
tool: 'set_assignment_stay',
needs: ['assignment'],
args: (s) => ({ ...onAssignment(s), stay_minutes: 15 }),
rest: { method: 'put', path: (s) => `${assignmentPath(s)}/stay`, body: () => ({ stay_minutes: 15 }) },
expect: ['assignment:updated'],
},
{
tool: 'unassign_place',
needs: ['assignment'],
@@ -109,6 +109,7 @@ const UNNUMBERED = [
'Migration20200101046390_a_trip_can_keep_its_costs_as_totals_only',
'Migration20200101046400_a_booked_flight_shows_where_it_is',
'Migration20200101046410_a_forwarded_booking_mail_lands_in_its_trip',
'Migration20200101046420_a_visit_keeps_its_own_stay',
];
// The last step the positional runner ever had; planLegacyBaseline refuses a
// schema_version past it.
@@ -1078,7 +1078,7 @@ const LEGACY_LIST_ROADTRIP_VISITS = `
SELECT a.id, a.day_id, a.order_index, a.place_id, p.name, p.lat, p.lng,
COALESCE(a.assignment_time, p.place_time) AS time,
COALESCE(a.assignment_end_time, p.end_time) AS end_time,
p.duration_minutes, a.end_day,
COALESCE(a.stay_minutes, p.duration_minutes) AS duration_minutes, a.end_day,
a.leg_transport_mode, a.incoming_leg_transport_mode, p.stop_type, p.fill_percent,
a.accommodation_id, stay.id AS stay_id, stay.check_in, stay.check_out, checkout.day_number AS checkout_day
FROM day_assignments a
@@ -1170,6 +1170,26 @@ describe('DayAssignmentsRepository.listRoadtripVisits (RPL2, roadtrip-plan.servi
});
});
it('ASSIGNREPO-036: two visits of one place keep their own stay, an unset one reads the place (#3092)', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const day = createDay(testDb, trip.id);
const place = createPlace(testDb, trip.id, { name: 'Viewpoint' });
await updateRows(t, Places, { id: place.id }, { duration_minutes: 45 });
const stopped = createDayAssignment(testDb, day.id, place.id, { order_index: 0 });
const passed = createDayAssignment(testDb, day.id, place.id, { order_index: 1 });
await withRequestContext(t.orm, () => assignments.setStayMinutes(passed.id, 0));
const visits = await assignments.listRoadtripVisits(trip.id);
expect(visits.map((v) => [v.id, v.duration_minutes])).toEqual([
[stopped.id, 45],
[passed.id, 0],
]);
expect(await assignmentCols(stopped.id, 'stay_minutes')).toEqual({ stay_minutes: null });
expect((await findRow(t, Places, { id: place.id }))?.duration_minutes).toBe(45);
});
it('ASSIGNREPO-033: empty array for a trip with no assignments', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
@@ -110,7 +110,31 @@ describe('Tool: assign_place_to_day', () => {
});
});
it('MDT-MCP-001: puts one stage of a multi-day tour on a day, refusing as REST does', async () => {
it('set_assignment_stay: changes the stay of one visit and leaves the other visit of the place alone (#3092)', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const day = createDay(testDb, trip.id);
const place = createPlace(testDb, trip.id);
const first = createDayAssignment(testDb, day.id, place.id);
const second = createDayAssignment(testDb, day.id, place.id);
await withHarness(user.id, async (h) => {
const changed = await h.client.callTool({
name: 'set_assignment_stay',
arguments: { tripId: trip.id, assignmentId: second.id, stay_minutes: 5 },
});
expect(parseToolResult(changed)).toMatchObject({ assignment: { id: second.id, stay_minutes: 5 } });
const other = createTrip(testDb, user.id);
const refused = await h.client.callTool({
name: 'set_assignment_stay',
arguments: { tripId: other.id, assignmentId: second.id, stay_minutes: 5 },
});
expect(refused.isError).toBe(true);
});
const rows = await findRows(orm, DayAssignments, { id: { $in: [first.id, second.id] } }, { id: 'asc' });
expect(rows.map((r) => r.stay_minutes)).toEqual([null, 5]);
});
it('MDT-MCP-001:puts one stage of a multi-day tour on a day, refusing as REST does', async () => {
const { user } = createUser(testDb);
const trip = createTrip(testDb, user.id);
const day = createDay(testDb, trip.id);
@@ -57,6 +57,7 @@ function makeRow(overrides: Partial<AssignmentWithPlaceRow> = {}): AssignmentWit
tour_place_id: null,
tour_route_geometry: null,
tour_stage_key: null,
stay_minutes: null,
...overrides,
};
}