Skip to content

Commit

Permalink
Merge pull request #28 from Accenture/ISSUE-26-best-practices-not-ava…
Browse files Browse the repository at this point in the history
…ilable

fix(26): fix issue encountered by michelin
  • Loading branch information
alicehaupais authored Feb 16, 2024
2 parents ff37f8c + 0e43545 commit 73fb3a7
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions EcoSonar-API/dataBase/bestPracticesRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit 73fb3a7

Please sign in to comment.