Add batch processing for product price retrieval: Implement getProductPricesInCitiesBatch method in FalukantService for handling multiple product price requests in a single API call. Update FalukantController and router to support new endpoint, and refactor RevenueSection and SaleSection components to utilize batch processing for improved performance and reduced API calls.

This commit is contained in:
Torsten Schulz (local)
2026-01-29 15:58:31 +01:00
parent c5ab17ad99
commit f1717920b6
5 changed files with 146 additions and 47 deletions

View File

@@ -114,36 +114,28 @@
}
},
async loadPricesForAllProducts() {
if (this.currentRegionId === null || this.currentRegionId === undefined) {
if (this.currentRegionId === null || this.currentRegionId === undefined || !this.products.length) {
return;
}
const requests = this.products.map(async (product) => {
if (this.loadingPrices.has(product.id)) return;
this.loadingPrices.add(product.id);
try {
const currentPrice = parseFloat(this.calculateProductRevenue(product).absolute);
const { data } = await apiClient.get('/api/falukant/products/prices-in-cities', {
params: {
productId: product.id,
currentPrice: currentPrice,
currentRegionId: this.currentRegionId
}
});
this.betterPricesMap = {
...this.betterPricesMap,
[product.id]: data || []
};
} catch (error) {
console.error(`Error loading prices for product ${product.id}:`, error);
this.betterPricesMap = {
...this.betterPricesMap,
[product.id]: []
};
} finally {
this.loadingPrices.delete(product.id);
const items = this.products.map((product) => ({
productId: product.id,
currentPrice: parseFloat(this.calculateProductRevenue(product).absolute)
}));
try {
const { data } = await apiClient.post('/api/falukant/products/prices-in-cities-batch', {
currentRegionId: this.currentRegionId,
items
});
this.betterPricesMap = { ...this.betterPricesMap };
for (const product of this.products) {
this.betterPricesMap[product.id] = (data && data[product.id]) ? data[product.id] : [];
}
});
await Promise.all(requests);
} catch (error) {
console.error('Error loading prices for products:', error);
for (const product of this.products) {
this.betterPricesMap = { ...this.betterPricesMap, [product.id]: [] };
}
}
},
getBetterPrices(productId) {
return this.betterPricesMap[productId] || [];

View File

@@ -293,27 +293,26 @@
}
},
async loadPricesForInventory() {
const requests = this.inventory.map(async (item) => {
const itemKey = `${item.region.id}-${item.product.id}-${item.quality}`;
if (this.loadingPrices.has(itemKey)) return;
this.loadingPrices.add(itemKey);
try {
const currentPrice = item.product.sellCost || 0;
const { data } = await apiClient.get('/api/falukant/products/prices-in-cities', {
params: {
productId: item.product.id,
currentPrice: currentPrice
}
});
item.betterPrices = data || [];
} catch (error) {
console.error(`Error loading prices for item ${itemKey}:`, error);
item.betterPrices = [];
} finally {
this.loadingPrices.delete(itemKey);
if (this.inventory.length === 0) return;
const currentRegionId = this.inventory[0]?.region?.id ?? null;
const items = this.inventory.map(item => ({
productId: item.product.id,
currentPrice: item.product.sellCost || 0
}));
try {
const { data } = await apiClient.post('/api/falukant/products/prices-in-cities-batch', {
currentRegionId,
items
});
for (const item of this.inventory) {
item.betterPrices = data && data[item.product.id] ? data[item.product.id] : [];
}
});
await Promise.all(requests);
} catch (error) {
console.error('Error loading prices for inventory:', error);
for (const item of this.inventory) {
item.betterPrices = [];
}
}
},
getCityPriceClass(branchType) {
if (branchType === 'store') return 'city-price-green';