Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add use case to retrieve file counts by type, access and category #88

Merged
merged 7 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from 5 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
12 changes: 12 additions & 0 deletions src/core/infra/repositories/ApiRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ export abstract class ApiRepository {
});
}

protected buildApiEndpoint(resourceName: string, operation: string, resourceId: number | string = undefined) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

love the refactor using this method 👍

let endpoint;
if (typeof resourceId === 'number') {
endpoint = `/${resourceName}/${resourceId}/${operation}`;
} else if (typeof resourceId === 'string') {
endpoint = `/${resourceName}/:persistentId/${operation}?persistentId=${resourceId}`;
} else {
endpoint = `/${resourceName}/${operation}`;
}
return endpoint;
}

private buildRequestConfig(authRequired: boolean, queryParams: object): AxiosRequestConfig {
const requestConfig: AxiosRequestConfig = {
params: queryParams,
Expand Down
21 changes: 10 additions & 11 deletions src/datasets/infra/repositories/DatasetsRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,45 @@ import { Dataset } from '../../domain/models/Dataset';
import { transformVersionResponseToDataset } from './transformers/datasetTransformers';

export class DatasetsRepository extends ApiRepository implements IDatasetsRepository {
private readonly resourceName: string = 'datasets';

public async getDatasetSummaryFieldNames(): Promise<string[]> {
return this.doGet('/datasets/summaryFieldNames')
return this.doGet(this.buildApiEndpoint(this.resourceName, 'summaryFieldNames'))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I like it better in FileRepository where the dataset resource name has this naming this.datasetsResourceName

return this.doGet(this.buildApiEndpoint(this.datasetsResourceName, 'summaryFieldNames'))

I think it helps visually seeing which resource is calling, specially because it seems like the resource called is not always the one in the repository name

.then((response) => response.data.data)
.catch((error) => {
throw error;
});
}

public async getPrivateUrlDataset(token: string): Promise<Dataset> {
return this.doGet(`/datasets/privateUrlDatasetVersion/${token}`)
return this.doGet(this.buildApiEndpoint(this.resourceName, `privateUrlDatasetVersion/${token}`))
.then((response) => transformVersionResponseToDataset(response))
.catch((error) => {
throw error;
});
}

public async getDataset(datasetId: number | string, datasetVersionId: string): Promise<Dataset> {
let endpoint;
if (typeof datasetId === 'number') {
endpoint = `/datasets/${datasetId}/versions/${datasetVersionId}`;
} else {
endpoint = `/datasets/:persistentId/versions/${datasetVersionId}?persistentId=${datasetId}`;
}
return this.doGet(endpoint, true)
return this.doGet(this.buildApiEndpoint(this.resourceName, `versions/${datasetVersionId}`, datasetId), true)
.then((response) => transformVersionResponseToDataset(response))
.catch((error) => {
throw error;
});
}

public async getDatasetCitation(datasetId: number, datasetVersionId: string): Promise<string> {
return this.doGet(`/datasets/${datasetId}/versions/${datasetVersionId}/citation`, true)
return this.doGet(
this.buildApiEndpoint(this.resourceName, `versions/${datasetVersionId}/citation`, datasetId),
true,
)
.then((response) => response.data.data.message)
.catch((error) => {
throw error;
});
}

public async getPrivateUrlDatasetCitation(token: string): Promise<string> {
return this.doGet(`/datasets/privateUrlDatasetVersion/${token}/citation`)
return this.doGet(this.buildApiEndpoint(this.resourceName, `privateUrlDatasetVersion/${token}/citation`))
.then((response) => response.data.data.message)
.catch((error) => {
throw error;
Expand Down
23 changes: 23 additions & 0 deletions src/files/domain/models/FileCounts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { FileAccessStatus } from './FileCriteria';

export interface FileCounts {
total: number;
perContentType: FileContentTypeCount[];
perAccessStatus: FileAccessStatusCount[];
perCategoryName: FileCategoryNameCount[];
}

export interface FileContentTypeCount {
contentType: string;
count: number;
}

export interface FileAccessStatusCount {
accessStatus: FileAccessStatus;
count: number;
}

export interface FileCategoryNameCount {
categoryName: string;
count: number;
}
3 changes: 3 additions & 0 deletions src/files/domain/repositories/IFilesRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { File } from '../models/File';
import { FileDataTable } from '../models/FileDataTable';
import { FileUserPermissions } from '../models/FileUserPermissions';
import { FileCriteria } from '../models/FileCriteria';
import { FileCounts } from '../models/FileCounts';

export interface IFilesRepository {
getDatasetFiles(
Expand All @@ -12,6 +13,8 @@ export interface IFilesRepository {
fileCriteria?: FileCriteria,
): Promise<File[]>;

getDatasetFileCounts(datasetId: number | string, datasetVersionId: string): Promise<FileCounts>;

getFileDownloadCount(fileId: number | string): Promise<number>;

getFileUserPermissions(fileId: number | string): Promise<FileUserPermissions>;
Expand Down
19 changes: 19 additions & 0 deletions src/files/domain/useCases/GetDatasetFileCounts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { UseCase } from '../../../core/domain/useCases/UseCase';
import { IFilesRepository } from '../repositories/IFilesRepository';
import { DatasetNotNumberedVersion } from '../../../datasets';
import { FileCounts } from '../models/FileCounts';

export class GetDatasetFileCounts implements UseCase<FileCounts> {
private filesRepository: IFilesRepository;

constructor(filesRepository: IFilesRepository) {
this.filesRepository = filesRepository;
}

async execute(
datasetId: number | string,
datasetVersionId: string | DatasetNotNumberedVersion = DatasetNotNumberedVersion.LATEST,
): Promise<FileCounts> {
return await this.filesRepository.getDatasetFileCounts(datasetId, datasetVersionId);
}
}
10 changes: 9 additions & 1 deletion src/files/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
import { FilesRepository } from './infra/repositories/FilesRepository';
import { GetDatasetFiles } from './domain/useCases/GetDatasetFiles';
import { GetDatasetFileCounts } from './domain/useCases/GetDatasetFileCounts';
import { GetFileDownloadCount } from './domain/useCases/GetFileDownloadCount';
import { GetFileUserPermissions } from './domain/useCases/GetFileUserPermissions';
import { GetFileDataTables } from './domain/useCases/GetFileDataTables';

const filesRepository = new FilesRepository();

const getDatasetFiles = new GetDatasetFiles(filesRepository);
const getDatasetFileCounts = new GetDatasetFileCounts(filesRepository);
const getFileDownloadCount = new GetFileDownloadCount(filesRepository);
const getFileUserPermissions = new GetFileUserPermissions(filesRepository);
const getFileDataTables = new GetFileDataTables(filesRepository);

export { getDatasetFiles, getFileDownloadCount, getFileUserPermissions, getFileDataTables };
export { getDatasetFiles, getFileDownloadCount, getFileUserPermissions, getFileDataTables, getDatasetFileCounts };

export { File, FileEmbargo, FileChecksum } from './domain/models/File';
export { FileUserPermissions } from './domain/models/FileUserPermissions';
export { FileCriteria, FileOrderCriteria, FileAccessStatus } from './domain/models/FileCriteria';
export {
FileCounts,
FileContentTypeCount,
FileAccessStatusCount,
FileCategoryNameCount,
} from './domain/models/FileCounts';
export {
FileDataTable,
FileDataVariable,
Expand Down
53 changes: 25 additions & 28 deletions src/files/infra/repositories/FilesRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { transformDataTablesResponseToDataTables } from './transformers/fileData
import { FileUserPermissions } from '../../domain/models/FileUserPermissions';
import { transformFileUserPermissionsResponseToFileUserPermissions } from './transformers/fileUserPermissionsTransformers';
import { FileCriteria } from '../../domain/models/FileCriteria';
import { FileCounts } from '../../domain/models/FileCounts';
import { transformFileCountsResponseToFileCounts } from './transformers/fileCountsTransformers';

export interface GetFilesQueryParams {
limit?: number;
Expand All @@ -19,19 +21,17 @@ export interface GetFilesQueryParams {
}

export class FilesRepository extends ApiRepository implements IFilesRepository {
private readonly datasetsResourceName: string = 'datasets';
private readonly filesResourceName: string = 'files';
private readonly accessResourceName: string = 'access/datafile';

public async getDatasetFiles(
datasetId: number | string,
datasetVersionId: string,
limit?: number,
offset?: number,
fileCriteria?: FileCriteria,
): Promise<File[]> {
let endpoint;
if (typeof datasetId === 'number') {
endpoint = `/datasets/${datasetId}/versions/${datasetVersionId}/files`;
} else {
endpoint = `/datasets/:persistentId/versions/${datasetVersionId}/files?persistentId=${datasetId}`;
}
const queryParams: GetFilesQueryParams = {};
if (limit !== undefined) {
queryParams.limit = limit;
Expand All @@ -42,49 +42,46 @@ export class FilesRepository extends ApiRepository implements IFilesRepository {
if (fileCriteria !== undefined) {
this.applyFileCriteriaToQueryParams(queryParams, fileCriteria);
}
return this.doGet(endpoint, true, queryParams)
return this.doGet(
this.buildApiEndpoint(this.datasetsResourceName, `versions/${datasetVersionId}/files`, datasetId),
true,
queryParams,
)
.then((response) => transformFilesResponseToFiles(response))
.catch((error) => {
throw error;
});
}

public async getDatasetFileCounts(datasetId: string | number, datasetVersionId: string): Promise<FileCounts> {
return this.doGet(
this.buildApiEndpoint(this.datasetsResourceName, `versions/${datasetVersionId}/files/counts`, datasetId),
true,
)
.then((response) => transformFileCountsResponseToFileCounts(response))
.catch((error) => {
throw error;
});
}

public async getFileDownloadCount(fileId: number | string): Promise<number> {
let endpoint;
if (typeof fileId === 'number') {
endpoint = `/files/${fileId}/downloadCount`;
} else {
endpoint = `/files/:persistentId/downloadCount?persistentId=${fileId}`;
}
return this.doGet(endpoint, true)
return this.doGet(this.buildApiEndpoint(this.filesResourceName, `downloadCount`, fileId), true)
.then((response) => response.data.data.message as number)
.catch((error) => {
throw error;
});
}

public async getFileUserPermissions(fileId: number | string): Promise<FileUserPermissions> {
let endpoint;
if (typeof fileId === 'number') {
endpoint = `/access/datafile/${fileId}/userPermissions`;
} else {
endpoint = `/access/datafile/:persistentId/userPermissions?persistentId=${fileId}`;
}
return this.doGet(endpoint, true)
return this.doGet(this.buildApiEndpoint(this.accessResourceName, `userPermissions`, fileId), true)
.then((response) => transformFileUserPermissionsResponseToFileUserPermissions(response))
.catch((error) => {
throw error;
});
}

public async getFileDataTables(fileId: string | number): Promise<FileDataTable[]> {
let endpoint;
if (typeof fileId === 'number') {
endpoint = `/files/${fileId}/dataTables`;
} else {
endpoint = `/files/:persistentId/dataTables?persistentId=${fileId}`;
}
return this.doGet(endpoint, true)
return this.doGet(this.buildApiEndpoint(this.filesResourceName, `dataTables`, fileId), true)
.then((response) => transformDataTablesResponseToDataTables(response))
.catch((error) => {
throw error;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { AxiosResponse } from 'axios';
import {
FileCounts,
FileContentTypeCount,
FileCategoryNameCount,
FileAccessStatusCount,
} from '../../../domain/models/FileCounts';
import { FileAccessStatus } from '../../../domain/models/FileCriteria';

export const transformFileCountsResponseToFileCounts = (response: AxiosResponse): FileCounts => {
const fileCountsPayload = response.data.data;
return {
total: fileCountsPayload.total,
perContentType: transformCountsPerContentTypePayload(fileCountsPayload.perContentType),
perAccessStatus: transformCountsPerAccessStatusPayload(fileCountsPayload.perAccessStatus),
perCategoryName: transformCountsPerCategoryNamePayload(fileCountsPayload.perCategoryName),
};
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const transformCountsPerContentTypePayload = (countsPerContentTypePayload: any): FileContentTypeCount[] => {
const fileContentTypeCounts: FileContentTypeCount[] = [];
const fileContentTypeCountKeys = Object.keys(countsPerContentTypePayload);
for (const fileContentTypeCountKey of fileContentTypeCountKeys) {
fileContentTypeCounts.push({
contentType: fileContentTypeCountKey,
count: countsPerContentTypePayload[fileContentTypeCountKey],
});
}
return fileContentTypeCounts;
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const transformCountsPerCategoryNamePayload = (countsPerCategoryNamePayload: any): FileCategoryNameCount[] => {
const fileCategoryNameCounts: FileCategoryNameCount[] = [];
const fileCategoryNameCountKeys = Object.keys(countsPerCategoryNamePayload);
for (const fileCategoryNameCountKey of fileCategoryNameCountKeys) {
fileCategoryNameCounts.push({
categoryName: fileCategoryNameCountKey,
count: countsPerCategoryNamePayload[fileCategoryNameCountKey],
});
}
return fileCategoryNameCounts;
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const transformCountsPerAccessStatusPayload = (countsPerAccessStatusPayload: any): FileAccessStatusCount[] => {
const fileAccessStatusCounts: FileAccessStatusCount[] = [];
const fileAccessStatusCountKeys = Object.keys(countsPerAccessStatusPayload);
for (const fileAccessStatusCountKey of fileAccessStatusCountKeys) {
fileAccessStatusCounts.push({
accessStatus: fileAccessStatusCountKey as FileAccessStatus,
count: countsPerAccessStatusPayload[fileAccessStatusCountKey],
});
}
return fileAccessStatusCounts;
};
2 changes: 1 addition & 1 deletion test/integration/environment/.env
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ POSTGRES_VERSION=13
DATAVERSE_DB_USER=dataverse
SOLR_VERSION=8.11.1
DATAVERSE_IMAGE_REGISTRY=ghcr.io
DATAVERSE_IMAGE_TAG=9714-files-api-extension-filters
DATAVERSE_IMAGE_TAG=9834-files-api-extension-file-counts
DATAVERSE_BOOTSTRAP_TIMEOUT=5m
Loading