Connect local write permissions to backend
This commit is contained in:
@@ -62,9 +62,16 @@ window.fetch = (input, init = {}) => {
|
|||||||
return nativeFetch(input, init);
|
return nativeFetch(input, init);
|
||||||
const headers = new Headers(init.headers || (typeof input !== "string" ? input.headers : undefined));
|
const headers = new Headers(init.headers || (typeof input !== "string" ? input.headers : undefined));
|
||||||
const selectedMode = localStorage.getItem("catalog-maker:database-mode") || "local";
|
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.
|
// Scrape storage is separate; existing catalog endpoints continue reading Local 9bplus.
|
||||||
headers.set("X-Database-Mode", selectedMode === "live" ? "live" : "local");
|
headers.set("X-Database-Mode", selectedMode === "live" ? "live" : "local");
|
||||||
headers.set("X-Scrape-Database", selectedMode === "scrape" ? "1" : "0");
|
headers.set("X-Scrape-Database", selectedMode === "scrape" ? "1" : "0");
|
||||||
|
headers.set("X-Local-Write-Access", localWritesEnabled ? "1" : "0");
|
||||||
return nativeFetch(input, { ...init, headers });
|
return nativeFetch(input, { ...init, headers });
|
||||||
};
|
};
|
||||||
let currentPosition = 1;
|
let currentPosition = 1;
|
||||||
|
|||||||
@@ -63,9 +63,16 @@ window.fetch = (input, init = {}) => {
|
|||||||
|
|
||||||
const headers = new Headers(init.headers || (typeof input !== "string" ? input.headers : undefined));
|
const headers = new Headers(init.headers || (typeof input !== "string" ? input.headers : undefined));
|
||||||
const selectedMode = localStorage.getItem("catalog-maker:database-mode") || "local";
|
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.
|
// Scrape storage is separate; existing catalog endpoints continue reading Local 9bplus.
|
||||||
headers.set("X-Database-Mode", selectedMode === "live" ? "live" : "local");
|
headers.set("X-Database-Mode", selectedMode === "live" ? "live" : "local");
|
||||||
headers.set("X-Scrape-Database", selectedMode === "scrape" ? "1" : "0");
|
headers.set("X-Scrape-Database", selectedMode === "scrape" ? "1" : "0");
|
||||||
|
headers.set("X-Local-Write-Access", localWritesEnabled ? "1" : "0");
|
||||||
return nativeFetch(input, { ...init, headers });
|
return nativeFetch(input, { ...init, headers });
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -230,6 +230,9 @@ function getRequestConfig(request) {
|
|||||||
database: {
|
database: {
|
||||||
...config.database,
|
...config.database,
|
||||||
mode: selectedMode,
|
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 }) {
|
export async function loadManufacturerCatalogState(config, { manufacturerId, position = 1 }) {
|
||||||
const refreshResults = [];
|
const refreshResults = [];
|
||||||
for (const sql of prepareManufacturerCatalogSqls({ manufacturerId })) {
|
if (config.database?.mode === "local" && config.database.allowWrites) {
|
||||||
refreshResults.push(
|
for (const sql of prepareManufacturerCatalogSqls({ manufacturerId })) {
|
||||||
await executeEndpointWrite(config, sql, {
|
refreshResults.push(
|
||||||
operation: "catalog-maker-refresh-manufacturer",
|
await executeEndpointWrite(config, sql, {
|
||||||
}),
|
operation: "catalog-maker-refresh-manufacturer",
|
||||||
);
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const productResult = await queryEndpoint(
|
const productResult = await queryEndpoint(
|
||||||
@@ -49,6 +51,7 @@ export async function loadManufacturerCatalogState(config, { manufacturerId, pos
|
|||||||
refresh: {
|
refresh: {
|
||||||
operation: "catalog-maker-refresh-manufacturer",
|
operation: "catalog-maker-refresh-manufacturer",
|
||||||
statements: refreshResults.length,
|
statements: refreshResults.length,
|
||||||
|
skipped: refreshResults.length === 0,
|
||||||
},
|
},
|
||||||
product: normalizeProduct(product),
|
product: normalizeProduct(product),
|
||||||
combinations: combinationsResult.items.map(normalizeCombination),
|
combinations: combinationsResult.items.map(normalizeCombination),
|
||||||
|
|||||||
Reference in New Issue
Block a user