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

Set a maximum number of datasets for each user #70

Merged
merged 3 commits into from
Oct 27, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions src/constants/maxDatasetsForUser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const maxDatasetsForUser = 5;

export default maxDatasetsForUser;
3 changes: 1 addition & 2 deletions src/constants/operationsResultsMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ const operationsResultsMessages = {
successfulDatasetCreation: (datasetName: string) =>
`"${datasetName}" dataset created successfuly`,
successfulDatasetUpdate: "The dataset was updated successfully",
failedDatasetCreation: (datasetName: string) =>
`"${datasetName}" dataset creation failed`,
noDatasets: "There are no datasets yet",
noDataset: (datasetId: string) =>
`There is no dataset with "${datasetId}" id`,
successfulDatasetDeletion: "The dataset was deleted successfully",
maxDatasetsReached: "You have reached the maximum number of datasets",
noInstructionsForDataset: (datasetId: string) =>
`No instructions found for a dataset with this id: ${datasetId}`,
successfulInstructionsCountIncrement:
Expand Down
67 changes: 39 additions & 28 deletions src/services/datasets/createDataset_service.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,59 @@
import createTransactionSession from "../../utilities/createTransactionSession";
import DatasetsModel from "../../models/DatasetsModel";
import { DatasetDocument, type DatasetInput } from "../../types/datasets";
import type { ServiceOperationResultType } from "../../types/response";
import ServiceOperationResult from "../../utilities/ServiceOperationResult";
import activitiesService from "../activities";
import operationsResultsMessages from "../../constants/operationsResultsMessages";
import maxDatasetsForUser from "../../constants/maxDatasetsForUser";

export default async function createDataset_service(
userId: string,
dataset: DatasetInput
): Promise<ServiceOperationResultType> {
const session = await createTransactionSession();

const newDataset = new DatasetDocument(dataset);

const datasetAdded = await DatasetsModel.updateOne(
{ _id: userId },
{ $push: { datasets: newDataset } },
{ upsert: true, session }
)
.then(() => true)
.catch(() => false);
const userDatasets = await DatasetsModel.findById(
userId,
{},
{ upsert: true }
);

if (datasetAdded) {
await session.commitTransaction();
activitiesService.registerDatasetActivity(
userId,
{
_id: newDataset._id,
name: newDataset.name,
description: newDataset.description,
},
newDataset.createdAt,
"New Resource"
);
if (!userDatasets) {
await DatasetsModel.create({
_id: userId,
datasets: [newDataset],
});
return successfulDatasetCreation(userId, newDataset);
}

Check warning on line 27 in src/services/datasets/createDataset_service.ts

View check run for this annotation

Codecov / codecov/patch

src/services/datasets/createDataset_service.ts#L27

Added line #L27 was not covered by tests

return ServiceOperationResult.success(
newDataset,
operationsResultsMessages.successfulDatasetCreation(newDataset.name)
if (userDatasets.datasets.length < maxDatasetsForUser) {
userDatasets.datasets.push(newDataset);
await userDatasets.save();
return successfulDatasetCreation(userId, newDataset);
} else {
return ServiceOperationResult.failure(
operationsResultsMessages.maxDatasetsReached

Check warning on line 35 in src/services/datasets/createDataset_service.ts

View check run for this annotation

Codecov / codecov/patch

src/services/datasets/createDataset_service.ts#L29-L35

Added lines #L29 - L35 were not covered by tests
);
}
}

function successfulDatasetCreation(
userId: string,
newDataset: DatasetDocument
) {
activitiesService.registerDatasetActivity(
userId,
{
_id: newDataset._id,
name: newDataset.name,
description: newDataset.description,
},
newDataset.createdAt,
"New Resource"
);

await session.abortTransaction();
return ServiceOperationResult.failure(
operationsResultsMessages.failedDatasetCreation(newDataset.name)
return ServiceOperationResult.success(
newDataset,
operationsResultsMessages.successfulDatasetCreation(newDataset.name)
);
}