From a575e8f2d9d67cf1b69da99a7cb5dbcdb236db46 Mon Sep 17 00:00:00 2001
From: alex <alex@alexloehr.net>
Date: Thu, 08 Oct 2026 15:39:23 +0000
Subject: [PATCH] GS-2555

---
 test/testKursLp.js |   81 ++++++++++++++++++++++++++++++++++++++++
 lib/libLp.js       |    9 ----
 2 files changed, 81 insertions(+), 9 deletions(-)

diff --git a/lib/libLp.js b/lib/libLp.js
index 36c2a05..cc681ff 100644
--- a/lib/libLp.js
+++ b/lib/libLp.js
@@ -14,19 +14,12 @@
 /**
  * Unterobjekte auswerten und LF kombinieren pro user
  * @param {[usr_id,login,firstname,lastname,obj_id,item_id,item_obj_id,status,status_changed]} data
- * @return {[usr_id,firstname,lastname,status,status_changed]}
+ * @return {[usr_id,login,firstname,lastname,status,status_changed]}
  */
 function alleAuswerten(data) {
    const grouped = _.groupBy(data, "usr_id")
    const ret = []
    for (const items of Object.values(grouped)) {
-      if (!items.length) continue
-
-      // const unterStatusse = items.map(it => it.status)
-      // const newStatus = auswerten(unterStatusse)
-      // const status_changed = _.max(items.map(u => u.status_changed))
-
-      // TODO GS-2555 Datum berücksichtigen für die Auswertung
       const statusData = items.map(({status_changed, status}) => ({status_changed, status}))
       const newStatus = auswerten(statusData)
       const status_changed = _.max(items.map(u => u.status_changed))
diff --git a/test/testKursLp.js b/test/testKursLp.js
index b79729b..588b0dd 100644
--- a/test/testKursLp.js
+++ b/test/testKursLp.js
@@ -5,7 +5,7 @@
 const db = require("../lib/db")
 const testData = require("./data")
 const dayjs = require("dayjs");
-const {auswerten, partitionDates} = require("../lib/libLp");
+const {alleAuswerten, auswerten, partitionDates} = require("../lib/libLp");
 
 /////////////////////////////////////////////////////////////////////////
 
@@ -274,6 +274,85 @@
 
    })
 
+   describe('the function alleAuswerten()', function () {
+
+      const recent = () => dayjs().subtract(1, "day").toDate()
+      const stale = () => dayjs().subtract(2, "month").toDate()
+      // build a raw row like the ones coming from getKursUnterobjektLp()
+      const row = (usr_id, status, date, extra = {}) => Object.assign({
+         usr_id,
+         login: `login${usr_id}`,
+         firstname: `First${usr_id}`,
+         lastname: `Last${usr_id}`,
+         obj_id: 32212,
+         item_id: 1,
+         item_obj_id: 100,
+         status,
+         status_changed: date,
+      }, extra)
+
+      it("should return an empty array for empty input", function () {
+         expect(alleAuswerten([])).to.be.an("array").and.to.have.lengthOf(0)
+      })
+
+      it("should return one row per user", function () {
+         const res = alleAuswerten([
+            row(1, 0, recent()), row(1, 2, recent()), row(1, 1, recent()),
+            row(2, 2, recent()),
+         ])
+         expect(res).to.be.an("array").and.to.have.lengthOf(2)
+         expect(res.map(r => r.usr_id).sort()).to.deep.equal([1, 2])
+      })
+
+      it("should group all items of a user into a single result row", function () {
+         const res = alleAuswerten([
+            row(1, 0, recent()), row(1, 0, recent()), row(1, 0, recent()),
+         ])
+         expect(res).to.have.lengthOf(1)
+         expect(res[0].usr_id).to.equal(1)
+      })
+
+      it("should keep users separated", function () {
+         const res = alleAuswerten([
+            row(1, 0, recent()), row(1, 2, recent()),  // user 1 -> mixed -> 1
+            row(2, 2, recent()), row(2, 2, recent()),  // user 2 -> all 2 -> 2
+         ])
+         const byId = Object.fromEntries(res.map(r => [r.usr_id, r.status]))
+         expect(byId[1]).to.equal(1)
+         expect(byId[2]).to.equal(2)
+      })
+
+      it("should order the result by usr_id ascending", function () {
+         const res = alleAuswerten([
+            row(7, 0, recent()), row(3, 0, recent()), row(7, 1, recent()),
+         ])
+         expect(res.map(r => r.usr_id)).to.deep.equal([3, 7])
+      })
+
+      /////// stale dates (outside the one-month ballpark)
+
+      it("stale: should return the status when all of a user's items are stale and equal", function () {
+         const res = alleAuswerten([row(1, 2, stale()), row(1, 2, stale())])
+         expect(res[0].status).to.equal(2)
+      })
+
+      it("stale: should return 1 when a user's stale items are mixed", function () {
+         const res = alleAuswerten([row(1, 0, stale()), row(1, 2, stale())])
+         expect(res[0].status).to.equal(1)
+      })
+
+      it("stale: a stale item should turn equal recent statuses into 1", function () {
+         const res = alleAuswerten([row(1, 0, recent()), row(1, 0, stale())])
+         expect(res[0].status).to.equal(1)
+      })
+
+      it("stale: should still return 3 when a recent item of the user is 3", function () {
+         const res = alleAuswerten([row(1, 3, recent()), row(1, 3, stale())])
+         expect(res[0].status).to.equal(3)
+      })
+
+   })
+
 })
 
 /////////////////////////////////////////////////////////////////////////

--
Gitblit v1.8.0