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:
@@ -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] || [];
|
||||
|
||||
Reference in New Issue
Block a user