2023-02-10 14:30:45 +00:00
|
|
|
import { cloneDeep } from 'lodash';
|
2022-12-13 19:59:39 +00:00
|
|
|
import getUri, { intToTrkptId } from '../lib/ids';
|
2022-11-27 15:18:50 +00:00
|
|
|
import { appendToArray } from './gpx';
|
2022-11-26 18:36:55 +00:00
|
|
|
import { getFamily, put } from './lib';
|
2023-02-08 19:44:28 +00:00
|
|
|
import { compactRteOrTrksegDebounced } from './rte';
|
2022-11-26 18:36:55 +00:00
|
|
|
import { putNewTrk } from './trk';
|
2022-12-13 19:59:39 +00:00
|
|
|
import { emptyWpt } from './wpt';
|
2022-11-26 18:36:55 +00:00
|
|
|
|
|
|
|
const emptyTrkseg: Trkseg = {
|
|
|
|
trkpt: undefined,
|
|
|
|
extensions: undefined,
|
|
|
|
};
|
|
|
|
|
2023-02-10 14:30:45 +00:00
|
|
|
export const getTrksegDocs: ({
|
|
|
|
id,
|
|
|
|
}: {
|
|
|
|
id: string;
|
|
|
|
}) => Promise<{ docs: any; trkseg: Trkseg }> = async (params) => {
|
2022-11-26 18:36:55 +00:00
|
|
|
const { id } = params;
|
|
|
|
const docs = await getFamily(id, { include_docs: true });
|
2023-02-10 14:30:45 +00:00
|
|
|
let trkseg: Trkseg;
|
|
|
|
if (docs.rows.length === 1) {
|
|
|
|
trkseg = docs.rows[0].doc.doc;
|
|
|
|
} else {
|
|
|
|
trkseg = cloneDeep(docs.rows[0].doc.doc);
|
|
|
|
if (!trkseg.trkpt) {
|
|
|
|
trkseg.trkpt = [];
|
2022-11-27 15:18:50 +00:00
|
|
|
}
|
2023-02-10 14:30:45 +00:00
|
|
|
docs.rows.slice(1).forEach((row: any) => {
|
|
|
|
trkseg.trkpt.push(row.doc.doc);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
// console.log({
|
|
|
|
// caller: 'getRteDocs',
|
|
|
|
// id,
|
|
|
|
// docs,
|
|
|
|
// nbRteptIn: docs.rows[0].doc.doc.rtept?.length,
|
|
|
|
// nbRteptTotal: rte?.rtept?.length,
|
|
|
|
// });
|
|
|
|
return { docs, trkseg };
|
2023-02-08 19:44:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export const getTrkseg = async (params: any) => {
|
|
|
|
const { id } = params;
|
|
|
|
const { docs, trkseg } = await getTrksegDocs(params);
|
|
|
|
if (docs.rows.length > 1) {
|
2023-02-10 14:30:45 +00:00
|
|
|
console.log({
|
|
|
|
caller: 'getTrkseg compactRteOrTrksegDebounced required',
|
|
|
|
id,
|
|
|
|
});
|
2023-02-08 19:44:28 +00:00
|
|
|
compactRteOrTrksegDebounced({ id, getDocs: getTrksegDocs });
|
|
|
|
}
|
2022-11-27 15:18:50 +00:00
|
|
|
return trkseg;
|
2022-11-26 18:36:55 +00:00
|
|
|
};
|
2022-12-13 19:59:39 +00:00
|
|
|
|
|
|
|
export const putTrkseg = async (trkSegId: string, trkSeg = emptyTrkseg) => {
|
|
|
|
await put(
|
|
|
|
trkSegId,
|
|
|
|
'trkseg',
|
|
|
|
(doc) => {
|
|
|
|
return trkSeg;
|
|
|
|
},
|
|
|
|
emptyTrkseg
|
|
|
|
);
|
|
|
|
return trkSegId;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const appendTrkpt = async (params: any) => {
|
|
|
|
const { trksegIdObj, trkpt } = params;
|
|
|
|
const idTrkseg = getUri('trkseg', trksegIdObj);
|
|
|
|
try {
|
|
|
|
await db.get(idTrkseg);
|
|
|
|
} catch {
|
|
|
|
await put(idTrkseg, 'trkseg', (doc) => doc, emptyTrkseg);
|
|
|
|
}
|
|
|
|
const trkptIdPart = intToTrkptId(new Date(trkpt.time as string).valueOf());
|
|
|
|
const trkptIdObj = { ...trksegIdObj, trkpt: trkptIdPart };
|
|
|
|
const trkptId = getUri('trkpt', trkptIdObj);
|
|
|
|
await put(trkptId, 'trkpt', (doc) => trkpt, emptyWpt);
|
|
|
|
return trkptId;
|
|
|
|
};
|