Skip to content

Commit

Permalink
Revert "Set a maximum number of datasets for each user"
Browse files Browse the repository at this point in the history
  • Loading branch information
AbdulrhmanGoni authored Oct 27, 2024
1 parent 507689f commit 8fd6b8f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 43 deletions.
3 changes: 0 additions & 3 deletions src/constants/maxDatasetsForUser.ts

This file was deleted.

3 changes: 2 additions & 1 deletion src/constants/operationsResultsMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ 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: 28 additions & 39 deletions src/services/datasets/createDataset_service.ts
Original file line number Diff line number Diff line change
@@ -1,59 +1,48 @@
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 userDatasets = await DatasetsModel.findById(
userId,
{},
{ upsert: true }
);
const datasetAdded = await DatasetsModel.updateOne(
{ _id: userId },
{ $push: { datasets: newDataset } },
{ upsert: true, session }
)
.then(() => true)
.catch(() => false);

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

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

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)
await session.abortTransaction();
return ServiceOperationResult.failure(
operationsResultsMessages.failedDatasetCreation(newDataset.name)
);
}

0 comments on commit 8fd6b8f

Please sign in to comment.