Skip to content

Commit

Permalink
fix: add getVersionDiff to DatasetRepository interface
Browse files Browse the repository at this point in the history
  • Loading branch information
ekraffmiller committed Nov 22, 2024
1 parent 19fe1c8 commit 91d3013
Show file tree
Hide file tree
Showing 9 changed files with 215 additions and 20 deletions.
48 changes: 48 additions & 0 deletions src/dataset/domain/models/DatasetVersionDiff.ts
Original file line number Diff line number Diff line change
@@ -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
}
7 changes: 7 additions & 0 deletions src/dataset/domain/repositories/DatasetRepository.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -8,6 +9,12 @@ export interface DatasetRepository {
getByPersistentId: (persistentId: string, version?: string) => Promise<Dataset | undefined>
getLocks(persistentId: string): Promise<DatasetLock[]>
getByPrivateUrlToken: (privateUrlToken: string) => Promise<Dataset | undefined>
getVersionDiff: (
persistentId: string,
oldVersion: string,
newVersion: string
) => Promise<DatasetVersionDiff>

create: (dataset: DatasetDTO, collectionId: string) => Promise<{ persistentId: string }>
updateMetadata: (datasetId: string | number, datasetDTO: DatasetDTO) => Promise<void>
getAllWithCount: (
Expand Down
15 changes: 15 additions & 0 deletions src/dataset/domain/useCases/getDatasetVersionDiff.ts
Original file line number Diff line number Diff line change
@@ -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<DatasetVersionDiff | undefined> {
return datasetRepository
.getVersionDiff(persistentId, oldVersion, newVersion)
.catch((error: Error) => {
throw new Error(error.message)
})
}
14 changes: 13 additions & 1 deletion src/dataset/infrastructure/mappers/JSDatasetMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -69,6 +70,18 @@ export class DatasetJSDataverseRepository implements DatasetRepository {
}
})
}
getVersionDiff(
persistentId: string,
oldVersion: string,
newVersion: string
): Promise<DatasetVersionDiff> {
return getDatasetVersionDiff
.execute(persistentId, oldVersion, newVersion)
.then((jsDatasetVersionDiff) => {
return JSDatasetMapper.toDatasetVersionDiff(jsDatasetVersionDiff)
})
}

private async getLatestPublishedVersionNumbers(
datasetDetails: IDatasetDetails
): Promise<IDatasetDetails> {
Expand All @@ -86,16 +99,14 @@ export class DatasetJSDataverseRepository implements DatasetRepository {
}

private async getVersionDiffDetails(datasetDetails: IDatasetDetails): Promise<IDatasetDetails> {
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
}
Expand Down
12 changes: 12 additions & 0 deletions src/stories/dataset/DatasetErrorMockRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: (
Expand Down Expand Up @@ -46,6 +47,17 @@ export class DatasetErrorMockRepository implements DatasetMockRepository {
}, FakerHelper.loadingTimout())
})
}
getVersionDiff(
_persistentId: string,
_oldVersion: string,
_newVersion: string
): Promise<DatasetVersionDiff> {
return new Promise((_resolve, reject) => {
setTimeout(() => {
reject('Error thrown from mock')
}, FakerHelper.loadingTimout())
})
}
publish(_persistentId: string, _versionUpdateType: VersionUpdateType): Promise<void> {
return new Promise((_resolve, reject) => {
setTimeout(() => {
Expand Down
14 changes: 13 additions & 1 deletion src/stories/dataset/DatasetMockRepository.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -44,7 +46,17 @@ export class DatasetMockRepository implements DatasetRepository {
}, FakerHelper.loadingTimout())
})
}

getVersionDiff(
_persistentId: string,
_oldVersion: string,
_newVersion: string
): Promise<DatasetVersionDiff> {
return new Promise((resolve) => {
setTimeout(() => {
resolve(DatasetVersionDiffMother.create())
}, FakerHelper.loadingTimout())
})
}
create(_dataset: DatasetDTO): Promise<{ persistentId: string }> {
return new Promise((resolve) => {
setTimeout(() => {
Expand Down
32 changes: 32 additions & 0 deletions tests/component/dataset/domain/models/DatasetVersionDiffMother.ts
Original file line number Diff line number Diff line change
@@ -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>): 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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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)
})
})

0 comments on commit 91d3013

Please sign in to comment.