This commit is contained in:
Eric van der Vlist 2023-02-13 15:59:41 +01:00
parent 6dfe98b655
commit 8d32fa253a
2 changed files with 66 additions and 44 deletions

View File

@ -26,10 +26,14 @@ const methods = {
const sendUpdate = async (params: any) => {
if (params) {
console.log({ caller: 'ChangeHandler / sendUpdate', params });
const { method, _dispatchId, id, ...otherParams } = params;
const returnValue = await methods[<keyof typeof methods>method](params);
if (returnValue) {
returnAgain(_dispatchId, returnValue);
try {
const { method, _dispatchId, id, ...otherParams } = params;
const returnValue = await methods[<keyof typeof methods>method](params);
if (returnValue) {
returnAgain(_dispatchId, returnValue);
}
} catch (error) {
console.error({ caller: 'ChangeHandler / sendUpdate', params, error });
}
}
};

View File

@ -420,53 +420,71 @@ export const appendTrk = async (params: any) => {
export const putGpx = async (params: any) => {
let { id, gpx } = params;
if (id === 'new') {
const date = !!gpx.metadata.time ? new Date(gpx.metadata.time) : new Date();
id = getUri('gpx', {
gpx: intToGpxId(date.valueOf()),
});
}
const previousShared = (await get(`${id}/4extensions`)).doc?.shared;
console.log({ caller: 'putGpx', params, id, gpx, previousShared });
const extensions = gpx?.extensions;
gpx.extensions = undefined;
gpx.wpt = undefined;
gpx.trk = undefined;
gpx.rte = undefined;
await put(id, 'gpx', (doc) => gpx, gpx);
if (extensions !== undefined) {
try {
await put(
`${id}/4extensions`,
'extensions',
(doc) => extensions,
extensions
);
} catch (error) {
console.error({ caller: 'putGpx / extensions', error });
try {
if (id === 'new') {
const date = !!gpx.metadata.time
? new Date(gpx.metadata.time)
: new Date();
id = getUri('gpx', {
gpx: intToGpxId(date.valueOf()),
});
}
}
if (previousShared !== extensions?.shared) {
const shared = extensions?.shared.split(/\s*,\s*/);
const family = (await getFamily(id, { include_docs: true })).rows.map(
(row: any) => {
return { ...row.doc, shared };
let previousShared;
try {
previousShared = (await get(`${id}/4extensions`)).doc?.shared;
} catch (error) {}
console.log({ caller: 'putGpx', params, id, gpx, previousShared });
const extensions = gpx?.extensions;
gpx.extensions = undefined;
gpx.wpt = undefined;
gpx.trk = undefined;
gpx.rte = undefined;
await put(id, 'gpx', (doc) => gpx, gpx);
if (extensions !== undefined) {
try {
await put(
`${id}/4extensions`,
'extensions',
(doc) => extensions,
extensions
);
} catch (error) {
console.error({
caller: 'putGpx / extensions',
error,
});
}
);
console.log({
caller: 'putGpx / updateShared',
}
if (previousShared !== extensions?.shared) {
const shared = extensions?.shared.split(/\s*,\s*/);
const family = (await getFamily(id, { include_docs: true })).rows.map(
(row: any) => {
return { ...row.doc, shared };
}
);
console.log({
caller: 'putGpx / updateShared',
params,
id,
gpx,
previousShared,
family,
});
await db.bulkDocs(family);
}
} catch (error) {
console.error({
caller: 'putGpx',
params,
id,
gpx,
previousShared,
family,
error,
});
await db.bulkDocs(family);
}
return id;