Files
TREK/client/src/repo/placeRepo.ts
T
Maurice 013aa3954d admin: ask once, then show and steer what is shared of place details
The first time an admin opens the settings on an installation that can share,
a dialog asks for the whole installation, with every field switched on and a
plain list of what never goes along. Nothing is sent before Agree; Not now
asks again in 14 days, No keeps the question away until the terms change, and
closing it decides nothing.

Inside the TREK Places API card, Share place details then holds the switch for
the installation and one per field, the figures of the last 30 days, the
transmission log and the way to withdraw everything sent. The place form
reports for each field whether the user typed it or a provider filled it.
2026-10-31 03:25:55 +00:00

173 lines
6.5 KiB
TypeScript

import { placesApi } from '../api/client'
import { offlineDb, replaceTripRows, upsertPlaces } from '../db/offlineDb'
import { mutationQueue, generateUUID, nextTempId } from '../sync/mutationQueue'
import { isEffectivelyOffline } from '../sync/networkMode'
import { onlineThenCache } from './withOfflineFallback'
import type { Place } from '../types'
/**
* What of a write the optimistic row keeps. `fieldOrigins` says which fields the
* user typed; it travels in the queued body for the server's origin tracking and
* is no column of the place.
*/
function placeRow(data: Record<string, unknown>): Record<string, unknown> {
const { fieldOrigins: _origins, ...row } = data
return row
}
export const placeRepo = {
async list(tripId: number | string, params?: Record<string, unknown>): Promise<{ places: Place[] }> {
return onlineThenCache(
async () => {
const result = await placesApi.list(tripId, params)
// Filtered lists are a subset; only the whole list may drop what it lacks.
void (params ? upsertPlaces(result.places) : replaceTripRows('places', Number(tripId), result.places))
return result
},
async () => ({
places: await offlineDb.places
.where('trip_id').equals(Number(tripId)).toArray(),
}),
)
},
async create(tripId: number | string, data: Record<string, unknown> & { name: string }): Promise<{ place: Place }> {
if (isEffectivelyOffline()) {
const tempId = nextTempId()
const tempPlace: Place = {
...(placeRow(data) as Partial<Place>),
id: tempId,
trip_id: Number(tripId),
name: (data.name as string) ?? 'New place',
} as Place
await offlineDb.places.put(tempPlace)
const id = generateUUID()
await mutationQueue.enqueue({
id,
tripId: Number(tripId),
method: 'POST',
url: `/trips/${tripId}/places`,
body: data,
resource: 'places',
tempId,
})
return { place: tempPlace }
}
const result = await placesApi.create(tripId, data)
offlineDb.places.put(result.place)
return result
},
async update(tripId: number | string, id: number | string, data: Record<string, unknown>): Promise<{ place: Place }> {
if (isEffectivelyOffline()) {
const existing = await offlineDb.places.get(Number(id))
// trip_id has to be there even when nothing was cached: every read goes
// through places.where('trip_id'), and clearTripData() evicts by it too —
// a row without it is invisible and never cleaned up.
const optimistic: Place = {
...(existing ?? {} as Place),
...(placeRow(data) as Partial<Place>),
id: Number(id),
trip_id: Number(tripId),
}
await offlineDb.places.put(optimistic)
const mutId = generateUUID()
const isTemp = Number(id) < 0
await mutationQueue.enqueue({
id: mutId,
tripId: Number(tripId),
method: 'PUT',
url: isTemp ? `/trips/${tripId}/places/{id}` : `/trips/${tripId}/places/${id}`,
body: data,
resource: 'places',
entityId: Number(id),
baseUpdatedAt: existing?.updated_at ?? null,
...(isTemp ? { tempEntityId: Number(id) } : {}),
})
return { place: optimistic }
}
const result = await placesApi.update(tripId, id, data)
offlineDb.places.put(result.place)
return result
},
async delete(tripId: number | string, id: number | string): Promise<{ success?: boolean; tourPlaceIds?: number[] }> {
if (isEffectivelyOffline()) {
// A tour is a place plus its facet; the server's cascade drops both.
await offlineDb.places.delete(Number(id))
await offlineDb.tours.delete(Number(id))
const mutId = generateUUID()
const isTemp = Number(id) < 0
await mutationQueue.enqueue({
id: mutId,
tripId: Number(tripId),
method: 'DELETE',
url: isTemp ? `/trips/${tripId}/places/{id}` : `/trips/${tripId}/places/${id}`,
body: undefined,
resource: 'places',
entityId: Number(id),
...(isTemp ? { tempEntityId: Number(id) } : {}),
})
return { success: true }
}
const result = await placesApi.delete(tripId, id)
offlineDb.places.delete(Number(id))
offlineDb.tours.delete(Number(id))
return result
},
async deleteMany(tripId: number | string, ids: number[]): Promise<{ deleted?: number[]; count?: number; tourPlaceIds?: number[] }> {
if (isEffectivelyOffline()) {
await offlineDb.places.bulkDelete(ids)
await offlineDb.tours.bulkDelete(ids)
for (const id of ids) {
const mutId = generateUUID()
const isTemp = id < 0
await mutationQueue.enqueue({
id: mutId,
tripId: Number(tripId),
method: 'DELETE',
url: isTemp ? `/trips/${tripId}/places/{id}` : `/trips/${tripId}/places/${id}`,
body: undefined,
resource: 'places',
entityId: id,
...(isTemp ? { tempEntityId: id } : {}),
})
}
return { deleted: ids, count: ids.length }
}
const result = await placesApi.bulkDelete(tripId, ids)
await offlineDb.places.bulkDelete(ids)
await offlineDb.tours.bulkDelete(ids)
return result
},
async updateMany(tripId: number | string, ids: number[], data: Record<string, unknown>): Promise<{ updated: number[]; count: number }> {
if (isEffectivelyOffline()) {
// Offline fans out one queued PUT per id (mirrors deleteMany's DELETE fan-out).
for (const id of ids) {
const existing = await offlineDb.places.get(id)
if (existing) await offlineDb.places.put({ ...existing, ...(data as Partial<Place>) })
const mutId = generateUUID()
const isTemp = id < 0
await mutationQueue.enqueue({
id: mutId,
tripId: Number(tripId),
method: 'PUT',
url: isTemp ? `/trips/${tripId}/places/{id}` : `/trips/${tripId}/places/${id}`,
body: data,
resource: 'places',
entityId: id,
baseUpdatedAt: existing?.updated_at ?? null,
...(isTemp ? { tempEntityId: id } : {}),
})
}
return { updated: ids, count: ids.length }
}
const result = await placesApi.bulkUpdate(tripId, ids, data as Parameters<typeof placesApi.bulkUpdate>[2])
const cached = await offlineDb.places.bulkGet(ids)
await offlineDb.places.bulkPut(cached.filter(Boolean).map(p => ({ ...(p as Place), ...(data as Partial<Place>) })))
return result
},
}