dyomedea/src/db/rtept.ts

52 lines
1.3 KiB
TypeScript
Raw Normal View History

import { getRteDocs, putRte } from './rte';
import getUri from '../lib/ids';
import { put } from './lib';
export const emptyRtept: Wpt = {
$: { lat: 0, lon: 0 },
ele: undefined,
time: undefined,
magvar: undefined,
geoidheight: undefined,
name: undefined,
cmt: undefined,
desc: undefined,
src: undefined,
link: undefined,
sym: undefined,
type: undefined,
fix: undefined,
sat: undefined,
hdop: undefined,
vdop: undefined,
pdop: undefined,
ageofdgpsdata: undefined,
dgpsid: undefined,
extensions: undefined,
};
export const putRtept = async (params: any) => {
const { idRte, index, rtept } = params;
2023-02-09 20:07:09 +00:00
const { docs } = await getRteDocs({ id: idRte });
const rte = docs.rows[0].doc.doc;
const nbRteptInRte = rte && rte.rtept ? rte.rtept.length : 0;
const nbRteptOutRte = docs.rows.length - 1;
const nbRtept = nbRteptInRte + nbRteptOutRte;
const positiveIndex = index >= 0 ? index : nbRtept - index;
2023-02-09 20:07:09 +00:00
// console.log({
// caller: 'putRtept',
// nbRteptInRte,
// nbRteptOutRte,
// nbRtept,
// positiveIndex,
// });
if (positiveIndex < nbRteptInRte) {
rte.rtept[positiveIndex] = rtept;
await putRte({ id: idRte, rte });
} else {
2023-02-09 20:07:09 +00:00
const reptId = docs.rows[1 + positiveIndex - nbRteptInRte].doc._id;
await put(reptId, 'rtept', () => rtept, emptyRtept);
}
2023-02-09 20:07:09 +00:00
return rtept;
};