dyomedea/src/db/rtept.ts

46 lines
1.3 KiB
TypeScript

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, rte } = await getRteDocs({ id: idRte });
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;
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;
};