Replacing shared by to

This commit is contained in:
Eric van der Vlist 2023-03-04 15:13:27 +01:00
parent acb6156ed2
commit f07bf07c3f
3 changed files with 22 additions and 22 deletions

View File

@ -80,7 +80,7 @@ const prune = (
}, },
extensions: any, extensions: any,
docs: any[], docs: any[],
shared: string[] to: string[]
) => { ) => {
if (typeof object === 'object') { if (typeof object === 'object') {
for (const key in object) { for (const key in object) {
@ -126,14 +126,14 @@ const prune = (
docs.push({ docs.push({
_id: getUri(key, subId), _id: getUri(key, subId),
type: key, type: key,
shared, to,
origin: state().remoteUrl, origin: state().remoteUrl,
doc: subObjects[index], doc: subObjects[index],
}); });
prune(subId, subObjects[index], previousIds, {}, docs, shared); prune(subId, subObjects[index], previousIds, {}, docs, to);
} }
object[key] = undefined; object[key] = undefined;
} else prune(id, object[key], previousIds, extensions, docs, shared); } else prune(id, object[key], previousIds, extensions, docs, to);
} }
} }
}; };
@ -187,7 +187,7 @@ export const pruneAndSaveImportedGpx = async (params: any) => {
}; };
convertTrkToRteWhenNeeded(gpx); convertTrkToRteWhenNeeded(gpx);
let previousGpx: Gpx | null = null; let previousGpx: Gpx | null = null;
let shared: string[]; let to: string[];
if (id === 'new') { if (id === 'new') {
const currentDateTime = new Date().toISOString(); const currentDateTime = new Date().toISOString();
const startTime = new Date(findStartTime(gpx, currentDateTime)); const startTime = new Date(findStartTime(gpx, currentDateTime));
@ -204,7 +204,7 @@ export const pruneAndSaveImportedGpx = async (params: any) => {
type: 'gpx', type: 'gpx',
origin: state().remoteUrl, origin: state().remoteUrl,
doc: gpx, doc: gpx,
// a new GPX isn't shared... // a new GPX isn't to...
}, },
{ {
_id: getUri('extensions', gpxId), _id: getUri('extensions', gpxId),
@ -216,8 +216,8 @@ export const pruneAndSaveImportedGpx = async (params: any) => {
} else { } else {
gpxId = getUri('gpx', id); gpxId = getUri('gpx', id);
previousGpx = (await getGpx({ id })) ?? null; previousGpx = (await getGpx({ id })) ?? null;
if (previousGpx?.extensions?.shared) { if (previousGpx?.extensions?.to) {
shared = previousGpx?.extensions?.shared.split(/\s*,\s*/); to = previousGpx?.extensions?.to.split(/\s*,\s*/);
} }
console.log({ console.log({
caller: 'pruneAndSaveImportedGpx / previousGpx', caller: 'pruneAndSaveImportedGpx / previousGpx',
@ -249,7 +249,7 @@ export const pruneAndSaveImportedGpx = async (params: any) => {
const trkId = getUri('trk', trkUri); const trkId = getUri('trk', trkUri);
previousIds.trk = trkId.trk; previousIds.trk = trkId.trk;
} }
prune(gpxId, gpx, previousIds, extensions, docs, shared); prune(gpxId, gpx, previousIds, extensions, docs, to);
console.log({ caller: 'pruneAndSaveImportedGpx / pruned', docs }); console.log({ caller: 'pruneAndSaveImportedGpx / pruned', docs });
try { try {
const result = await putAll(docs); const result = await putAll(docs);
@ -430,12 +430,12 @@ export const putGpx = async (params: any) => {
}); });
} }
let previousShared; let previousTo;
try { try {
previousShared = (await get(`${id}/4extensions`)).doc?.shared; previousTo = (await get(`${id}/4extensions`)).doc?.to;
} catch (error) {} } catch (error) {}
console.log({ caller: 'putGpx', params, id, gpx, previousShared }); console.log({ caller: 'putGpx', params, id, gpx, previousTo });
const extensions = gpx?.extensions; const extensions = gpx?.extensions;
gpx.extensions = undefined; gpx.extensions = undefined;
@ -460,19 +460,19 @@ export const putGpx = async (params: any) => {
} }
} }
if (previousShared !== extensions?.shared) { if (previousTo !== extensions?.to) {
const shared = extensions?.shared.split(/\s*,\s*/); const to = extensions?.to.split(/\s*,\s*/);
const family = (await getFamily(id, { include_docs: true })).rows.map( const family = (await getFamily(id, { include_docs: true })).rows.map(
(row: any) => { (row: any) => {
return { ...row.doc, shared }; return { ...row.doc, to };
} }
); );
console.log({ console.log({
caller: 'putGpx / updateShared', caller: 'putGpx / updateTo',
params, params,
id, id,
gpx, gpx,
previousShared, previousTo,
family, family,
}); });
await db.bulkDocs(family); await db.bulkDocs(family);

View File

@ -21,20 +21,20 @@ export const put = async (
current = await targetDb.get(_id); current = await targetDb.get(_id);
} catch { } catch {
const parentId = getParentId(_id); const parentId = getParentId(_id);
let shared; let to;
try { try {
const parent = await db.get(parentId); const parent = await db.get(parentId);
// console.log({ caller: 'put', parent }); // console.log({ caller: 'put', parent });
shared = parent.shared; to = parent.to;
} catch {} } catch {}
current = { _rev: undefined, shared, doc: cloneDeep(defaultDoc) }; current = { _rev: undefined, to, doc: cloneDeep(defaultDoc) };
} }
try { try {
putDoc = { putDoc = {
_id, _id,
_rev: current._rev, _rev: current._rev,
type, type,
shared: current.shared, to: current.to,
origin: current.origin ?? state().remoteUrl, origin: current.origin ?? state().remoteUrl,
doc: update(current.doc), doc: update(current.doc),
}; };

2
src/db/types.d.ts vendored
View File

@ -50,7 +50,7 @@ interface Extensions {
'dyo:useragent'?: string; 'dyo:useragent'?: string;
'dyo:minZoom'?: number; 'dyo:minZoom'?: number;
address?: any; address?: any;
shared?: string; to?: string;
} }
interface Trk { interface Trk {