mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-01 08:53:13 +00:00
fix(integrations): cut a journey day in the zone of its own first track, never in the UTC Dawarich writes times in
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import { Trips } from '../../db/entities/Trips.entity';
|
||||
import { TripsRepository } from '../../db/repositories/Trips.repository';
|
||||
import { DomainError } from '../common/domain-error';
|
||||
import { DawarichClient, DawarichError, type DawarichCreds } from './dawarich.client';
|
||||
import { DawarichClient, DawarichError, type DawarichCreds, type DawarichTrackFeature } from './dawarich.client';
|
||||
import { RECORDING_GAP, bucketPointsByDay, bucketTracksByDay, countPoints, offsetMinutesOf } from './dawarich.helpers';
|
||||
import { DawarichService } from './dawarich.service';
|
||||
import { InjectRepository } from '@mikro-orm/nestjs';
|
||||
@@ -207,7 +207,7 @@ export class DawarichTracksService {
|
||||
if (capabilities?.tracks !== false) {
|
||||
try {
|
||||
const { features, truncated } = await this.client.listTracks(creds, from, to);
|
||||
const sourceOffset = sourceOffsetOf(features[0]?.properties?.start_at);
|
||||
const sourceOffset = sourceOffsetOn(features, date);
|
||||
const offset = zoneOf(zone, sourceOffset);
|
||||
const day = bucketTracksByDay(features, {
|
||||
maxPointsPerDay: DAWARICH_TRACK_POINTS_PER_DAY,
|
||||
@@ -255,10 +255,21 @@ export class DawarichTracksService {
|
||||
}
|
||||
}
|
||||
|
||||
/** The zone a Dawarich time names; null when it names none. */
|
||||
function sourceOffsetOf(value: unknown): number | null {
|
||||
if (typeof value !== 'string' || !/(Z|[+-]\d{2}:?\d{2})$/.test(value)) return null;
|
||||
return offsetMinutesOf(value);
|
||||
/**
|
||||
* The zone Dawarich recorded the date in, read off the first track that starts
|
||||
* on it: the window opens on the day before, and a traveller who crossed zones
|
||||
* overnight was elsewhere then. A `Z` time names no zone, Dawarich just wrote
|
||||
* UTC, so it reveals nothing about where the day was.
|
||||
*/
|
||||
function sourceOffsetOn(features: DawarichTrackFeature[], date: string): number | null {
|
||||
for (const feature of features) {
|
||||
const startAt = feature.properties?.start_at;
|
||||
if (typeof startAt !== 'string' || !/[+-]\d{2}:?\d{2}$/.test(startAt)) continue;
|
||||
const offset = offsetMinutesOf(startAt);
|
||||
const localDate = new Date(Date.parse(startAt) + offset * 60_000).toISOString().slice(0, 10);
|
||||
if (localDate === date) return offset;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function zoneOf(
|
||||
|
||||
@@ -864,4 +864,45 @@ describe('DawarichTracksService.fetchDay', () => {
|
||||
|
||||
expect(h.listPoints).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it('DAWARICH-TRACKS-062: a track written in UTC names no zone, so asked for the source zone it still cuts in the one chosen', async () => {
|
||||
const h = harness({ capabilities: CAPABILITIES });
|
||||
h.listTracks.mockResolvedValue({
|
||||
features: [feature({ start_at: '2026-05-01T08:00:00Z', end_at: '2026-05-01T10:00:00Z' })],
|
||||
truncated: false,
|
||||
});
|
||||
|
||||
const day = await h.service.fetchDay(USER, '2026-05-01', { offsetMinutes: 120, useSourceOffset: true });
|
||||
|
||||
expect(day).toMatchObject({
|
||||
sourceKind: 'tracks',
|
||||
utcOffsetMinutes: 120,
|
||||
offsetSource: 'chosen',
|
||||
sourceOffsetMinutes: null,
|
||||
window: { from: '2026-04-30T22:00:00.000Z', to: '2026-05-01T22:00:00.000Z' },
|
||||
});
|
||||
expect(day.segments).toHaveLength(1);
|
||||
});
|
||||
|
||||
it('DAWARICH-TRACKS-063: the source zone is the one of the first track on the date, not of the evening before in another zone', async () => {
|
||||
const h = harness({ capabilities: CAPABILITIES });
|
||||
h.listTracks.mockResolvedValue({
|
||||
features: [
|
||||
// The evening before in Berlin, then the morning after landing in Tokyo.
|
||||
feature({ start_at: '2026-04-30T20:00:00+02:00', end_at: '2026-04-30T21:00:00+02:00' }),
|
||||
feature({ start_at: '2026-05-01T09:00:00+09:00', end_at: '2026-05-01T10:00:00+09:00' }),
|
||||
],
|
||||
truncated: false,
|
||||
});
|
||||
|
||||
const day = await h.service.fetchDay(USER, '2026-05-01', { offsetMinutes: 0, useSourceOffset: true });
|
||||
|
||||
expect(day).toMatchObject({
|
||||
utcOffsetMinutes: 540,
|
||||
offsetSource: 'source',
|
||||
sourceOffsetMinutes: 540,
|
||||
window: { from: '2026-04-30T15:00:00.000Z', to: '2026-05-01T15:00:00.000Z' },
|
||||
});
|
||||
expect(day.segments).toHaveLength(1);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user