Connect local write permissions to backend

This commit is contained in:
2026-08-05 19:05:07 +02:00
parent e6acb21d65
commit 2f3444adb8
4 changed files with 26 additions and 6 deletions
+7
View File
@@ -62,9 +62,16 @@ window.fetch = (input, init = {}) => {
return nativeFetch(input, init);
const headers = new Headers(init.headers || (typeof input !== "string" ? input.headers : undefined));
const selectedMode = localStorage.getItem("catalog-maker:database-mode") || "local";
const permissions = JSON.parse(localStorage.getItem("catalog-maker:db-permissions") || "{}");
const localWritesEnabled = selectedMode === "local" && [
permissions.allowInsertToggle,
permissions.allowUpdateToggle,
permissions.allowDeleteToggle,
].some(Boolean);
// Scrape storage is separate; existing catalog endpoints continue reading Local 9bplus.
headers.set("X-Database-Mode", selectedMode === "live" ? "live" : "local");
headers.set("X-Scrape-Database", selectedMode === "scrape" ? "1" : "0");
headers.set("X-Local-Write-Access", localWritesEnabled ? "1" : "0");
return nativeFetch(input, { ...init, headers });
};
let currentPosition = 1;
+7
View File
@@ -63,9 +63,16 @@ window.fetch = (input, init = {}) => {
const headers = new Headers(init.headers || (typeof input !== "string" ? input.headers : undefined));
const selectedMode = localStorage.getItem("catalog-maker:database-mode") || "local";
const permissions = JSON.parse(localStorage.getItem("catalog-maker:db-permissions") || "{}");
const localWritesEnabled = selectedMode === "local" && [
permissions.allowInsertToggle,
permissions.allowUpdateToggle,
permissions.allowDeleteToggle,
].some(Boolean);
// Scrape storage is separate; existing catalog endpoints continue reading Local 9bplus.
headers.set("X-Database-Mode", selectedMode === "live" ? "live" : "local");
headers.set("X-Scrape-Database", selectedMode === "scrape" ? "1" : "0");
headers.set("X-Local-Write-Access", localWritesEnabled ? "1" : "0");
return nativeFetch(input, { ...init, headers });
};
+3
View File
@@ -230,6 +230,9 @@ function getRequestConfig(request) {
database: {
...config.database,
mode: selectedMode,
allowWrites: selectedMode === "local"
&& config.database.allowWrites
&& request.headers["x-local-write-access"] === "1",
},
};
}
+3
View File
@@ -13,6 +13,7 @@ import {
export async function loadManufacturerCatalogState(config, { manufacturerId, position = 1 }) {
const refreshResults = [];
if (config.database?.mode === "local" && config.database.allowWrites) {
for (const sql of prepareManufacturerCatalogSqls({ manufacturerId })) {
refreshResults.push(
await executeEndpointWrite(config, sql, {
@@ -20,6 +21,7 @@ export async function loadManufacturerCatalogState(config, { manufacturerId, pos
}),
);
}
}
const productResult = await queryEndpoint(
config,
@@ -49,6 +51,7 @@ export async function loadManufacturerCatalogState(config, { manufacturerId, pos
refresh: {
operation: "catalog-maker-refresh-manufacturer",
statements: refreshResults.length,
skipped: refreshResults.length === 0,
},
product: normalizeProduct(product),
combinations: combinationsResult.items.map(normalizeCombination),