diff --git a/src/collections/infra/repositories/CollectionsRepository.ts b/src/collections/infra/repositories/CollectionsRepository.ts index 263b9399..84623409 100644 --- a/src/collections/infra/repositories/CollectionsRepository.ts +++ b/src/collections/infra/repositories/CollectionsRepository.ts @@ -67,13 +67,13 @@ export class CollectionsRepository extends ApiRepository implements ICollections collectionDTO: CollectionDTO, parentCollectionId: number | string = ROOT_COLLECTION_ALIAS ): Promise { - const requestBody = this.createCreateOrUpdateRequestBody(collectionDTO); + const requestBody = this.createCreateOrUpdateRequestBody(collectionDTO) return this.doPost(`/${this.collectionsResourceName}/${parentCollectionId}`, requestBody) .then((response) => response.data.data.id) .catch((error) => { - throw error; - }); + throw error + }) } public async getCollectionFacets( @@ -160,26 +160,30 @@ export class CollectionsRepository extends ApiRepository implements ICollections collectionIdOrAlias: string | number, updatedCollection: CollectionDTO ): Promise { - const requestBody = this.createCreateOrUpdateRequestBody(updatedCollection); + const requestBody = this.createCreateOrUpdateRequestBody(updatedCollection) return this.doPut(`/${this.collectionsResourceName}/${collectionIdOrAlias}`, requestBody) .then(() => undefined) .catch((error) => { - throw error; - }); + throw error + }) } - private createCreateOrUpdateRequestBody(collectionDTO: CollectionDTO): NewCollectionRequestPayload { - const dataverseContacts: NewCollectionContactRequestPayload[] = collectionDTO.contacts.map((contact) => ({ - contactEmail: contact - })); + private createCreateOrUpdateRequestBody( + collectionDTO: CollectionDTO + ): NewCollectionRequestPayload { + const dataverseContacts: NewCollectionContactRequestPayload[] = collectionDTO.contacts.map( + (contact) => ({ + contactEmail: contact + }) + ) const inputLevelsRequestBody: NewCollectionInputLevelRequestPayload[] = collectionDTO.inputLevels?.map((inputLevel) => ({ datasetFieldTypeName: inputLevel.datasetFieldName, include: inputLevel.include, required: inputLevel.required - })); + })) return { alias: collectionDTO.alias, @@ -193,7 +197,7 @@ export class CollectionsRepository extends ApiRepository implements ICollections facetIds: collectionDTO.facetIds, inputLevels: inputLevelsRequestBody } - }; + } } private applyCollectionSearchCriteriaToQueryParams( diff --git a/test/functional/collections/UpdateCollection.test.ts b/test/functional/collections/UpdateCollection.test.ts index cffec841..4fbde611 100644 --- a/test/functional/collections/UpdateCollection.test.ts +++ b/test/functional/collections/UpdateCollection.test.ts @@ -1,48 +1,54 @@ -import { ApiConfig, WriteError, createCollection, getCollection, updateCollection } from '../../../src' +import { + ApiConfig, + WriteError, + createCollection, + getCollection, + updateCollection +} from '../../../src' import { TestConstants } from '../../testHelpers/TestConstants' import { DataverseApiAuthMechanism } from '../../../src/core/infra/repositories/ApiConfig' import { createCollectionDTO } from '../../testHelpers/collections/collectionHelper' describe('execute', () => { - beforeEach(async () => { - ApiConfig.init( - TestConstants.TEST_API_URL, - DataverseApiAuthMechanism.API_KEY, - process.env.TEST_API_KEY - ) - }) + beforeEach(async () => { + ApiConfig.init( + TestConstants.TEST_API_URL, + DataverseApiAuthMechanism.API_KEY, + process.env.TEST_API_KEY + ) + }) - test('should successfully update a new collection', async () => { - const testNewCollectionAlias = 'updateCollection-functional-test' - const testNewCollection = createCollectionDTO(testNewCollectionAlias) - await createCollection.execute(testNewCollection) - const testNewName = 'Updated Name' - testNewCollection.name = testNewName - expect.assertions(1) - try { - await updateCollection.execute(testNewCollectionAlias, testNewCollection) - } catch (error) { - throw new Error('Collection should be updated') - } finally { - const updatedCollection = await getCollection.execute(testNewCollectionAlias) - expect(updatedCollection.name).toBe(testNewName) - } - }) + test('should successfully update a new collection', async () => { + const testNewCollectionAlias = 'updateCollection-functional-test' + const testNewCollection = createCollectionDTO(testNewCollectionAlias) + await createCollection.execute(testNewCollection) + const testNewName = 'Updated Name' + testNewCollection.name = testNewName + expect.assertions(1) + try { + await updateCollection.execute(testNewCollectionAlias, testNewCollection) + } catch (error) { + throw new Error('Collection should be updated') + } finally { + const updatedCollection = await getCollection.execute(testNewCollectionAlias) + expect(updatedCollection.name).toBe(testNewName) + } + }) - test('should throw an error when the parent collection does not exist', async () => { - const testNewCollection = createCollectionDTO() - expect.assertions(2) - let writeError: WriteError - try { - await updateCollection.execute(TestConstants.TEST_DUMMY_COLLECTION_ID, testNewCollection) - throw new Error('Use case should throw an error') - } catch (error) { - writeError = error - } finally { - expect(writeError).toBeInstanceOf(WriteError) - expect(writeError.message).toEqual( - `There was an error when writing the resource. Reason was: [404] Can't find dataverse with identifier='${TestConstants.TEST_DUMMY_COLLECTION_ID}'` - ) - } - }) + test('should throw an error when the parent collection does not exist', async () => { + const testNewCollection = createCollectionDTO() + expect.assertions(2) + let writeError: WriteError + try { + await updateCollection.execute(TestConstants.TEST_DUMMY_COLLECTION_ID, testNewCollection) + throw new Error('Use case should throw an error') + } catch (error) { + writeError = error + } finally { + expect(writeError).toBeInstanceOf(WriteError) + expect(writeError.message).toEqual( + `There was an error when writing the resource. Reason was: [404] Can't find dataverse with identifier='${TestConstants.TEST_DUMMY_COLLECTION_ID}'` + ) + } + }) })