From efa1b351855c787e25c5634ff1e5cbee553d0f8e Mon Sep 17 00:00:00 2001
From: "Gomez Goiri, Aitor" <aitor.gomez@tecnalia.com>
Date: Tue, 12 Apr 2022 11:43:18 +0200
Subject: [PATCH] Defining CaO value in composition command

---
 apps/sidenor-cli/index.js |  1 +
 apps/sidenor-cli/lib.js   | 22 ++++++++++++++++++++--
 2 files changed, 21 insertions(+), 2 deletions(-)

diff --git a/apps/sidenor-cli/index.js b/apps/sidenor-cli/index.js
index 88c53ed..0320297 100644
--- a/apps/sidenor-cli/index.js
+++ b/apps/sidenor-cli/index.js
@@ -27,6 +27,7 @@ program
     "--asset-id [value]",
     "Identifier of the slag whose composition has been measured"
   )
+  .option("--cao-percentage [value]", "CaO percentage in the slag")
   .action(loginAndCallAfterwards(editComposition))
 
 program
diff --git a/apps/sidenor-cli/lib.js b/apps/sidenor-cli/lib.js
index ea6eb62..816c74f 100644
--- a/apps/sidenor-cli/lib.js
+++ b/apps/sidenor-cli/lib.js
@@ -30,10 +30,28 @@ const createAsset = async (apiClient, { assetName }) => {
   return newAsset.id
 }
 
-const editComposition = async (apiClient, { assetId: paramAssetId }) => {
+const editComposition = async (
+  apiClient,
+  { assetId: paramAssetId, caoPercentage }
+) => {
   const assetId =
     paramAssetId || (await promptAssetSelection(await apiClient.richQuery()))
 
+  let chosenCaoPercentage = caoPercentage
+  if (!caoPercentage) {
+    const { inputCaoPercentage } = await inquirer.prompt([
+      {
+        type: "number",
+        name: "inputQuantity",
+        message:
+          "Choose the percentage of CaO content in the slag (min: 40%, max 70%):",
+        default: faker.datatype.number({ min: 40, max: 70 }),
+        validate: n => n >= 40 && n <= 70
+      }
+    ])
+    chosenCaoPercentage = inputCaoPercentage
+  }
+
   const previousVersion = await apiClient.getAsset(assetId)
 
   await apiClient.modifyAsset({
@@ -41,7 +59,7 @@ const editComposition = async (apiClient, { assetId: paramAssetId }) => {
     id: assetId,
     fields: {
       ...previousVersion.fields,
-      composition: "comp1"
+      composition: { cao: chosenCaoPercentage }
     }
   })
 
-- 
GitLab