Skip to content

Commit

Permalink
fix: new user case in createDataset_service fn
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
AbdulrhmanGoni committed Oct 27, 2024
1 parent 875db14 commit cd82741
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 35 deletions.
2 changes: 0 additions & 2 deletions src/constants/operationsResultsMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand Down
65 changes: 32 additions & 33 deletions src/services/datasets/createDataset_service.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -11,8 +10,6 @@ export default async function createDataset_service(
userId: string,
dataset: DatasetInput
): Promise<ServiceOperationResultType> {
const session = await createTransactionSession();

const newDataset = new DatasetDocument(dataset);

const userDatasets = await DatasetsModel.findById(
Expand All @@ -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);
}

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

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

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"
);

return ServiceOperationResult.success(
newDataset,
operationsResultsMessages.successfulDatasetCreation(newDataset.name)
);
}

0 comments on commit cd82741

Please sign in to comment.