Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

fix(dawarich): read unnamed stays again in turns across runs

Maurice5588b7efbb5c
diff --git a/server/src/nest/integrations/dawarich-sync.service.ts b/server/src/nest/integrations/dawarich-sync.service.ts
index f3913cd82..a213a8511 100644
--- a/server/src/nest/integrations/dawarich-sync.service.ts
+++ b/server/src/nest/integrations/dawarich-sync.service.ts
@@ -110,6 +110,12 @@ export class DawarichSyncService {
*/
private readonly inFlight = new Set<number>();
+ /**
+ * Per user, the last unnamed stay read again, so the next run goes on after
+ * it instead of asking about the same first MAX_UNNAMED_STAY_REREADS again.
+ */
+ private readonly lastUnnamedReread = new Map<number, number>();
+
constructor(
@InjectRepository(DawarichVisitSuggestions) private readonly suggestions: DawarichVisitSuggestionsRepository,
private readonly addons: AddonsService,
@@ -325,7 +331,7 @@ export class DawarichSyncService {
if (outcome.change === 'created') created++;
else if (outcome.change === 'updated') updated++;
}
- await this.rereadUnnamed(nameStay);
+ await this.rereadUnnamed(userId, nameStay);
const missing = await this.flagMissing(userId, tripId, from, to, seenIds);
return { created, updated, missing };
@@ -356,17 +362,23 @@ export class DawarichSyncService {
/**
* The unnamed stays of a run read again with the budget the others left,
* at most MAX_UNNAMED_STAY_REREADS of them, so a Dawarich that geocodes
- * nothing is not asked about the same stays in full on every run.
+ * nothing is not asked about the same stays in full on every run. Each run
+ * starts after the stay the last one stopped at, so every unnamed stay gets
+ * its turn even when the first ones never get a name.
*
* @txStandalone a name found later is a derived refresh of a stay already stored.
* @txIndependent one stay at a time after its network read.
*/
- private async rereadUnnamed(nameStay: StayNamer): Promise<void> {
+ private async rereadUnnamed(userId: number, nameStay: StayNamer): Promise<void> {
+ const after = this.lastUnnamedReread.get(userId) ?? 0;
+ const byId = [...nameStay.unnamed].sort((a, b) => a.id - b.id);
+ const turn = [...byId.filter((s) => s.id > after), ...byId.filter((s) => s.id <= after)];
// One after another: a read that finds the budget spent stops the rest.
- await nameStay.unnamed.slice(0, MAX_UNNAMED_STAY_REREADS).reduce<Promise<boolean>>(async (going, stay) => {
+ await turn.slice(0, MAX_UNNAMED_STAY_REREADS).reduce<Promise<boolean>>(async (going, stay) => {
if (!(await going)) return false;
const found = await nameStay.read(stay.startedAt, stay.endedAt);
if (found === undefined) return false;
+ this.lastUnnamedReread.set(userId, stay.id);
if (found !== null) await this.suggestions.renameNewVisit(stay.id, found);
return true;
}, Promise.resolve(true));
@@ -537,7 +549,7 @@ export class DawarichSyncService {
if (outcome?.change === 'created') created++;
else if (outcome?.change === 'updated') updated++;
}
- await this.rereadUnnamed(nameStay);
+ await this.rereadUnnamed(userId, nameStay);
return { created, updated, truncated };
}
diff --git a/server/tests/unit/nest/dawarich-sync.service.test.ts b/server/tests/unit/nest/dawarich-sync.service.test.ts
index 785ef6331..0604aac23 100644
--- a/server/tests/unit/nest/dawarich-sync.service.test.ts
+++ b/server/tests/unit/nest/dawarich-sync.service.test.ts
@@ -396,6 +396,30 @@ describe('DawarichSyncService, naming a stay Dawarich sends without a name (#310
expect(listGeocodedPoints).toHaveBeenCalledTimes(21 * listVisits.mock.calls.length);
expect((await rows()).find((r) => r.source_visit_id === '1100')?.name).toBe('Cinisi, Italy');
});
+
+ it('DAWARICH-SYNC-097: unnamed stays are read again in turns, so the later ones are not stuck behind the first few', async () => {
+ const at = (i: number) => `${VISIT_DAY}T09:${String(i).padStart(2, '0')}:00Z`;
+ const unnamed = Array.from({ length: 25 }, (_, i) =>
+ visit({ id: 1200 + i, name: 'Unknown Location', place: null, started_at: at(i) }),
+ );
+ withVisits(...unnamed);
+ const creds: DawarichCreds = { baseUrl: 'https://dawarich.test', apiKey: 'secret-key', allowInsecureTls: false };
+ const run = () => svc.syncTripWindow(USER, TRIP, creds, new Date(TRIP_START), new Date(TRIP_END));
+ await run();
+
+ const late = new Set(Array.from({ length: 5 }, (_, i) => at(20 + i).replace('Z', '.000Z')));
+ listGeocodedPoints
+ .mockReset()
+ .mockImplementation(async (_creds: DawarichCreds, from: Date) =>
+ late.has(from.toISOString()) ? [{ city: 'Cinisi', country_name: 'Italy' }] : [],
+ );
+ await run();
+ await run();
+
+ const names = (await rows()).map((r) => r.name);
+ expect(names.slice(0, 20).every((n) => n === 'Unnamed stay')).toBe(true);
+ expect(names.slice(20)).toEqual(Array(5).fill('Cinisi, Italy'));
+ });
});
describe('DawarichSyncService — new visits', () => {