diff --git a/src/constants/maxDatasetsForUser.ts b/src/constants/maxDatasetsForUser.ts new file mode 100644 index 0000000..6d2354b --- /dev/null +++ b/src/constants/maxDatasetsForUser.ts @@ -0,0 +1,3 @@ +const maxDatasetsForUser = 5; + +export default maxDatasetsForUser; diff --git a/src/constants/operationsResultsMessages.ts b/src/constants/operationsResultsMessages.ts index c24c661..0f72c6e 100644 --- a/src/constants/operationsResultsMessages.ts +++ b/src/constants/operationsResultsMessages.ts @@ -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: diff --git a/src/services/datasets/createDataset_service.ts b/src/services/datasets/createDataset_service.ts index 0f6212e..d375cdb 100644 --- a/src/services/datasets/createDataset_service.ts +++ b/src/services/datasets/createDataset_service.ts @@ -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 { - 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); + } - 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 ); } +} + +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) ); }