import type { JourneyDayImportRequest, JourneyDayImportResult, JourneyDayStays, JourneyGpxImportOptions, JourneyGpxImportResult, JourneyRecordedDaysResponse, JourneyRecordingPrep, JourneyRecordingPreview, JourneyRecordingPreviewRequest, JourneyRecordingSaveRequest, JourneyRecordingSaveResult, JourneyRecordingSource, } from '@trek/shared'; import apiClient, { postMultipart } from './client'; import { DAWARICH_UPSTREAM_TIMEOUT_MS } from './dawarich'; const base = (journeyId: number) => `/journeys/${journeyId}/recordings`; /** The preview and the save ask Dawarich first, so they get its longer wait. */ const upstream = { timeout: DAWARICH_UPSTREAM_TIMEOUT_MS }; /** * JRN-01: the recorded days a journey keeps, previewing one, keeping it and dropping it. * JRN-02: the stays of a day reviewed and taken over as drafts. * JRN-03: how the journey prepares its days for the caller. * #3085: the days read from a GPX file, and dropping a day of one source. */ export const journeyRecordingsApi = { list: (journeyId: number, signal?: AbortSignal): Promise => apiClient.get(base(journeyId), { signal, params: { includeGpx: true } }).then((r) => r.data), prep: (journeyId: number, signal?: AbortSignal): Promise => apiClient.get(`${base(journeyId)}/prep`, { signal }).then((r) => r.data), preview: ( journeyId: number, body: JourneyRecordingPreviewRequest, signal?: AbortSignal ): Promise => apiClient.post(`${base(journeyId)}/preview`, body, { ...upstream, signal }).then((r) => r.data), save: (journeyId: number, date: string, body: JourneyRecordingSaveRequest): Promise => apiClient.put(`${base(journeyId)}/${date}`, body, upstream).then((r) => r.data), /** Without a source the Dawarich day goes, as it always did. */ remove: (journeyId: number, date: string, source?: JourneyRecordingSource): Promise => apiClient.delete(`${base(journeyId)}/${date}`, source ? { params: { source } } : undefined).then(() => undefined), importGpx: ( journeyId: number, file: Blob, options: JourneyGpxImportOptions = {} ): Promise => { const fd = new FormData(); fd.append('file', file); if (options.date) fd.append('date', options.date); if (options.utcOffsetMinutes !== undefined) fd.append('utcOffsetMinutes', String(options.utcOffsetMinutes)); return postMultipart(`${base(journeyId)}/import-gpx`, fd); }, reviewDay: (journeyId: number, date: string, signal?: AbortSignal): Promise => apiClient.post(`${base(journeyId)}/${date}/stays/review`, {}, { ...upstream, signal }).then((r) => r.data), takeOverDay: (journeyId: number, date: string, body: JourneyDayImportRequest): Promise => apiClient.post(`${base(journeyId)}/${date}/stays/import`, body).then((r) => r.data), };