mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 16:33:16 +00:00
fix(places): the second CI run, split the library reads and test the rating order
The reads of what an entry of All places keeps beside its facts (tags, notes, votes, planned days, bookings, list roles) move from PlacesLibraryRepository into PlacesLibraryDetailsRepository, so neither file passes the size limit. New end to end tests cover the rating filter, the tab counts under it, the rating order and its cursor, and list_all_places with both. The test types follow the vote shapes, the edit target says it may be null, the bookings pair names the booking card that now holds the booking model, and the size baselines drop to the measured lengths.
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
import { castIntegerKysely, containsIgnoringCaseKysely } from '../dialect/kysely-functions';
|
||||
import type { DB } from '../kysely/db';
|
||||
import { memberRoleOf } from './_shared/collection-role';
|
||||
import type { EntityManager } from '@mikro-orm/core';
|
||||
import type { SqlEntityManager } from '@mikro-orm/sql';
|
||||
import { COORD_DEDUP_TOLERANCE } from '@trek/shared';
|
||||
@@ -122,22 +121,6 @@ interface LibraryTagMatch {
|
||||
reach: LibraryArms;
|
||||
}
|
||||
|
||||
/** A tag of a trip on one of its places (`place_tags`). */
|
||||
export interface LibraryTripTagRow {
|
||||
place_id: number;
|
||||
id: number;
|
||||
name: string;
|
||||
color: string | null;
|
||||
}
|
||||
|
||||
/** A tag a trip or a list owns, with the trip or list it belongs to. */
|
||||
export interface LibraryOwnedTagRow {
|
||||
owner_id: number;
|
||||
id: number;
|
||||
name: string;
|
||||
color: string | null;
|
||||
}
|
||||
|
||||
/** Where an entry stands in the list's order: its sort value and its key. */
|
||||
export interface LibraryPosition {
|
||||
value: string;
|
||||
@@ -235,55 +218,6 @@ export interface LibraryIdentityRow {
|
||||
category_icon: string | null;
|
||||
}
|
||||
|
||||
interface LibraryCollectionRole {
|
||||
id: number;
|
||||
role: 'owner' | 'admin' | 'editor' | 'viewer';
|
||||
}
|
||||
|
||||
interface LibraryNotesRow {
|
||||
id: number;
|
||||
notes: string | null;
|
||||
}
|
||||
|
||||
interface LibraryLabelRow {
|
||||
collection_place_id: number;
|
||||
id: number;
|
||||
name: string;
|
||||
color: string | null;
|
||||
}
|
||||
|
||||
export interface LibraryRatingRow {
|
||||
target_id: number;
|
||||
user_id: number;
|
||||
rating: number;
|
||||
/** Who voted, as the list's votes name them, and their stored picture. */
|
||||
username?: string;
|
||||
avatar?: string | null;
|
||||
/** When the vote was last changed (cast, when never changed since), and its row, to tell which of a person's votes came last. */
|
||||
changed_at?: string | null;
|
||||
id?: number;
|
||||
}
|
||||
|
||||
/** One of a person's votes: on a trip place (from_list 0) or a saved place. */
|
||||
export interface LibraryOwnVoteRow {
|
||||
from_list: number;
|
||||
use_id: number;
|
||||
rating: number;
|
||||
/** When the person last changed the vote, or cast it when they never changed it. */
|
||||
changed_at: string | null;
|
||||
id: number;
|
||||
}
|
||||
|
||||
interface LibraryDayRow {
|
||||
place_id: number;
|
||||
day_id: number;
|
||||
day_number: number;
|
||||
date: string | null;
|
||||
title: string | null;
|
||||
time: string | null;
|
||||
end_time: string | null;
|
||||
}
|
||||
|
||||
/**
|
||||
* PLC-07/PLC-08 "All places": the saved places of the lists a user can open
|
||||
* and the places of the trips they can open, read as one list. Not bound to an
|
||||
@@ -782,43 +716,6 @@ export class PlacesLibraryRepository {
|
||||
return row ? Number(row.anchor_key) : null;
|
||||
}
|
||||
|
||||
/** The tags on the trip places (PLC-21), by name. */
|
||||
async tripPlaceTags(placeIds: number[]): Promise<LibraryTripTagRow[]> {
|
||||
const rows = await this.db()
|
||||
.selectFrom('place_tags as pt')
|
||||
.innerJoin('tags as tg', 'tg.id', 'pt.tag_id')
|
||||
.select(['pt.place_id', 'tg.id', 'tg.name', 'tg.color'])
|
||||
.where('pt.place_id', 'in', idsOrNone(placeIds))
|
||||
.orderBy('tg.name')
|
||||
.orderBy('tg.id')
|
||||
.execute();
|
||||
// A legacy row can hold a number as its name, so it is read as text.
|
||||
return rows.map((row) => ({ ...row, name: String(row.name) }));
|
||||
}
|
||||
|
||||
/** Every tag the trips own (PLC-21), by name. */
|
||||
async tripTags(tripIds: number[]): Promise<LibraryOwnedTagRow[]> {
|
||||
const rows = await this.db()
|
||||
.selectFrom('tags as tg')
|
||||
.select(['tg.trip_id as owner_id', 'tg.id', 'tg.name', 'tg.color'])
|
||||
.where('tg.trip_id', 'in', idsOrNone(tripIds))
|
||||
.orderBy('tg.name')
|
||||
.orderBy('tg.id')
|
||||
.execute();
|
||||
return rows.map((row) => ({ ...row, owner_id: Number(row.owner_id), name: String(row.name) }));
|
||||
}
|
||||
|
||||
/** Every label of the lists (PLC-21: a list's tags), in each list's label order. */
|
||||
async listLabels(collectionIds: number[]): Promise<LibraryOwnedTagRow[]> {
|
||||
return await this.db()
|
||||
.selectFrom('collection_labels as lb')
|
||||
.select(['lb.collection_id as owner_id', 'lb.id', 'lb.name', 'lb.color'])
|
||||
.where('lb.collection_id', 'in', idsOrNone(collectionIds))
|
||||
.orderBy('lb.sort_order')
|
||||
.orderBy('lb.id')
|
||||
.execute();
|
||||
}
|
||||
|
||||
/** The trip places with their trip and category, for the entries of a page. */
|
||||
async tripPlaces(placeIds: number[], tripIds: number[]): Promise<LibraryTripPlaceRow[]> {
|
||||
return await this.db()
|
||||
@@ -917,187 +814,6 @@ export class PlacesLibraryRepository {
|
||||
.where('up.id', 'in', idsOrNone(ids))
|
||||
.execute();
|
||||
}
|
||||
|
||||
/**
|
||||
* The user's role on each of `collectionIds`: owner, or an accepted member
|
||||
* row's role read the way `resolveCollectionRole` reads it. A list with
|
||||
* neither reads as viewer, so it is never editable by mistake.
|
||||
*/
|
||||
async collectionRoles(userId: number, collectionIds: number[]): Promise<LibraryCollectionRole[]> {
|
||||
const db = this.db();
|
||||
const ids = idsOrNone(collectionIds);
|
||||
const lists = await db.selectFrom('collections').select(['id', 'owner_id']).where('id', 'in', ids).execute();
|
||||
const members = await db
|
||||
.selectFrom('collection_members')
|
||||
.select(['collection_id', 'role'])
|
||||
.where('user_id', '=', userId)
|
||||
.where('status', '=', 'accepted')
|
||||
.where('collection_id', 'in', ids)
|
||||
.execute();
|
||||
const memberRole = new Map(members.map((row) => [row.collection_id, row.role]));
|
||||
return lists.map((list) => {
|
||||
if (list.owner_id === userId) return { id: list.id, role: 'owner' as const };
|
||||
const stored = memberRole.get(list.id);
|
||||
return { id: list.id, role: stored === undefined ? ('viewer' as const) : memberRoleOf(stored) };
|
||||
});
|
||||
}
|
||||
|
||||
async tripPlaceNotes(placeIds: number[]): Promise<LibraryNotesRow[]> {
|
||||
return await this.db()
|
||||
.selectFrom('places')
|
||||
.select(['id', 'notes'])
|
||||
.where('id', 'in', idsOrNone(placeIds))
|
||||
.execute();
|
||||
}
|
||||
|
||||
async savedPlaceNotes(collectionPlaceIds: number[]): Promise<LibraryNotesRow[]> {
|
||||
return await this.db()
|
||||
.selectFrom('collection_places')
|
||||
.select(['id', 'notes'])
|
||||
.where('id', 'in', idsOrNone(collectionPlaceIds))
|
||||
.execute();
|
||||
}
|
||||
|
||||
/** The labels of the saved places, in each list's label order. */
|
||||
async savedPlaceLabels(collectionPlaceIds: number[]): Promise<LibraryLabelRow[]> {
|
||||
return await this.db()
|
||||
.selectFrom('collection_place_labels as cpl')
|
||||
.innerJoin('collection_labels as l', 'l.id', 'cpl.label_id')
|
||||
.select(['cpl.collection_place_id', 'l.id', 'l.name', 'l.color'])
|
||||
.where('cpl.collection_place_id', 'in', idsOrNone(collectionPlaceIds))
|
||||
.orderBy('l.sort_order')
|
||||
.orderBy('l.id')
|
||||
.execute();
|
||||
}
|
||||
|
||||
/** The votes on the saved places, each with who cast it (their name and stored picture). */
|
||||
async savedPlaceRatings(collectionPlaceIds: number[]): Promise<LibraryRatingRow[]> {
|
||||
return await this.db()
|
||||
.selectFrom('collection_place_ratings as cr')
|
||||
.innerJoin('users as vu', 'vu.id', 'cr.user_id')
|
||||
.select([
|
||||
'cr.collection_place_id as target_id',
|
||||
'cr.user_id',
|
||||
'cr.rating',
|
||||
(eb) => eb.fn.coalesce('cr.updated_at', 'cr.created_at').as('changed_at'),
|
||||
'cr.id',
|
||||
'vu.username',
|
||||
'vu.avatar',
|
||||
])
|
||||
.where('cr.collection_place_id', 'in', idsOrNone(collectionPlaceIds))
|
||||
.execute();
|
||||
}
|
||||
|
||||
/** The votes on the trip places, each with who cast it (their name and stored picture). */
|
||||
async tripPlaceRatings(placeIds: number[]): Promise<LibraryRatingRow[]> {
|
||||
return await this.db()
|
||||
.selectFrom('place_ratings as pr')
|
||||
.innerJoin('users as vu', 'vu.id', 'pr.user_id')
|
||||
.select([
|
||||
'pr.place_id as target_id',
|
||||
'pr.user_id',
|
||||
'pr.rating',
|
||||
(eb) => eb.fn.coalesce('pr.updated_at', 'pr.created_at').as('changed_at'),
|
||||
'pr.id',
|
||||
'vu.username',
|
||||
'vu.avatar',
|
||||
])
|
||||
.where('pr.place_id', 'in', idsOrNone(placeIds))
|
||||
.execute();
|
||||
}
|
||||
|
||||
/** Every vote of the user, on trip places and on saved places, in row order. */
|
||||
async votesOfUser(userId: number): Promise<LibraryOwnVoteRow[]> {
|
||||
const db = this.db();
|
||||
const trips = await db
|
||||
.selectFrom('place_ratings')
|
||||
.select([
|
||||
'place_id as use_id',
|
||||
'rating',
|
||||
(eb) => eb.fn.coalesce('updated_at', 'created_at').as('changed_at'),
|
||||
'id',
|
||||
])
|
||||
.where('user_id', '=', userId)
|
||||
.orderBy('id')
|
||||
.execute();
|
||||
const saved = await db
|
||||
.selectFrom('collection_place_ratings')
|
||||
.select([
|
||||
'collection_place_id as use_id',
|
||||
'rating',
|
||||
(eb) => eb.fn.coalesce('updated_at', 'created_at').as('changed_at'),
|
||||
'id',
|
||||
])
|
||||
.where('user_id', '=', userId)
|
||||
.orderBy('id')
|
||||
.execute();
|
||||
return [...trips.map((row) => ({ ...row, from_list: 0 })), ...saved.map((row) => ({ ...row, from_list: 1 }))];
|
||||
}
|
||||
|
||||
/** Up to `limit` of the people with a vote anywhere, by id, after the id `after`. */
|
||||
async voterIds(after: number, limit: number): Promise<number[]> {
|
||||
const db = this.db();
|
||||
const trips = await db
|
||||
.selectFrom('place_ratings')
|
||||
.select('user_id')
|
||||
.distinct()
|
||||
.where('user_id', '>', after)
|
||||
.orderBy('user_id')
|
||||
.limit(limit)
|
||||
.execute();
|
||||
const saved = await db
|
||||
.selectFrom('collection_place_ratings')
|
||||
.select('user_id')
|
||||
.distinct()
|
||||
.where('user_id', '>', after)
|
||||
.orderBy('user_id')
|
||||
.limit(limit)
|
||||
.execute();
|
||||
const ids = new Set([...trips, ...saved].map((row) => Number(row.user_id)));
|
||||
return [...ids].sort((a, b) => a - b).slice(0, limit);
|
||||
}
|
||||
|
||||
/** The days each place is planned on, in day order, with the time set there. */
|
||||
async plannedDays(placeIds: number[]): Promise<LibraryDayRow[]> {
|
||||
return await this.db()
|
||||
.selectFrom('day_assignments as da')
|
||||
.innerJoin('days as d', 'd.id', 'da.day_id')
|
||||
.select([
|
||||
'da.place_id',
|
||||
'd.id as day_id',
|
||||
'd.day_number',
|
||||
'd.date',
|
||||
'd.title',
|
||||
'da.assignment_time as time',
|
||||
'da.assignment_end_time as end_time',
|
||||
])
|
||||
.where('da.place_id', 'in', idsOrNone(placeIds))
|
||||
.orderBy('d.day_number')
|
||||
.orderBy('da.order_index')
|
||||
.orderBy('da.id')
|
||||
.execute();
|
||||
}
|
||||
|
||||
/** Which of the places a stay points at, and which a booking points at. */
|
||||
async bookedPlaces(placeIds: number[]): Promise<{ stays: number[]; bookings: number[] }> {
|
||||
const db = this.db();
|
||||
const ids = idsOrNone(placeIds);
|
||||
const stays = await db
|
||||
.selectFrom('day_accommodations')
|
||||
.select('place_id')
|
||||
.distinct()
|
||||
.where('place_id', 'in', ids)
|
||||
.execute();
|
||||
const bookings = await db
|
||||
.selectFrom('reservations')
|
||||
.select('place_id')
|
||||
.distinct()
|
||||
.where('place_id', 'in', ids)
|
||||
.execute();
|
||||
const present = (rows: { place_id: number | null }[]) =>
|
||||
rows.flatMap((row) => (row.place_id === null ? [] : [row.place_id]));
|
||||
return { stays: present(stays), bookings: present(bookings) };
|
||||
}
|
||||
}
|
||||
|
||||
/** A use as the union reads it, its numbers as numbers whatever the engine hands back. */
|
||||
|
||||
@@ -0,0 +1,323 @@
|
||||
import type { DB } from '../kysely/db';
|
||||
import { memberRoleOf } from './_shared/collection-role';
|
||||
import type { EntityManager } from '@mikro-orm/core';
|
||||
import type { SqlEntityManager } from '@mikro-orm/sql';
|
||||
|
||||
import type { Kysely } from 'kysely';
|
||||
|
||||
type LibraryDetailsDB = Pick<
|
||||
DB,
|
||||
| 'places'
|
||||
| 'tours'
|
||||
| 'day_accommodations'
|
||||
| 'reservations'
|
||||
| 'collection_places'
|
||||
| 'collections'
|
||||
| 'collection_members'
|
||||
| 'collection_place_labels'
|
||||
| 'collection_labels'
|
||||
| 'collection_place_ratings'
|
||||
| 'place_ratings'
|
||||
| 'day_assignments'
|
||||
| 'days'
|
||||
| 'tags'
|
||||
| 'place_tags'
|
||||
| 'users'
|
||||
>;
|
||||
|
||||
/** No row has the id 0: an empty set of ids reads as this one, so a statement keeps its shape and matches nothing. */
|
||||
const idsOrNone = (ids: readonly number[]): number[] => (ids.length > 0 ? [...ids] : [0]);
|
||||
|
||||
/** A tag of a trip on one of its places (`place_tags`). */
|
||||
export interface LibraryTripTagRow {
|
||||
place_id: number;
|
||||
id: number;
|
||||
name: string;
|
||||
color: string | null;
|
||||
}
|
||||
/** A tag a trip or a list owns, with the trip or list it belongs to. */
|
||||
export interface LibraryOwnedTagRow {
|
||||
owner_id: number;
|
||||
id: number;
|
||||
name: string;
|
||||
color: string | null;
|
||||
}
|
||||
interface LibraryCollectionRole {
|
||||
id: number;
|
||||
role: 'owner' | 'admin' | 'editor' | 'viewer';
|
||||
}
|
||||
interface LibraryNotesRow {
|
||||
id: number;
|
||||
notes: string | null;
|
||||
}
|
||||
interface LibraryLabelRow {
|
||||
collection_place_id: number;
|
||||
id: number;
|
||||
name: string;
|
||||
color: string | null;
|
||||
}
|
||||
export interface LibraryRatingRow {
|
||||
target_id: number;
|
||||
user_id: number;
|
||||
rating: number;
|
||||
/** Who voted, as the list's votes name them, and their stored picture. */
|
||||
username?: string;
|
||||
avatar?: string | null;
|
||||
/** When the vote was last changed (cast, when never changed since), and its row, to tell which of a person's votes came last. */
|
||||
changed_at?: string | null;
|
||||
id?: number;
|
||||
}
|
||||
/** One of a person's votes: on a trip place (from_list 0) or a saved place. */
|
||||
export interface LibraryOwnVoteRow {
|
||||
from_list: number;
|
||||
use_id: number;
|
||||
rating: number;
|
||||
/** When the person last changed the vote, or cast it when they never changed it. */
|
||||
changed_at: string | null;
|
||||
id: number;
|
||||
}
|
||||
interface LibraryDayRow {
|
||||
place_id: number;
|
||||
day_id: number;
|
||||
day_number: number;
|
||||
date: string | null;
|
||||
title: string | null;
|
||||
time: string | null;
|
||||
end_time: string | null;
|
||||
}
|
||||
|
||||
/**
|
||||
* What one entry of All places keeps beside its facts, read for the uses the
|
||||
* caller already found (PlacesLibraryRepository): the tags of its trips and
|
||||
* lists, the notes, the votes, the planned days and the bookings, and the
|
||||
* reader's role on its lists. Not bound to an entity, provided by the
|
||||
* places-library module over the context-resolving EntityManager like
|
||||
* PlacesLibraryRepository. Read only; the ids every method takes are the
|
||||
* caller's own access-checked ones.
|
||||
*/
|
||||
export class PlacesLibraryDetailsRepository {
|
||||
constructor(private readonly em: EntityManager) {}
|
||||
|
||||
/** The request's EntityManager, which throws outside a request context like every repository. */
|
||||
private db(): Kysely<LibraryDetailsDB> {
|
||||
return (this.em.getContext() as SqlEntityManager).getKysely<LibraryDetailsDB>();
|
||||
}
|
||||
|
||||
/** The tags on the trip places (PLC-21), by name. */
|
||||
async tripPlaceTags(placeIds: number[]): Promise<LibraryTripTagRow[]> {
|
||||
const rows = await this.db()
|
||||
.selectFrom('place_tags as pt')
|
||||
.innerJoin('tags as tg', 'tg.id', 'pt.tag_id')
|
||||
.select(['pt.place_id', 'tg.id', 'tg.name', 'tg.color'])
|
||||
.where('pt.place_id', 'in', idsOrNone(placeIds))
|
||||
.orderBy('tg.name')
|
||||
.orderBy('tg.id')
|
||||
.execute();
|
||||
// A legacy row can hold a number as its name, so it is read as text.
|
||||
return rows.map((row) => ({ ...row, name: String(row.name) }));
|
||||
}
|
||||
|
||||
/** Every tag the trips own (PLC-21), by name. */
|
||||
async tripTags(tripIds: number[]): Promise<LibraryOwnedTagRow[]> {
|
||||
const rows = await this.db()
|
||||
.selectFrom('tags as tg')
|
||||
.select(['tg.trip_id as owner_id', 'tg.id', 'tg.name', 'tg.color'])
|
||||
.where('tg.trip_id', 'in', idsOrNone(tripIds))
|
||||
.orderBy('tg.name')
|
||||
.orderBy('tg.id')
|
||||
.execute();
|
||||
return rows.map((row) => ({ ...row, owner_id: Number(row.owner_id), name: String(row.name) }));
|
||||
}
|
||||
|
||||
/** Every label of the lists (PLC-21: a list's tags), in each list's label order. */
|
||||
async listLabels(collectionIds: number[]): Promise<LibraryOwnedTagRow[]> {
|
||||
return await this.db()
|
||||
.selectFrom('collection_labels as lb')
|
||||
.select(['lb.collection_id as owner_id', 'lb.id', 'lb.name', 'lb.color'])
|
||||
.where('lb.collection_id', 'in', idsOrNone(collectionIds))
|
||||
.orderBy('lb.sort_order')
|
||||
.orderBy('lb.id')
|
||||
.execute();
|
||||
}
|
||||
|
||||
/**
|
||||
* The user's role on each of `collectionIds`: owner, or an accepted member
|
||||
* row's role read the way `resolveCollectionRole` reads it. A list with
|
||||
* neither reads as viewer, so it is never editable by mistake.
|
||||
*/
|
||||
async collectionRoles(userId: number, collectionIds: number[]): Promise<LibraryCollectionRole[]> {
|
||||
const db = this.db();
|
||||
const ids = idsOrNone(collectionIds);
|
||||
const lists = await db.selectFrom('collections').select(['id', 'owner_id']).where('id', 'in', ids).execute();
|
||||
const members = await db
|
||||
.selectFrom('collection_members')
|
||||
.select(['collection_id', 'role'])
|
||||
.where('user_id', '=', userId)
|
||||
.where('status', '=', 'accepted')
|
||||
.where('collection_id', 'in', ids)
|
||||
.execute();
|
||||
const memberRole = new Map(members.map((row) => [row.collection_id, row.role]));
|
||||
return lists.map((list) => {
|
||||
if (list.owner_id === userId) return { id: list.id, role: 'owner' as const };
|
||||
const stored = memberRole.get(list.id);
|
||||
return { id: list.id, role: stored === undefined ? ('viewer' as const) : memberRoleOf(stored) };
|
||||
});
|
||||
}
|
||||
|
||||
async tripPlaceNotes(placeIds: number[]): Promise<LibraryNotesRow[]> {
|
||||
return await this.db()
|
||||
.selectFrom('places')
|
||||
.select(['id', 'notes'])
|
||||
.where('id', 'in', idsOrNone(placeIds))
|
||||
.execute();
|
||||
}
|
||||
|
||||
async savedPlaceNotes(collectionPlaceIds: number[]): Promise<LibraryNotesRow[]> {
|
||||
return await this.db()
|
||||
.selectFrom('collection_places')
|
||||
.select(['id', 'notes'])
|
||||
.where('id', 'in', idsOrNone(collectionPlaceIds))
|
||||
.execute();
|
||||
}
|
||||
|
||||
/** The labels of the saved places, in each list's label order. */
|
||||
async savedPlaceLabels(collectionPlaceIds: number[]): Promise<LibraryLabelRow[]> {
|
||||
return await this.db()
|
||||
.selectFrom('collection_place_labels as cpl')
|
||||
.innerJoin('collection_labels as l', 'l.id', 'cpl.label_id')
|
||||
.select(['cpl.collection_place_id', 'l.id', 'l.name', 'l.color'])
|
||||
.where('cpl.collection_place_id', 'in', idsOrNone(collectionPlaceIds))
|
||||
.orderBy('l.sort_order')
|
||||
.orderBy('l.id')
|
||||
.execute();
|
||||
}
|
||||
|
||||
/** The votes on the saved places, each with who cast it (their name and stored picture). */
|
||||
async savedPlaceRatings(collectionPlaceIds: number[]): Promise<LibraryRatingRow[]> {
|
||||
return await this.db()
|
||||
.selectFrom('collection_place_ratings as cr')
|
||||
.innerJoin('users as vu', 'vu.id', 'cr.user_id')
|
||||
.select([
|
||||
'cr.collection_place_id as target_id',
|
||||
'cr.user_id',
|
||||
'cr.rating',
|
||||
(eb) => eb.fn.coalesce('cr.updated_at', 'cr.created_at').as('changed_at'),
|
||||
'cr.id',
|
||||
'vu.username',
|
||||
'vu.avatar',
|
||||
])
|
||||
.where('cr.collection_place_id', 'in', idsOrNone(collectionPlaceIds))
|
||||
.execute();
|
||||
}
|
||||
|
||||
/** The votes on the trip places, each with who cast it (their name and stored picture). */
|
||||
async tripPlaceRatings(placeIds: number[]): Promise<LibraryRatingRow[]> {
|
||||
return await this.db()
|
||||
.selectFrom('place_ratings as pr')
|
||||
.innerJoin('users as vu', 'vu.id', 'pr.user_id')
|
||||
.select([
|
||||
'pr.place_id as target_id',
|
||||
'pr.user_id',
|
||||
'pr.rating',
|
||||
(eb) => eb.fn.coalesce('pr.updated_at', 'pr.created_at').as('changed_at'),
|
||||
'pr.id',
|
||||
'vu.username',
|
||||
'vu.avatar',
|
||||
])
|
||||
.where('pr.place_id', 'in', idsOrNone(placeIds))
|
||||
.execute();
|
||||
}
|
||||
|
||||
/** Every vote of the user, on trip places and on saved places, in row order. */
|
||||
async votesOfUser(userId: number): Promise<LibraryOwnVoteRow[]> {
|
||||
const db = this.db();
|
||||
const trips = await db
|
||||
.selectFrom('place_ratings')
|
||||
.select([
|
||||
'place_id as use_id',
|
||||
'rating',
|
||||
(eb) => eb.fn.coalesce('updated_at', 'created_at').as('changed_at'),
|
||||
'id',
|
||||
])
|
||||
.where('user_id', '=', userId)
|
||||
.orderBy('id')
|
||||
.execute();
|
||||
const saved = await db
|
||||
.selectFrom('collection_place_ratings')
|
||||
.select([
|
||||
'collection_place_id as use_id',
|
||||
'rating',
|
||||
(eb) => eb.fn.coalesce('updated_at', 'created_at').as('changed_at'),
|
||||
'id',
|
||||
])
|
||||
.where('user_id', '=', userId)
|
||||
.orderBy('id')
|
||||
.execute();
|
||||
return [...trips.map((row) => ({ ...row, from_list: 0 })), ...saved.map((row) => ({ ...row, from_list: 1 }))];
|
||||
}
|
||||
|
||||
/** Up to `limit` of the people with a vote anywhere, by id, after the id `after`. */
|
||||
async voterIds(after: number, limit: number): Promise<number[]> {
|
||||
const db = this.db();
|
||||
const trips = await db
|
||||
.selectFrom('place_ratings')
|
||||
.select('user_id')
|
||||
.distinct()
|
||||
.where('user_id', '>', after)
|
||||
.orderBy('user_id')
|
||||
.limit(limit)
|
||||
.execute();
|
||||
const saved = await db
|
||||
.selectFrom('collection_place_ratings')
|
||||
.select('user_id')
|
||||
.distinct()
|
||||
.where('user_id', '>', after)
|
||||
.orderBy('user_id')
|
||||
.limit(limit)
|
||||
.execute();
|
||||
const ids = new Set([...trips, ...saved].map((row) => Number(row.user_id)));
|
||||
return [...ids].sort((a, b) => a - b).slice(0, limit);
|
||||
}
|
||||
|
||||
/** The days each place is planned on, in day order, with the time set there. */
|
||||
async plannedDays(placeIds: number[]): Promise<LibraryDayRow[]> {
|
||||
return await this.db()
|
||||
.selectFrom('day_assignments as da')
|
||||
.innerJoin('days as d', 'd.id', 'da.day_id')
|
||||
.select([
|
||||
'da.place_id',
|
||||
'd.id as day_id',
|
||||
'd.day_number',
|
||||
'd.date',
|
||||
'd.title',
|
||||
'da.assignment_time as time',
|
||||
'da.assignment_end_time as end_time',
|
||||
])
|
||||
.where('da.place_id', 'in', idsOrNone(placeIds))
|
||||
.orderBy('d.day_number')
|
||||
.orderBy('da.order_index')
|
||||
.orderBy('da.id')
|
||||
.execute();
|
||||
}
|
||||
|
||||
/** Which of the places a stay points at, and which a booking points at. */
|
||||
async bookedPlaces(placeIds: number[]): Promise<{ stays: number[]; bookings: number[] }> {
|
||||
const db = this.db();
|
||||
const ids = idsOrNone(placeIds);
|
||||
const stays = await db
|
||||
.selectFrom('day_accommodations')
|
||||
.select('place_id')
|
||||
.distinct()
|
||||
.where('place_id', 'in', ids)
|
||||
.execute();
|
||||
const bookings = await db
|
||||
.selectFrom('reservations')
|
||||
.select('place_id')
|
||||
.distinct()
|
||||
.where('place_id', 'in', ids)
|
||||
.execute();
|
||||
const present = (rows: { place_id: number | null }[]) =>
|
||||
rows.flatMap((row) => (row.place_id === null ? [] : [row.place_id]));
|
||||
return { stays: present(stays), bookings: present(bookings) };
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { LibraryOwnedTagRow, LibraryTripTagRow } from '../../db/repositories/PlacesLibrary.repository';
|
||||
import type { LibraryOwnedTagRow, LibraryTripTagRow } from '../../db/repositories/PlacesLibraryDetails.repository';
|
||||
import type { EntryRows } from './places-library.helpers';
|
||||
import type { PlacesLibraryEntryTag, PlacesLibraryTag } from '@trek/shared';
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import type { LibraryOwnVoteRow, LibraryUseRow } from '../../db/repositories/PlacesLibrary.repository';
|
||||
import type { LibraryUseRow } from '../../db/repositories/PlacesLibrary.repository';
|
||||
import type { LibraryOwnVoteRow } from '../../db/repositories/PlacesLibraryDetails.repository';
|
||||
import { castAfter, type EntryRows } from './places-library.helpers';
|
||||
|
||||
/** The copies of a place a vote lands on: trip places with their trip, and saved places. */
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { ADDON_IDS } from '../../addons';
|
||||
import type { PlaceWithTagsRow } from '../../db/repositories/Places.repository';
|
||||
import { PlacesLibraryRepository } from '../../db/repositories/PlacesLibrary.repository';
|
||||
import { PlacesLibraryDetailsRepository } from '../../db/repositories/PlacesLibraryDetails.repository';
|
||||
import { AddonsService } from '../addons/addons.service';
|
||||
import { logError } from '../audit/audit-log.logger';
|
||||
import { CollectionsService } from '../collections/collections.service';
|
||||
@@ -64,7 +64,7 @@ export class PlaceVotesService implements OnModuleInit {
|
||||
private readonly collections: CollectionsService,
|
||||
private readonly places: PlacesService,
|
||||
private readonly uow: UnitOfWork,
|
||||
private readonly repo: PlacesLibraryRepository,
|
||||
private readonly details: PlacesLibraryDetailsRepository,
|
||||
private readonly addons: AddonsService,
|
||||
private readonly membership: TripMembershipService,
|
||||
) {}
|
||||
@@ -180,7 +180,7 @@ export class PlaceVotesService implements OnModuleInit {
|
||||
|
||||
/** Up to `limit` people with a vote anywhere, by id, after `after`: the pages of carryOver's pass. */
|
||||
async voters(after: number, limit: number): Promise<number[]> {
|
||||
return this.repo.voterIds(after, limit);
|
||||
return this.details.voterIds(after, limit);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -231,7 +231,7 @@ export class PlaceVotesService implements OnModuleInit {
|
||||
return this.uow.transactional(async () => {
|
||||
const reach = await this.library.reach(userId);
|
||||
const uses = await this.library.usesOf(reach, null);
|
||||
const carried = votesToCarry(uses, await this.repo.votesOfUser(userId));
|
||||
const carried = votesToCarry(uses, await this.details.votesOfUser(userId));
|
||||
if (carried.length === 0) return [];
|
||||
const anchorOf = new Map(uses.map((use) => [`${use.from_list}:${use.use_id}`, use.anchor_key]));
|
||||
const anchors = new Set(carried.map((vote) => anchorOf.get(`${vote.fromList}:${vote.useId}`) ?? 0));
|
||||
|
||||
@@ -7,11 +7,11 @@ import {
|
||||
type LibraryCollectionPlaceRow,
|
||||
type LibraryIdentityRow,
|
||||
type LibraryPosition,
|
||||
type LibraryRatingRow,
|
||||
type LibrarySort,
|
||||
type LibraryTripPlaceRow,
|
||||
type LibraryUseRow,
|
||||
} from '../../db/repositories/PlacesLibrary.repository';
|
||||
import type { LibraryRatingRow } from '../../db/repositories/PlacesLibraryDetails.repository';
|
||||
import { avatarUrl } from '../common/avatarUrl';
|
||||
import {
|
||||
collectionStatusSchema,
|
||||
|
||||
@@ -4,6 +4,7 @@ import { UserPlaceMerges } from '../../db/entities/UserPlaceMerges.entity';
|
||||
import { UserPlaces } from '../../db/entities/UserPlaces.entity';
|
||||
import { PlaceVisitContextRepository } from '../../db/repositories/PlaceVisitContext.repository';
|
||||
import { PlacesLibraryRepository } from '../../db/repositories/PlacesLibrary.repository';
|
||||
import { PlacesLibraryDetailsRepository } from '../../db/repositories/PlacesLibraryDetails.repository';
|
||||
import { AddonsModule } from '../addons/addons.module';
|
||||
import { CollectionsModule } from '../collections/collections.module';
|
||||
import { McpSharedModule } from '../mcp-shared/mcp-shared.module';
|
||||
@@ -51,6 +52,11 @@ import { Module } from '@nestjs/common';
|
||||
useFactory: (em: EntityManager) => new PlacesLibraryRepository(em),
|
||||
inject: [EntityManager],
|
||||
},
|
||||
{
|
||||
provide: PlacesLibraryDetailsRepository,
|
||||
useFactory: (em: EntityManager) => new PlacesLibraryDetailsRepository(em),
|
||||
inject: [EntityManager],
|
||||
},
|
||||
{
|
||||
provide: PlaceVisitContextRepository,
|
||||
useFactory: (em: EntityManager) => new PlaceVisitContextRepository(em),
|
||||
@@ -66,6 +72,12 @@ import { Module } from '@nestjs/common';
|
||||
PlaceIdentitiesPruneJob,
|
||||
],
|
||||
// The visits and media (PlaceVisitsModule) and the votes (PlaceVotesModule) of All places, same domain, read through these.
|
||||
exports: [PlacesLibraryService, PlaceVisitsReaderService, PlaceVisitContextRepository, PlacesLibraryRepository],
|
||||
exports: [
|
||||
PlacesLibraryService,
|
||||
PlaceVisitsReaderService,
|
||||
PlaceVisitContextRepository,
|
||||
PlacesLibraryRepository,
|
||||
PlacesLibraryDetailsRepository,
|
||||
],
|
||||
})
|
||||
export class PlacesLibraryModule {}
|
||||
|
||||
@@ -7,6 +7,7 @@ import {
|
||||
type LibraryTripPlaceRow,
|
||||
type LibraryUseRow,
|
||||
} from '../../db/repositories/PlacesLibrary.repository';
|
||||
import { PlacesLibraryDetailsRepository } from '../../db/repositories/PlacesLibraryDetails.repository';
|
||||
import { CollectionsService } from '../collections/collections.service';
|
||||
import { badRequest, notFound } from '../common/domain-error';
|
||||
import { PermissionsService } from '../permissions/permissions.service';
|
||||
@@ -101,6 +102,7 @@ function armsOfReach(reach: LibraryReach): LibraryArms {
|
||||
export class PlacesLibraryService {
|
||||
constructor(
|
||||
private readonly repo: PlacesLibraryRepository,
|
||||
private readonly kept: PlacesLibraryDetailsRepository, // what each entry keeps beside its facts
|
||||
private readonly collections: CollectionsService,
|
||||
private readonly membership: TripMembershipService,
|
||||
private readonly permissions: PermissionsService,
|
||||
@@ -256,11 +258,11 @@ export class PlacesLibraryService {
|
||||
const placeIds = rows.flatMap((row) => row.trips.map((trip) => trip.id));
|
||||
const savedIds = rows.flatMap((row) => row.saved.map((saved) => saved.id));
|
||||
const onTrips = groupBy(
|
||||
await readInChunks(placeIds, (chunk) => this.repo.tripPlaceRatings(chunk)),
|
||||
await readInChunks(placeIds, (chunk) => this.kept.tripPlaceRatings(chunk)),
|
||||
(vote) => vote.target_id,
|
||||
);
|
||||
const onLists = groupBy(
|
||||
await readInChunks(savedIds, (chunk) => this.repo.savedPlaceRatings(chunk)),
|
||||
await readInChunks(savedIds, (chunk) => this.kept.savedPlaceRatings(chunk)),
|
||||
(vote) => vote.target_id,
|
||||
);
|
||||
return rows.map((row) =>
|
||||
@@ -278,8 +280,8 @@ export class PlacesLibraryService {
|
||||
private async tagsOf(rows: readonly EntryRows[]): Promise<PlacesLibraryEntryTag[][]> {
|
||||
const placeIds = rows.flatMap((row) => row.trips.map((trip) => trip.id));
|
||||
const savedIds = rows.flatMap((row) => row.saved.map((saved) => saved.id));
|
||||
const tripTags = await readInChunks(placeIds, (chunk) => this.repo.tripPlaceTags(chunk));
|
||||
const labels = await readInChunks(savedIds, (chunk) => this.repo.savedPlaceLabels(chunk));
|
||||
const tripTags = await readInChunks(placeIds, (chunk) => this.kept.tripPlaceTags(chunk));
|
||||
const labels = await readInChunks(savedIds, (chunk) => this.kept.savedPlaceLabels(chunk));
|
||||
return entryTagsOf(rows, tripTags, labels);
|
||||
}
|
||||
|
||||
@@ -301,7 +303,7 @@ export class PlacesLibraryService {
|
||||
saved: readonly LibraryCollectionPlaceRow[],
|
||||
): Promise<OriginAccess> {
|
||||
const listIds = [...new Set(saved.map((row) => row.collection_id))];
|
||||
const roles = listIds.length > 0 ? await this.repo.collectionRoles(user.id, listIds) : [];
|
||||
const roles = listIds.length > 0 ? await this.kept.collectionRoles(user.id, listIds) : [];
|
||||
const editableLists = new Set(roles.filter((role) => role.role !== 'viewer').map((role) => role.id));
|
||||
const asOwner = trips.some((row) => row.trip_owner_id === user.id)
|
||||
? await this.permissions.checkPermission('place_edit', user.role, user.id, user.id, false)
|
||||
@@ -325,12 +327,12 @@ export class PlacesLibraryService {
|
||||
const details: PlacesLibraryDetail[] = [];
|
||||
if (rows.trips.length > 0) {
|
||||
const placeIds = rows.trips.map((row) => row.id);
|
||||
const notes = new Map((await this.repo.tripPlaceNotes(placeIds)).map((row) => [row.id, row.notes]));
|
||||
const ratings = groupBy(await this.repo.tripPlaceRatings(placeIds), (row) => row.target_id);
|
||||
const days = groupBy(await this.repo.plannedDays(placeIds), (row) => row.place_id);
|
||||
const booked = await this.repo.bookedPlaces(placeIds);
|
||||
const tags = groupBy(await this.repo.tripPlaceTags(placeIds), (row) => row.place_id);
|
||||
const tripTags = tagsByOwner(await this.repo.tripTags([...new Set(rows.trips.map((row) => row.trip_id))]));
|
||||
const notes = new Map((await this.kept.tripPlaceNotes(placeIds)).map((row) => [row.id, row.notes]));
|
||||
const ratings = groupBy(await this.kept.tripPlaceRatings(placeIds), (row) => row.target_id);
|
||||
const days = groupBy(await this.kept.plannedDays(placeIds), (row) => row.place_id);
|
||||
const booked = await this.kept.bookedPlaces(placeIds);
|
||||
const tags = groupBy(await this.kept.tripPlaceTags(placeIds), (row) => row.place_id);
|
||||
const tripTags = tagsByOwner(await this.kept.tripTags([...new Set(rows.trips.map((row) => row.trip_id))]));
|
||||
for (const row of rows.trips) {
|
||||
const assigned = (tags.get(row.id) ?? []).map(tagOf);
|
||||
details.push({
|
||||
@@ -357,11 +359,11 @@ export class PlacesLibraryService {
|
||||
}
|
||||
if (rows.saved.length > 0) {
|
||||
const savedIds = rows.saved.map((row) => row.id);
|
||||
const notes = new Map((await this.repo.savedPlaceNotes(savedIds)).map((row) => [row.id, row.notes]));
|
||||
const labels = groupBy(await this.repo.savedPlaceLabels(savedIds), (row) => row.collection_place_id);
|
||||
const ratings = groupBy(await this.repo.savedPlaceRatings(savedIds), (row) => row.target_id);
|
||||
const notes = new Map((await this.kept.savedPlaceNotes(savedIds)).map((row) => [row.id, row.notes]));
|
||||
const labels = groupBy(await this.kept.savedPlaceLabels(savedIds), (row) => row.collection_place_id);
|
||||
const ratings = groupBy(await this.kept.savedPlaceRatings(savedIds), (row) => row.target_id);
|
||||
const listTags = tagsByOwner(
|
||||
await this.repo.listLabels([...new Set(rows.saved.map((row) => row.collection_id))]),
|
||||
await this.kept.listLabels([...new Set(rows.saved.map((row) => row.collection_id))]),
|
||||
);
|
||||
for (const row of rows.saved) {
|
||||
const assigned = (labels.get(row.id) ?? []).map(tagOf);
|
||||
|
||||
Reference in New Issue
Block a user