Rework entry types: merge hotel into stay, transport with modes, auto-transport

- Type set is now activity/stay/transport/flight/rental/note; hotel, travel
  and immigration are removed with idempotent startup data migrations
  (hotel->stay, travel->transport, immigration->activity with flag prefix)
- Transport entries carry an optional mode (train/bus/ferry/taxi/drive/other)
  that drives the chip/map icon; route stops expose transport_mode
- Creating a stay auto-creates a bridging transport to its neighbouring
  stays unless a transport/flight already covers the gap (one-shot)
- Summary: transports count replaces hotels/travelLegs
This commit is contained in:
2026-07-19 17:47:21 +07:00
parent b066e7b885
commit d393e16ae1
17 changed files with 469 additions and 62 deletions
+66 -13
View File
@@ -5,16 +5,8 @@ import { ENTRY_COLUMNS, attachParticipants } from '../util/entrySerialize.js';
import { validateSegments } from '../util/segments.js';
import { validateRental } from '../util/rental.js';
const ENTRY_TYPES = new Set([
'flight',
'immigration',
'travel',
'hotel',
'activity',
'rental',
'stay',
'note',
]);
const ENTRY_TYPES = new Set(['flight', 'transport', 'activity', 'rental', 'stay', 'note']);
const TRANSPORT_MODES = new Set(['train', 'bus', 'ferry', 'taxi', 'drive', 'other']);
const SPLIT_MODES = new Set(['equal', 'own', 'payer']);
const TIME_RE = /^([01]\d|2[0-3]):[0-5]\d$/;
@@ -199,6 +191,22 @@ function validateEntry(body, { partial, existing, memberIds }) {
}
}
// transport_mode: transport-only (null clears it).
if (has('transport_mode')) {
if (body.transport_mode === null) {
fields.transport_mode = null;
} else {
const effType = 'type' in fields ? fields.type : existing?.type;
if (effType !== 'transport') {
return { error: 'transport_mode is only allowed on transport entries' };
}
if (!TRANSPORT_MODES.has(body.transport_mode)) {
return { error: 'transport_mode must be one of train, bus, ferry, taxi, drive, other' };
}
fields.transport_mode = body.transport_mode;
}
}
return { fields, participants, hasParticipants: has('participants') };
}
@@ -217,6 +225,18 @@ export default function entriesRoutes(db) {
'INSERT OR IGNORE INTO entry_participants (entry_id, user_id) VALUES (?, ?)'
);
const clearParticipants = db.prepare('DELETE FROM entry_participants WHERE entry_id = ?');
const getStays = db.prepare(
`SELECT id, date, end_date, location_name, title FROM entries
WHERE trip_id = ? AND type = 'stay' ORDER BY date, id`
);
const existsTransportOrFlightInWindow = db.prepare(
`SELECT 1 FROM entries WHERE trip_id = ? AND type IN ('transport', 'flight')
AND date >= ? AND date <= ? LIMIT 1`
);
const insertAutoTransport = db.prepare(
`INSERT INTO entries (trip_id, date, type, title, details, sort_order, split_mode)
VALUES (?, ?, 'transport', ?, '', 0, 'equal')`
);
const memberIdSet = (tripId) => new Set(getMemberIds.all(tripId).map((r) => r.user_id));
@@ -227,6 +247,36 @@ export default function entriesRoutes(db) {
}
}
// Short name for an auto-transport title: first comma-segment of the stay's
// location_name, falling back to its title.
function shortName(stay) {
return (stay.location_name || stay.title).split(',')[0].trim();
}
// Auto-create a transport entry bridging `earlier` -> `later` (both stay
// rows) unless a transport/flight already covers the gap, or the window is
// inverted (overlapping stays).
function maybeCreateAutoTransport(tripId, earlier, later) {
const windowStart = earlier.end_date || earlier.date;
const windowEnd = later.date;
if (windowStart > windowEnd) return;
if (existsTransportOrFlightInWindow.get(tripId, windowStart, windowEnd)) return;
const title = `${shortName(earlier)}${shortName(later)}`;
insertAutoTransport.run(tripId, later.date, title);
}
// Fires once, on stay creation: bridges the new stay to its nearest
// chronological neighbour stay(s) before/after with an auto-transport entry.
function autoTransportForNewStay(tripId, newStayId) {
const stays = getStays.all(tripId);
const idx = stays.findIndex((s) => s.id === newStayId);
if (idx === -1) return;
const before = idx > 0 ? stays[idx - 1] : null;
const after = idx < stays.length - 1 ? stays[idx + 1] : null;
if (before) maybeCreateAutoTransport(tripId, before, stays[idx]);
if (after) maybeCreateAutoTransport(tripId, stays[idx], after);
}
// GET /api/trips/:id/entries
router.get('/trips/:id/entries', (req, res) => {
const tripId = Number(req.params.id);
@@ -256,8 +306,9 @@ export default function entriesRoutes(db) {
.prepare(
`INSERT INTO entries
(trip_id, date, end_date, type, title, details, start_time, end_time,
location_name, lat, lng, sort_order, price, paid_by, split_mode, segments, rental)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
location_name, lat, lng, sort_order, price, paid_by, split_mode, segments, rental,
transport_mode)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`
)
.run(
tripId,
@@ -276,11 +327,13 @@ export default function entriesRoutes(db) {
f.paid_by ?? null,
f.split_mode ?? 'equal',
f.segments ?? null,
f.rental ?? null
f.rental ?? null,
f.transport_mode ?? null
);
const id = Number(info.lastInsertRowid);
// participants provided as an array -> store rows; null/absent -> all members.
if (Array.isArray(check.participants)) writeParticipants(id, check.participants);
if (f.type === 'stay') autoTransportForNewStay(tripId, id);
return attachParticipants(db, getEntry.get(id));
})();