mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-10-18 21:16:47 +00:00
89f3055a29
The link table keyed by the trip id made both entities a one-to-one, and the test typecheck failed with TS2589 in the itinerary factory. It now holds its own id with trip_id UNIQUE, as trip_invite_tokens does, and its members hang off link_id. The factory error was not this table's alone: any further relation on trips, journeys or users tipped the same check. insertRow and createRow now take a caller's overrides as their own argument instead of a spread checked against RequiredEntityData, so a new relation no longer breaks the itinerary and tour factories. The suites that hand-build TripMembersService get a journalLinksStub.
44 lines
1.5 KiB
TypeScript
44 lines
1.5 KiB
TypeScript
import { TourWaypoints } from '../../../src/db/entities/TourWaypoints.entity';
|
|
import { Tours } from '../../../src/db/entities/Tours.entity';
|
|
import type { FactoryOrm } from './context';
|
|
import { findRow, insertRow, insertRows } from './rows';
|
|
import type { EntityData, EntityDTO } from '@mikro-orm/core';
|
|
|
|
export type TourRow = EntityDTO<Tours>;
|
|
|
|
/**
|
|
* Turns the place into a tour. A tour is keyed by its place, so it has no id
|
|
* of its own; its type defaults to 'hike', the one the tours migration seeds.
|
|
*/
|
|
export async function makeTour(orm: FactoryOrm, placeId: number, overrides: EntityData<Tours> = {}): Promise<TourRow> {
|
|
await insertRow(orm, Tours, { place: placeId, tourTypeRef: 'hike' }, overrides);
|
|
const tour = await findRow(orm, Tours, { place: placeId });
|
|
if (!tour) throw new Error(`makeTour: no tour for place ${placeId} after its insert`);
|
|
return tour;
|
|
}
|
|
|
|
type WaypointRole = 'start' | 'via' | 'end';
|
|
|
|
/**
|
|
* The tour's waypoints, in the order given and numbered from 0. Without a
|
|
* role the first is the start, the last the end and the rest are vias.
|
|
*/
|
|
export async function addTourWaypoints(
|
|
orm: FactoryOrm,
|
|
placeId: number,
|
|
points: Array<{ lat: number; lng: number; role?: WaypointRole }>,
|
|
): Promise<void> {
|
|
const roleAt = (i: number): WaypointRole => (i === 0 ? 'start' : i === points.length - 1 ? 'end' : 'via');
|
|
await insertRows(
|
|
orm,
|
|
TourWaypoints,
|
|
points.map((p, sequence) => ({
|
|
place: placeId,
|
|
lat: p.lat,
|
|
lng: p.lng,
|
|
role: p.role ?? roleAt(sequence),
|
|
sequence,
|
|
})),
|
|
);
|
|
}
|