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; 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; // console.log({ // caller: 'putRtept', // nbRteptInRte, // nbRteptOutRte, // nbRtept, // positiveIndex, // }); if (positiveIndex < nbRteptInRte) { rte.rtept[positiveIndex] = rtept; await putRte({ id: idRte, rte }); } else { const reptId = docs.rows[1 + positiveIndex - nbRteptInRte].doc._id; await put(reptId, 'rtept', () => rtept, emptyRtept); } return rtept; };