Files
TREK/server/src/nest/common/rowShape.ts
T
Maurice d0d3f455a8 fix(planner): let the day list see which stop a booking wrote
The row shapers name every column they pass on, and accommodation_id was in
none of them. So the field the day list hides on never reached the client, and
the booked night was listed as an ordinary place next to the overnight block
that already named it. The stop was correct in the table the whole time; the
three read paths just dropped it on the way out.

The stamp on the freshly written stop had the same hole from the other side: the
INSERT went in without the booking id and an UPDATE added it afterwards, so the
row handed back in the answer, the one the client that booked the night puts
straight into its store, carried none. It goes in with the INSERT now.

The shared link gains the same filter. It has no road trip view to need the stop
and already shows the booking as its own chip on the day, so the row was only
ever the same hotel twice.
2026-09-13 12:59:19 +02:00

77 lines
2.8 KiB
TypeScript

import type { AssignmentRow, Tag, Participant } from '../../types';
import type { PlaceRatingRow } from '../query-helpers/query-helpers.service';
/**
* Pure reshaping over rows the query helpers load. Neither function touches the
* database, so neither became a provider — they shipped in
* services/queryHelpers.ts next to the loaders only because the loaders were
* free functions too.
*/
/** Reshape a flat assignment+place DB row into the nested API response shape with embedded place, tags, and participants. */
export function formatAssignmentWithPlace(a: AssignmentRow, tags: Partial<Tag>[], participants: Participant[]) {
return {
id: a.id,
day_id: a.day_id,
place_id: a.place_id,
order_index: a.order_index,
notes: a.notes,
assignment_time: a.assignment_time ?? null,
assignment_end_time: a.assignment_end_time ?? null,
end_day: a.end_day === 1,
leg_transport_mode: a.leg_transport_mode ?? null,
incoming_leg_transport_mode: a.incoming_leg_transport_mode ?? null,
// Which booking put this stop here, if a booking did. The day list has nothing
// else to tell it from a place the traveller added, and it must not draw the
// hotel a second time under the overnight block that already names it.
accommodation_id: a.accommodation_id ?? null,
participants: participants || [],
created_at: a.created_at,
place: {
id: a.place_id,
name: a.place_name,
description: a.place_description,
lat: a.lat,
lng: a.lng,
address: a.address,
category_id: a.category_id,
price: a.price,
currency: a.place_currency,
place_time: a.place_time,
end_time: a.end_time,
duration_minutes: a.duration_minutes,
notes: a.place_notes,
image_url: a.image_url,
transport_mode: a.transport_mode,
google_place_id: a.google_place_id,
google_ftid: a.google_ftid,
osm_id: a.osm_id,
amap_poi_id: a.amap_poi_id,
website: a.website,
phone: a.phone,
// The rail marks a fuel stop as one; without it here every stop would need
// its own place request to find out what kind it is.
stop_type: a.stop_type ?? null,
// Same reason: the rail resets a range budget here and has to know how far this
// stop fills, not how far the traveller's default one does.
fill_percent: a.fill_percent ?? null,
category: a.category_id ? {
id: a.category_id,
name: a.category_name,
color: a.category_color,
icon: a.category_icon,
} : null,
tags: tags || [],
}
};
}
/** avg/count aggregate for a place's rating rows. */
export function ratingAggregate(ratings: PlaceRatingRow[] | undefined) {
const rows = ratings || [];
return {
rating_avg: rows.length > 0 ? rows.reduce((s, r) => s + r.rating, 0) / rows.length : null,
rating_count: rows.length,
};
}