From 58cab984b88ba1beefb82c856c816337700c3ee7 Mon Sep 17 00:00:00 2001 From: Rohan Juneja Date: Fri, 23 Feb 2024 13:52:50 -0800 Subject: [PATCH] sort predicates file --- src/controllers/cron/update_local_smartapi.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/controllers/cron/update_local_smartapi.js b/src/controllers/cron/update_local_smartapi.js index cfde728..b9f30e8 100644 --- a/src/controllers/cron/update_local_smartapi.js +++ b/src/controllers/cron/update_local_smartapi.js @@ -61,6 +61,22 @@ const getServerFromSpec = spec => { return sorted_servers[0].url; }; +const sortObject = object => { + if (Array.isArray(object)) { + return object.sort(); + } + + // apparently typeof null is object + if (typeof object === 'object' && object !== null) { + return Object.keys(object).sort().reduce((acc, key) => { + acc[key] = sortObject(object[key]); + return acc; + }, {}); + } + + return object; +} + const getTRAPIWithPredicatesEndpoint = specs => { const trapi = []; let excluded_list = config.EXCLUDE_LIST.map(api => api.id); @@ -192,12 +208,13 @@ const getOpsFromEndpoint = async metadata => { const getOpsFromPredicatesEndpoints = async specs => { const metadatas = getTRAPIWithPredicatesEndpoint(specs); + metadatas.sort((a, b) => a.association.smartapi.id.localeCompare(b.association.smartapi.id)); let res = []; debug(`Now caching predicates from ${metadatas.length} TRAPI APIs`); await Promise.allSettled(metadatas.map(metadata => getOpsFromEndpoint(metadata))).then(results => { results.map(rec => { if (rec.status === "fulfilled" && rec.value) { - res.push(rec.value); + res.push(sortObject(rec.value)); } }); });