2022-11-26 18:36:55 +00:00
|
|
|
import getUri from '../lib/ids';
|
2022-12-09 16:31:56 +00:00
|
|
|
import { appendToArray, putNewGpx } from './gpx';
|
|
|
|
import { getFamily, put } from './lib';
|
2022-11-26 18:36:55 +00:00
|
|
|
|
|
|
|
export const emptyRte: Rte = {
|
|
|
|
name: undefined,
|
|
|
|
cmt: undefined,
|
|
|
|
desc: undefined,
|
|
|
|
src: undefined,
|
|
|
|
link: undefined,
|
|
|
|
number: 0,
|
|
|
|
type: undefined,
|
|
|
|
extensions: undefined,
|
|
|
|
rtept: undefined,
|
|
|
|
};
|
|
|
|
|
|
|
|
export const putNewRte = async (id?: IdRte | IdGpx) => {
|
|
|
|
let finalId = { ...id };
|
|
|
|
if (!('rte' in finalId)) {
|
|
|
|
const gpxId = await putNewGpx(id);
|
|
|
|
finalId = { ...gpxId, rte: 0 };
|
|
|
|
}
|
|
|
|
const uri = getUri('rte', finalId);
|
|
|
|
await put(
|
|
|
|
uri,
|
|
|
|
'rte',
|
|
|
|
(rte) => {
|
|
|
|
return rte;
|
|
|
|
},
|
|
|
|
emptyRte
|
|
|
|
);
|
|
|
|
return finalId as IdRte;
|
|
|
|
};
|
2022-12-09 16:31:56 +00:00
|
|
|
|
|
|
|
export const getRte = async (params: any) => {
|
|
|
|
const { id } = params;
|
|
|
|
const docs = await getFamily(id, { include_docs: true });
|
|
|
|
let target: any[];
|
|
|
|
let rte: Rte | undefined = undefined;
|
2023-02-02 21:19:31 +00:00
|
|
|
docs.rows.every((row: any) => {
|
2022-12-09 16:31:56 +00:00
|
|
|
// level 0
|
|
|
|
if (row.doc.type === 'rte') {
|
2023-02-02 21:19:31 +00:00
|
|
|
if (!!rte) {
|
2023-02-03 10:59:40 +00:00
|
|
|
console.error({
|
|
|
|
caller: 'getRte',
|
|
|
|
id,
|
|
|
|
row,
|
|
|
|
target,
|
|
|
|
rte,
|
|
|
|
});
|
2023-02-02 21:19:31 +00:00
|
|
|
return false; // Hack to stop if getFamily fails
|
|
|
|
}
|
2022-12-09 16:31:56 +00:00
|
|
|
target = [row.doc.doc];
|
|
|
|
rte = row.doc.doc;
|
|
|
|
}
|
|
|
|
//level 1
|
|
|
|
if (row.doc.type === 'rtept') {
|
|
|
|
target.splice(1);
|
2023-01-07 14:41:16 +00:00
|
|
|
row.doc.doc.id = row.doc._id;
|
2022-12-09 16:31:56 +00:00
|
|
|
appendToArray(target.at(-1), row.doc.type, row.doc.doc);
|
|
|
|
target.push(row.doc.doc);
|
|
|
|
}
|
2023-02-02 21:19:31 +00:00
|
|
|
return true;
|
2022-12-09 16:31:56 +00:00
|
|
|
});
|
|
|
|
return rte;
|
|
|
|
};
|
2023-01-05 17:27:20 +00:00
|
|
|
|
|
|
|
export const putRte = async (params: any) => {
|
|
|
|
const { id, rte } = params;
|
|
|
|
rte.rtept = undefined;
|
|
|
|
await put(id, 'rte', (doc) => rte, rte);
|
|
|
|
return rte;
|
|
|
|
};
|