Skip to content

Commit e30ad18

Browse files
committed
feat: add alreadyLinked parameter to getCollectionsForLinking to get candidates for unlinking
1 parent e4c0880 commit e30ad18

File tree

5 files changed

+72
-8
lines changed

5 files changed

+72
-8
lines changed

docs/useCases.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,19 @@ getCollectionsForLinking
367367
.catch((error: Error) => {
368368
/* ... */
369369
})
370+
371+
// Case 3: [alreadyLinked] Optional flag. When true, returns collections currently linked (candidates to unlink). Defaults to false.
372+
const alreadyLinked = true
373+
374+
getCollectionsForLinking
375+
.execute('dataset', persistentId, searchTerm, alreadyLinked)
376+
.then((collections) => {
377+
// collections: CollectionSummary[]
378+
/* ... */
379+
})
380+
.catch((error: Error) => {
381+
/* ... */
382+
})
370383
```
371384

372385
_See [use case](../src/collections/domain/useCases/GetCollectionsForLinking.ts) implementation_.

src/collections/domain/repositories/ICollectionsRepository.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ export interface ICollectionsRepository {
6565
getCollectionsForLinking(
6666
objectType: LinkingObjectType,
6767
id: number | string,
68-
searchTerm: string
68+
searchTerm: string,
69+
alreadyLinked: boolean
6970
): Promise<CollectionSummary[]>
7071
}

src/collections/domain/useCases/GetCollectionsForLinking.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import { CollectionSummary } from '../models/CollectionSummary'
44

55
export type LinkingObjectType = 'collection' | 'dataset'
66

7+
// TODO:ME - Add to the interface and here the alreadyLinking param to get collections for unlinking
8+
// @param alreadyLinking - Optional flag. When true, returns collections currently linked (candidates to unlink). Defaults to false.
9+
710
export class GetCollectionsForLinking implements UseCase<CollectionSummary[]> {
811
private collectionsRepository: ICollectionsRepository
912

@@ -16,12 +19,19 @@ export class GetCollectionsForLinking implements UseCase<CollectionSummary[]> {
1619
* @param objectType - 'dataverse' when providing a collection identifier/alias; 'dataset' when providing a dataset persistentId.
1720
* @param id - For objectType 'dataverse', a numeric id or alias string. For 'dataset', the persistentId string (e.g., doi:...)
1821
* @param searchTerm - Optional search term to filter by collection name. Defaults to empty string (no filtering).
22+
* @param alreadyLinked - Optional flag. When true, returns collections currently linked (candidates to unlink). Defaults to false.
1923
*/
2024
async execute(
2125
objectType: LinkingObjectType,
2226
id: number | string,
23-
searchTerm = ''
27+
searchTerm = '',
28+
alreadyLinked = false
2429
): Promise<CollectionSummary[]> {
25-
return await this.collectionsRepository.getCollectionsForLinking(objectType, id, searchTerm)
30+
return await this.collectionsRepository.getCollectionsForLinking(
31+
objectType,
32+
id,
33+
searchTerm,
34+
alreadyLinked
35+
)
2636
}
2737
}

src/collections/infra/repositories/CollectionsRepository.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,8 @@ export class CollectionsRepository extends ApiRepository implements ICollections
490490
public async getCollectionsForLinking(
491491
objectType: LinkingObjectType,
492492
id: number | string,
493-
searchTerm: string
493+
searchTerm: string,
494+
alreadyLinked: boolean
494495
): Promise<CollectionSummary[]> {
495496
let path: string
496497
const queryParams = new URLSearchParams()
@@ -505,6 +506,10 @@ export class CollectionsRepository extends ApiRepository implements ICollections
505506
queryParams.set('searchTerm', searchTerm)
506507
}
507508

509+
if (alreadyLinked) {
510+
queryParams.set('alreadyLinking', 'true')
511+
}
512+
508513
return this.doGet(path, true, queryParams)
509514
.then((response) => {
510515
const payload = response.data.data as {

test/integration/collections/CollectionsRepository.test.ts

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,8 @@ describe('CollectionsRepository', () => {
819819
const results = await sut.getCollectionsForLinking(
820820
'collection',
821821
testCollectionAlias,
822-
'Scientific'
822+
'Scientific',
823+
false
823824
)
824825

825826
expect(Array.isArray(results)).toBe(true)
@@ -837,7 +838,12 @@ describe('CollectionsRepository', () => {
837838
testCollectionAlias
838839
)
839840

840-
const results = await sut.getCollectionsForLinking('dataset', persistentId, 'Scientific')
841+
const results = await sut.getCollectionsForLinking(
842+
'dataset',
843+
persistentId,
844+
'Scientific',
845+
false
846+
)
841847

842848
// Cleanup dataset (unpublished)
843849
await deleteUnpublishedDatasetViaApi(numericId)
@@ -848,15 +854,44 @@ describe('CollectionsRepository', () => {
848854
expect(found?.displayName).toBe('Scientific Research')
849855
})
850856

857+
test('should return collections for unlking when sending alreadyLinked param to true', async () => {
858+
// Link the test collection with the linking target collection
859+
await sut.linkCollection(testCollectionAlias, linkingTargetAlias)
860+
861+
const collectionsForLinking = await sut.getCollectionsForLinking(
862+
'collection',
863+
testCollectionAlias,
864+
'',
865+
false
866+
)
867+
868+
const collectionsForUnlinking = await sut.getCollectionsForLinking(
869+
'collection',
870+
testCollectionAlias,
871+
'',
872+
true
873+
)
874+
875+
expect(collectionsForLinking.length).toBe(0)
876+
expect(collectionsForUnlinking.length).toBeGreaterThan(0)
877+
expect(collectionsForUnlinking[0].alias).toBe(linkingTargetAlias)
878+
expect(collectionsForUnlinking[0].displayName).toBe('Scientific Research')
879+
})
880+
851881
it('should return error when collection does not exist', async () => {
852882
await expect(
853-
sut.getCollectionsForLinking('collection', TestConstants.TEST_DUMMY_COLLECTION_ALIAS, '')
883+
sut.getCollectionsForLinking(
884+
'collection',
885+
TestConstants.TEST_DUMMY_COLLECTION_ALIAS,
886+
'',
887+
false
888+
)
854889
).rejects.toThrow(ReadError)
855890
})
856891

857892
it('should return error when dataset does not exist', async () => {
858893
await expect(
859-
sut.getCollectionsForLinking('dataset', TestConstants.TEST_DUMMY_PERSISTENT_ID, '')
894+
sut.getCollectionsForLinking('dataset', TestConstants.TEST_DUMMY_PERSISTENT_ID, '', false)
860895
).rejects.toThrow(ReadError)
861896
})
862897
})

0 commit comments

Comments
 (0)