refactor sonarqube findings
Sonarqube Scanner / Prepare environment (push) Successful in 9s
Sonarqube Scanner / Build and test (push) Successful in 25s
Sonarqube Scanner / SonarQube analysis (push) Successful in 58s
Sonarqube Scanner / Deploy to Server (push) Successful in 6s

This commit is contained in:
Florian Zumpe
2026-10-03 16:55:09 +02:00
parent 0db91660b3
commit 535148639f
3 changed files with 72 additions and 56 deletions
+70 -54
View File
@@ -914,74 +914,90 @@ globalThis.Wegwichtel = globalThis.Wegwichtel || {};
return sendForm(path, method, data);
}
async function createServerRouteFromLocal(routePackage) {
const checkpoint = () => {
const latest = ns.LocalRoutes.get(routePackage.id);
// GPS samples may arrive while media requests are pending.
if (latest) routePackage.gpx = latest.gpx;
return ns.LocalRoutes.save(routePackage);
};
let id = routePackage.route.serverRouteId;
let previousPois = [];
function checkpointLocalUpload(routePackage) {
const latest = ns.LocalRoutes.get(routePackage.id);
// GPS samples may arrive while media requests are pending.
if (latest) routePackage.gpx = latest.gpx;
return ns.LocalRoutes.save(routePackage);
}
async function prepareServerRoute(routePackage) {
const id = routePackage.route.serverRouteId;
if (id) {
const payload = await readJson(`/routes/${encodeURIComponent(id)}`);
const existing = payload.route || payload;
if (existing.activatedAt || existing.activated_at) {
throw new Error(ns.I18n.t("editor.read-only-hint"));
}
previousPois = existing.pois || [];
await sendForm(`/routes/${encodeURIComponent(id)}`, 'PUT', localRouteUploadForm(routePackage));
} else {
const payload = await sendForm('/routes', 'POST', localRouteUploadForm(routePackage));
id = routeId(payload.route || payload);
if (!id) throw new Error(ns.I18n.t("editor.error.send-route"));
routePackage.route.serverRouteId = id;
checkpoint();
return { id, previousPois: existing.pois || [] };
}
const payload = await sendForm('/routes', 'POST', localRouteUploadForm(routePackage));
const createdId = routeId(payload.route || payload);
if (!createdId) throw new Error(ns.I18n.t("editor.error.send-route"));
routePackage.route.serverRouteId = createdId;
checkpointLocalUpload(routePackage);
return { id: createdId, previousPois: [] };
}
async function syncLocalPoi(base, poi, index, routePackage) {
const payload = await sendJson(poi.serverPoiId ? `${base}/${poi.serverPoiId}` : base,
poi.serverPoiId ? 'PUT' : 'POST', {
description: poi.description || '', lat: poi.lat, lon: poi.lon, sequence: index,
title: poi.title || ns.I18n.t("editor.station-number", { value1: index + 1 }),
triggerRadiusM: poi.triggerRadiusM || 25
});
poi.serverPoiId = poiId(payload.poi || payload);
if (!poi.serverPoiId) throw new Error(ns.I18n.t("editor.error.send-station", { value1: poi.title || '' }));
checkpointLocalUpload(routePackage);
}
async function syncLocalAudio(mediaBase, poi, oldPoi, routePackage) {
if (poi.audio) {
await uploadStoredMedia(`${mediaBase}/audio`, 'audio', poi.audio, 'audio.bin',
oldPoi?.audioUrl || poi.serverAudio ? 'PUT' : 'POST');
poi.serverAudio = true;
checkpointLocalUpload(routePackage);
} else if (oldPoi?.audioUrl) {
await sendDelete(`${mediaBase}/audio`);
poi.serverAudio = false;
checkpointLocalUpload(routePackage);
}
}
async function removeMissingServerItems(previous, current, serverIdKey, base) {
const retained = new Set(current.map(item => String(item[serverIdKey])));
for (const item of previous) {
if (!retained.has(String(item.id))) await sendDelete(`${base}/${item.id}`);
}
}
async function syncLocalPictures(mediaBase, poi, oldPoi, routePackage) {
const images = poi.images || [];
for (const [index, image] of images.entries()) {
const path = image.serverPictureId ? `${mediaBase}/pictures/${image.serverPictureId}` : `${mediaBase}/pictures`;
const picture = await uploadStoredMedia(path, 'picture', image, `bild-${index + 1}.bin`,
image.serverPictureId ? 'PUT' : 'POST');
image.serverPictureId = (picture.picture || picture).id;
if (!image.serverPictureId) throw new Error(ns.I18n.t("editor.error.send-file", { value1: image.name || '' }));
checkpointLocalUpload(routePackage);
}
await removeMissingServerItems(oldPoi?.images || [], images, 'serverPictureId', `${mediaBase}/pictures`);
}
async function createServerRouteFromLocal(routePackage) {
const { id, previousPois } = await prepareServerRoute(routePackage);
const base = `/routes/${encodeURIComponent(id)}/pois`;
const orderedPois = [...(routePackage.pois || [])]
.sort((left, right) => Number(left.sequence) - Number(right.sequence));
for (const [index, poi] of orderedPois.entries()) {
const base = `/routes/${encodeURIComponent(id)}/pois`;
const payload = await sendJson(poi.serverPoiId ? `${base}/${poi.serverPoiId}` : base,
poi.serverPoiId ? 'PUT' : 'POST', {
description: poi.description || '', lat: poi.lat, lon: poi.lon, sequence: index,
title: poi.title || ns.I18n.t("editor.station-number", { value1: index + 1 }),
triggerRadiusM: poi.triggerRadiusM || 25
});
poi.serverPoiId = poiId(payload.poi || payload);
if (!poi.serverPoiId) throw new Error(ns.I18n.t("editor.error.send-station", { value1: poi.title || '' }));
checkpoint();
await syncLocalPoi(base, poi, index, routePackage);
const mediaBase = `${base}/${poi.serverPoiId}`;
const oldPoi = previousPois.find(old => String(old.id) === String(poi.serverPoiId));
if (poi.audio) {
await uploadStoredMedia(`${mediaBase}/audio`, 'audio', poi.audio, 'audio.bin',
oldPoi?.audioUrl || poi.serverAudio ? 'PUT' : 'POST');
poi.serverAudio = true;
checkpoint();
} else if (oldPoi?.audioUrl) {
await sendDelete(`${mediaBase}/audio`);
poi.serverAudio = false;
checkpoint();
}
for (const [imageIndex, image] of (poi.images || []).entries()) {
const path = image.serverPictureId ? `${mediaBase}/pictures/${image.serverPictureId}` : `${mediaBase}/pictures`;
const picture = await uploadStoredMedia(path, 'picture', image, `bild-${imageIndex + 1}.bin`,
image.serverPictureId ? 'PUT' : 'POST');
image.serverPictureId = (picture.picture || picture).id;
if (!image.serverPictureId) throw new Error(ns.I18n.t("editor.error.send-file", { value1: image.name || '' }));
checkpoint();
}
for (const oldImage of oldPoi?.images || []) {
if (!(poi.images || []).some(image => String(image.serverPictureId) === String(oldImage.id))) {
await sendDelete(`${mediaBase}/pictures/${oldImage.id}`);
}
}
}
for (const oldPoi of previousPois) {
if (!orderedPois.some(poi => String(poi.serverPoiId) === String(oldPoi.id))) {
await sendDelete(`/routes/${encodeURIComponent(id)}/pois/${oldPoi.id}`);
}
await syncLocalAudio(mediaBase, poi, oldPoi, routePackage);
await syncLocalPictures(mediaBase, poi, oldPoi, routePackage);
}
await removeMissingServerItems(previousPois, orderedPois, 'serverPoiId', base);
return id;
}