From 07ed3b595886e217b66da864247b154f7fd27061 Mon Sep 17 00:00:00 2001 From: lingdocs <71590811+lingdocs@users.noreply.github.com> Date: Fri, 9 Sep 2022 18:25:57 +0400 Subject: [PATCH] better test summary for admin page --- account/src/routers/auth-router.ts | 43 ++++++++++++++++++++++++------ account/views/admin.ejs | 4 +-- 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/account/src/routers/auth-router.ts b/account/src/routers/auth-router.ts index 5685b17..e0bba0b 100644 --- a/account/src/routers/auth-router.ts +++ b/account/src/routers/auth-router.ts @@ -304,15 +304,42 @@ const authRouter = (passport: PassportStatic) => { } function getTestCompletionSummary(users: T.LingdocsUser[]) { - const tests: { id: string, passes: number }[] = []; - users.forEach(u => ( - Array.from(new Set(u.tests.map(x => x.id))).forEach(id => { - const ti = tests.findIndex(x => x.id === id); - if (ti > -1) tests[ti].passes++; - else tests.push({ id, passes: 1 }); - }) - )); + const tests: { id: string, passes: number, fails: number }[] = []; + users.forEach(u => { + const usersTests = removeDuplicateTests(u.tests) + usersTests.forEach(ut => { + const ti = tests.findIndex(x => x.id === ut.id); + if (ti === -1) { + tests.push({ + id: ut.id, + ...ut.done ? { passes: 1, fails: 0 } : { passes: 0, fails: 1 }, + }); + } + else tests[ti][ut.done ? "passes" : "fails"]++; + }); + }); return tests; } +function removeDuplicateTests(tests: T.TestResult[]): T.TestResult[] { + return tests.reduceRight((acc, curr) => { + const redundant = acc.filter(x => ((x.id === curr.id) && (x.done === curr.done))); + return redundant.length + ? acc + : [...acc, curr]; + }, [...tests]); +} + +// function getTestCompletionSummary(users: T.LingdocsUser[]) { +// const tests: { id: string, passes: number }[] = []; +// users.forEach(u => ( +// Array.from(new Set(u.tests.map(x => x.id))).forEach(id => { +// const ti = tests.findIndex(x => x.id === id); +// if (ti > -1) tests[ti].passes++; +// else tests.push({ id, passes: 1 }); +// }) +// )); +// return tests; +// } + export default authRouter; \ No newline at end of file diff --git a/account/views/admin.ejs b/account/views/admin.ejs index 5ac6e3f..2194252 100644 --- a/account/views/admin.ejs +++ b/account/views/admin.ejs @@ -101,9 +101,9 @@
-
Tests Completed
+
Tests Completed: Pass / Fail
<% for(var i=0; i < tests.length; i++) { %> -
<%= tests[i].id %>: <%= tests[i].passes %>
+
<%= tests[i].id %>: <%= tests[i].passes %> / <%= tests[i].fails %>
<% } %>