Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/collections/domain/dtos/CollectionDTO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export interface CollectionDTO {
metadataBlockNames?: string[]
facetIds?: string[]
inputLevels?: CollectionInputLevelDTO[]
inheritMetadataBlocksFromParent: boolean
inheritFacetsFromParent: boolean
}

export interface CollectionInputLevelDTO {
Expand Down
20 changes: 15 additions & 5 deletions src/collections/infra/repositories/CollectionsRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ export interface NewCollectionMetadataBlocksRequestPayload {
metadataBlockNames?: string[]
facetIds?: string[]
inputLevels?: NewCollectionInputLevelRequestPayload[]
inheritMetadataBlocksFromParent?: boolean
inheritFacetsFromParent?: boolean
}

export interface NewCollectionInputLevelRequestPayload {
Expand Down Expand Up @@ -190,18 +192,26 @@ export class CollectionsRepository extends ApiRepository implements ICollections
required: inputLevel.required
}))

const metadataBlocksRequestBody: NewCollectionMetadataBlocksRequestPayload = {
...(!collectionDTO.inheritMetadataBlocksFromParent && {
metadataBlockNames: collectionDTO.metadataBlockNames,
inputLevels: inputLevelsRequestBody
}),
...(!collectionDTO.inheritFacetsFromParent && {
facetIds: collectionDTO.facetIds
}),
inheritMetadataBlocksFromParent: collectionDTO.inheritMetadataBlocksFromParent,
inheritFacetsFromParent: collectionDTO.inheritFacetsFromParent
}

return {
alias: collectionDTO.alias,
name: collectionDTO.name,
dataverseContacts: dataverseContacts,
dataverseType: collectionDTO.type,
...(collectionDTO.description && { description: collectionDTO.description }),
...(collectionDTO.affiliation && { affiliation: collectionDTO.affiliation }),
metadataBlocks: {
metadataBlockNames: collectionDTO.metadataBlockNames,
facetIds: collectionDTO.facetIds,
inputLevels: inputLevelsRequestBody
}
metadataBlocks: metadataBlocksRequestBody
}
}

Expand Down
170 changes: 165 additions & 5 deletions test/integration/collections/CollectionsRepository.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { CollectionsRepository } from '../../../src/collections/infra/repositories/CollectionsRepository'
import { TestConstants } from '../../testHelpers/TestConstants'
import {
CollectionDTO,
CollectionFeaturedItemsDTO,
CollectionItemType,
CollectionPreview,
Expand Down Expand Up @@ -149,11 +150,15 @@ describe('CollectionsRepository', () => {
const testCreateCollectionAlias1 = 'createCollection-test-1'
const testCreateCollectionAlias2 = 'createCollection-test-2'
const testCreateCollectionAlias3 = 'createCollection-test-3'
const testCreateCollectionAlias4 = 'createCollection-test-4'
const testCreateCollectionAlias5 = 'createCollection-test-5'

afterAll(async () => {
await deleteCollectionViaApi(testCreateCollectionAlias1)
await deleteCollectionViaApi(testCreateCollectionAlias2)
await deleteCollectionViaApi(testCreateCollectionAlias3)
await deleteCollectionViaApi(testCreateCollectionAlias4)
await deleteCollectionViaApi(testCreateCollectionAlias5)
})

test('should create collection in root when no parent collection is set', async () => {
Expand Down Expand Up @@ -184,13 +189,48 @@ describe('CollectionsRepository', () => {
testCollectionId
)
expect(typeof actualId).toBe('number')

const collectionCreated = await sut.getCollection(actualId)

expect(collectionCreated.isMetadataBlockRoot).toBe(true)
expect(collectionCreated.isFacetRoot).toBe(true)
})

test('should create collection without input levels', async () => {
const newCollectionDTO = createCollectionDTO(testCreateCollectionAlias3)
newCollectionDTO.inputLevels = undefined
const actualId = await sut.createCollection(newCollectionDTO, testCollectionId)
expect(typeof actualId).toBe('number')
test('should create a collection to inherit metadata blocks from parent collection', async () => {
const childCollectionDTO = createCollectionDTO(testCreateCollectionAlias3)
childCollectionDTO.inheritMetadataBlocksFromParent = true

const childCollectionId = await sut.createCollection(childCollectionDTO)

const childCollection = await sut.getCollection(childCollectionId)

expect(childCollection.isMetadataBlockRoot).toBe(false)
expect(childCollection.isFacetRoot).toBe(true)
})

test('should create a collection to inherit facets from parent collection', async () => {
const childCollectionDTO = createCollectionDTO(testCreateCollectionAlias4)
childCollectionDTO.inheritFacetsFromParent = true

const childCollectionId = await sut.createCollection(childCollectionDTO)

const childCollection = await sut.getCollection(childCollectionId)

expect(childCollection.isMetadataBlockRoot).toBe(true)
expect(childCollection.isFacetRoot).toBe(false)
})

test('should create a collection to inherit metadata blocks and facets from parent collection', async () => {
const childCollectionDTO = createCollectionDTO(testCreateCollectionAlias5)
childCollectionDTO.inheritMetadataBlocksFromParent = true
childCollectionDTO.inheritFacetsFromParent = true

const childCollectionId = await sut.createCollection(childCollectionDTO)

const childCollection = await sut.getCollection(childCollectionId)

expect(childCollection.isMetadataBlockRoot).toBe(false)
expect(childCollection.isFacetRoot).toBe(false)
})

test('should return error when parent collection does not exist', async () => {
Expand Down Expand Up @@ -870,6 +910,126 @@ describe('CollectionsRepository', () => {
expect(updatedInputLevel?.required).toBe(false)
})

test('should update the collection to inherit metadata blocks from parent collection', async () => {
const parentCollectionAlias = 'inherit-metablocks-parent-update'
const parentCollectionDTO = createCollectionDTO(parentCollectionAlias)
const parentCollectionId = await sut.createCollection(parentCollectionDTO)

const childCollectionAlias = 'inherit-metablocks-child-update'
const childCollectionDTO = createCollectionDTO(childCollectionAlias)

const childCollectionId = await sut.createCollection(childCollectionDTO, parentCollectionId)

const childCollection = await sut.getCollection(childCollectionId)

expect(childCollection.isMetadataBlockRoot).toBe(true)
expect(childCollection.isFacetRoot).toBe(true)

const updatedChildCollectionDTO = createCollectionDTO(childCollectionAlias)
updatedChildCollectionDTO.inheritMetadataBlocksFromParent = true

await sut.updateCollection(childCollectionId, updatedChildCollectionDTO)

const childCollectionAfterUpdate = await sut.getCollection(childCollectionId)

expect(childCollectionAfterUpdate.isMetadataBlockRoot).toBe(false)
expect(childCollectionAfterUpdate.isFacetRoot).toBe(true)

await deleteCollectionViaApi(childCollectionAlias)
await deleteCollectionViaApi(parentCollectionAlias)
})

test('should update the collection to inherit facets from parent collection', async () => {
const parentCollectionAlias = 'inherit-facets-parent-update'
const parentCollectionDTO = createCollectionDTO(parentCollectionAlias)
const parentCollectionId = await sut.createCollection(parentCollectionDTO)

const childCollectionAlias = 'inherit-facets-child-update'
const childCollectionDTO = createCollectionDTO(childCollectionAlias)

const childCollectionId = await sut.createCollection(childCollectionDTO, parentCollectionId)

const childCollection = await sut.getCollection(childCollectionId)

expect(childCollection.isMetadataBlockRoot).toBe(true)
expect(childCollection.isFacetRoot).toBe(true)

const updatedChildCollectionDTO = createCollectionDTO(childCollectionAlias)
updatedChildCollectionDTO.inheritFacetsFromParent = true

await sut.updateCollection(childCollectionId, updatedChildCollectionDTO)

const childCollectionAfterUpdate = await sut.getCollection(childCollectionId)

expect(childCollectionAfterUpdate.isMetadataBlockRoot).toBe(true)
expect(childCollectionAfterUpdate.isFacetRoot).toBe(false)

await deleteCollectionViaApi(childCollectionAlias)
await deleteCollectionViaApi(parentCollectionAlias)
})

test('should update the collection to inherit metadata blocks and facets from parent collection', async () => {
const parentCollectionAlias = 'inherit-metablocks-facets-parent-update'
const parentCollectionDTO = createCollectionDTO(parentCollectionAlias)
const parentCollectionId = await sut.createCollection(parentCollectionDTO)

const childCollectionAlias = 'inherit-metablocks-facets-child-update'
const childCollectionDTO = createCollectionDTO(childCollectionAlias)

const childCollectionId = await sut.createCollection(childCollectionDTO, parentCollectionId)

const childCollection = await sut.getCollection(childCollectionId)

expect(childCollection.isMetadataBlockRoot).toBe(true)
expect(childCollection.isFacetRoot).toBe(true)

const updatedChildCollectionDTO = createCollectionDTO(childCollectionAlias)
updatedChildCollectionDTO.inheritFacetsFromParent = true
updatedChildCollectionDTO.inheritMetadataBlocksFromParent = true

await sut.updateCollection(childCollectionId, updatedChildCollectionDTO)

const childCollectionAfterUpdate = await sut.getCollection(childCollectionId)

expect(childCollectionAfterUpdate.isMetadataBlockRoot).toBe(false)
expect(childCollectionAfterUpdate.isFacetRoot).toBe(false)

await deleteCollectionViaApi(childCollectionAlias)
await deleteCollectionViaApi(parentCollectionAlias)
})

test('should not update root collection facets and keep isMetadataBlockRoot and isFacetRoot in true if facet ids are sent as undefined', async () => {
const rootCollection = await sut.getCollection()

const rootCollectionFacets = await sut.getCollectionFacets(rootCollection.alias)

const updatedRootCollectionDTO: CollectionDTO = {
alias: rootCollection.alias,
name: rootCollection.name,
contacts: [rootCollection.contacts?.[0].email as string],
type: rootCollection.type,
description: rootCollection.description,
affiliation: rootCollection.affiliation,
metadataBlockNames: undefined,
facetIds: undefined,
inputLevels: undefined,
inheritFacetsFromParent: false,
inheritMetadataBlocksFromParent: false
}

await sut.updateCollection(rootCollection.id, updatedRootCollectionDTO)

const rootCollectionAfterUpdate = await sut.getCollection()

const rootCollectionFacetsAfterUpdate = await sut.getCollectionFacets(rootCollection.alias)

expect(rootCollectionFacets).toStrictEqual(rootCollectionFacetsAfterUpdate)
expect(rootCollection.isMetadataBlockRoot).toBe(true)
expect(rootCollection.isFacetRoot).toBe(true)
expect(rootCollectionAfterUpdate.isMetadataBlockRoot).toBe(true)
expect(rootCollectionAfterUpdate.isFacetRoot).toBe(true)
})

test('should return error when collection does not exist', async () => {
const expectedError = new WriteError(
`[404] Can't find dataverse with identifier='${TestConstants.TEST_DUMMY_COLLECTION_ID}'`
Expand Down
10 changes: 7 additions & 3 deletions test/testHelpers/collections/collectionHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,9 @@ export const createCollectionDTO = (alias = 'test-collection'): CollectionDTO =>
required: true,
include: true
}
]
],
inheritFacetsFromParent: false,
inheritMetadataBlocksFromParent: false
}
}

Expand All @@ -180,14 +182,16 @@ export const createNewCollectionRequestPayload = (): NewCollectionRequestPayload
affiliation: 'test affiliation',
metadataBlocks: {
metadataBlockNames: ['citation', 'geospatial'],
facetIds: ['authorName', 'authorAffiliation'],
inputLevels: [
{
datasetFieldTypeName: 'geographicCoverage',
include: true,
required: true
}
]
],
facetIds: ['authorName', 'authorAffiliation'],
inheritMetadataBlocksFromParent: false,
inheritFacetsFromParent: false
}
}
}
Expand Down