perf: bound order and product image reads

This commit is contained in:
fatequod
2026-08-27 11:01:20 +02:00
parent faf1c3ea06
commit ec214cb62b
3 changed files with 79 additions and 6 deletions
+24 -3
View File
@@ -3,11 +3,30 @@ const { isLegacyApi } = require('../../src/utils');
const { loadConfig, isConfigured } = require('../../src/module-config');
const { PERMISSIONS, requirePermission } = require('../../src/register-permissions');
const MAX_PRODUCT_IDS = 100;
const IMAGE_CONCURRENCY = 5;
async function mapWithConcurrency(values, concurrency, worker) {
let nextIndex = 0;
const runners = Array.from({ length: Math.min(concurrency, values.length) }, async () => {
while (nextIndex < values.length) {
const index = nextIndex++;
await worker(values[index]);
}
});
await Promise.all(runners);
}
module.exports = async function(params, ctx) {
requirePermission(PERMISSIONS.ORDERS_VIEW, ctx);
const productIds = Array.isArray(params?.productIds) ? params.productIds : [];
const productIds = [...new Set(
(Array.isArray(params?.productIds) ? params.productIds : [])
.map(value => String(value || '').trim())
.filter(Boolean)
)];
if (productIds.length === 0) return {};
if (productIds.length > MAX_PRODUCT_IDS) throw new Error('productIds exceeds the 100 item limit');
const config = await loadConfig();
if (!isConfigured(config)) return {};
@@ -15,12 +34,14 @@ module.exports = async function(params, ctx) {
const api = new PrestashopApi(config.apiUrl, config.apiKey, isLegacyApi(config.versionCode));
const result = {};
for (const productId of productIds) {
await mapWithConcurrency(productIds, IMAGE_CONCURRENCY, async productId => {
try {
const url = await api.getProductCoverImageUrl(productId);
if (url) result[String(productId)] = url;
} catch (_) { /* product not found or no image */ }
}
});
return result;
};
module.exports._private = { mapWithConcurrency, MAX_PRODUCT_IDS, IMAGE_CONCURRENCY };