aktueller Stand
This commit is contained in:
@@ -115,10 +115,17 @@ async function fetchProfile(cookieHeader) {
|
||||
}
|
||||
}
|
||||
|
||||
async function fetchStores(cookieHeader, profileId) {
|
||||
function wait(ms = 0) {
|
||||
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||
}
|
||||
|
||||
async function fetchStores(cookieHeader, profileId, options = {}) {
|
||||
if (!profileId) {
|
||||
return [];
|
||||
}
|
||||
const delayBetweenRequestsMs = Number.isFinite(options.delayBetweenRequestsMs)
|
||||
? Math.max(0, options.delayBetweenRequestsMs)
|
||||
: 0;
|
||||
try {
|
||||
const response = await client.get(`/api/user/${profileId}/stores`, {
|
||||
headers: buildHeaders(cookieHeader),
|
||||
@@ -136,42 +143,37 @@ async function fetchStores(cookieHeader, profileId) {
|
||||
zip: store.zip || ''
|
||||
}));
|
||||
|
||||
return annotateStoresWithPickupSlots(normalized, cookieHeader);
|
||||
return annotateStoresWithPickupSlots(normalized, cookieHeader, delayBetweenRequestsMs);
|
||||
} catch (error) {
|
||||
console.warn('Stores konnten nicht geladen werden:', error.message);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
async function annotateStoresWithPickupSlots(stores, cookieHeader, concurrency = 5) {
|
||||
async function annotateStoresWithPickupSlots(stores, cookieHeader, delayBetweenRequestsMs = 0) {
|
||||
if (!Array.isArray(stores) || stores.length === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const cappedConcurrency = Math.max(1, Math.min(concurrency, stores.length));
|
||||
const results = new Array(stores.length);
|
||||
let pointer = 0;
|
||||
const delayMs = Number.isFinite(delayBetweenRequestsMs) ? Math.max(0, delayBetweenRequestsMs) : 0;
|
||||
const annotated = [];
|
||||
|
||||
async function worker() {
|
||||
while (true) {
|
||||
const currentIndex = pointer++;
|
||||
if (currentIndex >= stores.length) {
|
||||
return;
|
||||
}
|
||||
const store = stores[currentIndex];
|
||||
let hasPickupSlots = null;
|
||||
try {
|
||||
const pickups = await fetchPickups(store.id, cookieHeader);
|
||||
hasPickupSlots = Array.isArray(pickups) && pickups.length > 0;
|
||||
} catch (error) {
|
||||
console.warn(`Pickups für Store ${store.id} konnten nicht geprüft werden:`, error.message);
|
||||
}
|
||||
results[currentIndex] = { ...store, hasPickupSlots };
|
||||
for (let index = 0; index < stores.length; index += 1) {
|
||||
const store = stores[index];
|
||||
if (delayMs > 0 && index > 0) {
|
||||
await wait(delayMs);
|
||||
}
|
||||
let hasPickupSlots = null;
|
||||
try {
|
||||
const pickups = await fetchPickups(store.id, cookieHeader);
|
||||
hasPickupSlots = Array.isArray(pickups) && pickups.length > 0;
|
||||
} catch (error) {
|
||||
console.warn(`Pickups für Store ${store.id} konnten nicht geprüft werden:`, error.message);
|
||||
}
|
||||
annotated.push({ ...store, hasPickupSlots });
|
||||
}
|
||||
|
||||
await Promise.all(Array.from({ length: cappedConcurrency }, () => worker()));
|
||||
return results;
|
||||
return annotated;
|
||||
}
|
||||
|
||||
async function fetchPickups(storeId, cookieHeader) {
|
||||
|
||||
Reference in New Issue
Block a user