From 413d154a354c8a1d7de5ba20db51d7f1f5c8613e Mon Sep 17 00:00:00 2001 From: Dan Date: Wed, 10 Aug 2022 14:38:57 -0400 Subject: [PATCH] =?UTF-8?q?=E2=8F=AB=202265=20Update=20clinical=20data=20s?= =?UTF-8?q?ort=20(#796)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Test Logging + Rough Draft PaginationUpdate * Update Swagger * First Working Updated Total * Working All Docs Solution * Clean Up Total Samples * Clean up total samples 2 * Solution using Monggose Paginate + Manual Paginate * Improved Comment * Minor Comment Formatting * First Sort Logic Update * Draft Sort Update * Sort by Completion Percentage * Updated Sorting Logic from UI * Update let to const --- src/clinical/donor-repo.ts | 10 ++++- src/clinical/service-worker-thread/tasks.ts | 50 ++++++++++++++++++--- 2 files changed, 52 insertions(+), 8 deletions(-) diff --git a/src/clinical/donor-repo.ts b/src/clinical/donor-repo.ts index de938f416..2eaab6761 100644 --- a/src/clinical/donor-repo.ts +++ b/src/clinical/donor-repo.ts @@ -182,7 +182,7 @@ export const donorDao: DonorRepository = { query: ClinicalQuery, ): Promise> { const { - sort, + sort: querySort, page: queryPage, pageSize, entityTypes, @@ -191,8 +191,16 @@ export const donorDao: DonorRepository = { completionState, } = query; + const sortQuery = querySort.includes('-') ? { [querySort.slice(1)]: -1 } : { [querySort]: 1 }; + const sort = { + 'schemaMetadata.isValid': 1, + 'completionStats.coreCompletionPercentage': 1, + ...sortQuery, + }; + const projection = [ '-_id', + 'schemaMetadata', ...DONOR_ENTITY_CORE_FIELDS, ...entityTypes, ...getRequiredDonorFieldsForEntityTypes(entityTypes), diff --git a/src/clinical/service-worker-thread/tasks.ts b/src/clinical/service-worker-thread/tasks.ts index 836d6af5f..4976fb6ef 100644 --- a/src/clinical/service-worker-thread/tasks.ts +++ b/src/clinical/service-worker-thread/tasks.ts @@ -94,12 +94,48 @@ const isEntityInQuery = (entityName: ClinicalEntitySchemaNames, entityTypes: str queryEntityNames.includes(aliasEntityNames[entityName]) && entityTypes.includes(aliasEntityNames[entityName]); -const sortDocs = (sort: string) => (currentRecord: ClinicalInfo, nextRecord: ClinicalInfo) => { - const isDescending = sort.split('-')[1] !== undefined; - const key = !isDescending ? sort.split('-')[0] : sort.split('-')[1]; - const first = currentRecord[key] !== undefined ? (currentRecord[key] as number) : -1; - const next = nextRecord[key] !== undefined ? (nextRecord[key] as number) : -1; - const order = first === next ? 0 : first > next && isDescending ? -1 : 1; +const sortDocs = (sort: string, entityName: string, completionStats: CompletionRecord[]) => ( + currentRecord: ClinicalInfo, + nextRecord: ClinicalInfo, +) => { + const isDefaultSort = entityName === ClinicalEntitySchemaNames.DONOR && sort.includes('donorId'); + const currentDonor = currentRecord['donor_id']; + const nextDonor = nextRecord['donor_id']; + + // -1 Current lower index than Next, +1 Current higher index than Next + let order = 0; + const isDescending = sort.includes('-') ? -1 : 1; + + if (isDefaultSort) { + // Sort Clinically Incomplete donors to top (sorted by donorId at DB level) + const completionA = + completionStats.find(record => record.donorId && record.donorId === currentDonor) + ?.coreCompletionPercentage || 0; + + const completionB = + completionStats.find(record => record.donorId && record.donorId === nextDonor) + ?.coreCompletionPercentage || 0; + + const completionSort = completionA === completionB ? 0 : completionA > completionB ? -1 : 1; + order = completionSort; + } else { + // Sort by Selected Column + const key = isDescending === -1 ? sort.split('-')[1] : sort; + const first: any = currentRecord[key]; + const next: any = nextRecord[key]; + const firstExists = !(first === undefined || first === null); + const nextExists = !(next === undefined || next === null); + const valueSort = + (!firstExists && !nextExists) || first === next + ? 0 + : first > next || (firstExists && !nextExists) + ? 1 + : -1; + + order = valueSort; + } + + order = isDescending * order; return order; }; @@ -124,7 +160,7 @@ const mapEntityDocuments = ( } const totalDocs = entityName === ClinicalEntitySchemaNames.DONOR ? totalDonors : results.length; - let records = results.sort(sortDocs(sort)); + let records = results.sort(sortDocs(sort, entityName, completionStats)); if (records.length > pageSize) { // Manual Pagination