diff --git a/src/dataset/domain/models/DatasetVersionDiff.ts b/src/dataset/domain/models/DatasetVersionDiff.ts new file mode 100644 index 000000000..e0f94fcf0 --- /dev/null +++ b/src/dataset/domain/models/DatasetVersionDiff.ts @@ -0,0 +1,48 @@ +export interface DatasetVersionDiff { + oldVersion: VersionSummary + newVersion: VersionSummary + metadataChanges?: MetadataBlockDiff[] + filesAdded?: FileSummary[] + filesRemoved?: FileSummary[] + fileChanges?: FileDiff[] + filesReplaced?: FileReplacement[] + termsOfAccess?: FieldDiff[] +} + +export interface FileSummary { + fileName: string + MD5: string + type: string + fileId: number + filePath: string + description: string + isRestricted: boolean + tags: string[] + categories: string[] +} + +export interface VersionSummary { + versionNumber: string + lastUpdatedDate: string +} +export interface MetadataBlockDiff { + blockName: string + changed: FieldDiff[] +} + +export interface FileDiff { + fileName: string + md5: string + fileId: number + changed: FieldDiff[] +} + +export interface FileReplacement { + oldFile: FileSummary + newFile: FileSummary +} +export interface FieldDiff { + fieldName: string + oldValue: string + newValue: string +} diff --git a/src/dataset/domain/repositories/DatasetRepository.ts b/src/dataset/domain/repositories/DatasetRepository.ts index 0e708786b..7da556119 100644 --- a/src/dataset/domain/repositories/DatasetRepository.ts +++ b/src/dataset/domain/repositories/DatasetRepository.ts @@ -1,4 +1,5 @@ import { Dataset, DatasetLock } from '../models/Dataset' +import { DatasetVersionDiff } from '../models/DatasetVersionDiff' import { DatasetPaginationInfo } from '../models/DatasetPaginationInfo' import { DatasetDTO } from '../useCases/DTOs/DatasetDTO' import { DatasetsWithCount } from '../models/DatasetsWithCount' @@ -8,6 +9,12 @@ export interface DatasetRepository { getByPersistentId: (persistentId: string, version?: string) => Promise getLocks(persistentId: string): Promise getByPrivateUrlToken: (privateUrlToken: string) => Promise + getVersionDiff: ( + persistentId: string, + oldVersion: string, + newVersion: string + ) => Promise + create: (dataset: DatasetDTO, collectionId: string) => Promise<{ persistentId: string }> updateMetadata: (datasetId: string | number, datasetDTO: DatasetDTO) => Promise getAllWithCount: ( diff --git a/src/dataset/domain/useCases/getDatasetVersionDiff.ts b/src/dataset/domain/useCases/getDatasetVersionDiff.ts new file mode 100644 index 000000000..00ec7d1a8 --- /dev/null +++ b/src/dataset/domain/useCases/getDatasetVersionDiff.ts @@ -0,0 +1,15 @@ +import { DatasetRepository } from '../repositories/DatasetRepository' +import { DatasetVersionDiff } from '../models/DatasetVersionDiff' + +export async function getDatasetVersionDiff( + datasetRepository: DatasetRepository, + persistentId: string, + oldVersion: string, + newVersion: string +): Promise { + return datasetRepository + .getVersionDiff(persistentId, oldVersion, newVersion) + .catch((error: Error) => { + throw new Error(error.message) + }) +} diff --git a/src/dataset/infrastructure/mappers/JSDatasetMapper.ts b/src/dataset/infrastructure/mappers/JSDatasetMapper.ts index aa4a5fe92..d049ef8b0 100644 --- a/src/dataset/infrastructure/mappers/JSDatasetMapper.ts +++ b/src/dataset/infrastructure/mappers/JSDatasetMapper.ts @@ -8,6 +8,7 @@ import { DatasetVersionDiff as JSDatasetVersionDiff, DvObjectOwnerNode as JSUpwardHierarchyNode } from '@iqss/dataverse-client-javascript' +import { DatasetVersionDiff } from '../../domain/models/DatasetVersionDiff' import { Dataset, DatasetDownloadUrls, @@ -132,7 +133,18 @@ export class JSDatasetMapper { false return required } - + static toDatasetVersionDiff(jsDatasetVersionDiff: JSDatasetVersionDiff): DatasetVersionDiff { + return { + oldVersion: jsDatasetVersionDiff.oldVersion, + newVersion: jsDatasetVersionDiff.newVersion, + metadataChanges: jsDatasetVersionDiff.metadataChanges, + filesAdded: jsDatasetVersionDiff.filesAdded, + filesRemoved: jsDatasetVersionDiff.filesRemoved, + fileChanges: jsDatasetVersionDiff.fileChanges, + filesReplaced: jsDatasetVersionDiff.filesReplaced, + termsOfAccess: jsDatasetVersionDiff.termsOfAccess + } + } static toDatasetTitle(jsDatasetMetadataBlocks: JSDatasetMetadataBlocks): string { return jsDatasetMetadataBlocks[0].fields.title } diff --git a/src/dataset/infrastructure/repositories/DatasetJSDataverseRepository.ts b/src/dataset/infrastructure/repositories/DatasetJSDataverseRepository.ts index 25fe6876b..91c1d5232 100644 --- a/src/dataset/infrastructure/repositories/DatasetJSDataverseRepository.ts +++ b/src/dataset/infrastructure/repositories/DatasetJSDataverseRepository.ts @@ -1,5 +1,6 @@ import { DatasetRepository } from '../../domain/repositories/DatasetRepository' import { Dataset, DatasetLock, DatasetNonNumericVersion } from '../../domain/models/Dataset' +import { DatasetVersionDiff } from '../../domain/models/DatasetVersionDiff' import { createDataset, CreatedDatasetIdentifiers as JSDatasetIdentifiers, @@ -69,6 +70,18 @@ export class DatasetJSDataverseRepository implements DatasetRepository { } }) } + getVersionDiff( + persistentId: string, + oldVersion: string, + newVersion: string + ): Promise { + return getDatasetVersionDiff + .execute(persistentId, oldVersion, newVersion) + .then((jsDatasetVersionDiff) => { + return JSDatasetMapper.toDatasetVersionDiff(jsDatasetVersionDiff) + }) + } + private async getLatestPublishedVersionNumbers( datasetDetails: IDatasetDetails ): Promise { @@ -86,16 +99,14 @@ export class DatasetJSDataverseRepository implements DatasetRepository { } private async getVersionDiffDetails(datasetDetails: IDatasetDetails): Promise { - await getDatasetVersionDiff - .execute( - datasetDetails.jsDataset.persistentId, - DatasetNonNumericVersion.LATEST_PUBLISHED, - DatasetNonNumericVersion.DRAFT - ) - .then((datasetVersionDiff) => { - datasetDetails.datasetVersionDiff = datasetVersionDiff - return datasetDetails - }) + await this.getVersionDiff( + datasetDetails.jsDataset.persistentId, + DatasetNonNumericVersion.LATEST_PUBLISHED, + DatasetNonNumericVersion.DRAFT + ).then((datasetVersionDiff) => { + datasetDetails.datasetVersionDiff = datasetVersionDiff + return datasetDetails + }) return datasetDetails } diff --git a/src/stories/dataset/DatasetErrorMockRepository.ts b/src/stories/dataset/DatasetErrorMockRepository.ts index 2e35af675..5e72d0c67 100644 --- a/src/stories/dataset/DatasetErrorMockRepository.ts +++ b/src/stories/dataset/DatasetErrorMockRepository.ts @@ -5,6 +5,7 @@ import { DatasetsWithCount } from '../../dataset/domain/models/DatasetsWithCount import { DatasetDTO } from '../../dataset/domain/useCases/DTOs/DatasetDTO' import { FakerHelper } from '../../../tests/component/shared/FakerHelper' import { VersionUpdateType } from '../../dataset/domain/models/VersionUpdateType' +import { DatasetVersionDiff } from '@/dataset/domain/models/DatasetVersionDiff' export class DatasetErrorMockRepository implements DatasetMockRepository { getAllWithCount: ( @@ -46,6 +47,17 @@ export class DatasetErrorMockRepository implements DatasetMockRepository { }, FakerHelper.loadingTimout()) }) } + getVersionDiff( + _persistentId: string, + _oldVersion: string, + _newVersion: string + ): Promise { + return new Promise((_resolve, reject) => { + setTimeout(() => { + reject('Error thrown from mock') + }, FakerHelper.loadingTimout()) + }) + } publish(_persistentId: string, _versionUpdateType: VersionUpdateType): Promise { return new Promise((_resolve, reject) => { setTimeout(() => { diff --git a/src/stories/dataset/DatasetMockRepository.ts b/src/stories/dataset/DatasetMockRepository.ts index 446216e55..29c6a3a04 100644 --- a/src/stories/dataset/DatasetMockRepository.ts +++ b/src/stories/dataset/DatasetMockRepository.ts @@ -1,8 +1,10 @@ import { Dataset, DatasetLock } from '../../dataset/domain/models/Dataset' +import { DatasetVersionDiff } from '../../dataset/domain/models/DatasetVersionDiff' import { DatasetRepository } from '../../dataset/domain/repositories/DatasetRepository' import { DatasetMother } from '../../../tests/component/dataset/domain/models/DatasetMother' import { DatasetPaginationInfo } from '../../dataset/domain/models/DatasetPaginationInfo' import { DatasetItemTypePreviewMother } from '../../../tests/component/dataset/domain/models/DatasetItemTypePreviewMother' +import { DatasetVersionDiffMother } from '../../../tests/component/dataset/domain/models/DatasetVersionDiffMother' import { DatasetDTO } from '../../dataset/domain/useCases/DTOs/DatasetDTO' import { DatasetsWithCount } from '../../dataset/domain/models/DatasetsWithCount' import { FakerHelper } from '../../../tests/component/shared/FakerHelper' @@ -44,7 +46,17 @@ export class DatasetMockRepository implements DatasetRepository { }, FakerHelper.loadingTimout()) }) } - + getVersionDiff( + _persistentId: string, + _oldVersion: string, + _newVersion: string + ): Promise { + return new Promise((resolve) => { + setTimeout(() => { + resolve(DatasetVersionDiffMother.create()) + }, FakerHelper.loadingTimout()) + }) + } create(_dataset: DatasetDTO): Promise<{ persistentId: string }> { return new Promise((resolve) => { setTimeout(() => { diff --git a/tests/component/dataset/domain/models/DatasetVersionDiffMother.ts b/tests/component/dataset/domain/models/DatasetVersionDiffMother.ts new file mode 100644 index 000000000..e49fbeb47 --- /dev/null +++ b/tests/component/dataset/domain/models/DatasetVersionDiffMother.ts @@ -0,0 +1,32 @@ +import { faker } from '@faker-js/faker' +import { DatasetVersionDiff } from '../../../../../src/dataset/domain/models/DatasetVersionDiff' + +export class DatasetVersionDiffMother { + static create(props?: Partial): DatasetVersionDiff { + const datasetVersionDiff = { + persistentId: faker.datatype.uuid(), + oldVersion: { + versionNumber: '1.0', + lastUpdatedDate: '2023-05-15T08:21:03Z' + }, + newVersion: { + versionNumber: '1.1', + lastUpdatedDate: '2023-05-20T08:21:03Z' + }, + metadataBlockDiffs: [ + { + blockName: 'citation', + changed: [ + { + fieldName: 'title', + oldValue: 'Old Title', + newValue: 'New Title' + } + ] + } + ], + ...props + } + return datasetVersionDiff + } +} diff --git a/tests/component/dataset/infrastructure/mappers/JSDatasetMapper.spec.ts b/tests/component/dataset/infrastructure/mappers/JSDatasetMapper.spec.ts index e8098a3bf..28ff1f0ff 100644 --- a/tests/component/dataset/infrastructure/mappers/JSDatasetMapper.spec.ts +++ b/tests/component/dataset/infrastructure/mappers/JSDatasetMapper.spec.ts @@ -750,11 +750,9 @@ describe('JS Dataset Mapper', () => { latestPublishedVersionMajorNumber, latestPublishedVersionMinorNumber ) - console.log('actual', actual) - console.log('expectedDataset', expectedDatasetWithNextVersionNumbers) expect(expectedDatasetWithNextVersionNumbers).to.deep.equal(actual) }) - it.only('maps jsDataset model to the domain Dataset model when datasetVersionDiff is provided', () => { + it('maps jsDataset model to the domain Dataset model when datasetVersionDiff is provided', () => { const latestPublishedVersionMajorNumber = 1 const latestPublishedVersionMinorNumber = 2 const jsDatasetWithPublicationDate = { @@ -775,15 +773,63 @@ describe('JS Dataset Mapper', () => { latestPublishedVersionMinorNumber, jsDatasetVersionDiff ) - console.log('actual', actual) const expectedDatasetWithRequiredVersionUpdate = { ...expectedDatasetWithNextVersionNumbers, requiresMajorVersionUpdate: true } - console.log( - 'expectedDatasetWithRequiredVersionUpdate', - expectedDatasetWithRequiredVersionUpdate - ) expect(expectedDatasetWithRequiredVersionUpdate).to.deep.equal(actual) }) + it('maps jsDatasetVersionDiff model to the domain DatasetVersionDiff model', () => { + const expectedDatasetVersionDiff = { + oldVersion: { + versionNumber: '1.0', + lastUpdatedDate: '2023-05-15T08:21:03Z' + }, + newVersion: { + versionNumber: '2.0', + lastUpdatedDate: '2023-06-15T08:21:03Z' + }, + metadataChanges: [ + { + blockName: 'citation', + changed: [ + { + fieldName: 'title', + oldValue: 'Old Title', + newValue: 'New Title' + } + ] + } + ], + filesAdded: [ + { + fileName: 'file2.txt', + MD5: 'd41d8cd98f00b204e9800998ecf8427e', + type: 'text/plain', + fileId: 2, + filePath: '/path/to/file2.txt', + description: 'New file', + isRestricted: false, + tags: ['tag2'], + categories: ['category2'] + } + ], + filesRemoved: [ + { + fileName: 'file1.txt', + MD5: 'd41d8cd98f00b204e9800998ecf8427e', + type: 'text/plain', + fileId: 1, + filePath: '/path/to/file1.txt', + description: 'Test file', + isRestricted: false, + tags: ['tag1'], + categories: ['category1'] + } + ] + } + + const actual = JSDatasetMapper.toDatasetVersionDiff(jsDatasetVersionDiff) + expect(expectedDatasetVersionDiff).to.deep.equal(actual) + }) })