Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…vascript into feature/121-modify-getalldatasetpreviews-use-case-to-accept-collection-id
  • Loading branch information
MellyGray committed Feb 26, 2024
2 parents ac6dedd + 7496c69 commit b31ce74
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 49 deletions.
1 change: 1 addition & 0 deletions src/datasets/domain/models/DatasetNotNumberedVersion.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export enum DatasetNotNumberedVersion {
DRAFT = ':draft',
LATEST = ':latest',
LATEST_PUBLISHED = ':latest-published',
}
4 changes: 2 additions & 2 deletions src/datasets/domain/useCases/CreateDataset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ export class CreateDataset implements UseCase<CreatedDatasetIdentifiers> {
* @throws {ReadError} - If there are errors while reading data.
* @throws {WriteError} - If there are errors while writing data.
*/
async execute(newDataset: NewDatasetDTO, collectionId: string = 'root'): Promise<CreatedDatasetIdentifiers> {
async execute(newDataset: NewDatasetDTO, collectionId = 'root'): Promise<CreatedDatasetIdentifiers> {
const metadataBlocks = await this.getNewDatasetMetadataBlocks(newDataset);
this.newDatasetValidator.validate(newDataset, metadataBlocks);
return this.datasetsRepository.createDataset(newDataset, metadataBlocks, collectionId);
}

async getNewDatasetMetadataBlocks(newDataset: NewDatasetDTO): Promise<MetadataBlock[]> {
let metadataBlocks: MetadataBlock[] = [];
const metadataBlocks: MetadataBlock[] = [];
await Promise.all(
newDataset.metadataBlockValues.map(async (metadataBlockValue: NewDatasetMetadataBlockValuesDTO) => {
metadataBlocks.push(await this.metadataBlocksRepository.getMetadataBlockByName(metadataBlockValue.name));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export const transformMetadataBlockModelsToRequestPayload = (
newDatasetMetadataBlocksValues: NewDatasetMetadataBlockValuesDTO[],
metadataBlocks: MetadataBlock[],
): Record<string, MetadataBlockRequestPayload> => {
let metadataBlocksRequestPayload: Record<string, MetadataBlockRequestPayload> = {};
const metadataBlocksRequestPayload: Record<string, MetadataBlockRequestPayload> = {};
newDatasetMetadataBlocksValues.forEach(function (newDatasetMetadataBlockValues: NewDatasetMetadataBlockValuesDTO) {
const metadataBlock: MetadataBlock = metadataBlocks.find(
(metadataBlock) => metadataBlock.name == newDatasetMetadataBlockValues.name,
Expand All @@ -69,7 +69,7 @@ export const transformMetadataFieldModelsToRequestPayload = (
newDatasetMetadataFields: NewDatasetMetadataFieldsDTO,
metadataBlockFields: Record<string, MetadataFieldInfo>,
): MetadataFieldRequestPayload[] => {
let metadataFieldsRequestPayload: MetadataFieldRequestPayload[] = [];
const metadataFieldsRequestPayload: MetadataFieldRequestPayload[] = [];
for (const metadataFieldKey of Object.keys(newDatasetMetadataFields)) {
const newDatasetMetadataChildFieldValue: NewDatasetMetadataFieldValueDTO =
newDatasetMetadataFields[metadataFieldKey];
Expand Down Expand Up @@ -124,7 +124,7 @@ export const transformMetadataChildFieldValueToRequestPayload = (
newDatasetMetadataChildFieldValue: NewDatasetMetadataChildFieldValueDTO,
metadataBlockFieldInfo: MetadataFieldInfo,
): Record<string, MetadataFieldRequestPayload> => {
let metadataChildFieldRequestPayload: Record<string, MetadataFieldRequestPayload> = {};
const metadataChildFieldRequestPayload: Record<string, MetadataFieldRequestPayload> = {};
for (const metadataChildFieldKey of Object.keys(newDatasetMetadataChildFieldValue)) {
const childMetadataFieldInfo: MetadataFieldInfo = metadataBlockFieldInfo.childMetadataFields[metadataChildFieldKey];
const value: string = newDatasetMetadataChildFieldValue[metadataChildFieldKey] as unknown as string;
Expand Down
16 changes: 1 addition & 15 deletions src/files/infra/repositories/FilesRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { FileSearchCriteria, FileOrderCriteria } from '../../domain/models/FileC
import { FileCounts } from '../../domain/models/FileCounts';
import { transformFileCountsResponseToFileCounts } from './transformers/fileCountsTransformers';
import { FileDownloadSizeMode } from '../../domain/models/FileDownloadSizeMode';
import { DatasetNotNumberedVersion } from '../../../datasets';

export interface GetFilesQueryParams {
includeDeaccessioned: boolean;
Expand Down Expand Up @@ -146,7 +145,7 @@ export class FilesRepository extends ApiRepository implements IFilesRepository {
}

public async getFile(fileId: number | string, datasetVersionId: string): Promise<File> {
return this.doGet(this.getFileEndpoint(fileId, datasetVersionId), true)
return this.doGet(this.buildApiEndpoint(this.filesResourceName, `versions/${datasetVersionId}`, fileId), true)
.then((response) => transformFileResponseToFile(response))
.catch((error) => {
throw error;
Expand All @@ -169,19 +168,6 @@ export class FilesRepository extends ApiRepository implements IFilesRepository {
});
}

private getFileEndpoint(fileId: number | string, datasetVersionId: string): string {
if (datasetVersionId === DatasetNotNumberedVersion.DRAFT) {
return this.buildApiEndpoint(this.filesResourceName, 'draft', fileId);
}
if (datasetVersionId === DatasetNotNumberedVersion.LATEST) {
return this.buildApiEndpoint(this.filesResourceName, '', fileId);
}
// TODO: Implement once it is supported by the API https://github.com/IQSS/dataverse/issues/10280
throw new Error(
`Requesting a file by its dataset version is not yet supported. Requested version: ${datasetVersionId}. Please try using the :latest or :draft version instead.`,
);
}

private applyFileSearchCriteriaToQueryParams(
queryParams: GetFilesQueryParams | GetFilesTotalDownloadSizeQueryParams,
fileSearchCriteria: FileSearchCriteria,
Expand Down
29 changes: 3 additions & 26 deletions test/integration/files/FilesRepository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,26 +465,14 @@ describe('FilesRepository', () => {
assert.match(actual.name, testTextFile1Name);
});

test('should return Not Implemented Yet error when when providing a valid id and version is different than latest and draft', async () => {
// This tests can be removed once the API supports getting a file by version
let error: ReadError = undefined;

await sut.getFile(testFileId, '1.0').catch((e) => (error = e));

assert.match(
error.message,
`Requesting a file by its dataset version is not yet supported. Requested version: 1.0. Please try using the :latest or :draft version instead.`,
);
});

test('should return error when file does not exist', async () => {
let error: ReadError = undefined;

await sut.getFile(nonExistentFiledId, DatasetNotNumberedVersion.LATEST).catch((e) => (error = e));

assert.match(
error.message,
`There was an error when reading the resource. Reason was: [404] File with ID 200 not found.`,
`There was an error when reading the resource. Reason was: [404] File with ID ${nonExistentFiledId} not found.`,
);
});
});
Expand All @@ -501,18 +489,6 @@ describe('FilesRepository', () => {
assert.match(actual.name, testTextFile1Name);
});

test('should return Not Implemented Yet error when when providing a valid persistent id and version is different than latest and draft', async () => {
// This tests can be removed once the API supports getting a file by version
let error: ReadError = undefined;

await sut.getFile(testFilePersistentId, '1.0').catch((e) => (error = e));

assert.match(
error.message,
`Requesting a file by its dataset version is not yet supported. Requested version: 1.0. Please try using the :latest or :draft version instead.`,
);
});

test('should return error when file does not exist', async () => {
let error: ReadError = undefined;

Expand All @@ -521,11 +497,12 @@ describe('FilesRepository', () => {

assert.match(
error.message,
`There was an error when reading the resource. Reason was: [404] Datafile with Persistent ID nonExistentFiledPersistentId not found.`,
`There was an error when reading the resource. Reason was: [404] Datafile with Persistent ID ${nonExistentFiledPersistentId} not found.`,
);
});
});
});

describe('getFileCitation', () => {
test('should return citation when file exists', async () => {
const actualFileCitation = await sut.getFileCitation(testFileId, DatasetNotNumberedVersion.LATEST, false);
Expand Down
4 changes: 2 additions & 2 deletions test/unit/files/FilesRepository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,7 @@ describe('FilesRepository', () => {
});
describe('getFile' , () => {
describe('by numeric id', () => {
const expectedApiEndpoint = `${TestConstants.TEST_API_URL}/files/${testFile.id}/`;
const expectedApiEndpoint = `${TestConstants.TEST_API_URL}/files/${testFile.id}/versions/${DatasetNotNumberedVersion.LATEST}`;
const testGetFileResponse = {
data: {
status: 'OK',
Expand Down Expand Up @@ -826,7 +826,7 @@ describe('FilesRepository', () => {
});
});
describe('by persistent id', () => {
const expectedApiEndpoint = `${TestConstants.TEST_API_URL}/files/:persistentId/?persistentId=${TestConstants.TEST_DUMMY_PERSISTENT_ID}`;
const expectedApiEndpoint = `${TestConstants.TEST_API_URL}/files/:persistentId/versions/${DatasetNotNumberedVersion.LATEST}?persistentId=${TestConstants.TEST_DUMMY_PERSISTENT_ID}`;
const testGetFileResponse = {
data: {
status: 'OK',
Expand Down
2 changes: 1 addition & 1 deletion test/unit/files/GetFile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,4 @@ describe('execute', () => {

assert.match(actualError, testReadError);
})
});
});

0 comments on commit b31ce74

Please sign in to comment.