From 0e435458e9d9970216057b4075c2c18721a04e38 Mon Sep 17 00:00:00 2001 From: Haupais Date: Fri, 16 Feb 2024 16:56:37 +0100 Subject: [PATCH] fix(26): fix issue encountered by michelin --- .../dataBase/bestPracticesRepository.js | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/EcoSonar-API/dataBase/bestPracticesRepository.js b/EcoSonar-API/dataBase/bestPracticesRepository.js index 7ae9387..c85000a 100644 --- a/EcoSonar-API/dataBase/bestPracticesRepository.js +++ b/EcoSonar-API/dataBase/bestPracticesRepository.js @@ -10,6 +10,8 @@ const BestPracticesRepository = function () { this.insertBestPractices = async function (reports) { if (reports.length > 0) { reports = checkValues(reports) } + reports = reports.map((report) => replaceDotWithUnderscoreInKeys(report)) + return new Promise((resolve, reject) => { if (reports.length > 0) { bestpractices @@ -188,5 +190,31 @@ function checkValues (arrayToInsert) { return arrayToInsertSanitized } +function replaceDotWithUnderscoreInKeys (obj) { + if (typeof obj === 'object' && obj !== null) { + const newObj = Array.isArray(obj) ? [] : {} + + for (const key in obj) { + if (Object.prototype.hasOwnProperty.call(obj, key)) { + const newKey = key.replace(/\./g, '_') + const value = obj[key] + + if (typeof value === 'object' && value !== null) { + // If the value is an object, recursively call the function + newObj[newKey] = replaceDotWithUnderscoreInKeys(value) + } else { + // If the value is not an object, simply assign it to the new key + newObj[newKey] = value + } + } + } + + return newObj + } else { + // If the input is not an object, return it as is + return obj + } +} + const bestPracticesRepository = new BestPracticesRepository() module.exports = bestPracticesRepository