dyomedea/src/db/rtept.ts

48 lines
1.4 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) => {
2023-02-09 15:37:46 +00:00
console.log({ caller: 'putRtept', params });
const { idRte, index, rtept } = params;
const { docs, rte } = await getRteDocs({ id: idRte });
const nbRteptInRte = rte && rte.rtept ? rte.rtept.length : 0;
2023-02-09 15:37:46 +00:00
console.log({ caller: 'putRtept', nbRteptInRte });
const nbRteptOutRte = docs.rows.length - 1;
const nbRtept = nbRteptInRte + nbRteptOutRte;
const positiveIndex = index >= 0 ? index : nbRtept - index;
if (positiveIndex < nbRteptInRte) {
rte.rtept[positiveIndex] = rtept;
await putRte({ id: idRte, rte });
} else {
let rteptIdObj = getUri('rtept', docs.rows[1].id);
rteptIdObj.rtept = rteptIdObj.rtept + positiveIndex - nbRteptInRte;
const rteptUri = getUri('rtept', rteptIdObj);
await put(rteptUri, 'rtept', () => rtept, emptyRtept);
}
return rte;
};