2022-11-26 18:36:55 +00:00
|
|
|
import getUri from '../lib/ids';
|
2022-11-27 15:09:08 +00:00
|
|
|
import { appendToArray, putNewGpx } from './gpx';
|
2022-11-26 18:36:55 +00:00
|
|
|
import { getFamily, put } from './lib';
|
|
|
|
|
|
|
|
export const emptyTrk: Trk = {
|
|
|
|
name: undefined,
|
|
|
|
cmt: undefined,
|
|
|
|
desc: undefined,
|
|
|
|
src: undefined,
|
|
|
|
link: undefined,
|
|
|
|
number: 0,
|
|
|
|
type: undefined,
|
|
|
|
extensions: undefined,
|
|
|
|
trkseg: undefined,
|
|
|
|
};
|
|
|
|
|
|
|
|
export const putNewTrk = async (id?: IdTrk | IdGpx) => {
|
|
|
|
let finalId = { ...id };
|
|
|
|
if (!('trk' in finalId)) {
|
|
|
|
const gpxId = await putNewGpx(id);
|
|
|
|
finalId = { ...gpxId, trk: 0 };
|
|
|
|
}
|
|
|
|
const uri = getUri('trk', finalId);
|
|
|
|
await put(
|
|
|
|
uri,
|
|
|
|
'trk',
|
|
|
|
(trk) => {
|
|
|
|
return trk;
|
|
|
|
},
|
|
|
|
emptyTrk
|
|
|
|
);
|
|
|
|
return finalId as IdTrk;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getTrk = async (params: any) => {
|
|
|
|
const { id } = params;
|
|
|
|
const docs = await getFamily(id, { include_docs: true });
|
2022-11-27 15:09:08 +00:00
|
|
|
let target: any[];
|
|
|
|
let trk: Trk | undefined = undefined;
|
2023-02-02 21:19:31 +00:00
|
|
|
docs.rows.every((row: any) => {
|
2022-11-27 15:09:08 +00:00
|
|
|
// level 0
|
|
|
|
if (row.doc.type === 'trk') {
|
2023-02-02 21:19:31 +00:00
|
|
|
if (!!trk) {
|
|
|
|
return false; // Hack to stop if getFamily fails
|
|
|
|
}
|
2022-11-27 15:09:08 +00:00
|
|
|
target = [row.doc.doc];
|
|
|
|
trk = row.doc.doc;
|
|
|
|
}
|
|
|
|
//level 1
|
|
|
|
if (row.doc.type === 'trkseg') {
|
|
|
|
target.splice(1);
|
|
|
|
appendToArray(target.at(-1), row.doc.type, row.doc._id);
|
|
|
|
target.push(row.doc.doc);
|
|
|
|
}
|
2023-02-02 21:19:31 +00:00
|
|
|
return true;
|
2022-11-27 15:09:08 +00:00
|
|
|
});
|
|
|
|
return trk;
|
2022-11-26 18:36:55 +00:00
|
|
|
};
|
2023-01-08 21:05:08 +00:00
|
|
|
|
|
|
|
export const deleteTrk = async (params: any) => {
|
|
|
|
const { id } = params;
|
|
|
|
const docs = await getFamily(id, { include_docs: false });
|
|
|
|
console.log({ caller: 'deleteTrk' }, id, docs);
|
2023-01-09 13:23:41 +00:00
|
|
|
const deletedDocs = docs.rows.reverse().map((doc: any) => ({
|
2023-01-08 21:05:08 +00:00
|
|
|
_deleted: true,
|
|
|
|
_id: doc.id,
|
|
|
|
_rev: doc.value.rev,
|
|
|
|
}));
|
|
|
|
console.log({ caller: 'deleteTrk' }, id, docs, deletedDocs);
|
|
|
|
await db.bulkDocs(deletedDocs);
|
|
|
|
return id;
|
|
|
|
};
|