@@ -43,8 +43,10 @@ import { resolveErrorMessage } from './util/messaging';
4343
4444import { ViewInfo } from './ViewInfo' ;
4545import { createGridModelId } from './models' ;
46- import { SAMPLES_KEY } from './app/constants' ;
46+ import { SAMPLES_KEY , SOURCES_KEY } from './app/constants' ;
4747import { SCHEMAS } from './schemas' ;
48+ import { selectRows } from './query/selectRows' ;
49+ import { caseInsensitive } from './util/utils' ;
4850
4951export function selectAll (
5052 key : string ,
@@ -62,11 +64,18 @@ export function selectAll(
6264 } ) ;
6365}
6466
67+ type DataTypeRowIdsFromTransactionIds = {
68+ rowIds : string [ ] ;
69+ dataTypeIds : Record < number , number > ; // todo rename to count
70+ dataTypes ?: string [ ] ;
71+ dataTypeNameIds ?: Record < string , number > ;
72+ }
73+
6574export async function getGridIdsFromTransactionId (
6675 transactionAuditId : number | string ,
6776 dataType : string ,
6877 containerPath ?: string
69- ) : Promise < string [ ] > {
78+ ) : Promise < DataTypeRowIdsFromTransactionIds > {
7079 if ( ! transactionAuditId ) return ;
7180
7281 const failureMsg = `There was a problem retrieving the ${ dataType } from the last action.` ;
@@ -88,7 +97,11 @@ export async function getGridIdsFromTransactionId(
8897 }
8998
9099 // The server returns numbers, so we coerce to string; If we don't, it can lead to bugs (and has).
91- return response . rowIds . map ( rowId => rowId . toString ( ) ) ;
100+ const rowIds = response . rowIds . map ( rowId => rowId . toString ( ) ) ;
101+ return {
102+ rowIds,
103+ dataTypeIds : response [ 'dataTypeIds' ]
104+ }
92105}
93106
94107export async function selectGridIdsFromTransactionId (
@@ -97,33 +110,85 @@ export async function selectGridIdsFromTransactionId(
97110 transactionAuditId : number | string ,
98111 dataType : string ,
99112 actions : Actions
100- ) : Promise < string [ ] > {
113+ ) : Promise < DataTypeRowIdsFromTransactionIds > {
101114 if ( ! transactionAuditId ) return undefined ;
102115
103116 const modelId = createGridModelId ( gridIdPrefix , schemaQuery ) ;
104117 const selected = await getGridIdsFromTransactionId ( transactionAuditId , dataType ) ;
105- actions . replaceSelections ( modelId , selected ) ;
118+ actions . replaceSelections ( modelId , selected . rowIds ) ;
106119 return selected ;
107120}
108121
109- type SampleTypesFromTransactionIds = { rowIds : string [ ] ; sampleTypes : string [ ] } ;
110122
111- export async function getSampleTypesFromTransactionIds (
112- transactionAuditId : number | string
113- ) : Promise < SampleTypesFromTransactionIds > {
123+ async function getDataTypesFromTransactionId (
124+ transactionAuditId : number | string ,
125+ auditDataType : string ,
126+ schemaName : string ,
127+ queryName : string ,
128+ typeColumn : string
129+ ) : Promise < DataTypeRowIdsFromTransactionIds > {
114130 if ( ! transactionAuditId ) return undefined ;
115131
116- const rowIds = await getGridIdsFromTransactionId ( transactionAuditId , SAMPLES_KEY ) ;
117- const sampleTypes = await selectDistinctRows ( {
118- schemaName : SCHEMAS . EXP_TABLES . MATERIALS . schemaName ,
119- queryName : SCHEMAS . EXP_TABLES . MATERIALS . queryName ,
120- column : 'SampleSet/Name' ,
132+ const { rowIds, dataTypeIds } = await getGridIdsFromTransactionId ( transactionAuditId , auditDataType ) ;
133+ const distinct = await selectDistinctRows ( {
134+ schemaName,
135+ queryName,
136+ column : typeColumn ,
121137 filterArray : [ Filter . create ( 'RowId' , rowIds , Filter . Types . IN ) ] ,
122138 } ) ;
139+ return { rowIds, dataTypeIds, dataTypes : distinct . values } ;
140+ }
141+
142+ export function getSampleTypesFromTransactionIds (
143+ transactionAuditId : number | string
144+ ) : Promise < DataTypeRowIdsFromTransactionIds > {
145+ return getDataTypesFromTransactionId (
146+ transactionAuditId ,
147+ SAMPLES_KEY ,
148+ SCHEMAS . EXP_TABLES . MATERIALS . schemaName ,
149+ SCHEMAS . EXP_TABLES . MATERIALS . queryName ,
150+ 'SampleSet/Name'
151+ ) ;
152+ }
153+
154+ export async function getDataClassesFromTransactionIds (
155+ transactionAuditId : number | string
156+ ) : Promise < DataTypeRowIdsFromTransactionIds > {
157+ const results = await getDataTypesFromTransactionId (
158+ transactionAuditId ,
159+ SOURCES_KEY ,
160+ SCHEMAS . EXP_TABLES . DATA . schemaName ,
161+ SCHEMAS . EXP_TABLES . DATA . queryName ,
162+ 'DataClass/Name'
163+ ) ;
164+
165+ if ( ! results )
166+ return undefined ;
167+
168+ const { dataTypeIds, dataTypes } = results ;
169+ const dataTypeLcMap = Object . fromEntries ( ( dataTypes ?? [ ] ) . map ( dt => [ dt . toLowerCase ( ) , dt ] ) ) ;
170+
171+ const dataTypeNameIds = { } ;
172+ if ( dataTypeIds ) {
173+ const dataClasses = await selectRows ( {
174+ schemaQuery : SCHEMAS . EXP_TABLES . DATA_CLASSES ,
175+ columns : [ 'Name' , 'RowId' ] ,
176+ filterArray : [ Filter . create ( 'rowId' , Object . keys ( dataTypeIds ) , Filter . Types . IN ) ] ,
177+ containerFilter : Query . containerFilter . currentPlusProjectAndShared ,
178+ } )
179+
180+
181+ dataClasses . rows . forEach ( row => {
182+ const dataClassLc = caseInsensitive ( row , 'Name' ) ?. value ?. toLowerCase ( ) ;
183+ const rowId = caseInsensitive ( row , 'RowId' ) . value ;
184+ dataTypeNameIds [ dataTypeLcMap [ dataClassLc ] ] = dataTypeIds [ rowId ] ;
185+ } ) ;
186+ }
187+
123188 return {
124- rowIds ,
125- sampleTypes : sampleTypes . values ,
126- } ;
189+ ... results ,
190+ dataTypeNameIds
191+ }
127192}
128193
129194export interface ExportOptions {
0 commit comments