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';
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
|
|
|
export const putNewTrkseg = async (id?: IdTrk | IdGpx | IdTrkseg) => {
|
|
|
|
let finalId = { ...id };
|
|
|
|
if (!('trkseg' in finalId)) {
|
|
|
|
const trkId = await putNewTrk(id);
|
|
|
|
finalId = { ...trkId, trkseg: 0 };
|
|
|
|
}
|
|
|
|
const uri = getUri('trkseg', finalId);
|
|
|
|
await put(
|
|
|
|
uri,
|
|
|
|
'trkseg',
|
|
|
|
(trkseg) => {
|
|
|
|
return trkseg;
|
|
|
|
},
|
|
|
|
emptyTrkseg
|
|
|
|
);
|
|
|
|
return finalId as IdTrkseg;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getTrkseg = async (params: any) => {
|
|
|
|
const { id } = params;
|
|
|
|
const docs = await getFamily(id, { include_docs: true });
|
2022-11-27 15:18:50 +00:00
|
|
|
let target: any[];
|
|
|
|
let trkseg: Trkseg | undefined = undefined;
|
|
|
|
docs.rows.forEach((row: any) => {
|
|
|
|
// level 0
|
|
|
|
if (row.doc.type === 'trkseg') {
|
|
|
|
target = [row.doc.doc];
|
|
|
|
trkseg = row.doc.doc;
|
|
|
|
}
|
|
|
|
//level 1
|
|
|
|
if (row.doc.type === 'trkpt') {
|
|
|
|
target.splice(1);
|
2023-01-07 17:46:14 +00:00
|
|
|
row.doc.doc.id = row.doc._id;
|
2022-11-27 16:12:25 +00:00
|
|
|
appendToArray(target.at(-1), row.doc.type, row.doc.doc);
|
2022-11-27 15:18:50 +00:00
|
|
|
target.push(row.doc.doc);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
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;
|
|
|
|
};
|