Connect local write permissions to backend
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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 });
|
||||
};
|
||||
|
||||
|
||||
@@ -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",
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@@ -13,12 +13,14 @@ import {
|
||||
|
||||
export async function loadManufacturerCatalogState(config, { manufacturerId, position = 1 }) {
|
||||
const refreshResults = [];
|
||||
for (const sql of prepareManufacturerCatalogSqls({ manufacturerId })) {
|
||||
refreshResults.push(
|
||||
await executeEndpointWrite(config, sql, {
|
||||
operation: "catalog-maker-refresh-manufacturer",
|
||||
}),
|
||||
);
|
||||
if (config.database?.mode === "local" && config.database.allowWrites) {
|
||||
for (const sql of prepareManufacturerCatalogSqls({ manufacturerId })) {
|
||||
refreshResults.push(
|
||||
await executeEndpointWrite(config, sql, {
|
||||
operation: "catalog-maker-refresh-manufacturer",
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
const productResult = await queryEndpoint(
|
||||
@@ -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),
|
||||
|
||||
Reference in New Issue
Block a user