Skip to content

Commit 96b6e4b

Browse files
committed
feat: update model
1 parent 7e4475f commit 96b6e4b

File tree

10 files changed

+83
-23
lines changed

10 files changed

+83
-23
lines changed

docs/useCases.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -821,14 +821,25 @@ import { getDatasetDownloadCount } from '@iqss/dataverse-client-javascript'
821821
const datasetId = 1
822822
const includeMDC = true
823823

824-
getDatasetDownloadCount.execute(datasetId, includeMDC)
824+
getDatasetDownloadCount
825+
.execute(datasetId, includeMDC)
826+
.then((datasetDownloadCount: DatasetDownloadCount) => {
827+
/* ... */
828+
})
825829

826830
/* ... */
827831
```
828832

829833
_See [use case](../src/datasets/domain/useCases/GetDatasetDownloadCount.ts) implementation_.
834+
830835
The `datasetId` parameter is a number for numeric identifiers.
831-
The `includeMDC` parameter is optional. If MDC is enabled the count will be limited to the time before MDC start if the optional `includeMDC` parameter is not included or set to False. Setting `includeMDC` to True will ignore the `:MDCStartDate` setting and return a total count.
836+
The `includeMDC` parameter is optional.
837+
838+
Note:
839+
840+
- Setting `includeMDC` to True will ignore the `MDCStartDate` setting and return a total count.
841+
- If MDC isn't enabled, the download count will return a total count, without `MDCStartDate`.
842+
- If MDC is enabled but the `includeMDC` is false, the count will be limited to the time before `MDCStartDate`
832843

833844
## Files
834845

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export interface DatasetDownloadCount {
2+
id: number
3+
downloadCount: number
4+
MDCStartDate?: string
5+
}

src/datasets/domain/repositories/IDatasetsRepository.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { DatasetDTO } from '../dtos/DatasetDTO'
77
import { DatasetDeaccessionDTO } from '../dtos/DatasetDeaccessionDTO'
88
import { MetadataBlock } from '../../../metadataBlocks'
99
import { DatasetVersionDiff } from '../models/DatasetVersionDiff'
10+
import { DatasetDownloadCount } from '../models/DatasetDownloadCount'
1011

1112
export interface IDatasetsRepository {
1213
getDataset(
@@ -51,5 +52,5 @@ export interface IDatasetsRepository {
5152
datasetVersionId: string,
5253
deaccessionDTO: DatasetDeaccessionDTO
5354
): Promise<void>
54-
getDatasetDownloadCount(datasetId: number, includeMDC?: boolean): Promise<number>
55+
getDatasetDownloadCount(datasetId: number, includeMDC?: boolean): Promise<DatasetDownloadCount>
5556
}
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
import { UseCase } from '../../../core/domain/useCases/UseCase'
2+
import { DatasetDownloadCount } from '../models/DatasetDownloadCount'
23
import { IDatasetsRepository } from '../repositories/IDatasetsRepository'
3-
export class GetDatasetDownloadCount implements UseCase<number> {
4+
5+
export class GetDatasetDownloadCount implements UseCase<DatasetDownloadCount> {
46
private datasetsRepository: IDatasetsRepository
57

68
constructor(datasetsRepository: IDatasetsRepository) {
79
this.datasetsRepository = datasetsRepository
810
}
911

1012
/**
11-
* Returns the Dataset Download Count.
13+
* Returns a DatasetDownloadCount instance, with dataset id, count and MDCStartDate(optional).
1214
*
1315
* @param {number} [datasetId] - The dataset identifier.
1416
* @param {boolean} [includeMDC(optional)] - Indicates whether to consider include counts from MDC start date or not. The default value is false
15-
* @returns {Promise<number>}
17+
* @returns {Promise<DatasetDownloadCount>}
1618
*/
17-
async execute(datasetId: number, includeMDC?: boolean): Promise<number> {
19+
async execute(datasetId: number, includeMDC?: boolean): Promise<DatasetDownloadCount> {
1820
return await this.datasetsRepository.getDatasetDownloadCount(datasetId, includeMDC)
1921
}
2022
}

src/datasets/infra/repositories/DatasetsRepository.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { transformDatasetLocksResponseToDatasetLocks } from './transformers/data
1818
import { transformDatasetPreviewsResponseToDatasetPreviewSubset } from './transformers/datasetPreviewsTransformers'
1919
import { DatasetVersionDiff } from '../../domain/models/DatasetVersionDiff'
2020
import { transformDatasetVersionDiffResponseToDatasetVersionDiff } from './transformers/datasetVersionDiffTransformers'
21+
import { DatasetDownloadCount } from '../../domain/models/DatasetDownloadCount'
2122

2223
export interface GetAllDatasetPreviewsQueryParams {
2324
per_page?: number
@@ -236,7 +237,10 @@ export class DatasetsRepository extends ApiRepository implements IDatasetsReposi
236237
})
237238
}
238239

239-
public async getDatasetDownloadCount(datasetId: number, includeMDC?: boolean): Promise<number> {
240+
public async getDatasetDownloadCount(
241+
datasetId: number,
242+
includeMDC?: boolean
243+
): Promise<DatasetDownloadCount> {
240244
const queryParams = includeMDC !== undefined ? { includeMDC } : {}
241245

242246
return this.doGet(

test/environment/.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
POSTGRES_VERSION=13
22
DATAVERSE_DB_USER=dataverse
3-
SOLR_VERSION=9.3.0
3+
SOLR_VERSION=9.8.0
44
DATAVERSE_IMAGE_REGISTRY=docker.io
55
DATAVERSE_IMAGE_TAG=unstable
66
DATAVERSE_BOOTSTRAP_TIMEOUT=5m

test/functional/datasets/GetDatasetDownloadCount.test.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ describe('execute', () => {
6060

6161
await publishDataset.execute(createdDatasetIdentifiers.persistentId, VersionUpdateType.MAJOR)
6262

63-
const downloadCount = await getDatasetDownloadCount.execute(createdDatasetIdentifiers.numericId)
63+
const actual = await getDatasetDownloadCount.execute(createdDatasetIdentifiers.numericId)
6464

65-
expect(downloadCount).toBe(0)
65+
expect(actual.downloadCount).toBe(0)
6666

6767
await deletePublishedDatasetViaApi(createdDatasetIdentifiers.persistentId)
6868
})
@@ -72,12 +72,9 @@ describe('execute', () => {
7272

7373
await publishDataset.execute(createdDatasetIdentifiers.persistentId, VersionUpdateType.MAJOR)
7474

75-
const downloadCount = await getDatasetDownloadCount.execute(
76-
createdDatasetIdentifiers.numericId,
77-
true
78-
)
75+
const actual = await getDatasetDownloadCount.execute(createdDatasetIdentifiers.numericId, true)
7976

80-
expect(downloadCount).toBe(0)
77+
expect(actual.downloadCount).toBe(0)
8178

8279
await deletePublishedDatasetViaApi(createdDatasetIdentifiers.persistentId)
8380
})
@@ -89,9 +86,9 @@ describe('execute', () => {
8986
test('should return zero if dataset has no downloads', async () => {
9087
const createdDatasetIdentifiers = await createDataset.execute(testDataset)
9188

92-
const downloadCount = await getDatasetDownloadCount.execute(createdDatasetIdentifiers.numericId)
89+
const actual = await getDatasetDownloadCount.execute(createdDatasetIdentifiers.numericId)
9390

94-
expect(downloadCount).toBe(0)
91+
expect(actual.downloadCount).toBe(0)
9592

9693
await deleteUnpublishedDatasetViaApi(createdDatasetIdentifiers.numericId)
9794
})

test/integration/datasets/DatasetsRepository.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -939,7 +939,7 @@ describe('DatasetsRepository', () => {
939939
await waitForNoLocks(testDatasetIds.numericId, 10)
940940
const actual = await sut.getDatasetDownloadCount(testDatasetIds.numericId)
941941

942-
expect(actual).toBe(0)
942+
expect(actual.downloadCount).toBe(0)
943943

944944
await deletePublishedDatasetViaApi(testDatasetIds.persistentId)
945945
})
@@ -951,7 +951,7 @@ describe('DatasetsRepository', () => {
951951

952952
const actual = await sut.getDatasetDownloadCount(testDatasetIds.numericId, true)
953953

954-
expect(actual).toBe(0)
954+
expect(actual.downloadCount).toBe(0)
955955

956956
await deletePublishedDatasetViaApi(testDatasetIds.persistentId)
957957
})

test/unit/datasets/DatasetsRepository.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030
} from '../../testHelpers/datasets/datasetHelper'
3131
import { WriteError } from '../../../src'
3232
import { VersionUpdateType } from '../../../src/datasets/domain/models/Dataset'
33+
import { DatasetDownloadCount } from '../../../src/datasets/domain/models/DatasetDownloadCount'
3334

3435
describe('DatasetsRepository', () => {
3536
const sut: DatasetsRepository = new DatasetsRepository()
@@ -1014,4 +1015,36 @@ describe('DatasetsRepository', () => {
10141015
expect(error).toBeInstanceOf(Error)
10151016
})
10161017
})
1018+
1019+
describe('getDatasetDownloaCount', () => {
1020+
const testDatasetDownloadCount: DatasetDownloadCount = {
1021+
id: testDatasetModel.id,
1022+
downloadCount: 1,
1023+
MDCStartDate: '2021-01-01'
1024+
}
1025+
test('should return citation when response is successful', async () => {
1026+
jest.spyOn(axios, 'get').mockResolvedValue(testDatasetDownloadCount)
1027+
1028+
const actual = await sut.getDatasetDownloadCount(testDatasetModel.id)
1029+
1030+
expect(axios.get).toHaveBeenCalledWith(
1031+
`${TestConstants.TEST_API_URL}/datasets/${testDatasetModel.id}/download/count`,
1032+
TestConstants.TEST_EXPECTED_UNAUTHENTICATED_REQUEST_CONFIG
1033+
)
1034+
expect(actual).toStrictEqual(testDatasetDownloadCount)
1035+
})
1036+
1037+
test('should return error on repository read error', async () => {
1038+
jest.spyOn(axios, 'get').mockRejectedValue(TestConstants.TEST_ERROR_RESPONSE)
1039+
1040+
let error = undefined as unknown as ReadError
1041+
await sut.getDatasetDownloadCount(testDatasetModel.id).catch((e) => (error = e))
1042+
1043+
expect(axios.get).toHaveBeenCalledWith(
1044+
`${TestConstants.TEST_API_URL}/datasets/${testDatasetModel.id}/download/count`,
1045+
TestConstants.TEST_EXPECTED_UNAUTHENTICATED_REQUEST_CONFIG
1046+
)
1047+
expect(error).toBeInstanceOf(Error)
1048+
})
1049+
})
10171050
})

test/unit/datasets/GetDatasetDownloadCount.test.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
11
import { GetDatasetDownloadCount } from '../../../src/datasets/domain/useCases/GetDatasetDownloadCount'
22
import { IDatasetsRepository } from '../../../src/datasets/domain/repositories/IDatasetsRepository'
33
import { ReadError } from '../../../src/core/domain/repositories/ReadError'
4+
import { DatasetDownloadCount } from '../../../src/datasets/domain/models/DatasetDownloadCount'
45

56
describe('execute', () => {
67
const testDatasetId = 1
7-
const testCount = 10
8+
const testDatasetDownloadCount: DatasetDownloadCount = {
9+
id: testDatasetId,
10+
downloadCount: 1,
11+
MDCStartDate: '2021-01-01'
12+
}
813

914
test('should return count on repository success filtering by id', async () => {
1015
const filesRepositoryStub: IDatasetsRepository = {} as IDatasetsRepository
11-
filesRepositoryStub.getDatasetDownloadCount = jest.fn().mockResolvedValue(testCount)
16+
filesRepositoryStub.getDatasetDownloadCount = jest
17+
.fn()
18+
.mockResolvedValue(testDatasetDownloadCount)
1219
const sut = new GetDatasetDownloadCount(filesRepositoryStub)
1320

1421
const actual = await sut.execute(testDatasetId)
1522

16-
expect(actual).toBe(testCount)
23+
expect(actual).toBe(testDatasetDownloadCount)
1724
expect(filesRepositoryStub.getDatasetDownloadCount).toHaveBeenCalledWith(
1825
testDatasetId,
1926
undefined

0 commit comments

Comments
 (0)