fix(tours): count a multi-day tour in the activity sums only once all its stages are on days

The activity sums never read which stage a day holds, so a multi-day tour
counted its whole route on the last dated stage day while other stages
were on no day yet, and a recording on one stage took the place of the
whole plan beside it. With stages on days of their own, the route now
counts once every stage is on a dated day of the user's, on the last of
them, and is uncounted until then; a recording counts as what was done
and the plan is not added beside it.
This commit is contained in:
Maurice
2026-10-20 17:00:27 +02:00
parent e40e576e39
commit b284095ee0
6 changed files with 158 additions and 4 deletions
@@ -84,6 +84,7 @@ describe('TourActivitiesRepository stats reads (STA-02)', () => {
derived_from_place_id: null,
trip_id: trip.id,
trip_title: 'Alps',
tour_stage_key: null,
participant_count: 1,
joined: 0,
});
@@ -126,4 +127,12 @@ describe('TourActivitiesRepository stats reads (STA-02)', () => {
expect(await repo.listTracksForStats([theirs.id])).toEqual([]);
expect(await repo.listTracksForStats([])).toEqual([]);
});
it('STA2-REPO-014: names the stage a day holds of a multi-day tour, and none for a day that holds the whole tour', async () => {
const { user, trip, day, hike, onDay } = await fixture();
const onStage = await makeDayAssignment(t, day.id, hike.id, { tour_stage_key: 'hut-1' });
const rows = await repo.listExecutionsForStats([trip.id], user.id);
expect(rows.find((row) => row.assignment_id === onStage.id)?.tour_stage_key).toBe('hut-1');
expect(rows.find((row) => row.assignment_id === onDay.id)?.tour_stage_key).toBeNull();
});
});
@@ -20,6 +20,7 @@ function row(overrides: Partial<TourStatsExecutionRow> = {}): TourStatsExecution
trip_title: 'Alps',
assignment_id: 100,
date: '2026-07-02',
tour_stage_key: null,
participant_count: 0,
joined: 0,
...overrides,
@@ -225,4 +226,57 @@ describe('buildActivityStats (STA-02)', () => {
['Zugspitze', '2026-07-02'],
]);
});
describe('a multi-day tour with its stages on days of their own (MDT-02)', () => {
const keys = new Map([[10, ['start', 'hut-1', 'hut-2']]]);
const staged = (executions: TourStatsExecutionRow[], tracks: TourStatsTrackRow[] = []) =>
buildActivityStats({ executions, tracks, excluded: new Set(), stageKeys: keys });
const stage = (assignment_id: number, tour_stage_key: string, date: string | null) =>
row({ assignment_id, tour_stage_key, date });
it('STA2-HELP-017: counts the whole route once, on the last stage day, only once every stage is on a dated day', () => {
const all = staged([
stage(100, 'start', '2026-07-02'),
stage(101, 'hut-1', '2026-07-03'),
stage(102, 'hut-2', '2026-07-04'),
]);
expect(all.entries).toHaveLength(1);
expect(all.entries[0]).toMatchObject({ basis: 'plan', date: '2026-07-04', distance_km: 12.5, day_count: 3 });
// One stage on no day: the route is not counted on the days of the other two.
const partly = staged([stage(100, 'start', '2026-07-02'), stage(101, 'hut-1', '2026-07-03')]);
expect(partly.entries).toEqual([]);
expect(partly.uncounted).toEqual({ unassigned: 1, undated: 0 });
const undated = staged([
stage(100, 'start', '2026-07-02'),
stage(101, 'hut-1', '2026-07-03'),
stage(102, 'hut-2', null),
]);
expect(undated.entries).toEqual([]);
expect(undated.uncounted).toEqual({ unassigned: 0, undated: 1 });
// A day that holds the whole tour covers every stage.
const whole = staged([stage(100, 'start', '2026-07-02'), row({ assignment_id: 103, date: '2026-07-05' })]);
expect(whole.entries).toMatchObject([{ basis: 'plan', date: '2026-07-05' }]);
});
it('STA2-HELP-018: a recording on one stage counts as what was done, never beside the whole route', () => {
const result = staged(
[stage(100, 'start', '2026-07-02'), stage(101, 'hut-1', '2026-07-03'), stage(102, 'hut-2', '2026-07-04')],
[track({ assignment_id: 101 })],
);
expect(result.entries).toHaveLength(1);
expect(result.entries[0]).toMatchObject({ basis: 'track', date: '2026-07-03', day_count: 1 });
expect(result.entries.some((entry) => entry.basis === 'plan')).toBe(false);
});
it('STA2-HELP-019: a tour without stage keys, or with none of its days on a stage, is counted whole as before', () => {
const plain = buildActivityStats({ executions: [row()], tracks: [], excluded: new Set(), stageKeys: keys });
expect(plain.entries).toMatchObject([{ basis: 'plan', date: '2026-07-02' }]);
const executions = [stage(100, 'start', '2026-07-02')];
const noKeys = buildActivityStats({ executions, tracks: [], excluded: new Set() });
expect(noKeys.entries).toMatchObject([{ basis: 'plan', date: '2026-07-02' }]);
});
});
});
@@ -13,7 +13,7 @@ import { createSnapshotTestDb } from '../../helpers/db-mock';
import { makeDayAssignment } from '../../helpers/factories/itinerary';
import { makePlace } from '../../helpers/factories/places';
import { countRows, findRow, insertRow } from '../../helpers/factories/rows';
import { makeTour, makeTourActivity } from '../../helpers/factories/tours';
import { addTourWaypoints, makeTour, makeTourActivity } from '../../helpers/factories/tours';
import { addTripMember, makeDay, makeTrip } from '../../helpers/factories/trips';
import { makeUser } from '../../helpers/factories/users';
import { resetTestDb } from '../../helpers/test-db';
@@ -155,4 +155,29 @@ describe('TourActivityStatsService', () => {
const { user } = await makeUser(orm());
expect(await service.stats(user.id)).toEqual({ entries: [], uncounted: { unassigned: 0, undated: 0 } });
});
it('STA2-SVC-007: a multi-day tour with a stage on no day is not counted on the days of its other stages (MDT-02)', async () => {
const { user } = await makeUser(orm());
const trip = await makeTrip(orm(), user.id, { title: 'Hut to hut' });
const first = await makeDay(orm(), trip.id, { date: '2026-07-02' });
const second = await makeDay(orm(), trip.id, { date: '2026-07-03' });
const route = await makePlace(orm(), trip.id, { name: 'Three huts' });
await makeTour(orm(), route.id, { distance: 40 });
await addTourWaypoints(orm(), route.id, [
{ lat: 47, lng: 11, station_key: 'start' },
{ lat: 47.1, lng: 11, station_key: 'hut-1', stage_end: 1 },
{ lat: 47.2, lng: 11, station_key: 'hut-2', stage_end: 1 },
{ lat: 47.3, lng: 11, station_key: 'end' },
]);
await makeDayAssignment(orm(), first.id, route.id, { tour_stage_key: 'start' });
await makeDayAssignment(orm(), second.id, route.id, { tour_stage_key: 'hut-1' });
const partly = await service.stats(user.id);
expect(partly.entries).toEqual([]);
expect(partly.uncounted).toEqual({ unassigned: 1, undated: 0 });
const third = await makeDay(orm(), trip.id, { date: '2026-07-04' });
await makeDayAssignment(orm(), third.id, route.id, { tour_stage_key: 'hut-2' });
const whole = await service.stats(user.id);
expect(whole.entries).toMatchObject([{ place_id: route.id, basis: 'plan', date: '2026-07-04', distance_km: 40 }]);
});
});