From cd827412d1260358ed34d011266ee18cf300cd2d Mon Sep 17 00:00:00 2001 From: AbdulrhmanGoni Date: Sun, 27 Oct 2024 14:38:58 +0300 Subject: [PATCH] fix: new user case in `createDataset_service` fn Make `createDataset_service` function creates a new user datasets docment if the user has not one, Create `successfulDatasetCreation` to reuse it to register the new dataset creation activity and to return the success result Also cancel using transaction session because there is only one write operation and there is no need to transactions. Also delete failedDatasetCreation method from `operationsResultsMessages` object because its usage is strange and doesn't have a clear reason behind it. --- src/constants/operationsResultsMessages.ts | 2 - .../datasets/createDataset_service.ts | 65 +++++++++---------- 2 files changed, 32 insertions(+), 35 deletions(-) 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) + ); +}