mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-03 09:38:54 +00:00
fix(journey): a timed entry takes its place in the day by its time
A new entry always went to the end of its day, so writing up 1 am after 2 am left the night out of order, and changing an entry's time never moved it. A timed entry now lands before the first entry of its day that starts later, on create and when its time or day changes; untimed entries still go last, and the hand-made order of the others stays as it was. A save that leaves time and day untouched keeps an entry where it was dragged. Closes #2679
This commit is contained in:
@@ -241,6 +241,21 @@ export class JourneyEntriesRepository extends TrekRepository<JourneyEntries> {
|
||||
return rows as { entry_date: string; m: number }[];
|
||||
}
|
||||
|
||||
/** One day's entries in timeline order, with what placing a timed entry among them reads (#2679). */
|
||||
async listDayOrder(
|
||||
journeyId: number,
|
||||
entryDate: string,
|
||||
): Promise<{ id: number; entry_time: string | null; sort_order: number | null }[]> {
|
||||
return await this.kysely<JourneyEntriesDateSortKyselyDB>()
|
||||
.selectFrom('journey_entries')
|
||||
.select(['id', 'entry_time', 'sort_order'])
|
||||
.where('journey_id', '=', journeyId)
|
||||
.where('entry_date', '=', entryDate)
|
||||
.orderBy('sort_order', 'asc')
|
||||
.orderBy('id', 'asc')
|
||||
.execute();
|
||||
}
|
||||
|
||||
/** JG42 — `onPlaceCreated`'s next-order lookup: `SELECT MAX(sort_order) AS m FROM journey_entries WHERE journey_id = ? AND entry_date = ?`. The `?? -1` fallback stays the SERVICE's, unchanged (same text as Task 2's own JG74). */
|
||||
async maxSortOrderForDate(journeyId: number, entryDate: string): Promise<number | null> {
|
||||
const row = await this.kysely<JourneyEntriesDateSortKyselyDB>()
|
||||
|
||||
@@ -10,6 +10,7 @@ import { todayUtc } from '@trek/shared';
|
||||
import { TrekPhotoRegistrationService } from '../photos/trek-photo-registration.service';
|
||||
import { getCountryFromCoords } from '../atlas/atlas-geo';
|
||||
import { computeJourneyStats, type StatsInputPoint } from './journey-stats';
|
||||
import { makeRoomAtTime } from './journey-entry-order';
|
||||
import { Journeys } from '../../db/entities/Journeys.entity';
|
||||
import type { JourneysRepository } from '../../db/repositories/Journeys.repository';
|
||||
import { JourneyContributors } from '../../db/entities/JourneyContributors.entity';
|
||||
@@ -1209,10 +1210,13 @@ export class JourneyDomainService {
|
||||
? JSON.stringify(data.pros_cons)
|
||||
: null;
|
||||
|
||||
// The next position and the row that takes it in one transaction, so two
|
||||
// entries added at once cannot both read the same MAX (JG74).
|
||||
// The position and the row that takes it in one transaction, so two entries
|
||||
// added at once cannot both read the same MAX (JG74). A timed entry goes where
|
||||
// its time puts it in the day, an untimed one last (#2679).
|
||||
const insertedId = await this.uow.transactional(async () => {
|
||||
const maxOrder = await this.entriesRepo.maxSortOrderForDate(journeyId, data.entry_date);
|
||||
const sortOrder = data.entry_time
|
||||
? await makeRoomAtTime(this.entriesRepo, journeyId, data.entry_date, data.entry_time, now)
|
||||
: ((await this.entriesRepo.maxSortOrderForDate(journeyId, data.entry_date)) ?? -1) + 1;
|
||||
return await this.entriesRepo.insertEntry({
|
||||
journey_id: journeyId,
|
||||
author_id: userId,
|
||||
@@ -1230,7 +1234,7 @@ export class JourneyDomainService {
|
||||
tags: data.tags?.length ? JSON.stringify(data.tags) : null,
|
||||
pros_cons: prosConsJson,
|
||||
visibility: data.visibility || 'private',
|
||||
sort_order: (maxOrder ?? -1) + 1,
|
||||
sort_order: sortOrder,
|
||||
is_draft: data.is_draft ? 1 : 0,
|
||||
created_at: now,
|
||||
updated_at: now,
|
||||
@@ -1355,7 +1359,13 @@ export class JourneyDomainService {
|
||||
// was provided — the same condition the legacy field-count check tested.
|
||||
if (Object.keys(patch).length === 1) return decodeEntryRow(entry);
|
||||
|
||||
// A new time or day moves the entry to where it belongs in that day, unless the
|
||||
// caller placed it itself (#2679).
|
||||
const date = patch.entry_date ?? entry.entry_date;
|
||||
const time = patch.entry_time !== undefined ? patch.entry_time : entry.entry_time;
|
||||
const placeAt = patch.sort_order === undefined && (date !== entry.entry_date || time !== entry.entry_time) ? time : null;
|
||||
await this.uow.transactional(async () => {
|
||||
if (placeAt) patch.sort_order = await makeRoomAtTime(this.entriesRepo, entry.journey_id, date, placeAt, this.ts(), entryId);
|
||||
await this.entriesRepo.updateFields(entryId, patch);
|
||||
await this.journeysRepo.updateFields(entry.journey_id, { updated_at: this.ts() }); // touch the journey
|
||||
});
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
import type { JourneyEntriesRepository } from '../../db/repositories/JourneyEntries.repository';
|
||||
|
||||
/** "HH:MM" (seconds ignored) as minutes past midnight, or null for anything else. */
|
||||
function minutesOf(time: string | null | undefined): number | null {
|
||||
const match = /^(\d{1,2}):(\d{2})/.exec(time?.trim() ?? '');
|
||||
return match ? Number(match[1]) * 60 + Number(match[2]) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Where an entry at `time` belongs among its day's entries, given in their
|
||||
* current order: right before the first one that starts later. Entries
|
||||
* without a time are no anchor either way, and an unreadable `time` goes last.
|
||||
*/
|
||||
export function chronologicalSlot(day: readonly { entry_time: string | null }[], time: string): number {
|
||||
const at = minutesOf(time);
|
||||
if (at == null) return day.length;
|
||||
const later = day.findIndex((e) => {
|
||||
const other = minutesOf(e.entry_time);
|
||||
return other != null && other > at;
|
||||
});
|
||||
return later === -1 ? day.length : later;
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes room for a timed entry in its day and returns the sort_order it takes.
|
||||
*
|
||||
* A new entry used to go to the end of its day whatever its time, so writing
|
||||
* up the morning after the evening left the morning last (#2679). It now lands
|
||||
* before the first entry that starts later; the order of everything else,
|
||||
* dragged by hand or not, stays as it was. Run it inside the write's
|
||||
* transaction, so the room and the entry that takes it land together.
|
||||
*/
|
||||
export async function makeRoomAtTime(
|
||||
entries: JourneyEntriesRepository,
|
||||
journeyId: number,
|
||||
date: string,
|
||||
time: string,
|
||||
now: number,
|
||||
exceptId?: number,
|
||||
): Promise<number> {
|
||||
const day = (await entries.listDayOrder(journeyId, date)).filter((e) => e.id !== exceptId);
|
||||
const slot = chronologicalSlot(day, time);
|
||||
const moves = day
|
||||
.map((e, index) => ({ id: e.id, from: e.sort_order, to: index < slot ? index : index + 1 }))
|
||||
.filter((m) => m.from !== m.to);
|
||||
await Promise.all(moves.map((m) => entries.updateSortOrder(m.id, m.to, now)));
|
||||
return slot;
|
||||
}
|
||||
Reference in New Issue
Block a user