Skip to content
Open
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
22 changes: 22 additions & 0 deletions docs/useCases.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ The different use cases currently available in the package are classified below,
- [Update Collection Featured Items](#update-collection-featured-items)
- [Delete Collection Featured Items](#delete-collection-featured-items)
- [Delete a Collection Featured Item](#delete-a-collection-featured-item)
- [Set Default Contributor Role](#set-default-contributor-role)
- [Templates](#Templates)
- [Templates read use cases](#templates-read-use-cases)
- [Get a Template](#get-a-template)
Expand Down Expand Up @@ -576,6 +577,27 @@ deleteCollectionFeaturedItem.execute(featuredItemId)

_See [use case](../src/collections/domain/useCases/DeleteCollectionFeaturedItem.ts)_ definition.

#### Set Default Contributor Role

Sets the default contributor role of a collection, given a collection identifier and a role alias.

##### Example call:

```typescript
import { setDefaultContributorRole } from '@iqss/dataverse-client-javascript'

/* ... */

const collectionIdOrAlias = 12345
const roleAlias = 'curator'

setDefaultContributorRole.execute(collectionIdOrAlias, roleAlias)

Comment on lines +580 to +595
/* ... */
```

_See [use case](../src/collections/domain/useCases/SetDefaultContributorRole.ts)_ definition.

## Templates

### Templates Read Use Cases
Expand Down
4 changes: 4 additions & 0 deletions src/collections/domain/repositories/ICollectionsRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ export interface ICollectionsRepository {
getCollectionUserPermissions(
collectionIdOrAlias: number | string
): Promise<CollectionUserPermissions>
setDefaultContributorRole(
collectionIdOrAlias: number | string,
roleAlias: string
): Promise<void>
getCollectionItems(
collectionId?: string,
limit?: number,
Expand Down
26 changes: 26 additions & 0 deletions src/collections/domain/useCases/SetDefaultContributorRole.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { UseCase } from '../../../core/domain/useCases/UseCase'
import { ICollectionsRepository } from '../repositories/ICollectionsRepository'
import { ROOT_COLLECTION_ID } from '../models/Collection'

export class SetDefaultContributorRole implements UseCase<void> {
private collectionsRepository: ICollectionsRepository

constructor(collectionsRepository: ICollectionsRepository) {
this.collectionsRepository = collectionsRepository
}

/**
* Sets the default Role that is assigned to contributors in the given collection.
*
* @param {number | string} [collectionIdOrAlias = ':root'] - A generic collection identifier, which can be either a string (for queries by CollectionAlias), or a number (for queries by CollectionId)
* If this parameter is not set, the default value is: ':root'
* @param {string} [roleAlias] - The alias of the role to be assigned
* @returns {Promise<void>}
Comment thread
ChengShi-1 marked this conversation as resolved.
*/
async execute(
collectionIdOrAlias: number | string = ROOT_COLLECTION_ID,
roleAlias: string
): Promise<void> {
return await this.collectionsRepository.setDefaultContributorRole(collectionIdOrAlias, roleAlias)
}
}
5 changes: 4 additions & 1 deletion src/collections/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { LinkCollection } from './domain/useCases/LinkCollection'
import { UnlinkCollection } from './domain/useCases/UnlinkCollection'
import { GetCollectionLinks } from './domain/useCases/GetCollectionLinks'
import { GetCollectionsForLinking } from './domain/useCases/GetCollectionsForLinking'
import { SetDefaultContributorRole } from './domain/useCases/SetDefaultContributorRole'

const collectionsRepository = new CollectionsRepository()

Expand All @@ -36,6 +37,7 @@ const linkCollection = new LinkCollection(collectionsRepository)
const unlinkCollection = new UnlinkCollection(collectionsRepository)
const getCollectionLinks = new GetCollectionLinks(collectionsRepository)
const getCollectionsForLinking = new GetCollectionsForLinking(collectionsRepository)
const setDefaultContributorRole = new SetDefaultContributorRole(collectionsRepository)

export {
getCollection,
Expand All @@ -54,7 +56,8 @@ export {
linkCollection,
unlinkCollection,
getCollectionLinks,
getCollectionsForLinking
getCollectionsForLinking,
setDefaultContributorRole
}
export { Collection, CollectionInputLevel } from './domain/models/Collection'
export { CollectionFacet } from './domain/models/CollectionFacet'
Expand Down
14 changes: 14 additions & 0 deletions src/collections/infra/repositories/CollectionsRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,20 @@ export class CollectionsRepository extends ApiRepository implements ICollections
})
}

public async setDefaultContributorRole(
collectionIdOrAlias: number | string,
roleAlias: string
): Promise<void> {
return this.doPut(
`/${this.collectionsResourceName}/${collectionIdOrAlias}/defaultContributorRole/${roleAlias}`,
{}
)
.then(() => undefined)
.catch((error) => {
throw error
})
}
Comment on lines +170 to +182

public async getCollectionItems(
collectionId?: string,
limit?: number,
Expand Down
29 changes: 29 additions & 0 deletions test/unit/collections/SetDefaultContributorRole.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { ICollectionsRepository } from '../../../src/collections/domain/repositories/ICollectionsRepository'
import { WriteError } from '../../../src'
import { SetDefaultContributorRole } from '../../../src/collections/domain/useCases/SetDefaultContributorRole'

describe('execute', () => {
test('should set default contributor role on repository success', async () => {
const collectionRepositoryStub: ICollectionsRepository = {} as ICollectionsRepository
collectionRepositoryStub.setDefaultContributorRole = jest.fn().mockResolvedValue(undefined)
const testSetDefaultContributorRole = new SetDefaultContributorRole(
collectionRepositoryStub
)

await expect(testSetDefaultContributorRole.execute(1, "curator")).resolves.toBeUndefined()
expect(collectionRepositoryStub.setDefaultContributorRole).toHaveBeenCalledWith(1, "curator")
Comment on lines +13 to +14
})

test('should return error result on repository error', async () => {
const collectionRepositoryStub: ICollectionsRepository = {} as ICollectionsRepository
collectionRepositoryStub.setDefaultContributorRole = jest
.fn()
.mockRejectedValue(new WriteError())
const testSetDefaultContributorRole = new SetDefaultContributorRole(
collectionRepositoryStub
)

await expect(testSetDefaultContributorRole.execute(1, "curator")).rejects.toThrow(WriteError)
expect(collectionRepositoryStub.setDefaultContributorRole).toHaveBeenCalledWith(1, "curator")
Comment on lines +26 to +27
})
})
Loading