mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-04 01:44:13 +00:00
acc294dd8e
A day of a tour that names nobody now counts for every member of the trip, not only for its owner. In the planner an empty participant list means everyone, and a full member list is stored back as empty, so members got nothing for most shared tours. Only a day that names participants without the user stays theirs. The tours of a shared trip on no day or only on undated days are now named as not counting for members as well. A whole route a multi-day tour's stages were planned from is no longer dropped on the server as soon as any stage has a day. Each entry carries derived_from_place_id and the shared summary leaves the route out only while one of its stages is done, counted and in the period and type. A route walked last year now still counts when a stage of it is planned for next month or was left out because it fell through, in the Atlas and over MCP alike. A tour on several days is dated on the last of them, as is a recording on no day, so it no longer counts while it is still under way. The info text, the get_tour_activity_stats description and the wiki say so.
159 lines
8.0 KiB
TypeScript
159 lines
8.0 KiB
TypeScript
/**
|
||
* TourActivityStatsService (STA-02): the user's own tour activities across
|
||
* every trip they can open, and leaving one out of them. The service comes out
|
||
* of ToursModule over a snapshot test db; rows are seeded through the ORM
|
||
* factories.
|
||
*/
|
||
import { AssignmentParticipants } from '../../../src/db/entities/AssignmentParticipants.entity';
|
||
import { TourActivityExclusions } from '../../../src/db/entities/TourActivityExclusions.entity';
|
||
import { Tours } from '../../../src/db/entities/Tours.entity';
|
||
import { TourActivityStatsService } from '../../../src/nest/tours/tour-activity-stats.service';
|
||
import { ToursModule } from '../../../src/nest/tours/tours.module';
|
||
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 { addTripMember, makeDay, makeTrip } from '../../helpers/factories/trips';
|
||
import { makeUser } from '../../helpers/factories/users';
|
||
import { resetTestDb } from '../../helpers/test-db';
|
||
import { createTestModule, type TestModule } from '../../helpers/test-module';
|
||
import { MikroORM } from '@mikro-orm/core';
|
||
import { NotFoundException } from '@nestjs/common';
|
||
|
||
import { afterAll, beforeAll, beforeEach, describe, expect, it } from 'vitest';
|
||
|
||
const db = createSnapshotTestDb();
|
||
let t: TestModule;
|
||
let service: TourActivityStatsService;
|
||
const orm = () => t.get(MikroORM);
|
||
|
||
beforeAll(async () => {
|
||
t = await createTestModule({ db, imports: [ToursModule] });
|
||
service = t.get(TourActivityStatsService);
|
||
});
|
||
beforeEach(() => resetTestDb(db));
|
||
afterAll(async () => {
|
||
await t.close();
|
||
db.close();
|
||
});
|
||
|
||
const RECORDED = JSON.stringify([
|
||
[47, 11, 600],
|
||
[47.01, 11.005, 760],
|
||
[47.02, 11, 880],
|
||
]);
|
||
|
||
/**
|
||
* The user's trip with a hike on a day and a paddle tour recorded on another;
|
||
* a trip of a friend the user is a member of, with one tour whose day names
|
||
* only the friend, one naming only the user and one naming nobody, which is
|
||
* everyone's; and a trip the user cannot open.
|
||
*/
|
||
async function fixture() {
|
||
const { user } = await makeUser(orm());
|
||
const { user: friend } = await makeUser(orm());
|
||
const { user: stranger } = await makeUser(orm());
|
||
|
||
const trip = await makeTrip(orm(), user.id, { title: 'Alps' });
|
||
const day1 = await makeDay(orm(), trip.id, { date: '2026-07-02' });
|
||
const day2 = await makeDay(orm(), trip.id, { date: '2026-07-03' });
|
||
const hike = await makePlace(orm(), trip.id, { name: 'Ridge' });
|
||
await makeTour(orm(), hike.id, { distance: 12.5, elevation_gain: 800, elevation_loss: 790 });
|
||
await makeDayAssignment(orm(), day1.id, hike.id);
|
||
const paddle = await makePlace(orm(), trip.id, { name: 'Lake' });
|
||
await makeTour(orm(), paddle.id, { tourTypeRef: 'kayak', distance: 9 });
|
||
const onDay2 = await makeDayAssignment(orm(), day2.id, paddle.id);
|
||
const watch = await makePlace(orm(), trip.id, { name: 'Watch export', route_geometry: RECORDED });
|
||
await makeTour(orm(), watch.id, { origin: 'gpx' });
|
||
await makeTourActivity(orm(), paddle.id, watch.id, { assignment: onDay2.id });
|
||
const idle = await makePlace(orm(), trip.id, { name: 'Idle' });
|
||
await makeTour(orm(), idle.id);
|
||
|
||
const theirs = await makeTrip(orm(), friend.id, { title: 'Theirs' });
|
||
await addTripMember(orm(), theirs.id, user.id);
|
||
const theirDay = await makeDay(orm(), theirs.id, { date: '2026-08-10' });
|
||
const alone = await makePlace(orm(), theirs.id, { name: 'Alone' });
|
||
await makeTour(orm(), alone.id, { distance: 30 });
|
||
const aloneOn = await makeDayAssignment(orm(), theirDay.id, alone.id);
|
||
await insertRow(orm(), AssignmentParticipants, { assignment: aloneOn.id, user: friend.id });
|
||
const everyone = await makePlace(orm(), theirs.id, { name: 'Everyone' });
|
||
await makeTour(orm(), everyone.id, { distance: 5 });
|
||
await makeDayAssignment(orm(), theirDay.id, everyone.id);
|
||
const together = await makePlace(orm(), theirs.id, { name: 'Together' });
|
||
await makeTour(orm(), together.id, { distance: 7 });
|
||
const togetherOn = await makeDayAssignment(orm(), theirDay.id, together.id);
|
||
await insertRow(orm(), AssignmentParticipants, { assignment: togetherOn.id, user: user.id });
|
||
|
||
const closed = await makeTrip(orm(), stranger.id);
|
||
const closedDay = await makeDay(orm(), closed.id, { date: '2026-07-01' });
|
||
const hidden = await makePlace(orm(), closed.id, { name: 'Hidden' });
|
||
await makeTour(orm(), hidden.id);
|
||
await makeDayAssignment(orm(), closedDay.id, hidden.id);
|
||
return { user, friend, hike, paddle, watch, alone, everyone, together, togetherOn, hidden };
|
||
}
|
||
|
||
describe('TourActivityStatsService', () => {
|
||
it('STA2-SVC-001: lists the own tours of every trip the user can open, plan or recording, newest first', async () => {
|
||
const { user, hike, paddle, watch, everyone, together } = await fixture();
|
||
const result = await service.stats(user.id);
|
||
expect(result.entries.map((entry) => [entry.place_id, entry.basis, entry.date])).toEqual([
|
||
[everyone.id, 'plan', '2026-08-10'],
|
||
[together.id, 'plan', '2026-08-10'],
|
||
[paddle.id, 'track', '2026-07-03'],
|
||
[hike.id, 'plan', '2026-07-02'],
|
||
]);
|
||
expect(result.entries[2]).toMatchObject({
|
||
tour_type: 'kayak',
|
||
trip: { title: 'Alps' },
|
||
track: { place_id: watch.id, name: 'Watch export' },
|
||
ascent_m: null,
|
||
descent_m: null,
|
||
});
|
||
expect(result.entries[2]?.distance_km).toBeCloseTo(2.35, 1);
|
||
expect(result.uncounted).toEqual({ unassigned: 1, undated: 0 });
|
||
});
|
||
|
||
it('STA2-SVC-002: the friend sees their own and everyone’s tours, and not the user’s trip they cannot open', async () => {
|
||
const { friend, alone, everyone, together } = await fixture();
|
||
const result = await service.stats(friend.id);
|
||
// Together names only the user, so it is not the friend's, owner or not.
|
||
expect(result.entries.map((entry) => entry.place_id)).toEqual([alone.id, everyone.id]);
|
||
expect(result.entries.some((entry) => entry.place_id === together.id)).toBe(false);
|
||
});
|
||
|
||
it('STA2-SVC-003: leaving a tour out keeps it listed, flagged; counting it again clears the flag', async () => {
|
||
const { user, hike } = await fixture();
|
||
const left = await service.setExcluded(user.id, { place_id: hike.id, excluded: true });
|
||
expect(left.entries.find((entry) => entry.place_id === hike.id)?.excluded).toBe(true);
|
||
expect(await findRow(orm(), Tours, { place: hike.id })).toMatchObject({ distance: 12.5 });
|
||
const back = await service.setExcluded(user.id, { place_id: hike.id, excluded: false });
|
||
expect(back.entries.find((entry) => entry.place_id === hike.id)?.excluded).toBe(false);
|
||
expect(await countRows(orm(), TourActivityExclusions, {})).toBe(0);
|
||
});
|
||
|
||
it('STA2-SVC-004: a tour of a trip the user cannot open reads as missing and nothing is written', async () => {
|
||
const { user, hidden } = await fixture();
|
||
await expect(service.setExcluded(user.id, { place_id: hidden.id, excluded: true })).rejects.toBeInstanceOf(
|
||
NotFoundException,
|
||
);
|
||
await expect(service.setExcluded(user.id, { place_id: 999_999, excluded: true })).rejects.toBeInstanceOf(
|
||
NotFoundException,
|
||
);
|
||
expect(await countRows(orm(), TourActivityExclusions, {})).toBe(0);
|
||
});
|
||
|
||
it('STA2-SVC-005: one user leaving a tour out changes nobody else’s list', async () => {
|
||
const { user, friend, together, togetherOn } = await fixture();
|
||
await insertRow(orm(), AssignmentParticipants, { assignment: togetherOn.id, user: friend.id });
|
||
await service.setExcluded(user.id, { place_id: together.id, excluded: true });
|
||
const friends = await service.stats(friend.id);
|
||
expect(friends.entries.find((entry) => entry.place_id === together.id)?.excluded).toBe(false);
|
||
});
|
||
|
||
it('STA2-SVC-006: a user without trips has nothing to count', async () => {
|
||
const { user } = await makeUser(orm());
|
||
expect(await service.stats(user.id)).toEqual({ entries: [], uncounted: { unassigned: 0, undated: 0 } });
|
||
});
|
||
});
|