@@ -6,6 +6,10 @@ import {
6
6
BRCDataCatalogOrganism ,
7
7
WorkflowCategory ,
8
8
} from "../../../app/apis/catalog/brc-analytics-catalog/common/entities" ;
9
+ import {
10
+ getGenomeId ,
11
+ getOrganismId ,
12
+ } from "../../../app/apis/catalog/brc-analytics-catalog/common/utils" ;
9
13
import {
10
14
Organisms as SourceOrganisms ,
11
15
Workflow as SourceWorkflow ,
@@ -28,6 +32,9 @@ async function buildCatalog(): Promise<void> {
28
32
const organisms = buildOrganisms ( genomes ) ;
29
33
const workflows = await buildWorkflows ( ) ;
30
34
35
+ verifyUniqueIds ( "assembly" , genomes , getGenomeId ) ;
36
+ verifyUniqueIds ( "organism" , organisms , getOrganismId ) ;
37
+
31
38
console . log ( "Assemblies:" , genomes . length ) ;
32
39
await saveJson ( "catalog/output/assemblies.json" , genomes ) ;
33
40
@@ -228,6 +235,33 @@ async function saveJson(filePath: string, data: unknown): Promise<void> {
228
235
await fsp . writeFile ( filePath , JSON . stringify ( data , undefined , 2 ) + "\n" ) ;
229
236
}
230
237
238
+ /**
239
+ * Take a list of entities and check for duplicate IDs, as calculated by the given function, and throw an error if there are any.
240
+ * @param entityName - Name of the entity type, to use in the error message.
241
+ * @param entities - Array of entities.
242
+ * @param getId - Function to get an entity's ID.
243
+ */
244
+ function verifyUniqueIds < T > (
245
+ entityName : string ,
246
+ entities : T [ ] ,
247
+ getId : ( entity : T ) => string
248
+ ) : void {
249
+ const idCounts = new Map < string , number > ( ) ;
250
+ for ( const entity of entities ) {
251
+ const id = getId ( entity ) ;
252
+ idCounts . set ( id , ( idCounts . get ( id ) ?? 0 ) + 1 ) ;
253
+ }
254
+ const duplicateIdEntries = Array . from ( idCounts . entries ( ) ) . filter (
255
+ ( [ , count ] ) => count > 1
256
+ ) ;
257
+ if ( duplicateIdEntries . length > 0 ) {
258
+ const duplicateIds = duplicateIdEntries . map ( ( [ id ] ) => id ) ;
259
+ throw new Error (
260
+ `Duplicate ${ entityName } IDs found: ${ duplicateIds . join ( ", " ) } `
261
+ ) ;
262
+ }
263
+ }
264
+
231
265
function accumulateArrayValue < T > ( array : T [ ] | undefined , value : T ) : T [ ] {
232
266
if ( ! array ) return [ value ] ;
233
267
if ( array . includes ( value ) ) return array ;
0 commit comments