Skip to content

Commit

Permalink
Added: formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
GPortas committed Oct 23, 2024
1 parent 513b312 commit 09f1158
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 52 deletions.
28 changes: 16 additions & 12 deletions src/collections/infra/repositories/CollectionsRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ export class CollectionsRepository extends ApiRepository implements ICollections
collectionDTO: CollectionDTO,
parentCollectionId: number | string = ROOT_COLLECTION_ALIAS
): Promise<number> {
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(
Expand Down Expand Up @@ -160,26 +160,30 @@ export class CollectionsRepository extends ApiRepository implements ICollections
collectionIdOrAlias: string | number,
updatedCollection: CollectionDTO
): Promise<void> {
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,
Expand All @@ -193,7 +197,7 @@ export class CollectionsRepository extends ApiRepository implements ICollections
facetIds: collectionDTO.facetIds,
inputLevels: inputLevelsRequestBody
}
};
}
}

private applyCollectionSearchCriteriaToQueryParams(
Expand Down
86 changes: 46 additions & 40 deletions test/functional/collections/UpdateCollection.test.ts
Original file line number Diff line number Diff line change
@@ -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}'`
)
}
})
})

0 comments on commit 09f1158

Please sign in to comment.