From 7fec9227437a79607546060486ec8b19f011c487 Mon Sep 17 00:00:00 2001
From: Audrey Hamelers <hamelers@ebi.ac.uk>
Date: Wed, 28 Jul 2021 15:15:36 +0100
Subject: [PATCH] improve admin claim grouping

---
 app/components/dashboard/MyQueue.jsx          |  6 +-----
 .../entities/manuscript/data-access.js        | 20 +++++++++----------
 2 files changed, 11 insertions(+), 15 deletions(-)

diff --git a/app/components/dashboard/MyQueue.jsx b/app/components/dashboard/MyQueue.jsx
index 75fd032e8..02213506a 100644
--- a/app/components/dashboard/MyQueue.jsx
+++ b/app/components/dashboard/MyQueue.jsx
@@ -115,10 +115,6 @@ class MyQueue extends React.Component {
               unclaimed.push(m)
             }
           })
-          const otherUserCount =
-            mine[0] && unclaimed[0]
-              ? total - mine[0].count - unclaimed[0].count
-              : claimed.length
           return (
             <React.Fragment>
               {errors.map(e => (
@@ -185,7 +181,7 @@ class MyQueue extends React.Component {
               )}
               {claimed.length > 0 && (
                 <React.Fragment>
-                  <H2>{`In process with another user (${otherUserCount})`}</H2>
+                  <H2>{`In process with another user (${claimed[0].count})`}</H2>
                   <DashboardList
                     currentUser={currentUser}
                     errors={setErrors}
diff --git a/server/xpub-model/entities/manuscript/data-access.js b/server/xpub-model/entities/manuscript/data-access.js
index bb926a93d..8d2f9d4d2 100644
--- a/server/xpub-model/entities/manuscript/data-access.js
+++ b/server/xpub-model/entities/manuscript/data-access.js
@@ -310,6 +310,12 @@ class Manuscript extends EpmcBaseModel {
               q.whereIn('manuscript.id', manuscriptIds)
             } else if (orgId) {
               q.where('manuscript.organization_id', '=', orgId)
+            } else if (claimedBy) {
+              q.select(
+                raw(
+                  `CASE WHEN "claimed_by" = '${user}' THEN 1 WHEN "claimed_by" is null THEN 2 ELSE 3 END as claim_group`,
+                ),
+              )
             }
           })
           .whereIn('manuscript.status', statuses)
@@ -320,9 +326,7 @@ class Manuscript extends EpmcBaseModel {
         sq.from('base_query')
           .modify(q => {
             if (isAdmin && claimedBy) {
-              q.orderByRaw(
-                `CASE WHEN "claimed_by" = '${user}' THEN 1 WHEN "claimed_by" is null THEN 2 ELSE 3 END`,
-              )
+              q.orderBy('claim_group')
             }
           })
           .orderBy('updated', 'desc')
@@ -332,17 +336,13 @@ class Manuscript extends EpmcBaseModel {
         if (claimedBy) {
           q.leftOuterJoin(
             sq => {
-              sq.select(raw('count("id"), "claimed_by"'))
+              sq.select(raw('count("id"), "claim_group"'))
                 .from('base_query')
-                .groupBy('claimed_by')
+                .groupBy('claim_group')
                 .as('counts')
             },
             join => {
-              join.on(
-                raw(
-                  'bq.claimed_by = counts.claimed_by or (counts.claimed_by is null and bq.claimed_by is null)',
-                ),
-              )
+              join.on(raw('bq.claim_group = counts.claim_group'))
             },
           )
         }
-- 
GitLab