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 { id, index, wpt } = params; const { docs } = await getRteDocs({ id: id }); 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] = wpt; await putRte({ id, rte }); } else { const reptId = docs.rows[1 + positiveIndex - nbRteptInRte].doc._id; await put(reptId, 'rtept', () => wpt, emptyRtept); } return wpt; };