From c75f5c15f2dd6c0361e0a98098cd11449eec41e7 Mon Sep 17 00:00:00 2001
From: "Gomez Goiri, Aitor" <aitor.gomez@tecnalia.com>
Date: Tue, 12 Apr 2022 11:42:17 +0200
Subject: [PATCH] Considering empty tables

---
 apps/pubadmin-cli/index.js | 55 ++++++++++++++++++++++----------------
 1 file changed, 32 insertions(+), 23 deletions(-)

diff --git a/apps/pubadmin-cli/index.js b/apps/pubadmin-cli/index.js
index 86ddc4b..6c73a1f 100644
--- a/apps/pubadmin-cli/index.js
+++ b/apps/pubadmin-cli/index.js
@@ -53,32 +53,41 @@ const toSteelLine = stat => [
 
 const toCementLine = stat => [stat.total.toFixed(3), humanizePrice(stat.price)]
 
+const printMainTable = ({ perSteelCompany = [] }) => {
+  const header = [
+    "",
+    "Total slag\nt",
+    "Reused\nt [%]",
+    "Discarded\nt [%]",
+    "Price (€/t)\n Avg [Min, Max]"
+  ]
+  let body = Object.entries(perSteelCompany).map(([org, val]) => [
+    org,
+    ...toSteelLine(val)
+  ])
+  if (body.length === 0) {
+    body = [header.map(() => "-")]
+  }
+  console.log(table([header, ...body], TABLE_CONFIG))
+}
+
+const printCementOrgsTable = ({ perCementCompany = [] }) => {
+  const header = ["", "Total slag\nt", "Price (€/t)\n Avg [Min, Max]"]
+  let body = Object.entries(perCementCompany).map(([org, val]) => [
+    org,
+    ...toCementLine(val)
+  ])
+  if (body.length === 0) {
+    body = [header.map(() => "-")]
+  }
+  console.log(table([header, ...body], TABLE_CONFIG))
+}
+
 const getStats = async apiClient => {
   const stats = await apiClient.getStats()
 
-  let tableValues = [
-    [
-      "",
-      "Total slag\nt",
-      "Reused\nt [%]",
-      "Discarded\nt [%]",
-      "Price (€/t)\n Avg [Min, Max]"
-    ],
-    ...Object.entries(stats.perSteelCompany || []).map(([org, val]) => [
-      org,
-      ...toSteelLine(val)
-    ])
-  ]
-  console.log(table(tableValues, TABLE_CONFIG))
-
-  tableValues = [
-    ["", "Total slag\nt", "Price (€/t)\n Avg [Min, Max]"],
-    ...Object.entries(stats.perCementCompany || []).map(([org, val]) => [
-      org,
-      ...toCementLine(val)
-    ])
-  ]
-  console.log(table(tableValues, TABLE_CONFIG))
+  printMainTable(stats)
+  printCementOrgsTable(stats)
 }
 
 const program = createCLIApp()
-- 
GitLab