diff --git a/bin/build-db b/bin/build-db index bb9f4bee7..d874b3241 100755 --- a/bin/build-db +++ b/bin/build-db @@ -22,35 +22,60 @@ try { process.exit(1); } -function getJson(path) { +function parseAndValidate(path, idKeyName = "id") { const raw = JSON.parse(fs.readFileSync(path).toString()); + if (idKeyName && Array.isArray(raw)) { + try { + validatePrimaryKeys(raw, idKeyName); + } catch (err) { + console.error(`🚨 ERROR building ${path}:\n ${err}`); + process.exit(1); + } + } // Adapts a standard JSON string to what is expected to be the format // used in Elm's template strings (`"""{}"""`). return JSON.stringify(raw).replaceAll("\\", "\\\\"); } +/** + * Validates that unique identifiers are actually unique in provided datasource. + */ +function validatePrimaryKeys(records, idKeyName) { + const ids = records.map((record) => record[idKeyName]).sort(); + const duplicates = ids.filter((item, index) => ids.indexOf(item) !== index); + if (duplicates.length > 0) { + throw new Error(`Duplicate ${idKeyName}: ${duplicates}`); + } +} + const targetDbFile = "src/Static/Json.elm"; const elmTemplate = fs.readFileSync(`${targetDbFile}-template`).toString(); const elmWithFixtures = elmTemplate // Transverse JSON data - .replace("%countriesJson%", getJson("public/data/countries.json")) - .replace("%impactsJson%", getJson("public/data/impacts.json")) - .replace("%transportsJson%", getJson("public/data/transports.json")) + .replace("%countriesJson%", parseAndValidate("public/data/countries.json", "code")) + .replace("%impactsJson%", parseAndValidate("public/data/impacts.json")) + .replace("%transportsJson%", parseAndValidate("public/data/transports.json")) // Food JSON data - .replace("%foodIngredientsJson%", getJson("public/data/food/ingredients.json")) + .replace("%foodIngredientsJson%", parseAndValidate("public/data/food/ingredients.json", "id")) .replace( "%foodProcessesJson%", - getJson(NODE_ENV === "test" ? dataFiles.foodDetailed : dataFiles.foodNoDetails), + parseAndValidate(NODE_ENV === "test" ? dataFiles.foodDetailed : dataFiles.foodNoDetails, "id"), ) - .replace("%foodProductExamplesJson%", getJson("public/data/food/examples.json")) + .replace("%foodProductExamplesJson%", parseAndValidate("public/data/food/examples.json", "id")) // Textile JSON data - .replace("%textileMaterialsJson%", getJson("public/data/textile/materials.json")) + .replace("%textileMaterialsJson%", parseAndValidate("public/data/textile/materials.json", "id")) .replace( "%textileProcessesJson%", - getJson(NODE_ENV === "test" ? dataFiles.textileDetailed : dataFiles.textileNoDetails), + parseAndValidate( + NODE_ENV === "test" ? dataFiles.textileDetailed : dataFiles.textileNoDetails, + "uuid", + ), + ) + .replace( + "%textileProductExamplesJson%", + parseAndValidate("public/data/textile/examples.json", "id"), ) - .replace("%textileProductExamplesJson%", getJson("public/data/textile/examples.json")) - .replace("%textileProductsJson%", getJson("public/data/textile/products.json")); + .replace("%textileProductsJson%", parseAndValidate("public/data/textile/products.json", "id")); const header = "---- THIS FILE WAS GENERATED FROM THE FILE `Json.elm-template` BY THE `/bin/build-db` SCRIPT"; diff --git a/public/data/textile/examples.json b/public/data/textile/examples.json index 0e9ca887f..710b892cf 100644 --- a/public/data/textile/examples.json +++ b/public/data/textile/examples.json @@ -447,7 +447,7 @@ } }, { - "id": "f48d7a6b-68db-4d1c-9623-6ec0ab89cbdb", + "id": "6a2da9ff-9120-4a47-91ad-a8e72be45f4a", "name": "Tshirt coton (150g) - Remanufacturé", "category": "Tshirt / Polo", "query": { diff --git a/src/Data/Textile/Inputs.elm b/src/Data/Textile/Inputs.elm index bb667d593..ca03a9055 100644 --- a/src/Data/Textile/Inputs.elm +++ b/src/Data/Textile/Inputs.elm @@ -446,7 +446,7 @@ getOutOfEuropeEOLProbability materialInputs = |> getMaterialCategoryShare Origin.Synthetic in Split.fromFloat - (if Split.toPercent syntheticMaterialsShare >= 10 then + (if Split.toPercent syntheticMaterialsShare >= 50 then 0.121 else diff --git a/tests/e2e-textile.json b/tests/e2e-textile.json index 97dd124fe..ee03eaee0 100644 --- a/tests/e2e-textile.json +++ b/tests/e2e-textile.json @@ -207,7 +207,7 @@ "swe": 0.08000116470598892, "tre": 0.5022110547143145, "wtu": 35.180613817523366, - "ecs": 5039.336827561499, + "ecs": 4811.167813476992, "pef": 3052.638092437028 } },