Skip to content

Commit 1a5db45

Browse files
committed
Add Publish Collection use case and functional test
1 parent 75c2f56 commit 1a5db45

File tree

5 files changed

+79
-1
lines changed

5 files changed

+79
-1
lines changed

src/collections/domain/repositories/ICollectionsRepository.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export interface ICollectionsRepository {
99
parentCollectionId: number | string
1010
): Promise<number>
1111
getCollectionFacets(collectionIdOrAlias: number | string): Promise<string[]>
12+
publishCollection(collectionId: number | string): Promise<void>
1213
getCollectionUserPermissions(
1314
collectionIdOrAlias: number | string
1415
): Promise<CollectionUserPermissions>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { UseCase } from '../../../core/domain/useCases/UseCase'
2+
import { ICollectionsRepository } from '../repositories/ICollectionsRepository' // Assuming Axios for HTTP requests
3+
4+
export class PublishCollection implements UseCase<void> {
5+
private collectionsRepository: ICollectionsRepository
6+
7+
constructor(collectionsRepository: ICollectionsRepository) {
8+
this.collectionsRepository = collectionsRepository
9+
}
10+
11+
/**
12+
* Publishes a collection, given its identifier and the type of version update type.
13+
*
14+
* @param {number | string} [collectionId] - The collection identifier, which can be a string (for collection alias), or a number (for numeric identifiers).
15+
* @returns {Promise<void>} - This method does not return anything upon successful completion.
16+
*/
17+
async execute(collectionId: number | string): Promise<void> {
18+
return await this.collectionsRepository.publishCollection(collectionId)
19+
}
20+
}

src/collections/index.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,23 @@ import { GetCollectionFacets } from './domain/useCases/GetCollectionFacets'
44
import { GetCollectionUserPermissions } from './domain/useCases/GetCollectionUserPermissions'
55

66
import { CollectionsRepository } from './infra/repositories/CollectionsRepository'
7+
import { PublishCollection } from './domain/useCases/PublishCollection'
78

89
const collectionsRepository = new CollectionsRepository()
910

1011
const getCollection = new GetCollection(collectionsRepository)
1112
const createCollection = new CreateCollection(collectionsRepository)
1213
const getCollectionFacets = new GetCollectionFacets(collectionsRepository)
1314
const getCollectionUserPermissions = new GetCollectionUserPermissions(collectionsRepository)
15+
const publishCollection = new PublishCollection(collectionsRepository)
1416

15-
export { getCollection, createCollection, getCollectionFacets, getCollectionUserPermissions }
17+
export {
18+
getCollection,
19+
createCollection,
20+
getCollectionFacets,
21+
getCollectionUserPermissions,
22+
publishCollection
23+
}
1624
export { Collection, CollectionInputLevel } from './domain/models/Collection'
1725
export { CollectionUserPermissions } from './domain/models/CollectionUserPermissions'
1826
export { CollectionDTO, CollectionInputLevelDTO } from './domain/dtos/CollectionDTO'

src/collections/infra/repositories/CollectionsRepository.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,16 @@ export class CollectionsRepository extends ApiRepository implements ICollections
8888
throw error
8989
})
9090
}
91+
public async publishCollection(collectionId: string | number): Promise<void> {
92+
return this.doPost(
93+
this.buildApiEndpoint(this.collectionsResourceName, `actions/:publish`, collectionId),
94+
{}
95+
)
96+
.then(() => undefined)
97+
.catch((error) => {
98+
throw error
99+
})
100+
}
91101

92102
public async getCollectionUserPermissions(
93103
collectionIdOrAlias: number | string
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { ApiConfig, createCollection, publishCollection, WriteError } from '../../../src'
2+
import { TestConstants } from '../../testHelpers/TestConstants'
3+
import { DataverseApiAuthMechanism } from '../../../src/core/infra/repositories/ApiConfig'
4+
import {
5+
createCollectionDTO,
6+
deleteCollectionViaApi
7+
} from '../../testHelpers/collections/collectionHelper'
8+
9+
const testNewCollection = createCollectionDTO('test-publish-collection')
10+
11+
describe('execute', () => {
12+
beforeEach(async () => {
13+
ApiConfig.init(
14+
TestConstants.TEST_API_URL,
15+
DataverseApiAuthMechanism.API_KEY,
16+
process.env.TEST_API_KEY
17+
)
18+
})
19+
20+
test('should successfully publish a collection', async () => {
21+
const createdCollectiontIdentifier = await createCollection.execute(testNewCollection)
22+
23+
const response = await publishCollection.execute(createdCollectiontIdentifier)
24+
25+
expect(response).toBeUndefined()
26+
await deleteCollectionViaApi(testNewCollection.alias)
27+
})
28+
29+
test('should throw an error when trying to publish a collection that does not exist', async () => {
30+
const nonExistentTestCollectionId = 4567
31+
const expectedError = new WriteError(
32+
`[404] Can't find dataverse with identifier='${nonExistentTestCollectionId}'`
33+
)
34+
35+
await expect(publishCollection.execute(nonExistentTestCollectionId)).rejects.toThrow(
36+
expectedError
37+
)
38+
})
39+
})

0 commit comments

Comments
 (0)