Files
TREK/server/src/db/repositories/TourActivityExclusions.repository.ts
T
Maurice bde2ad2ca8 feat(atlas): sum up the tours you have done, by period and type (STA-02)
With the Tours addon on, the Atlas panel gets a third tab, Tours, beside
Stats and Bucket List: what you have done on your tours across all your
trips. A period (all time, this year, the last 12 months or an earlier year)
and a tour type narrow it; the tab shows the distance large with the number
of tours at its corner, the ascent and descent, and the types with their
distance. "Show tours" opens every tour of the period in a dialog with the
same filters, the sums, the split by type and one row per tour.

What counts is said, never guessed. A tour counts once the day it is on is
over, with the values of the planning, badged "Planned" and explained as
not measured. A linked actual track (ACT-01) replaces those values for its
execution, badged "Recorded" and named, and is never added on top nor
counted as a tour of its own. A tour on several days counts once, a whole
route does not count beside the stages planned from it, a paddle tour sums
no heights, and a value nobody knows stays out of the sum, which then reads
"at least". Only the user's own part counts: a day names them among its
participants, or names nobody on a trip of their own; being able to open
somebody else's trip makes none of its tours theirs. Tours still ahead, on
no day or only on undated days are named as not counting. A tour that fell
through can be left out of the sums ("Leave out"): it stays in the plan
and in the list, dimmed, and "Count again" brings it back. Only the user's
own numbers change.

Server: GET /api/tours/activity-stats answers the user's activity entries
across every trip they can open, and PUT /api/tours/activity-stats/exclusions
leaves a tour out or counts it again (the tour must be in such a trip, else
404). A tour_activity_exclusions table keeps the exclusions per user and
tour. The sums are added up by a shared summary in @trek/shared, so the
Atlas and the assistant say the same numbers. MCP: get_tour_activity_stats
and set_tour_activity_excluded over the same service. The tab reads live
and needs a connection, like the rest of the Atlas statistics.
2026-10-20 15:23:55 +02:00

47 lines
1.7 KiB
TypeScript

import type { TourActivityExclusions } from '../entities/TourActivityExclusions.entity';
import type { DB } from '../kysely/db';
import { TrekRepository } from './_shared/trek-repository';
type ExclusionsDB = Pick<DB, 'tour_activity_exclusions'>;
/**
* `tour_activity_exclusions` (STA-02): the tours a user left out of their own
* activity sums. Personal: every statement names the user, so one person's
* choice never reaches anyone else's sums. The caller checks that the user
* can open the tour's trip before writing.
*/
export class TourActivityExclusionsRepository extends TrekRepository<TourActivityExclusions> {
private db() {
return this.kysely<ExclusionsDB>();
}
/** The tours the user left out, by place id. */
async listPlaceIds(user_id: number): Promise<number[]> {
const rows = await this.db()
.selectFrom('tour_activity_exclusions')
.select('place_id')
.where('user_id', '=', user_id)
.orderBy('place_id', 'asc')
.execute();
return rows.map((row) => row.place_id);
}
/** Leaves the tour out of the user's sums; a second call changes nothing. */
async exclude(user_id: number, place_id: number): Promise<void> {
await this.db()
.insertInto('tour_activity_exclusions')
.values({ user_id, place_id })
.onConflict((oc) => oc.columns(['user_id', 'place_id']).doNothing())
.execute();
}
/** Counts the tour again; nothing to do when it was not left out. */
async include(user_id: number, place_id: number): Promise<void> {
await this.db()
.deleteFrom('tour_activity_exclusions')
.where('user_id', '=', user_id)
.where('place_id', '=', place_id)
.execute();
}
}