Skip to content

Commit 1839da5

Browse files
committed
feat: get collection items optional show type counts param
1 parent 5e6ee42 commit 1839da5

File tree

8 files changed

+95
-85
lines changed

8 files changed

+95
-85
lines changed

src/collections/domain/models/CollectionItemSubset.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export interface CollectionItemSubset {
66
items: (CollectionPreview | DatasetPreview | FilePreview)[]
77
facets: CollectionItemsFacet[]
88
totalItemCount: number
9-
countPerObjectType: CountPerObjectType
9+
countPerObjectType?: CountPerObjectType
1010
}
1111

1212
export interface CollectionItemsFacet {

src/collections/domain/repositories/ICollectionsRepository.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ export interface ICollectionsRepository {
2626
collectionId?: string,
2727
limit?: number,
2828
offset?: number,
29-
collectionSearchCriteria?: CollectionSearchCriteria
29+
collectionSearchCriteria?: CollectionSearchCriteria,
30+
showTypeCounts?: boolean
3031
): Promise<CollectionItemSubset>
3132
getMyDataCollectionItems(
3233
roleIds: number[],

src/collections/domain/useCases/GetCollectionItems.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,22 @@ export class GetCollectionItems implements UseCase<CollectionItemSubset> {
1818
* @param {number} [limit] - Limit for pagination (optional).
1919
* @param {number} [offset] - Offset for pagination (optional).
2020
* @param {CollectionSearchCriteria} [collectionSearchCriteria] - Supports filtering the collection items by different properties (optional).
21+
* @param {boolean} [showTypeCounts] - If true, the response will include the count per object type (optional).
2122
* @returns {Promise<CollectionItemSubset>}
2223
*/
2324
async execute(
2425
collectionId?: string,
2526
limit?: number,
2627
offset?: number,
27-
collectionSearchCriteria?: CollectionSearchCriteria
28+
collectionSearchCriteria?: CollectionSearchCriteria,
29+
showTypeCounts = false
2830
): Promise<CollectionItemSubset> {
2931
return await this.collectionsRepository.getCollectionItems(
3032
collectionId,
3133
limit,
3234
offset,
33-
collectionSearchCriteria
35+
collectionSearchCriteria,
36+
showTypeCounts
3437
)
3538
}
3639
}

src/collections/infra/repositories/CollectionsRepository.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,14 @@ export class CollectionsRepository extends ApiRepository implements ICollections
156156
collectionId?: string,
157157
limit?: number,
158158
offset?: number,
159-
collectionSearchCriteria?: CollectionSearchCriteria
159+
collectionSearchCriteria?: CollectionSearchCriteria,
160+
showTypeCounts?: boolean
160161
): Promise<CollectionItemSubset> {
161162
const queryParams = new URLSearchParams({
162163
[GetCollectionItemsQueryParams.QUERY]: '*',
163164
[GetCollectionItemsQueryParams.SHOW_FACETS]: 'true',
164165
[GetCollectionItemsQueryParams.SORT]: SortType.DATE,
165-
[GetCollectionItemsQueryParams.ORDER]: OrderType.DESC,
166-
[GetCollectionItemsQueryParams.SHOW_TYPE_COUNTS]: 'true'
166+
[GetCollectionItemsQueryParams.ORDER]: OrderType.DESC
167167
})
168168

169169
if (collectionId) {
@@ -178,6 +178,10 @@ export class CollectionsRepository extends ApiRepository implements ICollections
178178
queryParams.set(GetCollectionItemsQueryParams.START, offset.toString())
179179
}
180180

181+
if (showTypeCounts) {
182+
queryParams.set(GetCollectionItemsQueryParams.SHOW_TYPE_COUNTS, 'true')
183+
}
184+
181185
if (collectionSearchCriteria) {
182186
this.applyCollectionSearchCriteriaToQueryParams(queryParams, collectionSearchCriteria)
183187
}

src/collections/infra/repositories/transformers/collectionTransformers.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import { CollectionFacet } from '../../../domain/models/CollectionFacet'
1010
import { CollectionFacetPayload } from './CollectionFacetPayload'
1111
import {
1212
CollectionItemsFacet,
13-
CollectionItemSubset
13+
CollectionItemSubset,
14+
CountPerObjectType
1415
} from '../../../domain/models/CollectionItemSubset'
1516
import { DatasetPreview } from '../../../../datasets'
1617
import { FilePreview } from '../../../../files'
@@ -101,9 +102,9 @@ export const transformCollectionItemsResponseToCollectionItemSubset = (
101102
const responseDataPayload = response.data.data
102103
const itemsPayload = responseDataPayload.items
103104
const facetsPayload = responseDataPayload.facets as CollectionItemsFacetPayload
104-
const countPerObjectTypePayload = responseDataPayload[
105-
'total_count_per_object_type'
106-
] as CollectionItemsCountPerObjectTypePayload
105+
const countPerObjectTypePayload = responseDataPayload['total_count_per_object_type'] as
106+
| CollectionItemsCountPerObjectTypePayload
107+
| undefined
107108

108109
const items: (DatasetPreview | FilePreview | CollectionPreview)[] = []
109110

@@ -136,17 +137,19 @@ export const transformCollectionItemsResponseToCollectionItemSubset = (
136137
})
137138
)
138139

139-
const countPerObjectType = {
140-
collections: countPerObjectTypePayload['Dataverses'],
141-
datasets: countPerObjectTypePayload['Datasets'],
142-
files: countPerObjectTypePayload['Files']
143-
}
140+
const countPerObjectType: CountPerObjectType | undefined = countPerObjectTypePayload
141+
? {
142+
collections: countPerObjectTypePayload['Dataverses'],
143+
datasets: countPerObjectTypePayload['Datasets'],
144+
files: countPerObjectTypePayload['Files']
145+
}
146+
: undefined
144147

145148
return {
146149
items,
147150
facets,
148151
totalItemCount: responseDataPayload.total_count,
149-
countPerObjectType
152+
...(countPerObjectType && { countPerObjectType })
150153
}
151154
}
152155

test/integration/collections/CollectionsRepository.test.ts

Lines changed: 12 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -530,9 +530,6 @@ describe('CollectionsRepository', () => {
530530
expect(actualCollectionPreview.type).toBe(CollectionItemType.COLLECTION)
531531

532532
expect(actual.totalItemCount).toBe(3)
533-
expect(actual.countPerObjectType.collections).toBe(1)
534-
expect(actual.countPerObjectType.datasets).toBe(1)
535-
expect(actual.countPerObjectType.files).toBe(1)
536533

537534
expect(actual.facets).toEqual(expectedFacetsAll)
538535

@@ -554,9 +551,6 @@ describe('CollectionsRepository', () => {
554551
)
555552
expect(actual.totalItemCount).toBe(1)
556553
expect((actual.items[0] as FilePreview).name).toBe(expectedFileName)
557-
expect(actual.countPerObjectType.collections).toBe(0)
558-
expect(actual.countPerObjectType.datasets).toBe(0)
559-
expect(actual.countPerObjectType.files).toBe(1)
560554

561555
const collectionSearchCriteriaForDataset = new CollectionSearchCriteria().withSearchText(
562556
'Dataset created using'
@@ -570,9 +564,6 @@ describe('CollectionsRepository', () => {
570564

571565
expect(actual.totalItemCount).toBe(1)
572566
expect((actual.items[0] as DatasetPreview).title).toBe(expectedDatasetDescription)
573-
expect(actual.countPerObjectType.collections).toBe(0)
574-
expect(actual.countPerObjectType.datasets).toBe(1)
575-
expect(actual.countPerObjectType.files).toBe(0)
576567

577568
const collectionSearchCriteriaForDatasetAndCollection =
578569
new CollectionSearchCriteria().withSearchText('the')
@@ -585,9 +576,6 @@ describe('CollectionsRepository', () => {
585576
expect(actual.totalItemCount).toBe(2)
586577
expect((actual.items[0] as DatasetPreview).title).toBe(expectedDatasetDescription)
587578
expect((actual.items[1] as CollectionPreview).name).toBe(expectedCollectionsName)
588-
expect(actual.countPerObjectType.collections).toBe(1)
589-
expect(actual.countPerObjectType.datasets).toBe(1)
590-
expect(actual.countPerObjectType.files).toBe(0)
591579

592580
// Test search text, limit and offset
593581
actual = await sut.getCollectionItems(
@@ -599,9 +587,6 @@ describe('CollectionsRepository', () => {
599587
expect(actual.items.length).toBe(1)
600588
expect(actual.totalItemCount).toBe(2)
601589
expect((actual.items[0] as CollectionPreview).name).toBe(expectedCollectionsName)
602-
expect(actual.countPerObjectType.collections).toBe(1)
603-
expect(actual.countPerObjectType.datasets).toBe(1)
604-
expect(actual.countPerObjectType.files).toBe(0)
605590

606591
// Test type collection
607592
const collectionSearchCriteriaForCollectionType =
@@ -616,9 +601,6 @@ describe('CollectionsRepository', () => {
616601
expect(actual.totalItemCount).toBe(1)
617602
expect((actual.items[0] as CollectionPreview).name).toBe(expectedCollectionsName)
618603
expect(actual.facets).toEqual(expectedFacetsFromCollectionOnly)
619-
expect(actual.countPerObjectType.collections).toBe(1)
620-
expect(actual.countPerObjectType.datasets).toBe(1)
621-
expect(actual.countPerObjectType.files).toBe(1)
622604

623605
// Test type dataset
624606
const collectionSearchCriteriaForDatasetType = new CollectionSearchCriteria().withItemTypes([
@@ -634,9 +616,6 @@ describe('CollectionsRepository', () => {
634616
expect(actual.totalItemCount).toBe(1)
635617
expect((actual.items[0] as DatasetPreview).title).toBe(expectedDatasetDescription)
636618
expect(actual.facets).toEqual(expectedFacetsFromDatasetOnly)
637-
expect(actual.countPerObjectType.collections).toBe(1)
638-
expect(actual.countPerObjectType.datasets).toBe(1)
639-
expect(actual.countPerObjectType.files).toBe(1)
640619

641620
// Test type file
642621
const collectionSearchCriteriaForFileType = new CollectionSearchCriteria().withItemTypes([
@@ -652,9 +631,6 @@ describe('CollectionsRepository', () => {
652631
expect(actual.totalItemCount).toBe(1)
653632
expect((actual.items[0] as FilePreview).name).toBe(expectedFileName)
654633
expect(actual.facets).toEqual(expectedFacetsFromFileOnly)
655-
expect(actual.countPerObjectType.collections).toBe(1)
656-
expect(actual.countPerObjectType.datasets).toBe(1)
657-
expect(actual.countPerObjectType.files).toBe(1)
658634

659635
// Test multiple types
660636
const collectionSearchCriteriaForMultiTypes = new CollectionSearchCriteria().withItemTypes([
@@ -672,9 +648,6 @@ describe('CollectionsRepository', () => {
672648
expect((actual.items[0] as FilePreview).name).toBe(expectedFileName)
673649
expect((actual.items[1] as CollectionPreview).name).toBe(expectedCollectionsName)
674650
expect(actual.facets).toEqual(expectedFacetsFromCollectionAndFile)
675-
expect(actual.countPerObjectType.collections).toBe(1)
676-
expect(actual.countPerObjectType.datasets).toBe(1)
677-
expect(actual.countPerObjectType.files).toBe(1)
678651

679652
// Test Sort by name ascending
680653
const collectionSearchCriteriaNameAscending = new CollectionSearchCriteria()
@@ -692,9 +665,6 @@ describe('CollectionsRepository', () => {
692665
expect((actual.items[0] as DatasetPreview).type).toBe(CollectionItemType.DATASET)
693666
expect((actual.items[1] as CollectionPreview).type).toBe(CollectionItemType.COLLECTION)
694667
expect((actual.items[2] as FilePreview).type).toBe(CollectionItemType.FILE)
695-
expect(actual.countPerObjectType.collections).toBe(1)
696-
expect(actual.countPerObjectType.datasets).toBe(1)
697-
expect(actual.countPerObjectType.files).toBe(1)
698668

699669
// Test Sort by name descending
700670
const collectionSearchCriteriaNameDescending = new CollectionSearchCriteria()
@@ -712,9 +682,6 @@ describe('CollectionsRepository', () => {
712682
expect((actual.items[0] as FilePreview).type).toBe(CollectionItemType.FILE)
713683
expect((actual.items[1] as CollectionPreview).type).toBe(CollectionItemType.COLLECTION)
714684
expect((actual.items[2] as DatasetPreview).type).toBe(CollectionItemType.DATASET)
715-
expect(actual.countPerObjectType.collections).toBe(1)
716-
expect(actual.countPerObjectType.datasets).toBe(1)
717-
expect(actual.countPerObjectType.files).toBe(1)
718685

719686
// Test Sort by date ascending
720687
const collectionSearchCriteriaDateAscending = new CollectionSearchCriteria()
@@ -732,9 +699,6 @@ describe('CollectionsRepository', () => {
732699
expect((actual.items[0] as CollectionPreview).type).toBe(CollectionItemType.COLLECTION)
733700
expect((actual.items[1] as DatasetPreview).type).toBe(CollectionItemType.DATASET)
734701
expect((actual.items[2] as FilePreview).type).toBe(CollectionItemType.FILE)
735-
expect(actual.countPerObjectType.collections).toBe(1)
736-
expect(actual.countPerObjectType.datasets).toBe(1)
737-
expect(actual.countPerObjectType.files).toBe(1)
738702

739703
// Test Sort by date descending
740704
const collectionSearchCriteriaDateDescending = new CollectionSearchCriteria()
@@ -752,9 +716,6 @@ describe('CollectionsRepository', () => {
752716
expect((actual.items[0] as DatasetPreview).type).toBe(CollectionItemType.DATASET)
753717
expect((actual.items[1] as FilePreview).type).toBe(CollectionItemType.FILE)
754718
expect((actual.items[2] as CollectionPreview).type).toBe(CollectionItemType.COLLECTION)
755-
expect(actual.countPerObjectType.collections).toBe(1)
756-
expect(actual.countPerObjectType.datasets).toBe(1)
757-
expect(actual.countPerObjectType.files).toBe(1)
758719

759720
// Test with Filter query related to the collection
760721
const collectionSearchCriteriaFilterQueryCollection =
@@ -770,9 +731,6 @@ describe('CollectionsRepository', () => {
770731
expect(actual.totalItemCount).toBe(1)
771732
expect((actual.items[0] as CollectionPreview).name).toBe(expectedCollectionsName)
772733
expect(actual.facets).toEqual(expectedFacetsFromCollectionOnly)
773-
expect(actual.countPerObjectType.collections).toBe(1)
774-
expect(actual.countPerObjectType.datasets).toBe(0)
775-
expect(actual.countPerObjectType.files).toBe(0)
776734

777735
// Test with Filter query related to the dataset
778736
const collectionSearchCriteriaFilterQueryDataset =
@@ -790,9 +748,6 @@ describe('CollectionsRepository', () => {
790748
expect(actual.totalItemCount).toBe(1)
791749
expect((actual.items[0] as DatasetPreview).title).toBe(expectedDatasetDescription)
792750
expect(actual.facets).toEqual(expectedFacetsFromDatasetOnly)
793-
expect(actual.countPerObjectType.collections).toBe(0)
794-
expect(actual.countPerObjectType.datasets).toBe(1)
795-
expect(actual.countPerObjectType.files).toBe(0)
796751

797752
// Test with Filter query related to the file
798753
const collectionSearchCriteriaFilterQuerieCollAndFile =
@@ -809,9 +764,18 @@ describe('CollectionsRepository', () => {
809764
expect(actual.totalItemCount).toBe(1)
810765
expect((actual.items[0] as FilePreview).name).toBe(expectedFileName)
811766
expect(actual.facets).toEqual(expectedFacetsFromFileOnly)
812-
expect(actual.countPerObjectType.collections).toBe(0)
813-
expect(actual.countPerObjectType.datasets).toBe(0)
814-
expect(actual.countPerObjectType.files).toBe(1)
767+
768+
// Test with showTypeCounts param in true
769+
actual = await sut.getCollectionItems(
770+
testCollectionAlias,
771+
undefined,
772+
undefined,
773+
undefined,
774+
true
775+
)
776+
expect(actual.countPerObjectType?.collections).toBe(1)
777+
expect(actual.countPerObjectType?.datasets).toBe(1)
778+
expect(actual.countPerObjectType?.files).toBe(1)
815779
})
816780

817781
test('should return error when collection does not exist', async () => {

test/unit/collections/CollectionsRepository.test.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -419,8 +419,7 @@ describe('CollectionsRepository', () => {
419419
[GetCollectionItemsQueryParams.QUERY]: '*',
420420
[GetCollectionItemsQueryParams.SHOW_FACETS]: 'true',
421421
[GetCollectionItemsQueryParams.SORT]: SortType.DATE,
422-
[GetCollectionItemsQueryParams.ORDER]: OrderType.DESC,
423-
[GetCollectionItemsQueryParams.SHOW_TYPE_COUNTS]: 'true'
422+
[GetCollectionItemsQueryParams.ORDER]: OrderType.DESC
424423
})
425424

426425
const expectedRequestConfigApiKey = {
@@ -465,7 +464,6 @@ describe('CollectionsRepository', () => {
465464
[GetCollectionItemsQueryParams.SHOW_FACETS]: 'true',
466465
[GetCollectionItemsQueryParams.SORT]: SortType.DATE,
467466
[GetCollectionItemsQueryParams.ORDER]: OrderType.DESC,
468-
[GetCollectionItemsQueryParams.SHOW_TYPE_COUNTS]: 'true',
469467
[GetCollectionItemsQueryParams.PER_PAGE]: testLimit.toString(),
470468
[GetCollectionItemsQueryParams.START]: testOffset.toString()
471469
})
@@ -513,7 +511,6 @@ describe('CollectionsRepository', () => {
513511
[GetCollectionItemsQueryParams.SHOW_FACETS]: 'true',
514512
[GetCollectionItemsQueryParams.SORT]: SortType.DATE,
515513
[GetCollectionItemsQueryParams.ORDER]: OrderType.DESC,
516-
[GetCollectionItemsQueryParams.SHOW_TYPE_COUNTS]: 'true',
517514
[GetCollectionItemsQueryParams.SUBTREE]: testCollectionId
518515
})
519516

@@ -557,8 +554,7 @@ describe('CollectionsRepository', () => {
557554
[GetCollectionItemsQueryParams.QUERY]: '*',
558555
[GetCollectionItemsQueryParams.SHOW_FACETS]: 'true',
559556
[GetCollectionItemsQueryParams.SORT]: SortType.DATE,
560-
[GetCollectionItemsQueryParams.ORDER]: OrderType.DESC,
561-
[GetCollectionItemsQueryParams.SHOW_TYPE_COUNTS]: 'true'
557+
[GetCollectionItemsQueryParams.ORDER]: OrderType.DESC
562558
})
563559

564560
const expectedRequestConfigApiKey = {

0 commit comments

Comments
 (0)