mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-06 18:54:17 +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) };
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user