2023-02-09 20:07:09 +00:00
|
|
|
import { cloneDeep, debounce, memoize, property, wrap } from 'lodash';
|
2023-02-08 19:44:28 +00:00
|
|
|
import { delay } from '../lib/delay';
|
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
|
|
|
|
2023-02-08 19:44:28 +00:00
|
|
|
// See https://stackoverflow.com/questions/28787436/debounce-a-function-with-argument
|
|
|
|
const debounceByParam = (
|
|
|
|
targetFunc: (params: any) => any,
|
|
|
|
resolver: (params: any) => any,
|
|
|
|
...debounceParams: any
|
|
|
|
) =>
|
|
|
|
wrap(
|
|
|
|
memoize(() => debounce(targetFunc, ...debounceParams), resolver),
|
|
|
|
(getMemoizedFunc, ...params) => getMemoizedFunc(...params)(...params)
|
|
|
|
);
|
|
|
|
|
|
|
|
const compactRteOrTrkseg = (params: any) => {
|
|
|
|
const { getDocs } = params;
|
|
|
|
getDocs(params).then(({ docs }) => {
|
|
|
|
if (docs.rows.length > 1) {
|
|
|
|
const compactedDocs = docs.rows.map((row: any) => {
|
|
|
|
if (['rte', 'trkseg'].includes(row.doc.type)) {
|
|
|
|
return row.doc;
|
|
|
|
}
|
|
|
|
return { ...row.doc, doc: undefined, _deleted: true };
|
|
|
|
});
|
|
|
|
db.bulkDocs(compactedDocs);
|
2023-02-08 21:39:04 +00:00
|
|
|
console.log({ caller: 'compactRteOrTrkseg', params, compactedDocs });
|
2023-02-08 19:44:28 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export const compactRteOrTrksegDebounced = debounceByParam(
|
|
|
|
compactRteOrTrkseg,
|
|
|
|
property('id'),
|
2023-02-09 12:57:14 +00:00
|
|
|
60 * 60 * 1000 * (1 + Math.random())
|
2023-02-08 19:44:28 +00:00
|
|
|
);
|
|
|
|
|
2023-02-08 21:39:04 +00:00
|
|
|
export const getRteDocs: ({
|
|
|
|
id,
|
|
|
|
}: {
|
|
|
|
id: string;
|
|
|
|
}) => Promise<{ docs: any; rte: Rte }> = async (params) => {
|
2022-12-09 16:31:56 +00:00
|
|
|
const { id } = params;
|
|
|
|
const docs = await getFamily(id, { include_docs: true });
|
2023-02-09 20:07:09 +00:00
|
|
|
let rte: Rte;
|
|
|
|
if (docs.rows.length === 1) {
|
|
|
|
rte = docs.rows[0].doc.doc;
|
|
|
|
} else {
|
|
|
|
rte = cloneDeep(docs.rows[0].doc.doc);
|
|
|
|
if (!rte.rtept) {
|
|
|
|
rte.rtept = [];
|
2022-12-09 16:31:56 +00:00
|
|
|
}
|
2023-02-09 20:07:09 +00:00
|
|
|
docs.rows.slice(1).forEach((row: any) => {
|
|
|
|
rte.rtept.push(row.doc.doc);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
console.log({
|
|
|
|
caller: 'getRteDocs',
|
|
|
|
id,
|
|
|
|
docs,
|
2023-02-10 05:35:10 +00:00
|
|
|
nbRteptIn: docs.rows[0].doc.doc.rtept?.length,
|
2023-02-09 20:07:09 +00:00
|
|
|
nbRteptTotal: rte?.rtept?.length,
|
2022-12-09 16:31:56 +00:00
|
|
|
});
|
2023-02-08 19:44:28 +00:00
|
|
|
return { docs, rte };
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getRte = async (params: any) => {
|
|
|
|
const { id } = params;
|
|
|
|
const { docs, rte } = await getRteDocs(params);
|
|
|
|
if (docs.rows.length > 1) {
|
|
|
|
compactRteOrTrksegDebounced({ id, getDocs: getRteDocs });
|
|
|
|
}
|
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;
|
|
|
|
await put(id, 'rte', (doc) => rte, rte);
|
|
|
|
return rte;
|
|
|
|
};
|
2023-02-03 13:57:40 +00:00
|
|
|
|
|
|
|
export const deleteRte = async (params: any) => {
|
|
|
|
const { id } = params;
|
|
|
|
const docs = await getFamily(id, { include_docs: false });
|
|
|
|
console.log({ caller: 'deleteRte' }, id, docs);
|
|
|
|
const deletedDocs = docs.rows.reverse().map((doc: any) => ({
|
|
|
|
_deleted: true,
|
|
|
|
_id: doc.id,
|
|
|
|
_rev: doc.value.rev,
|
|
|
|
}));
|
|
|
|
console.log({ caller: 'deleteRte' }, id, docs, deletedDocs);
|
|
|
|
await db.bulkDocs(deletedDocs);
|
|
|
|
return id;
|
|
|
|
};
|