diff --git a/src/constants/operationsResultsMessages.ts b/src/constants/operationsResultsMessages.ts index 771f120..0f72c6e 100644 --- a/src/constants/operationsResultsMessages.ts +++ b/src/constants/operationsResultsMessages.ts @@ -2,8 +2,6 @@ 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`, diff --git a/src/services/datasets/createDataset_service.ts b/src/services/datasets/createDataset_service.ts index da5dbb6..d375cdb 100644 --- a/src/services/datasets/createDataset_service.ts +++ b/src/services/datasets/createDataset_service.ts @@ -1,4 +1,3 @@ -import createTransactionSession from "../../utilities/createTransactionSession"; import DatasetsModel from "../../models/DatasetsModel"; import { DatasetDocument, type DatasetInput } from "../../types/datasets"; import type { ServiceOperationResultType } from "../../types/response"; @@ -11,8 +10,6 @@ export default async function createDataset_service( userId: string, dataset: DatasetInput ): Promise { - const session = await createTransactionSession(); - const newDataset = new DatasetDocument(dataset); const userDatasets = await DatasetsModel.findById( @@ -21,40 +18,42 @@ export default async function createDataset_service( { upsert: true } ); - if (userDatasets && userDatasets.datasets.length < maxDatasetsForUser) { - userDatasets.datasets.push(newDataset); - const datasetAdded = await userDatasets - .save({ session }) - .then(() => true) - .catch(() => false); - - if (datasetAdded) { - await session.commitTransaction(); - activitiesService.registerDatasetActivity( - userId, - { - _id: newDataset._id, - name: newDataset.name, - description: newDataset.description, - }, - newDataset.createdAt, - "New Resource" - ); - - return ServiceOperationResult.success( - newDataset, - operationsResultsMessages.successfulDatasetCreation(newDataset.name) - ); - } + if (!userDatasets) { + await DatasetsModel.create({ + _id: userId, + datasets: [newDataset], + }); + return successfulDatasetCreation(userId, newDataset); + } - session.abortTransaction(); - return ServiceOperationResult.failure( - operationsResultsMessages.failedDatasetCreation(newDataset.name) - ); + if (userDatasets.datasets.length < maxDatasetsForUser) { + userDatasets.datasets.push(newDataset); + await userDatasets.save(); + return successfulDatasetCreation(userId, newDataset); } else { - session.abortTransaction(); 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" + ); + + return ServiceOperationResult.success( + newDataset, + operationsResultsMessages.successfulDatasetCreation(newDataset.name) + ); +}